| OLD | NEW |
| 1 library IndexedDB1Test; | 1 library IndexedDB1Test; |
| 2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
| 3 import '../../pkg/unittest/lib/html_individual_config.dart'; | 3 import '../../pkg/unittest/lib/html_individual_config.dart'; |
| 4 import 'dart:async'; |
| 4 import 'dart:html' as html; | 5 import 'dart:html' as html; |
| 5 import 'dart:indexed_db' as idb; | 6 import 'dart:indexed_db' as idb; |
| 7 import 'utils.dart'; |
| 6 | 8 |
| 7 const String DB_NAME = 'Test'; | 9 const String DB_NAME = 'Test'; |
| 8 const String STORE_NAME = 'TEST'; | 10 const String STORE_NAME = 'TEST'; |
| 9 const int VERSION = 1; | 11 const int VERSION = 1; |
| 10 | 12 |
| 13 Future testUpgrade() { |
| 14 var dbName = 'version_db'; |
| 15 var upgraded = false; |
| 16 |
| 17 // Delete any existing DBs. |
| 18 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 19 |
| 20 return html.window.indexedDB.open(dbName, version: 1, |
| 21 onUpgradeNeeded: (e) {}); |
| 22 }).then((db) { |
| 23 db.close(); |
| 24 }).then((_) { |
| 25 return html.window.indexedDB.open(dbName, version: 2, |
| 26 onUpgradeNeeded: (e) { |
| 27 // Bug 8265, we're getting the wrong type here. |
| 28 //expect(e.oldVersion, 1); |
| 29 //expect(e.newVersion, 2); |
| 30 upgraded = true; |
| 31 }); |
| 32 }).then((_) { |
| 33 expect(upgraded, isTrue); |
| 34 }); |
| 35 } |
| 36 |
| 11 testReadWrite(key, value, matcher, | 37 testReadWrite(key, value, matcher, |
| 12 [dbName = DB_NAME, | 38 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () { |
| 13 storeName = STORE_NAME, | 39 createObjectStore(e) { |
| 14 version = VERSION]) => () { | 40 var store = e.target.result.createObjectStore(storeName); |
| 15 var db; | |
| 16 | |
| 17 fail(e) { | |
| 18 guardAsync(() { | |
| 19 throw new Exception('IndexedDB failure'); | |
| 20 }); | |
| 21 } | |
| 22 | |
| 23 createObjectStore(db) { | |
| 24 var store = db.createObjectStore(storeName); | |
| 25 expect(store, isNotNull); | 41 expect(store, isNotNull); |
| 26 } | 42 } |
| 27 | 43 |
| 28 step2(e) { | 44 var db; |
| 29 var transaction = db.transaction(storeName, 'readonly'); | 45 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 30 var request = transaction.objectStore(storeName).getObject(key); | 46 return html.window.indexedDB.open(dbName, version: version, |
| 31 request.onSuccess.listen(expectAsync1((e) { | 47 onUpgradeNeeded: createObjectStore); |
| 32 var object = e.target.result; | 48 }).then((result) { |
| 49 db = result; |
| 50 var transaction = db.transaction([storeName], 'readwrite'); |
| 51 transaction.objectStore(storeName).put(value, key); |
| 52 return transaction.completed; |
| 53 }).then((_) { |
| 54 var transaction = db.transaction(storeName, 'readonly'); |
| 55 return transaction.objectStore(storeName).getObject(key); |
| 56 }).then((object) { |
| 33 db.close(); | 57 db.close(); |
| 34 expect(object, matcher); | 58 expect(object, matcher); |
| 35 })); | 59 }).whenComplete(() { |
| 36 request.onError.listen(fail); | 60 if (db != null) { |
| 37 } | 61 db.close(); |
| 38 | 62 } |
| 39 step1() { | 63 }); |
| 40 var transaction = db.transaction([storeName], 'readwrite'); | |
| 41 var request = transaction.objectStore(storeName).put(value, key); | |
| 42 request.onError.listen(fail); | |
| 43 | |
| 44 transaction.onComplete.listen(expectAsync1(step2)); | |
| 45 } | |
| 46 | |
| 47 initDb(e) { | |
| 48 db = e.target.result; | |
| 49 if (version != db.version) { | |
| 50 // Legacy 'setVersion' upgrade protocol. Chrome 23 and earlier. | |
| 51 var request = db.setVersion('$version'); | |
| 52 request.onSuccess.listen( | |
| 53 expectAsync1((e) { | |
| 54 createObjectStore(db); | |
| 55 var transaction = e.target.result; | |
| 56 transaction.onComplete.listen(expectAsync1((e) => step1())); | |
| 57 transaction.onError.listen(fail); | |
| 58 }) | |
| 59 ); | |
| 60 request.onError.listen(fail); | |
| 61 } else { | |
| 62 step1(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 openDb(e) { | |
| 67 var request = html.window.indexedDB.open(dbName, version); | |
| 68 expect(request, isNotNull); | |
| 69 request.onSuccess.listen(expectAsync1(initDb)); | |
| 70 request.onError.listen(fail); | |
| 71 if (request is idb.OpenDBRequest) { | |
| 72 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses | |
| 73 // setVersion instead. This path take by FireFox 15, Chrome 24. | |
| 74 request.onUpgradeNeeded.listen((e) { | |
| 75 guardAsync(() { | |
| 76 createObjectStore(e.target.result); | |
| 77 }); | |
| 78 }); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // Delete any existing DB. | |
| 83 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName); | |
| 84 deleteRequest.onSuccess.listen(expectAsync1(openDb)); | |
| 85 deleteRequest.onError.listen(fail); | |
| 86 }; | 64 }; |
| 87 | 65 |
| 88 testReadWriteTyped(key, value, matcher, | 66 testReadWriteTyped(key, value, matcher, |
| 89 [dbName = DB_NAME, | 67 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () { |
| 90 storeName = STORE_NAME, | 68 void createObjectStore(e) { |
| 91 version = VERSION]) => () { | 69 var store = e.target.result.createObjectStore(storeName); |
| 92 idb.Database db; | |
| 93 | |
| 94 fail(e) { | |
| 95 guardAsync(() { | |
| 96 throw new Exception('IndexedDB failure'); | |
| 97 }); | |
| 98 } | |
| 99 | |
| 100 createObjectStore(db) { | |
| 101 idb.ObjectStore store = db.createObjectStore(storeName); | |
| 102 expect(store, isNotNull); | 70 expect(store, isNotNull); |
| 103 } | 71 } |
| 104 | 72 |
| 105 step2(e) { | 73 idb.Database db; |
| 106 idb.Transaction transaction = db.transaction(storeName, 'readonly'); | 74 // Delete any existing DBs. |
| 107 idb.Request request = transaction.objectStore(storeName).getObject(key); | 75 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 108 request.onSuccess.listen(expectAsync1((e) { | 76 return html.window.indexedDB.open(dbName, version: version, |
| 109 var object = e.target.result; | 77 onUpgradeNeeded: createObjectStore); |
| 78 }).then((idb.Database result) { |
| 79 db = result; |
| 80 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); |
| 81 transaction.objectStore(storeName).put(value, key); |
| 82 |
| 83 return transaction.completed; |
| 84 }).then((idb.Database result) { |
| 85 idb.Transaction transaction = db.transaction(storeName, 'readonly'); |
| 86 return transaction.objectStore(storeName).getObject(key); |
| 87 }).then((object) { |
| 110 db.close(); | 88 db.close(); |
| 111 expect(object, matcher); | 89 expect(object, matcher); |
| 112 })); | 90 }).whenComplete(() { |
| 113 request.onError.listen(fail); | 91 if (db != null) { |
| 114 } | 92 db.close(); |
| 115 | 93 } |
| 116 step1() { | 94 }); |
| 117 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); | |
| 118 idb.Request request = transaction.objectStore(storeName).put(value, key); | |
| 119 request.onError.listen(fail); | |
| 120 | |
| 121 transaction.onComplete.listen(expectAsync1(step2)); | |
| 122 } | |
| 123 | |
| 124 initDb(e) { | |
| 125 db = e.target.result; | |
| 126 if (version != db.version) { | |
| 127 // Legacy 'setVersion' upgrade protocol. | |
| 128 idb.Request request = db.setVersion('$version'); | |
| 129 request.onSuccess.listen( | |
| 130 expectAsync1((e) { | |
| 131 createObjectStore(db); | |
| 132 idb.Transaction transaction = e.target.result; | |
| 133 transaction.onComplete.listen(expectAsync1((e) => step1())); | |
| 134 transaction.onError.listen(fail); | |
| 135 }) | |
| 136 ); | |
| 137 request.onError.listen(fail); | |
| 138 } else { | |
| 139 step1(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 openDb(e) { | |
| 144 idb.Request request = html.window.indexedDB.open(dbName, version); | |
| 145 expect(request, isNotNull); | |
| 146 request.onSuccess.listen(expectAsync1(initDb)); | |
| 147 request.onError.listen(fail); | |
| 148 if (request is idb.OpenDBRequest) { | |
| 149 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses | |
| 150 // setVersion instead. | |
| 151 request.onUpgradeNeeded.listen((e) { | |
| 152 guardAsync(() { | |
| 153 createObjectStore(e.target.result); | |
| 154 }); | |
| 155 }); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 // Delete any existing DB. | |
| 160 idb.Request deleteRequest = html.window.indexedDB.deleteDatabase(dbName); | |
| 161 deleteRequest.onSuccess.listen(expectAsync1(openDb)); | |
| 162 deleteRequest.onError.listen(fail); | |
| 163 }; | 95 }; |
| 164 | 96 |
| 165 tests_dynamic() { | 97 tests_dynamic() { |
| 166 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); | 98 futureTest('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); |
| 167 test('test2', testReadWrite(123, 12345, equals(12345))); | 99 futureTest('test2', testReadWrite(123, 12345, equals(12345))); |
| 168 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); | 100 futureTest('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); |
| 169 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); | 101 futureTest('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); |
| 170 test('test4', testReadWrite(123, false, equals(false))); | 102 futureTest('test4', testReadWrite(123, false, equals(false))); |
| 171 } | 103 } |
| 172 | 104 |
| 173 tests_typed() { | 105 tests_typed() { |
| 174 test('test1', testReadWriteTyped(123, 'Hoot!', equals('Hoot!'))); | 106 futureTest('test1', testReadWriteTyped(123, 'Hoot!', equals('Hoot!'))); |
| 175 test('test2', testReadWriteTyped(123, 12345, equals(12345))); | 107 futureTest('test2', testReadWriteTyped(123, 12345, equals(12345))); |
| 176 test('test3', testReadWriteTyped(123, [1, 2, 3], equals([1, 2, 3]))); | 108 futureTest('test3', testReadWriteTyped(123, [1, 2, 3], equals([1, 2, 3]))); |
| 177 test('test4', testReadWriteTyped(123, [2, 3, 4], equals([2, 3, 4]))); | 109 futureTest('test4', testReadWriteTyped(123, [2, 3, 4], equals([2, 3, 4]))); |
| 178 test('test4', testReadWriteTyped(123, false, equals(false))); | 110 futureTest('test4', testReadWriteTyped(123, false, equals(false))); |
| 179 } | 111 } |
| 180 | 112 |
| 181 main() { | 113 main() { |
| 182 useHtmlIndividualConfiguration(); | 114 useHtmlIndividualConfiguration(); |
| 183 | 115 |
| 184 // Test that indexed_db is properly flagged as supported or not. | 116 // Test that indexed_db is properly flagged as supported or not. |
| 185 // Note that the rest of the indexed_db tests assume that this has been | 117 // Note that the rest of the indexed_db tests assume that this has been |
| 186 // checked. | 118 // checked. |
| 187 group('supported', () { | 119 group('supported', () { |
| 188 test('supported', () { | 120 test('supported', () { |
| 189 expect(idb.IdbFactory.supported, true); | 121 expect(idb.IdbFactory.supported, true); |
| 190 }); | 122 }); |
| 191 }); | 123 }); |
| 192 | 124 |
| 193 test('throws when unsupported', () { | 125 group('functional', () { |
| 194 var expectation = idb.IdbFactory.supported ? returnsNormally : throws; | 126 test('throws when unsupported', () { |
| 127 var expectation = idb.IdbFactory.supported ? returnsNormally : throws; |
| 195 | 128 |
| 196 expect(() { | 129 expect(() { |
| 197 var db = html.window.indexedDB; | 130 var db = html.window.indexedDB; |
| 198 }, expectation); | 131 }, expectation); |
| 132 }); |
| 133 |
| 134 // Don't bother with these tests if it's unsupported. |
| 135 if (idb.IdbFactory.supported) { |
| 136 futureTest('upgrade', testUpgrade); |
| 137 tests_dynamic(); |
| 138 tests_typed(); |
| 139 } |
| 199 }); | 140 }); |
| 200 | |
| 201 // Don't bother with these tests if it's unsupported. | |
| 202 if (idb.IdbFactory.supported) { | |
| 203 tests_dynamic(); | |
| 204 tests_typed(); | |
| 205 } | |
| 206 } | 141 } |
| OLD | NEW |