OLD | NEW |
(Empty) | |
| 1 library IndexedDB4Test; |
| 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 // Test for KeyRange and Cursor. |
| 9 |
| 10 const String DB_NAME = 'Test4'; |
| 11 const String STORE_NAME = 'TEST'; |
| 12 const int VERSION = 1; |
| 13 |
| 14 Future<Database> createAndOpenDb() { |
| 15 return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { |
| 16 return html.window.indexedDB.open(DB_NAME, version: VERSION, |
| 17 onUpgradeNeeded: (e) { |
| 18 var db = e.target.result; |
| 19 db.createObjectStore(STORE_NAME); |
| 20 }); |
| 21 }); |
| 22 } |
| 23 |
| 24 Future<Database> writeItems(Database db) { |
| 25 Future<Object> write(index) { |
| 26 var transaction = db.transaction(STORE_NAME, 'readwrite'); |
| 27 return transaction.objectStore(STORE_NAME).put( |
| 28 {'content': 'Item $index'}, index); |
| 29 } |
| 30 |
| 31 var future = write(0); |
| 32 for (var i = 1; i < 100; ++i) { |
| 33 future = future.then((_) => write(i)); |
| 34 } |
| 35 |
| 36 // Chain on the DB so we return it at the end. |
| 37 return future.then((_) => db); |
| 38 } |
| 39 |
| 40 Future<Database> setupDb() { |
| 41 return createAndOpenDb().then(writeItems); |
| 42 } |
| 43 |
| 44 testRange(db, range, expectedFirst, expectedLast) { |
| 45 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| 46 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 47 var cursors = objectStore.openCursor(range: range, autoAdvance: true) |
| 48 .asBroadcastStream(); |
| 49 |
| 50 int lastKey; |
| 51 cursors.listen((cursor) { |
| 52 lastKey = cursor.key; |
| 53 var value = cursor.value; |
| 54 expect(value['content'], 'Item ${cursor.key}'); |
| 55 }); |
| 56 |
| 57 if (expectedFirst != null) { |
| 58 cursors.first.then((cursor) { |
| 59 expect(cursor.key, expectedFirst); |
| 60 }); |
| 61 } |
| 62 if (expectedLast != null) { |
| 63 cursors.last.then((cursor) { |
| 64 expect(lastKey, expectedLast); |
| 65 }); |
| 66 } |
| 67 |
| 68 return cursors.length.then((length) { |
| 69 if (expectedFirst == null) { |
| 70 expect(length, isZero); |
| 71 } else { |
| 72 expect(length, expectedLast - expectedFirst + 1); |
| 73 } |
| 74 }); |
| 75 } |
| 76 |
| 77 main() { |
| 78 useHtmlConfiguration(); |
| 79 |
| 80 // Don't bother with these tests if it's unsupported. |
| 81 // Support is tested in indexeddb_1_test |
| 82 if (IdbFactory.supported) { |
| 83 var db; |
| 84 setUp(() { |
| 85 if (db == null) { |
| 86 return setupDb().then((result) { |
| 87 db = result; |
| 88 }); |
| 89 } |
| 90 }); |
| 91 test('only1', () => testRange(db, new KeyRange.only(55), 55, 55)); |
| 92 test('only2', () => testRange(db, new KeyRange.only(100), null, null)); |
| 93 test('only3', () => testRange(db, new KeyRange.only(-1), null, null)); |
| 94 |
| 95 test('lower1', () => |
| 96 testRange(db, new KeyRange.lowerBound(40), 40, 99)); |
| 97 // OPTIONALS lower2() => testRange(db, new KeyRange.lowerBound(40, open: tru
e), 41, 99); |
| 98 test('lower2', () => |
| 99 testRange(db, new KeyRange.lowerBound(40, true), 41, 99)); |
| 100 // OPTIONALS lower3() => testRange(db, new KeyRange.lowerBound(40, open: fal
se), 40, 99); |
| 101 test('lower3', () => |
| 102 testRange(db, new KeyRange.lowerBound(40, false), 40, 99)); |
| 103 |
| 104 test('upper1', () => |
| 105 testRange(db, new KeyRange.upperBound(40), 0, 40)); |
| 106 // OPTIONALS upper2() => testRange(db, new KeyRange.upperBound(40, open: tru
e), 0, 39); |
| 107 test('upper2', () => |
| 108 testRange(db, new KeyRange.upperBound(40, true), 0, 39)); |
| 109 // upper3() => testRange(db, new KeyRange.upperBound(40, open: false), 0, 40
); |
| 110 test('upper3', () => |
| 111 testRange(db, new KeyRange.upperBound(40, false), 0, 40)); |
| 112 |
| 113 test('bound1', () => |
| 114 testRange(db, new KeyRange.bound(20, 30), 20, 30)); |
| 115 |
| 116 test('bound2', () => |
| 117 testRange(db, new KeyRange.bound(-100, 200), 0, 99)); |
| 118 |
| 119 bound3() => |
| 120 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, upperOpen: true), |
| 121 testRange(db, new KeyRange.bound(20, 30, false, true), 20, 29); |
| 122 |
| 123 bound4() => |
| 124 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true), |
| 125 testRange(db, new KeyRange.bound(20, 30, true), 21, 30); |
| 126 |
| 127 bound5() => |
| 128 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true, u
pperOpen: true), |
| 129 testRange(db, new KeyRange.bound(20, 30, true, true), 21, 29); |
| 130 } |
| 131 } |
OLD | NEW |