Chromium Code Reviews| 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 STORE_NAME = 'TEST'; | 8 const String STORE_NAME = 'TEST'; |
| 9 const int VERSION = 1; | 9 const int VERSION = 1; |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 expect(e.oldVersion, 1); | 30 expect(e.oldVersion, 1); |
| 31 expect(e.newVersion, 2); | 31 expect(e.newVersion, 2); |
| 32 upgraded = true; | 32 upgraded = true; |
| 33 }); | 33 }); |
| 34 }).then((_) { | 34 }).then((_) { |
| 35 expect(upgraded, isTrue); | 35 expect(upgraded, isTrue); |
| 36 }); | 36 }); |
| 37 } | 37 } |
| 38 | 38 |
| 39 testReadWrite(key, value, matcher, | 39 testReadWrite(key, value, matcher, |
| 40 [dbName, storeName = STORE_NAME, version = VERSION]) => () { | 40 [dbName, storeName = STORE_NAME, version = VERSION, |
| 41 stringifyResult = false]) => () { | |
| 41 if (dbName == null) { | 42 if (dbName == null) { |
| 42 dbName = nextDatabaseName(); | 43 dbName = nextDatabaseName(); |
| 43 } | 44 } |
| 44 createObjectStore(e) { | 45 createObjectStore(e) { |
| 45 var store = e.target.result.createObjectStore(storeName); | 46 var store = e.target.result.createObjectStore(storeName); |
| 46 expect(store, isNotNull); | 47 expect(store, isNotNull); |
| 47 } | 48 } |
| 48 | 49 |
| 49 var db; | 50 var db; |
| 50 return html.window.indexedDB.deleteDatabase(dbName).then((_) { | 51 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 51 return html.window.indexedDB.open(dbName, version: version, | 52 return html.window.indexedDB.open(dbName, version: version, |
| 52 onUpgradeNeeded: createObjectStore); | 53 onUpgradeNeeded: createObjectStore); |
| 53 }).then((result) { | 54 }).then((result) { |
| 54 db = result; | 55 db = result; |
| 55 var transaction = db.transactionList([storeName], 'readwrite'); | 56 var transaction = db.transactionList([storeName], 'readwrite'); |
| 56 transaction.objectStore(storeName).put(value, key); | 57 transaction.objectStore(storeName).put(value, key); |
| 57 return transaction.completed; | 58 return transaction.completed; |
| 58 }).then((_) { | 59 }).then((_) { |
| 59 var transaction = db.transaction(storeName, 'readonly'); | 60 var transaction = db.transaction(storeName, 'readonly'); |
| 60 return transaction.objectStore(storeName).getObject(key); | 61 return transaction.objectStore(storeName).getObject(key); |
| 61 }).then((object) { | 62 }).then((object) { |
| 62 db.close(); | 63 db.close(); |
| 63 expect(object, matcher); | 64 if (stringifyResult) { |
| 65 // Stringify the numbers to verify that we're correctly returning ints | |
| 66 // as ints vs doubles. | |
| 67 expect(object.toString(), matcher); | |
| 68 } else { | |
| 69 expect(object, matcher); | |
| 70 } | |
| 64 }).whenComplete(() { | 71 }).whenComplete(() { |
| 65 if (db != null) { | 72 if (db != null) { |
| 66 db.close(); | 73 db.close(); |
| 67 } | 74 } |
| 68 return html.window.indexedDB.deleteDatabase(dbName); | 75 return html.window.indexedDB.deleteDatabase(dbName); |
| 69 }); | 76 }); |
| 70 }; | 77 }; |
| 71 | 78 |
| 72 testReadWriteTyped(key, value, matcher, | 79 testReadWriteTyped(key, value, matcher, |
| 73 [dbName, storeName = STORE_NAME, version = VERSION]) => () { | 80 [dbName, storeName = STORE_NAME, version = VERSION, |
| 81 stringifyResult = false]) => () { | |
| 74 if (dbName == null) { | 82 if (dbName == null) { |
| 75 dbName = nextDatabaseName(); | 83 dbName = nextDatabaseName(); |
| 76 } | 84 } |
| 77 void createObjectStore(e) { | 85 void createObjectStore(e) { |
| 78 var store = e.target.result.createObjectStore(storeName); | 86 var store = e.target.result.createObjectStore(storeName); |
| 79 expect(store, isNotNull); | 87 expect(store, isNotNull); |
| 80 } | 88 } |
| 81 | 89 |
| 82 idb.Database db; | 90 idb.Database db; |
| 83 // Delete any existing DBs. | 91 // Delete any existing DBs. |
| 84 return html.window.indexedDB.deleteDatabase(dbName).then((_) { | 92 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 85 return html.window.indexedDB.open(dbName, version: version, | 93 return html.window.indexedDB.open(dbName, version: version, |
| 86 onUpgradeNeeded: createObjectStore); | 94 onUpgradeNeeded: createObjectStore); |
| 87 }).then((idb.Database result) { | 95 }).then((idb.Database result) { |
| 88 db = result; | 96 db = result; |
| 89 idb.Transaction transaction = db.transactionList([storeName], 'readwrite') ; | 97 idb.Transaction transaction = db.transactionList([storeName], 'readwrite') ; |
| 90 transaction.objectStore(storeName).put(value, key); | 98 transaction.objectStore(storeName).put(value, key); |
| 91 | 99 |
| 92 return transaction.completed; | 100 return transaction.completed; |
| 93 }).then((idb.Database result) { | 101 }).then((idb.Database result) { |
| 94 idb.Transaction transaction = db.transaction(storeName, 'readonly'); | 102 idb.Transaction transaction = db.transaction(storeName, 'readonly'); |
| 95 return transaction.objectStore(storeName).getObject(key); | 103 return transaction.objectStore(storeName).getObject(key); |
| 96 }).then((object) { | 104 }).then((object) { |
| 97 db.close(); | 105 db.close(); |
| 98 expect(object, matcher); | 106 if (stringifyResult) { |
| 107 // Stringify the numbers to verify that we're correctly returning ints | |
| 108 // as ints vs doubles. | |
| 109 expect(object.toString(), matcher); | |
| 110 } else { | |
| 111 expect(object, matcher); | |
| 112 } | |
| 99 }).whenComplete(() { | 113 }).whenComplete(() { |
| 100 if (db != null) { | 114 if (db != null) { |
| 101 db.close(); | 115 db.close(); |
| 102 } | 116 } |
| 103 return html.window.indexedDB.deleteDatabase(dbName); | 117 return html.window.indexedDB.deleteDatabase(dbName); |
| 104 }); | 118 }); |
| 105 }; | 119 }; |
| 106 | 120 |
| 107 void testTypes(testFunction) { | 121 void testTypes(testFunction) { |
| 108 test('String', testFunction(123, 'Hoot!', equals('Hoot!'))); | 122 test('String', testFunction(123, 'Hoot!', equals('Hoot!'))); |
| 109 test('int', testFunction(123, 12345, equals(12345))); | 123 test('int', testFunction(123, 12345, equals(12345))); |
| 110 test('List', testFunction(123, [1, 2, 3], equals([1, 2, 3]))); | 124 test('List', testFunction(123, [1, 2, 3], equals([1, 2, 3]))); |
| 111 test('List 2', testFunction(123, [2, 3, 4], equals([2, 3, 4]))); | 125 test('List 2', testFunction(123, [2, 3, 4], equals([2, 3, 4]))); |
| 112 test('bool', testFunction(123, [true, false], equals([true, false]))); | 126 test('bool', testFunction(123, [true, false], equals([true, false]))); |
| 127 test('largeInt', testFunction(123, 1371854424211, | |
| 128 equals("1371854424211"), null, STORE_NAME, VERSION, true)); | |
| 129 test('largeIntInMap', testFunction(123, {'time': 4503599627370492}, | |
| 130 equals("{time: 4503599627370492}"), null, STORE_NAME, VERSION, true)); | |
|
blois
2013/10/17 22:16:15
Can you add a double test or two here too?
| |
| 113 var now = new DateTime.now(); | 131 var now = new DateTime.now(); |
| 114 test('DateTime', testFunction(123, now, | 132 test('DateTime', testFunction(123, now, |
| 115 predicate((date) => | 133 predicate((date) => |
| 116 date.millisecondsSinceEpoch == now.millisecondsSinceEpoch))); | 134 date.millisecondsSinceEpoch == now.millisecondsSinceEpoch))); |
| 117 } | 135 } |
| 118 | 136 |
| 119 main() { | 137 main() { |
| 120 useHtmlIndividualConfiguration(); | 138 useHtmlIndividualConfiguration(); |
| 121 | 139 |
| 122 // Test that indexed_db is properly flagged as supported or not. | 140 // Test that indexed_db is properly flagged as supported or not. |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 149 test('upgrade', testUpgrade); | 167 test('upgrade', testUpgrade); |
| 150 group('dynamic', () { | 168 group('dynamic', () { |
| 151 testTypes(testReadWrite); | 169 testTypes(testReadWrite); |
| 152 }); | 170 }); |
| 153 group('typed', () { | 171 group('typed', () { |
| 154 testTypes(testReadWriteTyped); | 172 testTypes(testReadWriteTyped); |
| 155 }); | 173 }); |
| 156 } | 174 } |
| 157 }); | 175 }); |
| 158 } | 176 } |
| OLD | NEW |