OLD | NEW |
1 #library('IndexedDB4Test'); | 1 #library('IndexedDB4Test'); |
2 #import('../../pkg/unittest/unittest.dart'); | 2 #import('../../pkg/unittest/unittest.dart'); |
3 #import('../../pkg/unittest/html_config.dart'); | 3 #import('../../pkg/unittest/html_config.dart'); |
4 #import('dart:html'); | 4 #import('dart:html'); |
5 | 5 |
6 // Test for IDBKeyRange and IDBCursor. | 6 // Test for IDBKeyRange and IDBCursor. |
7 | 7 |
8 const String DB_NAME = 'Test'; | 8 const String DB_NAME = 'Test'; |
9 const String STORE_NAME = 'TEST'; | 9 const String STORE_NAME = 'TEST'; |
10 const String VERSION = '1'; | 10 const int VERSION = 1; |
11 | 11 |
12 class Test { | 12 class Test { |
| 13 fail(message) => (e) { |
| 14 guardAsync(() { |
| 15 Expect.fail('IndexedDB failure: $message'); |
| 16 }); |
| 17 }; |
| 18 |
| 19 _createObjectStore(db) { |
| 20 try { |
| 21 // Nuke object store if it already exists. |
| 22 db.deleteObjectStore(STORE_NAME); |
| 23 } |
| 24 on IDBDatabaseException catch(e) { } // Chrome |
| 25 on DOMException catch(e) { } // Firefox |
| 26 db.createObjectStore(STORE_NAME); |
| 27 } |
| 28 |
13 var db; | 29 var db; |
14 | 30 |
15 start() { | 31 _openDb(afterOpen()) { |
16 var request = window.indexedDB.open(DB_NAME); | 32 var request = window.indexedDB.open(DB_NAME, VERSION); |
17 Expect.isNotNull(request); | 33 if (request is IDBOpenDBRequest) { |
18 request.on.success.add(expectAsync1(initDb)); | 34 // New upgrade protocol. |
19 request.on.error.add(fail('open')); | 35 request.on.success.add(expectAsync1((e) { |
| 36 db = e.target.result; |
| 37 afterOpen(); |
| 38 })); |
| 39 request.on.upgradeNeeded.add((e) { |
| 40 guardAsync(() { |
| 41 _createObjectStore(e.target.result); |
| 42 }); |
| 43 }); |
| 44 request.on.error.add(fail('open')); |
| 45 } else { |
| 46 // Legacy setVersion upgrade protocol. |
| 47 request.on.success.add(expectAsync1((e) { |
| 48 db = e.target.result; |
| 49 if (db.version != '$VERSION') { |
| 50 var setRequest = db.setVersion('$VERSION'); |
| 51 setRequest.on.success.add( |
| 52 expectAsync1((e) { |
| 53 _createObjectStore(db); |
| 54 var transaction = e.target.result; |
| 55 transaction.on.complete.add( |
| 56 expectAsync1((e) => afterOpen())); |
| 57 transaction.on.error.add(fail('Upgrade')); |
| 58 })); |
| 59 setRequest.on.error.add(fail('setVersion error')); |
| 60 } else { |
| 61 afterOpen(); |
| 62 } |
| 63 })); |
| 64 request.on.error.add(fail('open')); |
| 65 } |
20 } | 66 } |
21 | 67 |
22 initDb(e) { | 68 _createAndOpenDb(afterOpen()) { |
23 db = e.target.result; | 69 var request = window.indexedDB.deleteDatabase(DB_NAME); |
24 // TODO. Some browsers do this the w3 way - passing the VERSION to the | 70 request.on.success.add(expectAsync1((e) { _openDb(afterOpen); })); |
25 // open call and listening to onversionchange. Can we feature-detect the | 71 request.on.error.add(fail('delete old Db')); |
26 // difference and make it work? | |
27 var request = db.setVersion(VERSION); | |
28 request.on.success.add( | |
29 expectAsync1((e) { | |
30 try { | |
31 // Nuke object store if it already exists. | |
32 db.deleteObjectStore(STORE_NAME); | |
33 } on IDBDatabaseException catch (e) { } | |
34 db.createObjectStore(STORE_NAME); | |
35 | |
36 var transaction = e.target.result; | |
37 transaction.on.complete.add(expectAsync1((e) => writeItems(0))); | |
38 transaction.on.error.add(fail); | |
39 }) | |
40 ); | |
41 request.on.error.add(fail('setVersion error')); | |
42 } | 72 } |
43 | 73 |
44 writeItems(int index) { | 74 writeItems(int index) { |
45 if (index < 100) { | 75 if (index < 100) { |
46 var transaction = db.transaction([STORE_NAME], 'readwrite'); | 76 var transaction = db.transaction([STORE_NAME], 'readwrite'); |
47 var request = transaction.objectStore(STORE_NAME) | 77 var request = transaction.objectStore(STORE_NAME) |
48 .put('Item $index', index); | 78 .put('Item $index', index); |
49 request.on.success.add( | 79 request.on.success.add(expectAsync1((e) { |
50 expectAsync1((e) { | |
51 writeItems(index + 1); | 80 writeItems(index + 1); |
52 }) | 81 } |
53 ); | 82 )); |
54 request.on.error.add(fail('put')); | 83 request.on.error.add(fail('put')); |
55 } | 84 } |
56 } | 85 } |
57 | 86 |
58 fail(message) => (e) { | 87 setupDb() { _createAndOpenDb(() => writeItems(0)); } |
59 guardAsync(() { | |
60 Expect.fail('IndexedDB failure: $message'); | |
61 }); | |
62 }; | |
63 | 88 |
64 testRange(range, expectedFirst, expectedLast) { | 89 testRange(range, expectedFirst, expectedLast) { |
65 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); | 90 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); |
66 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 91 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
67 IDBRequest cursorRequest = objectStore.openCursor(range); | 92 IDBRequest cursorRequest = objectStore.openCursor(range); |
68 int itemCount = 0; | 93 int itemCount = 0; |
69 int firstKey = null; | 94 int firstKey = null; |
70 int lastKey = null; | 95 int lastKey = null; |
71 cursorRequest.on.success.add(expectAsync1((e) { | 96 cursorRequest.on.success.add(expectAsync1((e) { |
72 var cursor = e.target.result; | 97 var cursor = e.target.result; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 bound5() => | 144 bound5() => |
120 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), | 145 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), |
121 21, 29); | 146 21, 29); |
122 | 147 |
123 } | 148 } |
124 | 149 |
125 main() { | 150 main() { |
126 useHtmlConfiguration(); | 151 useHtmlConfiguration(); |
127 | 152 |
128 var test_ = new Test(); | 153 var test_ = new Test(); |
129 test('prepare', test_.start); | 154 test('prepare', test_.setupDb); |
130 | 155 |
131 test('only1', test_.only1); | 156 test('only1', test_.only1); |
132 test('only2', test_.only2); | 157 test('only2', test_.only2); |
133 test('only3', test_.only3); | 158 test('only3', test_.only3); |
134 | 159 |
135 test('lower1', test_.lower1); | 160 test('lower1', test_.lower1); |
136 test('lower2', test_.lower2); | 161 test('lower2', test_.lower2); |
137 test('lower3', test_.lower3); | 162 test('lower3', test_.lower3); |
138 | 163 |
139 test('upper1', test_.upper1); | 164 test('upper1', test_.upper1); |
140 test('upper2', test_.upper2); | 165 test('upper2', test_.upper2); |
141 test('upper3', test_.upper3); | 166 test('upper3', test_.upper3); |
142 | 167 |
143 test('bound1', test_.bound1); | 168 test('bound1', test_.bound1); |
144 test('bound2', test_.bound2); | 169 test('bound2', test_.bound2); |
145 test('bound3', test_.bound3); | 170 test('bound3', test_.bound3); |
146 test('bound4', test_.bound4); | 171 test('bound4', test_.bound4); |
147 test('bound5', test_.bound5); | 172 test('bound5', test_.bound5); |
148 } | 173 } |
OLD | NEW |