OLD | NEW |
(Empty) | |
| 1 library IndexedDB3Test; |
| 2 import 'package:unittest/unittest.dart'; |
| 3 import 'package:unittest/html_config.dart'; |
| 4 import 'dart:async'; |
| 5 import 'dart:html' as html; |
| 6 import 'dart:indexed_db'; |
| 7 |
| 8 // Read with cursor. |
| 9 |
| 10 const String DB_NAME = 'Test3'; |
| 11 const String STORE_NAME = 'TEST'; |
| 12 const int VERSION = 1; |
| 13 |
| 14 |
| 15 Future<Database> createAndOpenDb() { |
| 16 return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { |
| 17 return html.window.indexedDB.open(DB_NAME, version: VERSION, |
| 18 onUpgradeNeeded: (e) { |
| 19 var db = e.target.result; |
| 20 db.createObjectStore(STORE_NAME); |
| 21 }); |
| 22 }); |
| 23 } |
| 24 |
| 25 Future<Database> writeItems(Database db) { |
| 26 Future<Object> write(index) { |
| 27 var transaction = db.transaction(STORE_NAME, 'readwrite'); |
| 28 transaction.objectStore(STORE_NAME).put('Item $index', index); |
| 29 return transaction.completed; |
| 30 } |
| 31 |
| 32 var future = write(0); |
| 33 for (var i = 1; i < 100; ++i) { |
| 34 future = future.then((_) => write(i)); |
| 35 } |
| 36 |
| 37 // Chain on the DB so we return it at the end. |
| 38 return future.then((_) => db); |
| 39 } |
| 40 |
| 41 Future<Database> setupDb() { |
| 42 return createAndOpenDb().then(writeItems); |
| 43 } |
| 44 |
| 45 Future<Database> readAllViaCursor(Database db) { |
| 46 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| 47 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 48 int itemCount = 0; |
| 49 int sumKeys = 0; |
| 50 var lastKey = null; |
| 51 |
| 52 var cursors = objectStore.openCursor().asBroadcastStream(); |
| 53 cursors.listen( |
| 54 (cursor) { |
| 55 ++itemCount; |
| 56 lastKey = cursor.key; |
| 57 sumKeys += cursor.key; |
| 58 expect(cursor.value, 'Item ${cursor.key}'); |
| 59 cursor.next(); |
| 60 }); |
| 61 cursors.last.then( |
| 62 (cursor) { |
| 63 expect(lastKey, 99); |
| 64 expect(sumKeys, (100 * 99) ~/ 2); |
| 65 expect(itemCount, 100); |
| 66 }); |
| 67 |
| 68 return cursors.last.then((_) => db); |
| 69 } |
| 70 |
| 71 Future<Database> readAllReversedViaCursor(Database db) { |
| 72 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| 73 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 74 int itemCount = 0; |
| 75 int sumKeys = 0; |
| 76 var lastKey = null; |
| 77 |
| 78 var cursors = objectStore.openCursor(direction: 'prev').asBroadcastStream(); |
| 79 cursors.listen( |
| 80 (cursor) { |
| 81 ++itemCount; |
| 82 lastKey = cursor.key; |
| 83 sumKeys += cursor.key; |
| 84 expect(cursor.value, 'Item ${cursor.key}'); |
| 85 cursor.next(); |
| 86 }); |
| 87 cursors.last.then( |
| 88 (cursor) { |
| 89 expect(lastKey, 0); |
| 90 expect(sumKeys, (100 * 99) ~/ 2); |
| 91 expect(itemCount, 100); |
| 92 }); |
| 93 return cursors.last.then((_) => db); |
| 94 } |
| 95 |
| 96 main() { |
| 97 useHtmlConfiguration(); |
| 98 |
| 99 // Don't bother with these tests if it's unsupported. |
| 100 // Support is tested in indexeddb_1_test |
| 101 if (IdbFactory.supported) { |
| 102 var db; |
| 103 test('prepare', () { |
| 104 return setupDb().then((result) { db = result; }); |
| 105 }); |
| 106 test('readAll1', () { |
| 107 return readAllViaCursor(db); |
| 108 }); |
| 109 |
| 110 test('readAll2', () { |
| 111 return readAllReversedViaCursor(db); |
| 112 }); |
| 113 } |
| 114 } |
OLD | NEW |