OLD | NEW |
1 library IndexedDB3Test; | 1 library IndexedDB3Test; |
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 import 'dart:indexed_db'; |
6 | 6 |
7 // Read with cursor. | 7 // Read with cursor. |
8 | 8 |
9 const String DB_NAME = 'Test'; | 9 const String DB_NAME = 'Test'; |
10 const String STORE_NAME = 'TEST'; | 10 const String STORE_NAME = 'TEST'; |
(...skipping 16 matching lines...) Expand all Loading... |
27 catch(e) { } // Chrome and Firefox | 27 catch(e) { } // Chrome and Firefox |
28 db.createObjectStore(STORE_NAME); | 28 db.createObjectStore(STORE_NAME); |
29 } | 29 } |
30 | 30 |
31 var db; | 31 var db; |
32 | 32 |
33 _openDb(afterOpen()) { | 33 _openDb(afterOpen()) { |
34 var request = window.indexedDB.open(DB_NAME, VERSION); | 34 var request = window.indexedDB.open(DB_NAME, VERSION); |
35 if (request is OpenDBRequest) { | 35 if (request is OpenDBRequest) { |
36 // New upgrade protocol. FireFox 15, Chrome 24, hopefully IE10. | 36 // New upgrade protocol. FireFox 15, Chrome 24, hopefully IE10. |
37 request.on.success.add(expectAsync1((e) { | 37 request.onSuccess.listen(expectAsync1((e) { |
38 db = e.target.result; | 38 db = e.target.result; |
39 afterOpen(); | 39 afterOpen(); |
40 })); | 40 })); |
41 request.on.upgradeNeeded.add((e) { | 41 request.onUpgradeNeeded.listen((e) { |
42 guardAsync(() { | 42 guardAsync(() { |
43 _createObjectStore(e.target.result); | 43 _createObjectStore(e.target.result); |
44 }); | 44 }); |
45 }); | 45 }); |
46 request.on.error.add(fail('open')); | 46 request.onError.listen(fail('open')); |
47 } else { | 47 } else { |
48 // Legacy setVersion upgrade protocol. Chrome < 23. | 48 // Legacy setVersion upgrade protocol. Chrome < 23. |
49 request.on.success.add(expectAsync1((e) { | 49 request.onSuccess.listen(expectAsync1((e) { |
50 db = e.target.result; | 50 db = e.target.result; |
51 if (db.version != '$VERSION') { | 51 if (db.version != '$VERSION') { |
52 var setRequest = db.setVersion('$VERSION'); | 52 var setRequest = db.setVersion('$VERSION'); |
53 setRequest.on.success.add( | 53 setRequest.onSuccess.listen( |
54 expectAsync1((e) { | 54 expectAsync1((e) { |
55 _createObjectStore(db); | 55 _createObjectStore(db); |
56 var transaction = e.target.result; | 56 var transaction = e.target.result; |
57 transaction.on.complete.add( | 57 transaction.onComplete.listen( |
58 expectAsync1((e) => afterOpen())); | 58 expectAsync1((e) => afterOpen())); |
59 transaction.on.error.add(fail('Upgrade')); | 59 transaction.onError.listen(fail('Upgrade')); |
60 })); | 60 })); |
61 setRequest.on.error.add(fail('setVersion error')); | 61 setRequest.onError.listen(fail('setVersion error')); |
62 } else { | 62 } else { |
63 afterOpen(); | 63 afterOpen(); |
64 } | 64 } |
65 })); | 65 })); |
66 request.on.error.add(fail('open')); | 66 request.onError.listen(fail('open')); |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 _createAndOpenDb(afterOpen()) { | 70 _createAndOpenDb(afterOpen()) { |
71 var request = window.indexedDB.deleteDatabase(DB_NAME); | 71 var request = window.indexedDB.deleteDatabase(DB_NAME); |
72 request.on.success.add(expectAsync1((e) { _openDb(afterOpen); })); | 72 request.onSuccess.listen(expectAsync1((e) { _openDb(afterOpen); })); |
73 request.on.error.add(fail('delete old Db')); | 73 request.onError.listen(fail('delete old Db')); |
74 } | 74 } |
75 | 75 |
76 writeItems(int index) { | 76 writeItems(int index) { |
77 if (index < 100) { | 77 if (index < 100) { |
78 var transaction = db.transaction([STORE_NAME], 'readwrite'); | 78 var transaction = db.transaction([STORE_NAME], 'readwrite'); |
79 var request = transaction.objectStore(STORE_NAME) | 79 var request = transaction.objectStore(STORE_NAME) |
80 .put('Item $index', index); | 80 .put('Item $index', index); |
81 request.on.success.add(expectAsync1((e) { | 81 request.onSuccess.listen(expectAsync1((e) { |
82 writeItems(index + 1); | 82 writeItems(index + 1); |
83 } | 83 } |
84 )); | 84 )); |
85 request.on.error.add(fail('put')); | 85 request.onError.listen(fail('put')); |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 setupDb() { _createAndOpenDb(() => writeItems(0)); } | 89 setupDb() { _createAndOpenDb(() => writeItems(0)); } |
90 | 90 |
91 readAllViaCursor() { | 91 readAllViaCursor() { |
92 Transaction txn = db.transaction(STORE_NAME, 'readonly'); | 92 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
93 ObjectStore objectStore = txn.objectStore(STORE_NAME); | 93 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
94 Request cursorRequest = objectStore.openCursor(); | 94 Request cursorRequest = objectStore.openCursor(); |
95 int itemCount = 0; | 95 int itemCount = 0; |
96 int sumKeys = 0; | 96 int sumKeys = 0; |
97 int lastKey = null; | 97 int lastKey = null; |
98 cursorRequest.on.success.add(expectAsync1((e) { | 98 cursorRequest.onSuccess.listen(expectAsync1((e) { |
99 var cursor = e.target.result; | 99 var cursor = e.target.result; |
100 if (cursor != null) { | 100 if (cursor != null) { |
101 lastKey = cursor.key; | 101 lastKey = cursor.key; |
102 itemCount += 1; | 102 itemCount += 1; |
103 sumKeys += cursor.key; | 103 sumKeys += cursor.key; |
104 window.console.log('${cursor.key} ${cursor.value}'); | 104 window.console.log('${cursor.key} ${cursor.value}'); |
105 expect(cursor.value, 'Item ${cursor.key}'); | 105 expect(cursor.value, 'Item ${cursor.key}'); |
106 cursor.continueFunction(); | 106 cursor.continueFunction(); |
107 } else { | 107 } else { |
108 // Done | 108 // Done |
109 expect(lastKey, 99); | 109 expect(lastKey, 99); |
110 expect(itemCount, 100); | 110 expect(itemCount, 100); |
111 expect(sumKeys, (100 * 99) ~/ 2); | 111 expect(sumKeys, (100 * 99) ~/ 2); |
112 } | 112 } |
113 }, count:101)); | 113 }, count:101)); |
114 cursorRequest.on.error.add(fail('openCursor')); | 114 cursorRequest.onError.listen(fail('openCursor')); |
115 } | 115 } |
116 | 116 |
117 readAllReversedViaCursor() { | 117 readAllReversedViaCursor() { |
118 Transaction txn = db.transaction(STORE_NAME, 'readonly'); | 118 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
119 ObjectStore objectStore = txn.objectStore(STORE_NAME); | 119 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
120 // TODO: create a KeyRange(0,100) | 120 // TODO: create a KeyRange(0,100) |
121 Request cursorRequest = objectStore.openCursor(null, 'prev'); | 121 Request cursorRequest = objectStore.openCursor(null, 'prev'); |
122 int itemCount = 0; | 122 int itemCount = 0; |
123 int sumKeys = 0; | 123 int sumKeys = 0; |
124 int lastKey = null; | 124 int lastKey = null; |
125 cursorRequest.on.success.add(expectAsync1((e) { | 125 cursorRequest.onSuccess.listen(expectAsync1((e) { |
126 var cursor = e.target.result; | 126 var cursor = e.target.result; |
127 if (cursor != null) { | 127 if (cursor != null) { |
128 lastKey = cursor.key; | 128 lastKey = cursor.key; |
129 itemCount += 1; | 129 itemCount += 1; |
130 sumKeys += cursor.key; | 130 sumKeys += cursor.key; |
131 expect(cursor.value, 'Item ${cursor.key}'); | 131 expect(cursor.value, 'Item ${cursor.key}'); |
132 cursor.continueFunction(); | 132 cursor.continueFunction(); |
133 } else { | 133 } else { |
134 // Done | 134 // Done |
135 expect(lastKey, 0); // i.e. first key (scanned in reverse). | 135 expect(lastKey, 0); // i.e. first key (scanned in reverse). |
136 expect(itemCount, 100); | 136 expect(itemCount, 100); |
137 expect(sumKeys, (100 * 99) ~/ 2); | 137 expect(sumKeys, (100 * 99) ~/ 2); |
138 } | 138 } |
139 }, count:101)); | 139 }, count:101)); |
140 cursorRequest.on.error.add(fail('openCursor')); | 140 cursorRequest.onError.listen(fail('openCursor')); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 main() { | 144 main() { |
145 useHtmlConfiguration(); | 145 useHtmlConfiguration(); |
146 | 146 |
147 // Don't bother with these tests if it's unsupported. | 147 // Don't bother with these tests if it's unsupported. |
148 // Support is tested in indexeddb_1_test | 148 // Support is tested in indexeddb_1_test |
149 if (IdbFactory.supported) { | 149 if (IdbFactory.supported) { |
150 var test_ = new Test(); | 150 var test_ = new Test(); |
151 test('prepare', test_.setupDb); | 151 test('prepare', test_.setupDb); |
152 test('readAll1', test_.readAllViaCursor); | 152 test('readAll1', test_.readAllViaCursor); |
153 test('readAll2', test_.readAllReversedViaCursor); | 153 test('readAll2', test_.readAllReversedViaCursor); |
154 } | 154 } |
155 } | 155 } |
OLD | NEW |