OLD | NEW |
(Empty) | |
| 1 library IndexedDB1Test; |
| 2 import 'package:unittest/unittest.dart'; |
| 3 import 'package:unittest/html_individual_config.dart'; |
| 4 import 'dart:async'; |
| 5 import 'dart:html' as html; |
| 6 import 'dart:math' as math; |
| 7 import 'dart:indexed_db' as idb; |
| 8 |
| 9 const String STORE_NAME = 'TEST'; |
| 10 const int VERSION = 1; |
| 11 |
| 12 var databaseNameIndex = 0; |
| 13 String nextDatabaseName() { |
| 14 return 'Test1_${databaseNameIndex++}'; |
| 15 } |
| 16 |
| 17 Future testUpgrade() { |
| 18 var dbName = nextDatabaseName(); |
| 19 var upgraded = false; |
| 20 |
| 21 // Delete any existing DBs. |
| 22 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 23 |
| 24 return html.window.indexedDB.open(dbName, version: 1, |
| 25 onUpgradeNeeded: (e) {}); |
| 26 }).then((db) { |
| 27 db.close(); |
| 28 }).then((_) { |
| 29 return html.window.indexedDB.open(dbName, version: 2, |
| 30 onUpgradeNeeded: (e) { |
| 31 expect(e.oldVersion, 1); |
| 32 expect(e.newVersion, 2); |
| 33 upgraded = true; |
| 34 }); |
| 35 }).then((_) { |
| 36 expect(upgraded, isTrue); |
| 37 }); |
| 38 } |
| 39 |
| 40 testReadWrite(key, value, matcher, |
| 41 [dbName, storeName = STORE_NAME, version = VERSION, |
| 42 stringifyResult = false]) => () { |
| 43 if (dbName == null) { |
| 44 dbName = nextDatabaseName(); |
| 45 } |
| 46 createObjectStore(e) { |
| 47 var store = e.target.result.createObjectStore(storeName); |
| 48 expect(store, isNotNull); |
| 49 } |
| 50 |
| 51 var db; |
| 52 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 53 return html.window.indexedDB.open(dbName, version: version, |
| 54 onUpgradeNeeded: createObjectStore); |
| 55 }).then((result) { |
| 56 db = result; |
| 57 var transaction = db.transactionList([storeName], 'readwrite'); |
| 58 transaction.objectStore(storeName).put(value, key); |
| 59 return transaction.completed; |
| 60 }).then((_) { |
| 61 var transaction = db.transaction(storeName, 'readonly'); |
| 62 return transaction.objectStore(storeName).getObject(key); |
| 63 }).then((object) { |
| 64 db.close(); |
| 65 if (stringifyResult) { |
| 66 // Stringify the numbers to verify that we're correctly returning ints |
| 67 // as ints vs doubles. |
| 68 expect(object.toString(), matcher); |
| 69 } else { |
| 70 expect(object, matcher); |
| 71 } |
| 72 }).whenComplete(() { |
| 73 if (db != null) { |
| 74 db.close(); |
| 75 } |
| 76 return html.window.indexedDB.deleteDatabase(dbName); |
| 77 }); |
| 78 }; |
| 79 |
| 80 testReadWriteTyped(key, value, matcher, |
| 81 [dbName, storeName = STORE_NAME, version = VERSION, |
| 82 stringifyResult = false]) => () { |
| 83 if (dbName == null) { |
| 84 dbName = nextDatabaseName(); |
| 85 } |
| 86 void createObjectStore(e) { |
| 87 var store = e.target.result.createObjectStore(storeName); |
| 88 expect(store, isNotNull); |
| 89 } |
| 90 |
| 91 idb.Database db; |
| 92 // Delete any existing DBs. |
| 93 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 94 return html.window.indexedDB.open(dbName, version: version, |
| 95 onUpgradeNeeded: createObjectStore); |
| 96 }).then((idb.Database result) { |
| 97 db = result; |
| 98 idb.Transaction transaction = db.transactionList([storeName], 'readwrite')
; |
| 99 transaction.objectStore(storeName).put(value, key); |
| 100 |
| 101 return transaction.completed; |
| 102 }).then((idb.Database result) { |
| 103 idb.Transaction transaction = db.transaction(storeName, 'readonly'); |
| 104 return transaction.objectStore(storeName).getObject(key); |
| 105 }).then((object) { |
| 106 db.close(); |
| 107 if (stringifyResult) { |
| 108 // Stringify the numbers to verify that we're correctly returning ints |
| 109 // as ints vs doubles. |
| 110 expect(object.toString(), matcher); |
| 111 } else { |
| 112 expect(object, matcher); |
| 113 } |
| 114 }).whenComplete(() { |
| 115 if (db != null) { |
| 116 db.close(); |
| 117 } |
| 118 return html.window.indexedDB.deleteDatabase(dbName); |
| 119 }); |
| 120 }; |
| 121 |
| 122 void testTypes(testFunction) { |
| 123 test('String', testFunction(123, 'Hoot!', equals('Hoot!'))); |
| 124 test('int', testFunction(123, 12345, equals(12345))); |
| 125 test('List', testFunction(123, [1, 2, 3], equals([1, 2, 3]))); |
| 126 test('List 2', testFunction(123, [2, 3, 4], equals([2, 3, 4]))); |
| 127 test('bool', testFunction(123, [true, false], equals([true, false]))); |
| 128 test('largeInt', testFunction(123, 1371854424211, |
| 129 equals("1371854424211"), null, STORE_NAME, VERSION, true)); |
| 130 test('largeDoubleConvertedToInt', testFunction(123, 1371854424211.0, |
| 131 equals("1371854424211"), null, STORE_NAME, VERSION, true)); |
| 132 test('largeIntInMap', testFunction(123, {'time': 4503599627370492}, |
| 133 equals("{time: 4503599627370492}"), null, STORE_NAME, VERSION, true)); |
| 134 var now = new DateTime.now(); |
| 135 test('DateTime', testFunction(123, now, |
| 136 predicate((date) => |
| 137 date.millisecondsSinceEpoch == now.millisecondsSinceEpoch))); |
| 138 } |
| 139 |
| 140 main() { |
| 141 useHtmlIndividualConfiguration(); |
| 142 |
| 143 // Test that indexed_db is properly flagged as supported or not. |
| 144 // Note that the rest of the indexed_db tests assume that this has been |
| 145 // checked. |
| 146 group('supported', () { |
| 147 test('supported', () { |
| 148 expect(idb.IdbFactory.supported, true); |
| 149 }); |
| 150 }); |
| 151 |
| 152 group('supportsDatabaseNames', () { |
| 153 test('supported', () { |
| 154 expect(html.window.indexedDB.supportsDatabaseNames, isTrue); |
| 155 }); |
| 156 }); |
| 157 |
| 158 group('functional', () { |
| 159 test('throws when unsupported', () { |
| 160 var expectation = idb.IdbFactory.supported ? returnsNormally : throws; |
| 161 |
| 162 expect(() { |
| 163 var db = html.window.indexedDB; |
| 164 db.open('random_db'); |
| 165 }, expectation); |
| 166 }); |
| 167 |
| 168 // Don't bother with these tests if it's unsupported. |
| 169 if (idb.IdbFactory.supported) { |
| 170 test('upgrade', testUpgrade); |
| 171 group('dynamic', () { |
| 172 testTypes(testReadWrite); |
| 173 }); |
| 174 group('typed', () { |
| 175 testTypes(testReadWriteTyped); |
| 176 }); |
| 177 } |
| 178 }); |
| 179 } |
OLD | NEW |