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