| 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:async'; |
| 5 import 'dart:html' as html; | 5 import 'dart:html' as html; |
| 6 import 'dart:indexed_db' as idb; | 6 import 'dart:indexed_db' as idb; |
| 7 | 7 |
| 8 const String DB_NAME = 'Test'; | |
| 9 const String STORE_NAME = 'TEST'; | 8 const String STORE_NAME = 'TEST'; |
| 10 const int VERSION = 1; | 9 const int VERSION = 1; |
| 11 | 10 |
| 11 var databaseNameIndex = 0; |
| 12 String nextDatabaseName() { |
| 13 return 'Test1_${databaseNameIndex++}'; |
| 14 } |
| 15 |
| 12 Future testUpgrade() { | 16 Future testUpgrade() { |
| 13 var dbName = 'version_db'; | 17 var dbName = nextDatabaseName(); |
| 14 var upgraded = false; | 18 var upgraded = false; |
| 15 | 19 |
| 16 // Delete any existing DBs. | 20 // Delete any existing DBs. |
| 17 return html.window.indexedDB.deleteDatabase(dbName).then((_) { | 21 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 18 | 22 |
| 19 return html.window.indexedDB.open(dbName, version: 1, | 23 return html.window.indexedDB.open(dbName, version: 1, |
| 20 onUpgradeNeeded: (e) {}); | 24 onUpgradeNeeded: (e) {}); |
| 21 }).then((db) { | 25 }).then((db) { |
| 22 db.close(); | 26 db.close(); |
| 23 }).then((_) { | 27 }).then((_) { |
| 24 return html.window.indexedDB.open(dbName, version: 2, | 28 return html.window.indexedDB.open(dbName, version: 2, |
| 25 onUpgradeNeeded: (e) { | 29 onUpgradeNeeded: (e) { |
| 26 // Bug 8265, we're getting the wrong type here. | 30 // Bug 8265, we're getting the wrong type here. |
| 27 //expect(e.oldVersion, 1); | 31 //expect(e.oldVersion, 1); |
| 28 //expect(e.newVersion, 2); | 32 //expect(e.newVersion, 2); |
| 29 upgraded = true; | 33 upgraded = true; |
| 30 }); | 34 }); |
| 31 }).then((_) { | 35 }).then((_) { |
| 32 expect(upgraded, isTrue); | 36 expect(upgraded, isTrue); |
| 33 }); | 37 }); |
| 34 } | 38 } |
| 35 | 39 |
| 36 testReadWrite(key, value, matcher, | 40 testReadWrite(key, value, matcher, |
| 37 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () { | 41 [dbName, storeName = STORE_NAME, version = VERSION]) => () { |
| 42 if (dbName == null) { |
| 43 dbName = nextDatabaseName(); |
| 44 } |
| 38 createObjectStore(e) { | 45 createObjectStore(e) { |
| 39 var store = e.target.result.createObjectStore(storeName); | 46 var store = e.target.result.createObjectStore(storeName); |
| 40 expect(store, isNotNull); | 47 expect(store, isNotNull); |
| 41 } | 48 } |
| 42 | 49 |
| 43 var db; | 50 var db; |
| 44 return html.window.indexedDB.deleteDatabase(dbName).then((_) { | 51 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 45 return html.window.indexedDB.open(dbName, version: version, | 52 return html.window.indexedDB.open(dbName, version: version, |
| 46 onUpgradeNeeded: createObjectStore); | 53 onUpgradeNeeded: createObjectStore); |
| 47 }).then((result) { | 54 }).then((result) { |
| 48 db = result; | 55 db = result; |
| 49 var transaction = db.transaction([storeName], 'readwrite'); | 56 var transaction = db.transaction([storeName], 'readwrite'); |
| 50 transaction.objectStore(storeName).put(value, key); | 57 transaction.objectStore(storeName).put(value, key); |
| 51 return transaction.completed; | 58 return transaction.completed; |
| 52 }).then((_) { | 59 }).then((_) { |
| 53 var transaction = db.transaction(storeName, 'readonly'); | 60 var transaction = db.transaction(storeName, 'readonly'); |
| 54 return transaction.objectStore(storeName).getObject(key); | 61 return transaction.objectStore(storeName).getObject(key); |
| 55 }).then((object) { | 62 }).then((object) { |
| 56 db.close(); | 63 db.close(); |
| 57 expect(object, matcher); | 64 expect(object, matcher); |
| 58 }).whenComplete(() { | 65 }).whenComplete(() { |
| 59 if (db != null) { | 66 if (db != null) { |
| 60 db.close(); | 67 db.close(); |
| 61 } | 68 } |
| 69 return html.window.indexedDB.deleteDatabase(dbName); |
| 62 }); | 70 }); |
| 63 }; | 71 }; |
| 64 | 72 |
| 65 testReadWriteTyped(key, value, matcher, | 73 testReadWriteTyped(key, value, matcher, |
| 66 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () { | 74 [dbName, storeName = STORE_NAME, version = VERSION]) => () { |
| 75 if (dbName == null) { |
| 76 dbName = nextDatabaseName(); |
| 77 } |
| 67 void createObjectStore(e) { | 78 void createObjectStore(e) { |
| 68 var store = e.target.result.createObjectStore(storeName); | 79 var store = e.target.result.createObjectStore(storeName); |
| 69 expect(store, isNotNull); | 80 expect(store, isNotNull); |
| 70 } | 81 } |
| 71 | 82 |
| 72 idb.Database db; | 83 idb.Database db; |
| 73 // Delete any existing DBs. | 84 // Delete any existing DBs. |
| 74 return html.window.indexedDB.deleteDatabase(dbName).then((_) { | 85 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 75 return html.window.indexedDB.open(dbName, version: version, | 86 return html.window.indexedDB.open(dbName, version: version, |
| 76 onUpgradeNeeded: createObjectStore); | 87 onUpgradeNeeded: createObjectStore); |
| 77 }).then((idb.Database result) { | 88 }).then((idb.Database result) { |
| 78 db = result; | 89 db = result; |
| 79 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); | 90 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); |
| 80 transaction.objectStore(storeName).put(value, key); | 91 transaction.objectStore(storeName).put(value, key); |
| 81 | 92 |
| 82 return transaction.completed; | 93 return transaction.completed; |
| 83 }).then((idb.Database result) { | 94 }).then((idb.Database result) { |
| 84 idb.Transaction transaction = db.transaction(storeName, 'readonly'); | 95 idb.Transaction transaction = db.transaction(storeName, 'readonly'); |
| 85 return transaction.objectStore(storeName).getObject(key); | 96 return transaction.objectStore(storeName).getObject(key); |
| 86 }).then((object) { | 97 }).then((object) { |
| 87 db.close(); | 98 db.close(); |
| 88 expect(object, matcher); | 99 expect(object, matcher); |
| 89 }).whenComplete(() { | 100 }).whenComplete(() { |
| 90 if (db != null) { | 101 if (db != null) { |
| 91 db.close(); | 102 db.close(); |
| 92 } | 103 } |
| 104 return html.window.indexedDB.deleteDatabase(dbName); |
| 93 }); | 105 }); |
| 94 }; | 106 }; |
| 95 | 107 |
| 96 tests_dynamic() { | 108 tests_dynamic() { |
| 97 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); | 109 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); |
| 98 test('test2', testReadWrite(123, 12345, equals(12345))); | 110 test('test2', testReadWrite(123, 12345, equals(12345))); |
| 99 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); | 111 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); |
| 100 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); | 112 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); |
| 101 test('test4', testReadWrite(123, false, equals(false))); | 113 test('test4', testReadWrite(123, false, equals(false))); |
| 102 } | 114 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 }); | 150 }); |
| 139 | 151 |
| 140 // Don't bother with these tests if it's unsupported. | 152 // Don't bother with these tests if it's unsupported. |
| 141 if (idb.IdbFactory.supported) { | 153 if (idb.IdbFactory.supported) { |
| 142 test('upgrade', testUpgrade); | 154 test('upgrade', testUpgrade); |
| 143 tests_dynamic(); | 155 tests_dynamic(); |
| 144 tests_typed(); | 156 tests_typed(); |
| 145 } | 157 } |
| 146 }); | 158 }); |
| 147 } | 159 } |
| OLD | NEW |