OLD | NEW |
(Empty) | |
| 1 library IndexedDB1Test; |
| 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' as idb; |
| 7 |
| 8 main() { |
| 9 useHtmlConfiguration(); |
| 10 |
| 11 if (!idb.IdbFactory.supported) { |
| 12 return; |
| 13 } |
| 14 |
| 15 var dbName = 'test_db_5'; |
| 16 var storeName = 'test_store'; |
| 17 var indexName = 'name_index'; |
| 18 var db; |
| 19 |
| 20 test('init', () { |
| 21 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 22 return html.window.indexedDB.open(dbName, version: 1, |
| 23 onUpgradeNeeded: (e) { |
| 24 var db = e.target.result; |
| 25 var objectStore = db.createObjectStore(storeName, |
| 26 autoIncrement: true); |
| 27 var index = objectStore.createIndex(indexName, 'name_index', |
| 28 unique: false); |
| 29 }); |
| 30 }).then((database) { |
| 31 db = database; |
| 32 }); |
| 33 }); |
| 34 |
| 35 if (html.window.indexedDB.supportsDatabaseNames) { |
| 36 test('getDatabaseNames', () { |
| 37 return html.window.indexedDB.getDatabaseNames().then((names) { |
| 38 expect(names.contains(dbName), isTrue); |
| 39 }); |
| 40 }); |
| 41 } |
| 42 |
| 43 var value = {'name_index': 'one', 'value': 'add_value'}; |
| 44 test('add/delete', () { |
| 45 var transaction = db.transaction(storeName, 'readwrite'); |
| 46 var key; |
| 47 return transaction.objectStore(storeName).add(value).then((addedKey) { |
| 48 key = addedKey; |
| 49 }).then((_) { |
| 50 return transaction.completed; |
| 51 }).then((_) { |
| 52 transaction = db.transaction(storeName, 'readonly'); |
| 53 return transaction.objectStore(storeName).getObject(key); |
| 54 }).then((readValue) { |
| 55 expect(readValue['value'], value['value']); |
| 56 return transaction.completed; |
| 57 }).then((_) { |
| 58 transaction = db.transactionList([storeName], 'readwrite'); |
| 59 return transaction.objectStore(storeName).delete(key); |
| 60 }).then((_) { |
| 61 return transaction.completed; |
| 62 }).then((_) { |
| 63 var transaction = db.transactionList([storeName], 'readonly'); |
| 64 return transaction.objectStore(storeName).count(); |
| 65 }).then((count) { |
| 66 expect(count, 0); |
| 67 }); |
| 68 }); |
| 69 |
| 70 test('clear/count', () { |
| 71 var transaction = db.transaction(storeName, 'readwrite'); |
| 72 transaction.objectStore(storeName).add(value); |
| 73 |
| 74 return transaction.completed.then((_) { |
| 75 transaction = db.transaction(storeName, 'readonly'); |
| 76 return transaction.objectStore(storeName).count(); |
| 77 }).then((count) { |
| 78 expect(count, 1); |
| 79 }).then((_) { |
| 80 return transaction.completed; |
| 81 }).then((_) { |
| 82 transaction = db.transactionList([storeName], 'readwrite'); |
| 83 transaction.objectStore(storeName).clear(); |
| 84 return transaction.completed; |
| 85 }).then((_) { |
| 86 var transaction = db.transactionList([storeName], 'readonly'); |
| 87 return transaction.objectStore(storeName).count(); |
| 88 }).then((count) { |
| 89 expect(count, 0); |
| 90 }); |
| 91 }); |
| 92 |
| 93 test('index', () { |
| 94 var transaction = db.transaction(storeName, 'readwrite'); |
| 95 transaction.objectStore(storeName).add(value); |
| 96 transaction.objectStore(storeName).add(value); |
| 97 transaction.objectStore(storeName).add(value); |
| 98 transaction.objectStore(storeName).add(value); |
| 99 |
| 100 return transaction.completed.then((_) { |
| 101 transaction = db.transactionList([storeName], 'readonly'); |
| 102 var index = transaction.objectStore(storeName).index(indexName); |
| 103 return index.count(); |
| 104 }).then((count) { |
| 105 expect(count, 4); |
| 106 return transaction.completed; |
| 107 }).then((_) { |
| 108 transaction = db.transaction(storeName, 'readonly'); |
| 109 var index = transaction.objectStore(storeName).index(indexName); |
| 110 return index.openCursor(autoAdvance: true).length; |
| 111 }).then((cursorsLength) { |
| 112 expect(cursorsLength, 4); |
| 113 return transaction.completed; |
| 114 }).then((_) { |
| 115 transaction = db.transaction(storeName, 'readonly'); |
| 116 var index = transaction.objectStore(storeName).index(indexName); |
| 117 return index.openKeyCursor(autoAdvance: true).length; |
| 118 }).then((cursorsLength) { |
| 119 expect(cursorsLength, 4); |
| 120 return transaction.completed; |
| 121 }).then((_) { |
| 122 transaction = db.transaction(storeName, 'readonly'); |
| 123 var index = transaction.objectStore(storeName).index(indexName); |
| 124 return index.get('one'); |
| 125 }).then((readValue) { |
| 126 expect(readValue['value'], value['value']); |
| 127 return transaction.completed; |
| 128 }).then((_) { |
| 129 transaction = db.transaction(storeName, 'readwrite'); |
| 130 transaction.objectStore(storeName).clear(); |
| 131 return transaction.completed; |
| 132 }); |
| 133 }); |
| 134 |
| 135 var deleteValue = {'name_index': 'two', 'value': 'delete_value'}; |
| 136 var updateValue = {'name_index': 'three', 'value': 'update_value'}; |
| 137 var updatedValue = {'name_index': 'three', 'value': 'updated_value'}; |
| 138 test('cursor', () { |
| 139 var transaction = db.transaction(storeName, 'readwrite'); |
| 140 transaction.objectStore(storeName).add(value); |
| 141 transaction.objectStore(storeName).add(deleteValue); |
| 142 transaction.objectStore(storeName).add(updateValue); |
| 143 |
| 144 return transaction.completed.then((_) { |
| 145 transaction = db.transactionList([storeName], 'readwrite'); |
| 146 var index = transaction.objectStore(storeName).index(indexName); |
| 147 var cursors = index.openCursor().asBroadcastStream(); |
| 148 |
| 149 cursors.listen((cursor) { |
| 150 var value = cursor.value; |
| 151 if (value['value'] == 'delete_value') { |
| 152 cursor.delete().then((_) { |
| 153 cursor.next(); |
| 154 }); |
| 155 } else if (value['value'] == 'update_value') { |
| 156 cursor.update(updatedValue).then((_) { |
| 157 cursor.next(); |
| 158 }); |
| 159 } else { |
| 160 cursor.next(); |
| 161 } |
| 162 }); |
| 163 return cursors.last; |
| 164 |
| 165 }).then((_) { |
| 166 return transaction.completed; |
| 167 }).then((_) { |
| 168 transaction = db.transaction(storeName, 'readonly'); |
| 169 var index = transaction.objectStore(storeName).index(indexName); |
| 170 return index.get('three'); |
| 171 }).then((readValue) { |
| 172 expect(readValue['value'], 'updated_value'); |
| 173 return transaction.completed; |
| 174 }).then((_) { |
| 175 transaction = db.transaction(storeName, 'readonly'); |
| 176 var index = transaction.objectStore(storeName).index(indexName); |
| 177 return index.get('two'); |
| 178 }).then((readValue) { |
| 179 expect(readValue, isNull); |
| 180 return transaction.completed; |
| 181 }); |
| 182 }); |
| 183 } |
OLD | NEW |