| 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:async'; | 4 import 'dart:async'; |
| 5 import 'dart:html' as html; | 5 import 'dart:html' as html; |
| 6 import 'dart:indexed_db'; | 6 import 'dart:indexed_db'; |
| 7 import 'utils.dart'; | 7 import 'utils.dart'; |
| 8 | 8 |
| 9 // Test for KeyRange and Cursor. | 9 // Test for KeyRange and Cursor. |
| 10 | 10 |
| 11 const String DB_NAME = 'Test'; | 11 const String DB_NAME = 'Test'; |
| 12 const String STORE_NAME = 'TEST'; | 12 const String STORE_NAME = 'TEST'; |
| 13 const int VERSION = 1; | 13 const int VERSION = 1; |
| 14 | 14 |
| 15 Future<Database> createAndOpenDb() { | 15 Future<Database> createAndOpenDb() { |
| 16 return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { | 16 return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { |
| 17 return html.window.indexedDB.open(DB_NAME, version: VERSION, | 17 return html.window.indexedDB.open(DB_NAME, version: VERSION, |
| 18 onUpgradeNeeded: (e) { | 18 onUpgradeNeeded: (e) { |
| 19 var db = e.target.result; | 19 var db = e.target.result; |
| 20 db.createObjectStore(STORE_NAME); | 20 db.createObjectStore(STORE_NAME); |
| 21 }); | 21 }); |
| 22 }); | 22 }); |
| 23 } | 23 } |
| 24 | 24 |
| 25 Future<Database> writeItems(Database db) { | 25 Future<Database> writeItems(Database db) { |
| 26 Future<Object> write(index) { | 26 Future<Object> write(index) { |
| 27 var transaction = db.transaction([STORE_NAME], 'readwrite'); | 27 var transaction = db.transaction([STORE_NAME], 'readwrite'); |
| 28 return transaction.objectStore(STORE_NAME).put('Item $index', index); | 28 return transaction.objectStore(STORE_NAME).put( |
| 29 {'content': 'Item $index'}, index); |
| 29 } | 30 } |
| 30 | 31 |
| 31 var future = write(0); | 32 var future = write(0); |
| 32 for (var i = 1; i < 100; ++i) { | 33 for (var i = 1; i < 100; ++i) { |
| 33 future = future.then((_) => write(i)); | 34 future = future.then((_) => write(i)); |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Chain on the DB so we return it at the end. | 37 // Chain on the DB so we return it at the end. |
| 37 return future.then((_) => db); | 38 return future.then((_) => db); |
| 38 } | 39 } |
| 39 | 40 |
| 40 Future<Database> setupDb() { | 41 Future<Database> setupDb() { |
| 41 return createAndOpenDb().then(writeItems); | 42 return createAndOpenDb().then(writeItems); |
| 42 } | 43 } |
| 43 | 44 |
| 44 testRange(db, range, expectedFirst, expectedLast) { | 45 testRange(db, range, expectedFirst, expectedLast) { |
| 45 Transaction txn = db.transaction(STORE_NAME, 'readonly'); | 46 Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| 46 ObjectStore objectStore = txn.objectStore(STORE_NAME); | 47 ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 47 var cursors = objectStore.openCursor(range: range, autoAdvance: true) | 48 var cursors = objectStore.openCursor(range: range, autoAdvance: true) |
| 48 .asBroadcastStream(); | 49 .asBroadcastStream(); |
| 49 | 50 |
| 50 int lastKey; | 51 int lastKey; |
| 51 cursors.listen((cursor) { | 52 cursors.listen((cursor) { |
| 52 lastKey = cursor.key; | 53 lastKey = cursor.key; |
| 54 var value = cursor.value; |
| 55 expect(value['content'], 'Item ${cursor.key}'); |
| 53 }); | 56 }); |
| 54 | 57 |
| 55 if (expectedFirst != null) { | 58 if (expectedFirst != null) { |
| 56 cursors.first.then((cursor) { | 59 cursors.first.then((cursor) { |
| 57 expect(cursor.key, expectedFirst); | 60 expect(cursor.key, expectedFirst); |
| 58 }); | 61 }); |
| 59 } | 62 } |
| 60 if (expectedLast != null) { | 63 if (expectedLast != null) { |
| 61 cursors.last.then((cursor) { | 64 cursors.last.then((cursor) { |
| 62 expect(lastKey, expectedLast); | 65 expect(lastKey, expectedLast); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 124 |
| 122 bound4() => | 125 bound4() => |
| 123 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true), | 126 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true), |
| 124 testRange(db, new KeyRange.bound(20, 30, true), 21, 30); | 127 testRange(db, new KeyRange.bound(20, 30, true), 21, 30); |
| 125 | 128 |
| 126 bound5() => | 129 bound5() => |
| 127 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true, u
pperOpen: true), | 130 // OPTIONALS testRange(db, new KeyRange.bound(20, 30, lowerOpen: true, u
pperOpen: true), |
| 128 testRange(db, new KeyRange.bound(20, 30, true, true), 21, 29); | 131 testRange(db, new KeyRange.bound(20, 30, true, true), 21, 29); |
| 129 } | 132 } |
| 130 } | 133 } |
| OLD | NEW |