OLD | NEW |
1 #library('IndexedDB3Test'); | 1 #library('IndexedDB3Test'); |
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 // Read with cursor. | 6 // Read with cursor. |
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 int VERSION = 1; | 10 const int VERSION = 1; |
11 | 11 |
12 class Test { | 12 class Test { |
13 fail(message) => (e) { | 13 fail(message) => (e) { |
14 guardAsync(() { | 14 guardAsync(() { |
15 Expect.fail('IndexedDB failure: $message'); | 15 expect(false, isTrue, reason: 'IndexedDB failure: $message'); |
16 }); | 16 }); |
17 }; | 17 }; |
18 | 18 |
19 _createObjectStore(db) { | 19 _createObjectStore(db) { |
20 try { | 20 try { |
21 // Nuke object store if it already exists. | 21 // Nuke object store if it already exists. |
22 db.deleteObjectStore(STORE_NAME); | 22 db.deleteObjectStore(STORE_NAME); |
23 } | 23 } |
24 on IDBDatabaseException catch(e) { } // Chrome | 24 on IDBDatabaseException catch(e) { } // Chrome |
25 on DOMException catch(e) { } // Firefox | 25 on DOMException catch(e) { } // Firefox |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 int itemCount = 0; | 93 int itemCount = 0; |
94 int sumKeys = 0; | 94 int sumKeys = 0; |
95 int lastKey = null; | 95 int lastKey = null; |
96 cursorRequest.on.success.add(expectAsync1((e) { | 96 cursorRequest.on.success.add(expectAsync1((e) { |
97 var cursor = e.target.result; | 97 var cursor = e.target.result; |
98 if (cursor != null) { | 98 if (cursor != null) { |
99 lastKey = cursor.key; | 99 lastKey = cursor.key; |
100 itemCount += 1; | 100 itemCount += 1; |
101 sumKeys += cursor.key; | 101 sumKeys += cursor.key; |
102 window.console.log('${cursor.key} ${cursor.value}'); | 102 window.console.log('${cursor.key} ${cursor.value}'); |
103 Expect.equals('Item ${cursor.key}', cursor.value); | 103 expect(cursor.value, 'Item ${cursor.key}'); |
104 cursor.continueFunction(); | 104 cursor.continueFunction(); |
105 } else { | 105 } else { |
106 // Done | 106 // Done |
107 Expect.equals(99, lastKey); | 107 expect(lastKey, 99); |
108 Expect.equals(100, itemCount); | 108 expect(itemCount, 100); |
109 Expect.equals((100 * 99) ~/ 2, sumKeys); | 109 expect(sumKeys, (100 * 99) ~/ 2); |
110 } | 110 } |
111 }, count:101)); | 111 }, count:101)); |
112 cursorRequest.on.error.add(fail('openCursor')); | 112 cursorRequest.on.error.add(fail('openCursor')); |
113 } | 113 } |
114 | 114 |
115 readAllReversedViaCursor() { | 115 readAllReversedViaCursor() { |
116 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); | 116 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); |
117 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 117 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
118 // TODO: create a IDBKeyRange(0,100) | 118 // TODO: create a IDBKeyRange(0,100) |
119 IDBRequest cursorRequest = objectStore.openCursor(null, 'prev'); | 119 IDBRequest cursorRequest = objectStore.openCursor(null, 'prev'); |
120 int itemCount = 0; | 120 int itemCount = 0; |
121 int sumKeys = 0; | 121 int sumKeys = 0; |
122 int lastKey = null; | 122 int lastKey = null; |
123 cursorRequest.on.success.add(expectAsync1((e) { | 123 cursorRequest.on.success.add(expectAsync1((e) { |
124 var cursor = e.target.result; | 124 var cursor = e.target.result; |
125 if (cursor != null) { | 125 if (cursor != null) { |
126 lastKey = cursor.key; | 126 lastKey = cursor.key; |
127 itemCount += 1; | 127 itemCount += 1; |
128 sumKeys += cursor.key; | 128 sumKeys += cursor.key; |
129 Expect.equals('Item ${cursor.key}', cursor.value); | 129 expect(cursor.value, 'Item ${cursor.key}'); |
130 cursor.continueFunction(); | 130 cursor.continueFunction(); |
131 } else { | 131 } else { |
132 // Done | 132 // Done |
133 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | 133 expect(lastKey, 0); // i.e. first key (scanned in reverse). |
134 Expect.equals(100, itemCount); | 134 expect(itemCount, 100); |
135 Expect.equals((100 * 99) ~/ 2, sumKeys); | 135 expect(sumKeys, (100 * 99) ~/ 2); |
136 } | 136 } |
137 }, count:101)); | 137 }, count:101)); |
138 cursorRequest.on.error.add(fail('openCursor')); | 138 cursorRequest.on.error.add(fail('openCursor')); |
139 } | 139 } |
140 } | 140 } |
141 | 141 |
142 main() { | 142 main() { |
143 useHtmlConfiguration(); | 143 useHtmlConfiguration(); |
144 | 144 |
145 var test_ = new Test(); | 145 var test_ = new Test(); |
146 test('prepare', test_.setupDb); | 146 test('prepare', test_.setupDb); |
147 test('readAll1', test_.readAllViaCursor); | 147 test('readAll1', test_.readAllViaCursor); |
148 test('readAll2', test_.readAllReversedViaCursor); | 148 test('readAll2', test_.readAllReversedViaCursor); |
149 } | 149 } |
OLD | NEW |