| 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 | 6 |
| 6 // Read with cursor. | 7 // Read with 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. FireFox 15, Chrome 24, hopefully IE10. | 35 // New upgrade protocol. FireFox 15, Chrome 24, hopefully IE10. |
| 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 readAllViaCursor() { | 90 readAllViaCursor() { |
| 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(); | 93 Request cursorRequest = objectStore.openCursor(); |
| 93 int itemCount = 0; | 94 int itemCount = 0; |
| 94 int sumKeys = 0; | 95 int sumKeys = 0; |
| 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 lastKey = cursor.key; | 100 lastKey = cursor.key; |
| 100 itemCount += 1; | 101 itemCount += 1; |
| 101 sumKeys += cursor.key; | 102 sumKeys += cursor.key; |
| 102 window.console.log('${cursor.key} ${cursor.value}'); | 103 window.console.log('${cursor.key} ${cursor.value}'); |
| 103 expect(cursor.value, 'Item ${cursor.key}'); | 104 expect(cursor.value, 'Item ${cursor.key}'); |
| 104 cursor.continueFunction(); | 105 cursor.continueFunction(); |
| 105 } else { | 106 } else { |
| 106 // Done | 107 // Done |
| 107 expect(lastKey, 99); | 108 expect(lastKey, 99); |
| 108 expect(itemCount, 100); | 109 expect(itemCount, 100); |
| 109 expect(sumKeys, (100 * 99) ~/ 2); | 110 expect(sumKeys, (100 * 99) ~/ 2); |
| 110 } | 111 } |
| 111 }, count:101)); | 112 }, count:101)); |
| 112 cursorRequest.on.error.add(fail('openCursor')); | 113 cursorRequest.on.error.add(fail('openCursor')); |
| 113 } | 114 } |
| 114 | 115 |
| 115 readAllReversedViaCursor() { | 116 readAllReversedViaCursor() { |
| 116 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); | 117 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| 117 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 118 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 118 // TODO: create a IDBKeyRange(0,100) | 119 // TODO: create a KeyRange(0,100) |
| 119 IDBRequest cursorRequest = objectStore.openCursor(null, 'prev'); | 120 Request cursorRequest = objectStore.openCursor(null, 'prev'); |
| 120 int itemCount = 0; | 121 int itemCount = 0; |
| 121 int sumKeys = 0; | 122 int sumKeys = 0; |
| 122 int lastKey = null; | 123 int lastKey = null; |
| 123 cursorRequest.on.success.add(expectAsync1((e) { | 124 cursorRequest.on.success.add(expectAsync1((e) { |
| 124 var cursor = e.target.result; | 125 var cursor = e.target.result; |
| 125 if (cursor != null) { | 126 if (cursor != null) { |
| 126 lastKey = cursor.key; | 127 lastKey = cursor.key; |
| 127 itemCount += 1; | 128 itemCount += 1; |
| 128 sumKeys += cursor.key; | 129 sumKeys += cursor.key; |
| 129 expect(cursor.value, 'Item ${cursor.key}'); | 130 expect(cursor.value, 'Item ${cursor.key}'); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 140 } | 141 } |
| 141 | 142 |
| 142 main() { | 143 main() { |
| 143 useHtmlConfiguration(); | 144 useHtmlConfiguration(); |
| 144 | 145 |
| 145 var test_ = new Test(); | 146 var test_ = new Test(); |
| 146 test('prepare', test_.setupDb); | 147 test('prepare', test_.setupDb); |
| 147 test('readAll1', test_.readAllViaCursor); | 148 test('readAll1', test_.readAllViaCursor); |
| 148 test('readAll2', test_.readAllReversedViaCursor); | 149 test('readAll2', test_.readAllReversedViaCursor); |
| 149 } | 150 } |
| OLD | NEW |