OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 void testLocalStorage() { | 6 void testLocalStorage() { |
7 forLayoutTests(); | 7 forLayoutTests(); |
8 test('GetItem', () { | 8 |
9 final value = window.localStorage.getItem('does not exist'); | 9 void testWithLocalStorage(String name, fn()) { |
10 Expect.isNull(value); | 10 test(name, () { |
| 11 window.localStorage['key1'] = 'val1'; |
| 12 window.localStorage['key2'] = 'val2'; |
| 13 window.localStorage['key3'] = 'val3'; |
| 14 |
| 15 try { |
| 16 fn(); |
| 17 } finally { |
| 18 window.localStorage.clear(); |
| 19 } |
| 20 }); |
| 21 } |
| 22 |
| 23 testWithLocalStorage('containsValue', () { |
| 24 Expect.isFalse(window.localStorage.containsValue('does not exist')); |
| 25 Expect.isFalse(window.localStorage.containsValue('key1')); |
| 26 Expect.isTrue(window.localStorage.containsValue('val1')); |
| 27 Expect.isTrue(window.localStorage.containsValue('val3')); |
11 }); | 28 }); |
12 test('SetItem', () { | 29 |
13 final key = 'foo'; | 30 testWithLocalStorage('containsKey', () { |
14 final value = 'bar'; | 31 Expect.isFalse(window.localStorage.containsKey('does not exist')); |
15 window.localStorage.setItem(key, value); | 32 Expect.isFalse(window.localStorage.containsKey('val1')); |
16 final stored = window.localStorage.getItem(key); | 33 Expect.isTrue(window.localStorage.containsKey('key1')); |
17 Expect.equals(value, stored); | 34 Expect.isTrue(window.localStorage.containsKey('key3')); |
| 35 }); |
| 36 |
| 37 testWithLocalStorage('[]', () { |
| 38 Expect.isNull(window.localStorage['does not exist']); |
| 39 Expect.equals('val1', window.localStorage['key1']); |
| 40 Expect.equals('val3', window.localStorage['key3']); |
| 41 }); |
| 42 |
| 43 testWithLocalStorage('[]=', () { |
| 44 Expect.isNull(window.localStorage['key4']); |
| 45 window.localStorage['key4'] = 'val4'; |
| 46 Expect.equals('val4', window.localStorage['key4']); |
| 47 |
| 48 Expect.equals('val3', window.localStorage['key3']); |
| 49 window.localStorage['key3'] = 'val3-new'; |
| 50 Expect.equals('val3-new', window.localStorage['key3']); |
| 51 }); |
| 52 |
| 53 testWithLocalStorage('putIfAbsent', () { |
| 54 Expect.isNull(window.localStorage['key4']); |
| 55 Expect.equals('val4', |
| 56 window.localStorage.putIfAbsent('key4', () => 'val4')); |
| 57 Expect.equals('val4', window.localStorage['key4']); |
| 58 |
| 59 Expect.equals('val3', window.localStorage['key3']); |
| 60 Expect.equals('val3', window.localStorage.putIfAbsent( |
| 61 'key3', () => Expect.fail('should not be called'))); |
| 62 Expect.equals('val3', window.localStorage['key3']); |
| 63 }); |
| 64 |
| 65 testWithLocalStorage('remove', () { |
| 66 Expect.isNull(window.localStorage.remove('does not exist')); |
| 67 Expect.equals('val3', window.localStorage.remove('key3')); |
| 68 Expect.mapEquals({'key1': 'val1', 'key2': 'val2'}, window.localStorage); |
| 69 }); |
| 70 |
| 71 testWithLocalStorage('clear', () { |
| 72 window.localStorage.clear(); |
| 73 Expect.mapEquals({}, window.localStorage); |
| 74 }); |
| 75 |
| 76 testWithLocalStorage('forEach', () { |
| 77 Map<String, String> results = {}; |
| 78 window.localStorage.forEach((k, v) { |
| 79 results[k] = v; |
| 80 }); |
| 81 Expect.mapEquals({'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}, |
| 82 results); |
| 83 }); |
| 84 |
| 85 testWithLocalStorage('getKeys', () { |
| 86 Expect.setEquals(['key1', 'key2', 'key3'], window.localStorage.getKeys()); |
| 87 }); |
| 88 |
| 89 testWithLocalStorage('getVals', () { |
| 90 Expect.setEquals(['val1', 'val2', 'val3'], |
| 91 window.localStorage.getValues()); |
| 92 }); |
| 93 |
| 94 testWithLocalStorage('length', () { |
| 95 Expect.equals(3, window.localStorage.length); |
| 96 window.localStorage.clear(); |
| 97 Expect.equals(0, window.localStorage.length); |
| 98 }); |
| 99 |
| 100 testWithLocalStorage('isEmpty', () { |
| 101 Expect.isFalse(window.localStorage.isEmpty()); |
| 102 window.localStorage.clear(); |
| 103 Expect.isTrue(window.localStorage.isEmpty()); |
18 }); | 104 }); |
19 } | 105 } |
OLD | NEW |