OLD | NEW |
1 library IndexedDB4Test; | 1 library IndexedDB4Test; |
2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
4 import 'dart:html'; | 4 import 'dart:html'; |
| 5 import 'dart:indexed_db'; |
5 | 6 |
6 // Test for IDBKeyRange and IDBCursor. | 7 // Test for KeyRange and Cursor. |
7 | 8 |
8 const String DB_NAME = 'Test'; | 9 const String DB_NAME = 'Test'; |
9 const String STORE_NAME = 'TEST'; | 10 const String STORE_NAME = 'TEST'; |
10 const int VERSION = 1; | 11 const int VERSION = 1; |
11 | 12 |
12 class Test { | 13 class Test { |
13 fail(message) => (e) { | 14 fail(message) => (e) { |
14 guardAsync(() { | 15 guardAsync(() { |
15 expect(false, isTrue, reason: 'IndexedDB failure: $message'); | 16 expect(false, isTrue, reason: 'IndexedDB failure: $message'); |
16 }); | 17 }); |
17 }; | 18 }; |
18 | 19 |
19 _createObjectStore(db) { | 20 _createObjectStore(db) { |
20 try { | 21 try { |
21 // Nuke object store if it already exists. | 22 // Nuke object store if it already exists. |
22 db.deleteObjectStore(STORE_NAME); | 23 db.deleteObjectStore(STORE_NAME); |
23 } | 24 } |
24 on IDBDatabaseException catch(e) { } // Chrome | 25 on DatabaseException catch(e) { } // Chrome |
25 on DOMException catch(e) { } // Firefox | 26 on DomException catch(e) { } // Firefox |
26 db.createObjectStore(STORE_NAME); | 27 db.createObjectStore(STORE_NAME); |
27 } | 28 } |
28 | 29 |
29 var db; | 30 var db; |
30 | 31 |
31 _openDb(afterOpen()) { | 32 _openDb(afterOpen()) { |
32 var request = window.indexedDB.open(DB_NAME, VERSION); | 33 var request = window.indexedDB.open(DB_NAME, VERSION); |
33 if (request is IDBOpenDBRequest) { | 34 if (request is OpenDBRequest) { |
34 // New upgrade protocol. | 35 // New upgrade protocol. |
35 request.on.success.add(expectAsync1((e) { | 36 request.on.success.add(expectAsync1((e) { |
36 db = e.target.result; | 37 db = e.target.result; |
37 afterOpen(); | 38 afterOpen(); |
38 })); | 39 })); |
39 request.on.upgradeNeeded.add((e) { | 40 request.on.upgradeNeeded.add((e) { |
40 guardAsync(() { | 41 guardAsync(() { |
41 _createObjectStore(e.target.result); | 42 _createObjectStore(e.target.result); |
42 }); | 43 }); |
43 }); | 44 }); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 writeItems(index + 1); | 81 writeItems(index + 1); |
81 } | 82 } |
82 )); | 83 )); |
83 request.on.error.add(fail('put')); | 84 request.on.error.add(fail('put')); |
84 } | 85 } |
85 } | 86 } |
86 | 87 |
87 setupDb() { _createAndOpenDb(() => writeItems(0)); } | 88 setupDb() { _createAndOpenDb(() => writeItems(0)); } |
88 | 89 |
89 testRange(range, expectedFirst, expectedLast) { | 90 testRange(range, expectedFirst, expectedLast) { |
90 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); | 91 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
91 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 92 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
92 IDBRequest cursorRequest = objectStore.openCursor(range); | 93 Request cursorRequest = objectStore.openCursor(range); |
93 int itemCount = 0; | 94 int itemCount = 0; |
94 int firstKey = null; | 95 int firstKey = null; |
95 int lastKey = null; | 96 int lastKey = null; |
96 cursorRequest.on.success.add(expectAsync1((e) { | 97 cursorRequest.on.success.add(expectAsync1((e) { |
97 var cursor = e.target.result; | 98 var cursor = e.target.result; |
98 if (cursor != null) { | 99 if (cursor != null) { |
99 if (firstKey == null) firstKey = cursor.key; | 100 if (firstKey == null) firstKey = cursor.key; |
100 lastKey = cursor.key; | 101 lastKey = cursor.key; |
101 itemCount += 1; | 102 itemCount += 1; |
102 expect(cursor.value, 'Item ${cursor.key}'); | 103 expect(cursor.value, 'Item ${cursor.key}'); |
103 cursor.continueFunction(); | 104 cursor.continueFunction(); |
104 } else { | 105 } else { |
105 // Done | 106 // Done |
106 expect(firstKey, expectedFirst); | 107 expect(firstKey, expectedFirst); |
107 expect(lastKey, expectedLast); | 108 expect(lastKey, expectedLast); |
108 if (expectedFirst == null) { | 109 if (expectedFirst == null) { |
109 expect(itemCount, isZero); | 110 expect(itemCount, isZero); |
110 } else { | 111 } else { |
111 expect(itemCount, expectedLast - expectedFirst + 1); | 112 expect(itemCount, expectedLast - expectedFirst + 1); |
112 } | 113 } |
113 } | 114 } |
114 }, | 115 }, |
115 count: 1 + ((expectedFirst == null) ? | 116 count: 1 + ((expectedFirst == null) ? |
116 0 : (expectedLast - expectedFirst + 1)))); | 117 0 : (expectedLast - expectedFirst + 1)))); |
117 cursorRequest.on.error.add(fail('openCursor')); | 118 cursorRequest.on.error.add(fail('openCursor')); |
118 } | 119 } |
119 | 120 |
120 only1() => testRange(new IDBKeyRange.only(55), 55, 55); | 121 only1() => testRange(new KeyRange.only(55), 55, 55); |
121 only2() => testRange(new IDBKeyRange.only(100), null, null); | 122 only2() => testRange(new KeyRange.only(100), null, null); |
122 only3() => testRange(new IDBKeyRange.only(-1), null, null); | 123 only3() => testRange(new KeyRange.only(-1), null, null); |
123 | 124 |
124 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); | 125 lower1() => testRange(new KeyRange.lowerBound(40), 40, 99); |
125 // OPTIONALS lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true),
41, 99); | 126 // OPTIONALS lower2() => testRange(new KeyRange.lowerBound(40, open: true), 41
, 99); |
126 lower2() => testRange(new IDBKeyRange.lowerBound(40, true), 41, 99); | 127 lower2() => testRange(new KeyRange.lowerBound(40, true), 41, 99); |
127 // OPTIONALS lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false)
, 40, 99); | 128 // OPTIONALS lower3() => testRange(new KeyRange.lowerBound(40, open: false), 4
0, 99); |
128 lower3() => testRange(new IDBKeyRange.lowerBound(40, false), 40, 99); | 129 lower3() => testRange(new KeyRange.lowerBound(40, false), 40, 99); |
129 | 130 |
130 upper1() => testRange(new IDBKeyRange.upperBound(40), 0, 40); | 131 upper1() => testRange(new KeyRange.upperBound(40), 0, 40); |
131 // OPTIONALS upper2() => testRange(new IDBKeyRange.upperBound(40, open: true),
0, 39); | 132 // OPTIONALS upper2() => testRange(new KeyRange.upperBound(40, open: true), 0,
39); |
132 upper2() => testRange(new IDBKeyRange.upperBound(40, true), 0, 39); | 133 upper2() => testRange(new KeyRange.upperBound(40, true), 0, 39); |
133 // upper3() => testRange(new IDBKeyRange.upperBound(40, open: false), 0, 40); | 134 // upper3() => testRange(new KeyRange.upperBound(40, open: false), 0, 40); |
134 upper3() => testRange(new IDBKeyRange.upperBound(40, false), 0, 40); | 135 upper3() => testRange(new KeyRange.upperBound(40, false), 0, 40); |
135 | 136 |
136 bound1() => testRange(new IDBKeyRange.bound(20, 30), 20, 30); | 137 bound1() => testRange(new KeyRange.bound(20, 30), 20, 30); |
137 | 138 |
138 bound2() => testRange(new IDBKeyRange.bound(-100, 200), 0, 99); | 139 bound2() => testRange(new KeyRange.bound(-100, 200), 0, 99); |
139 | 140 |
140 bound3() => | 141 bound3() => |
141 // OPTIONALS testRange(new IDBKeyRange.bound(20, 30, upperOpen: true), | 142 // OPTIONALS testRange(new KeyRange.bound(20, 30, upperOpen: true), |
142 testRange(new IDBKeyRange.bound(20, 30, false, true), | 143 testRange(new KeyRange.bound(20, 30, false, true), |
143 20, 29); | 144 20, 29); |
144 | 145 |
145 bound4() => | 146 bound4() => |
146 // OPTIONALS testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true), | 147 // OPTIONALS testRange(new KeyRange.bound(20, 30, lowerOpen: true), |
147 testRange(new IDBKeyRange.bound(20, 30, true), | 148 testRange(new KeyRange.bound(20, 30, true), |
148 21, 30); | 149 21, 30); |
149 | 150 |
150 bound5() => | 151 bound5() => |
151 // OPTIONALS testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, uppe
rOpen: true), | 152 // OPTIONALS testRange(new KeyRange.bound(20, 30, lowerOpen: true, upperOp
en: true), |
152 testRange(new IDBKeyRange.bound(20, 30, true, true), | 153 testRange(new KeyRange.bound(20, 30, true, true), |
153 21, 29); | 154 21, 29); |
154 | 155 |
155 } | 156 } |
156 | 157 |
157 main() { | 158 main() { |
158 useHtmlConfiguration(); | 159 useHtmlConfiguration(); |
159 | 160 |
160 var test_ = new Test(); | 161 var test_ = new Test(); |
161 test('prepare', test_.setupDb); | 162 test('prepare', test_.setupDb); |
162 | 163 |
163 test('only1', test_.only1); | 164 test('only1', test_.only1); |
164 test('only2', test_.only2); | 165 test('only2', test_.only2); |
165 test('only3', test_.only3); | 166 test('only3', test_.only3); |
166 | 167 |
167 test('lower1', test_.lower1); | 168 test('lower1', test_.lower1); |
168 test('lower2', test_.lower2); | 169 test('lower2', test_.lower2); |
169 test('lower3', test_.lower3); | 170 test('lower3', test_.lower3); |
170 | 171 |
171 test('upper1', test_.upper1); | 172 test('upper1', test_.upper1); |
172 test('upper2', test_.upper2); | 173 test('upper2', test_.upper2); |
173 test('upper3', test_.upper3); | 174 test('upper3', test_.upper3); |
174 | 175 |
175 test('bound1', test_.bound1); | 176 test('bound1', test_.bound1); |
176 test('bound2', test_.bound2); | 177 test('bound2', test_.bound2); |
177 test('bound3', test_.bound3); | 178 test('bound3', test_.bound3); |
178 test('bound4', test_.bound4); | 179 test('bound4', test_.bound4); |
179 test('bound5', test_.bound5); | 180 test('bound5', test_.bound5); |
180 } | 181 } |
OLD | NEW |