| OLD | NEW |
| 1 library IndexedDB4Test; | 1 library IndexedDB4Test; |
| 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:async'; |
| 5 import 'dart:html' as html; |
| 5 import 'dart:indexed_db'; | 6 import 'dart:indexed_db'; |
| 7 import 'utils.dart'; |
| 6 | 8 |
| 7 // Test for KeyRange and Cursor. | 9 // Test for KeyRange and Cursor. |
| 8 | 10 |
| 9 const String DB_NAME = 'Test'; | 11 const String DB_NAME = 'Test'; |
| 10 const String STORE_NAME = 'TEST'; | 12 const String STORE_NAME = 'TEST'; |
| 11 const int VERSION = 1; | 13 const int VERSION = 1; |
| 12 | 14 |
| 13 class Test { | 15 Future<Database> createAndOpenDb() { |
| 14 fail(message) => (e) { | 16 return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { |
| 15 guardAsync(() { | 17 return html.window.indexedDB.open(DB_NAME, version: VERSION, |
| 16 expect(false, isTrue, reason: 'IndexedDB failure: $message'); | 18 onUpgradeNeeded: (e) { |
| 17 }); | 19 var db = e.target.result; |
| 18 }; | 20 db.createObjectStore(STORE_NAME); |
| 21 }); |
| 22 }); |
| 23 } |
| 19 | 24 |
| 20 _createObjectStore(db) { | 25 Future<Database> writeItems(Database db) { |
| 21 try { | 26 Future<Object> write(index) { |
| 22 // Nuke object store if it already exists. | 27 var transaction = db.transaction([STORE_NAME], 'readwrite'); |
| 23 db.deleteObjectStore(STORE_NAME); | 28 return transaction.objectStore(STORE_NAME).put('Item $index', index); |
| 24 } | |
| 25 catch(e) { } // Chrome and Firefox | |
| 26 db.createObjectStore(STORE_NAME); | |
| 27 } | 29 } |
| 28 | 30 |
| 29 var db; | 31 var future = write(0); |
| 30 | 32 for (var i = 1; i < 100; ++i) { |
| 31 _openDb(afterOpen()) { | 33 future = future.then((_) => write(i)); |
| 32 var request = window.indexedDB.open(DB_NAME, VERSION); | |
| 33 if (request is OpenDBRequest) { | |
| 34 // New upgrade protocol. | |
| 35 request.onSuccess.listen(expectAsync1((e) { | |
| 36 db = e.target.result; | |
| 37 afterOpen(); | |
| 38 })); | |
| 39 request.onUpgradeNeeded.listen((e) { | |
| 40 guardAsync(() { | |
| 41 _createObjectStore(e.target.result); | |
| 42 }); | |
| 43 }); | |
| 44 request.onError.listen(fail('open')); | |
| 45 } else { | |
| 46 // Legacy setVersion upgrade protocol. | |
| 47 request.onSuccess.listen(expectAsync1((e) { | |
| 48 db = e.target.result; | |
| 49 if (db.version != '$VERSION') { | |
| 50 var setRequest = db.setVersion('$VERSION'); | |
| 51 setRequest.onSuccess.listen( | |
| 52 expectAsync1((e) { | |
| 53 _createObjectStore(db); | |
| 54 var transaction = e.target.result; | |
| 55 transaction.onComplete.listen( | |
| 56 expectAsync1((e) => afterOpen())); | |
| 57 transaction.onError.listen(fail('Upgrade')); | |
| 58 })); | |
| 59 setRequest.onError.listen(fail('setVersion error')); | |
| 60 } else { | |
| 61 afterOpen(); | |
| 62 } | |
| 63 })); | |
| 64 request.onError.listen(fail('open')); | |
| 65 } | |
| 66 } | 34 } |
| 67 | 35 |
| 68 _createAndOpenDb(afterOpen()) { | 36 // Chain on the DB so we return it at the end. |
| 69 var request = window.indexedDB.deleteDatabase(DB_NAME); | 37 return future.then((_) => db); |
| 70 request.onSuccess.listen(expectAsync1((e) { _openDb(afterOpen); })); | 38 } |
| 71 request.onError.listen(fail('delete old Db')); | 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 itemCount = 0; |
| 51 if (expectedFirst != null) { |
| 52 cursors.first.then((cursor) { |
| 53 expect(cursor.key, expectedFirst); |
| 54 }); |
| 55 } |
| 56 if (expectedLast != null) { |
| 57 cursors.last.then((cursor) { |
| 58 expect(cursor.key, expectedLast); |
| 59 }); |
| 72 } | 60 } |
| 73 | 61 |
| 74 writeItems(int index) { | 62 return cursors.length.then((length) { |
| 75 if (index < 100) { | 63 if (expectedFirst == null) { |
| 76 var transaction = db.transaction([STORE_NAME], 'readwrite'); | 64 expect(length, isZero); |
| 77 var request = transaction.objectStore(STORE_NAME) | 65 } else { |
| 78 .put('Item $index', index); | 66 expect(length, expectedLast - expectedFirst + 1); |
| 79 request.onSuccess.listen(expectAsync1((e) { | |
| 80 writeItems(index + 1); | |
| 81 } | |
| 82 )); | |
| 83 request.onError.listen(fail('put')); | |
| 84 } | 67 } |
| 85 } | 68 }); |
| 86 | |
| 87 setupDb() { _createAndOpenDb(() => writeItems(0)); } | |
| 88 | |
| 89 testRange(range, expectedFirst, expectedLast) { | |
| 90 Transaction txn = db.transaction(STORE_NAME, 'readonly'); | |
| 91 ObjectStore objectStore = txn.objectStore(STORE_NAME); | |
| 92 Request cursorRequest = objectStore.openCursor(range); | |
| 93 int itemCount = 0; | |
| 94 int firstKey = null; | |
| 95 int lastKey = null; | |
| 96 cursorRequest.onSuccess.listen(expectAsync1((e) { | |
| 97 var cursor = e.target.result; | |
| 98 if (cursor != null) { | |
| 99 if (firstKey == null) firstKey = cursor.key; | |
| 100 lastKey = cursor.key; | |
| 101 itemCount += 1; | |
| 102 expect(cursor.value, 'Item ${cursor.key}'); | |
| 103 cursor.continueFunction(); | |
| 104 } else { | |
| 105 // Done | |
| 106 expect(firstKey, expectedFirst); | |
| 107 expect(lastKey, expectedLast); | |
| 108 if (expectedFirst == null) { | |
| 109 expect(itemCount, isZero); | |
| 110 } else { | |
| 111 expect(itemCount, expectedLast - expectedFirst + 1); | |
| 112 } | |
| 113 } | |
| 114 }, | |
| 115 count: 1 + ((expectedFirst == null) ? | |
| 116 0 : (expectedLast - expectedFirst + 1)))); | |
| 117 cursorRequest.onError.listen(fail('openCursor')); | |
| 118 } | |
| 119 | |
| 120 only1() => testRange(new KeyRange.only(55), 55, 55); | |
| 121 only2() => testRange(new KeyRange.only(100), null, null); | |
| 122 only3() => testRange(new KeyRange.only(-1), null, null); | |
| 123 | |
| 124 lower1() => testRange(new KeyRange.lowerBound(40), 40, 99); | |
| 125 // OPTIONALS lower2() => testRange(new KeyRange.lowerBound(40, open: true), 41
, 99); | |
| 126 lower2() => testRange(new KeyRange.lowerBound(40, true), 41, 99); | |
| 127 // OPTIONALS lower3() => testRange(new KeyRange.lowerBound(40, open: false), 4
0, 99); | |
| 128 lower3() => testRange(new KeyRange.lowerBound(40, false), 40, 99); | |
| 129 | |
| 130 upper1() => testRange(new KeyRange.upperBound(40), 0, 40); | |
| 131 // OPTIONALS upper2() => testRange(new KeyRange.upperBound(40, open: true), 0,
39); | |
| 132 upper2() => testRange(new KeyRange.upperBound(40, true), 0, 39); | |
| 133 // upper3() => testRange(new KeyRange.upperBound(40, open: false), 0, 40); | |
| 134 upper3() => testRange(new KeyRange.upperBound(40, false), 0, 40); | |
| 135 | |
| 136 bound1() => testRange(new KeyRange.bound(20, 30), 20, 30); | |
| 137 | |
| 138 bound2() => testRange(new KeyRange.bound(-100, 200), 0, 99); | |
| 139 | |
| 140 bound3() => | |
| 141 // OPTIONALS testRange(new KeyRange.bound(20, 30, upperOpen: true), | |
| 142 testRange(new KeyRange.bound(20, 30, false, true), | |
| 143 20, 29); | |
| 144 | |
| 145 bound4() => | |
| 146 // OPTIONALS testRange(new KeyRange.bound(20, 30, lowerOpen: true), | |
| 147 testRange(new KeyRange.bound(20, 30, true), | |
| 148 21, 30); | |
| 149 | |
| 150 bound5() => | |
| 151 // OPTIONALS testRange(new KeyRange.bound(20, 30, lowerOpen: true, upperOp
en: true), | |
| 152 testRange(new KeyRange.bound(20, 30, true, true), | |
| 153 21, 29); | |
| 154 | |
| 155 } | 69 } |
| 156 | 70 |
| 157 main() { | 71 main() { |
| 158 useHtmlConfiguration(); | 72 useHtmlConfiguration(); |
| 159 | 73 |
| 160 // Don't bother with these tests if it's unsupported. | 74 // Don't bother with these tests if it's unsupported. |
| 161 // Support is tested in indexeddb_1_test | 75 // Support is tested in indexeddb_1_test |
| 162 if (IdbFactory.supported) { | 76 if (IdbFactory.supported) { |
| 163 var test_ = new Test(); | 77 var db; |
| 164 test('prepare', test_.setupDb); | 78 futureTest('prepare', () { |
| 79 return setupDb().then((result) { |
| 80 db = result; |
| 81 }); |
| 82 }); |
| 165 | 83 |
| 166 test('only1', test_.only1); | 84 futureTest('only1', () => testRange(db, new KeyRange.only(55), 55, 55)); |
| 167 test('only2', test_.only2); | |
| 168 test('only3', test_.only3); | |
| 169 | 85 |
| 170 test('lower1', test_.lower1); | 86 futureTest('only1', () => testRange(db, new KeyRange.only(55), 55, 55)); |
| 171 test('lower2', test_.lower2); | 87 futureTest('only2', () => testRange(db, new KeyRange.only(100), null, null))
; |
| 172 test('lower3', test_.lower3); | 88 futureTest('only3', () => testRange(db, new KeyRange.only(-1), null, null)); |
| 173 | 89 |
| 174 test('upper1', test_.upper1); | 90 futureTest('lower1', () => |
| 175 test('upper2', test_.upper2); | 91 testRange(db, new KeyRange.lowerBound(40), 40, 99)); |
| 176 test('upper3', test_.upper3); | 92 // OPTIONALS lower2() => testRange(db, new KeyRange.lowerBound(40, open: tru
e), 41, 99); |
| 93 futureTest('lower2', () => |
| 94 testRange(db, new KeyRange.lowerBound(40, true), 41, 99)); |
| 95 // OPTIONALS lower3() => testRange(db, new KeyRange.lowerBound(40, open: fal
se), 40, 99); |
| 96 futureTest('lower3', () => |
| 97 testRange(db, new KeyRange.lowerBound(40, false), 40, 99)); |
| 177 | 98 |
| 178 test('bound1', test_.bound1); | 99 futureTest('upper1', () => |
| 179 test('bound2', test_.bound2); | 100 testRange(db, new KeyRange.upperBound(40), 0, 40)); |
| 180 test('bound3', test_.bound3); | 101 // OPTIONALS upper2() => testRange(db, new KeyRange.upperBound(40, open: tru
e), 0, 39); |
| 181 test('bound4', test_.bound4); | 102 futureTest('upper2', () => |
| 182 test('bound5', test_.bound5); | 103 testRange(db, new KeyRange.upperBound(40, true), 0, 39)); |
| 104 // upper3() => testRange(db, new KeyRange.upperBound(40, open: false), 0, 40
); |
| 105 futureTest('upper3', () => |
| 106 testRange(db, new KeyRange.upperBound(40, false), 0, 40)); |
| 107 |
| 108 futureTest('bound1', () => |
| 109 testRange(db, new KeyRange.bound(20, 30), 20, 30)); |
| 110 |
| 111 futureTest('bound2', () => |
| 112 testRange(db, new KeyRange.bound(-100, 200), 0, 99)); |
| 113 |
| 114 bound3() => |
| 115 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, upperOpen: true), |
| 116 testRange(db, new KeyRange.bound(20, 30, false, true), 20, 29); |
| 117 |
| 118 bound4() => |
| 119 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true), |
| 120 testRange(db, new KeyRange.bound(20, 30, true), 21, 30); |
| 121 |
| 122 bound5() => |
| 123 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true, u
pperOpen: true), |
| 124 testRange(db, new KeyRange.bound(20, 30, true, true), 21, 29); |
| 183 } | 125 } |
| 184 } | 126 } |
| OLD | NEW |