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