OLD | NEW |
(Empty) | |
| 1 library IndexedDB1Test; |
| 2 import 'package:unittest/unittest.dart'; |
| 3 import 'package:unittest/html_config.dart'; |
| 4 import 'dart:async'; |
| 5 import 'dart:html' as html; |
| 6 import 'dart:indexed_db' as idb; |
| 7 import 'dart:collection'; |
| 8 import 'utils.dart'; |
| 9 |
| 10 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. |
| 11 |
| 12 const String DB_NAME = 'Test2'; |
| 13 const String STORE_NAME = 'TEST'; |
| 14 const int VERSION = 1; |
| 15 |
| 16 testReadWrite(key, value, check, |
| 17 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) { |
| 18 |
| 19 createObjectStore(e) { |
| 20 var store = e.target.result.createObjectStore(storeName); |
| 21 expect(store, isNotNull); |
| 22 } |
| 23 |
| 24 var db; |
| 25 // Delete any existing DBs. |
| 26 return html.window.indexedDB.deleteDatabase(dbName).then(expectAsync((_) { |
| 27 return html.window.indexedDB.open(dbName, version: version, |
| 28 onUpgradeNeeded: createObjectStore); |
| 29 })).then(expectAsync((result) { |
| 30 db = result; |
| 31 var transaction = db.transactionList([storeName], 'readwrite'); |
| 32 transaction.objectStore(storeName).put(value, key); |
| 33 |
| 34 return transaction.completed; |
| 35 })).then(expectAsync((db) { |
| 36 var transaction = db.transaction(storeName, 'readonly'); |
| 37 return transaction.objectStore(storeName).getObject(key); |
| 38 })).then(expectAsync((object) { |
| 39 db.close(); |
| 40 check(value, object); |
| 41 })).catchError((e) { |
| 42 if (db != null) { |
| 43 db.close(); |
| 44 } |
| 45 throw e; |
| 46 }); |
| 47 } |
| 48 |
| 49 List<String> get nonNativeListData { |
| 50 var list = new List<String>(); |
| 51 list.add("data"); |
| 52 list.add("clone"); |
| 53 list.add("error"); |
| 54 list.add("test"); |
| 55 return list; |
| 56 } |
| 57 |
| 58 main() { |
| 59 useHtmlConfiguration(); |
| 60 |
| 61 var obj1 = {'a': 100, 'b': 's'}; |
| 62 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
| 63 |
| 64 var obj3 = {}; |
| 65 obj3['a'] = 100; |
| 66 obj3['b'] = obj3; // Cycle. |
| 67 |
| 68 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. |
| 69 obj4['a'] = 100; |
| 70 obj4['b'] = 's'; |
| 71 |
| 72 var cyclic_list = [1, 2, 3]; |
| 73 cyclic_list[1] = cyclic_list; |
| 74 |
| 75 go(name, data) => test(name, |
| 76 () => testReadWrite(123, data, verifyGraph)); |
| 77 |
| 78 test('test_verifyGraph', () { |
| 79 // Nice to know verifyGraph is working before we rely on it. |
| 80 verifyGraph(obj4, obj4); |
| 81 verifyGraph(obj1, new Map.from(obj1)); |
| 82 verifyGraph(obj4, new Map.from(obj4)); |
| 83 |
| 84 var l1 = [1,2,3]; |
| 85 var l2 = [const [1, 2, 3], const [1, 2, 3]]; |
| 86 verifyGraph([l1, l1], l2); |
| 87 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); |
| 88 |
| 89 verifyGraph(cyclic_list, cyclic_list); |
| 90 }); |
| 91 |
| 92 // Don't bother with these tests if it's unsupported. |
| 93 // Support is tested in indexeddb_1_test |
| 94 if (idb.IdbFactory.supported) { |
| 95 go('test_simple', obj1); |
| 96 go('test_DAG', obj2); |
| 97 go('test_cycle', obj3); |
| 98 go('test_simple_splay', obj4); |
| 99 go('const_array_1', const [const [1], const [2]]); |
| 100 go('const_array_dag', const [const [1], const [1]]); |
| 101 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
| 102 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
| 103 go('cyclic_list', cyclic_list); |
| 104 go('non-native lists', nonNativeListData); |
| 105 } |
| 106 } |
OLD | NEW |