| 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_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
| 4 import 'dart:async'; |
| 4 import 'dart:html' as html; | 5 import 'dart:html' as html; |
| 5 import 'dart:indexed_db' as idb; | 6 import 'dart:indexed_db' as idb; |
| 6 import 'dart:collection'; | 7 import 'dart:collection'; |
| 7 import 'utils.dart'; | 8 import 'utils.dart'; |
| 8 | 9 |
| 9 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. | 10 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. |
| 10 | 11 |
| 11 const String DB_NAME = 'Test'; | 12 const String DB_NAME = 'Test'; |
| 12 const String STORE_NAME = 'TEST'; | 13 const String STORE_NAME = 'TEST'; |
| 13 const int VERSION = 1; | 14 const int VERSION = 1; |
| 14 | 15 |
| 15 testReadWrite(key, value, check, | 16 testReadWrite(key, value, check, |
| 16 [dbName = DB_NAME, | 17 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) { |
| 17 storeName = STORE_NAME, | |
| 18 version = VERSION]) => () { | |
| 19 var db; | |
| 20 | 18 |
| 21 fail(e) { | 19 createObjectStore(e) { |
| 22 guardAsync(() { | 20 var store = e.target.result.createObjectStore(storeName); |
| 23 throw new Exception('IndexedDB failure'); | |
| 24 }); | |
| 25 } | |
| 26 | |
| 27 createObjectStore(db) { | |
| 28 var store = db.createObjectStore(storeName); | |
| 29 expect(store, isNotNull); | 21 expect(store, isNotNull); |
| 30 } | 22 } |
| 31 | 23 |
| 32 step2(e) { | 24 var db; |
| 33 var transaction = db.transaction(storeName, 'readonly'); | 25 // Delete any existing DBs. |
| 34 var request = transaction.objectStore(storeName).getObject(key); | 26 return html.window.indexedDB.deleteDatabase(dbName).then((_) { |
| 35 request.onSuccess.listen(expectAsync1((e) { | 27 html.window.console.log('open'); |
| 36 var object = e.target.result; | 28 return html.window.indexedDB.open(dbName, version: version, |
| 29 onUpgradeNeeded: createObjectStore); |
| 30 }).then((result) { |
| 31 html.window.console.log('write'); |
| 32 db = result; |
| 33 var transaction = db.transaction([storeName], 'readwrite'); |
| 34 transaction.objectStore(storeName).put(value, key); |
| 35 |
| 36 return transaction.completed; |
| 37 }).then((db) { |
| 38 html.window.console.log('read'); |
| 39 var transaction = db.transaction(storeName, 'readonly'); |
| 40 return transaction.objectStore(storeName).getObject(key); |
| 41 }).then((object) { |
| 42 html.window.console.log('got close'); |
| 37 db.close(); | 43 db.close(); |
| 38 check(value, object); | 44 check(value, object); |
| 39 })); | 45 }).catchError((e) { |
| 40 request.onError.listen(fail); | 46 if (db != null) { |
| 41 } | 47 db.close(); |
| 42 | 48 } |
| 43 step1() { | 49 throw e; |
| 44 var transaction = db.transaction([storeName], 'readwrite'); | 50 }); |
| 45 var request = transaction.objectStore(storeName).put(value, key); | 51 } |
| 46 request.onError.listen(fail); | |
| 47 | |
| 48 transaction.onComplete.listen(expectAsync1(step2)); | |
| 49 } | |
| 50 | |
| 51 initDb(e) { | |
| 52 db = e.target.result; | |
| 53 if (version != db.version) { | |
| 54 // Legacy 'setVersion' upgrade protocol. | |
| 55 var request = db.setVersion('$version'); | |
| 56 request.onSuccess.listen( | |
| 57 expectAsync1((e) { | |
| 58 createObjectStore(db); | |
| 59 var transaction = e.target.result; | |
| 60 transaction.onComplete.listen(expectAsync1((e) => step1())); | |
| 61 transaction.onError.listen(fail); | |
| 62 }) | |
| 63 ); | |
| 64 request.onError.listen(fail); | |
| 65 } else { | |
| 66 step1(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 openDb(e) { | |
| 71 var request = html.window.indexedDB.open(dbName, version); | |
| 72 expect(request, isNotNull); | |
| 73 request.onSuccess.listen(expectAsync1(initDb)); | |
| 74 request.onError.listen(fail); | |
| 75 if (request is idb.OpenDBRequest) { | |
| 76 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses | |
| 77 // setVersion instead. | |
| 78 request.onUpgradeNeeded.listen((e) { | |
| 79 guardAsync(() { | |
| 80 createObjectStore(e.target.result); | |
| 81 }); | |
| 82 }); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 // Delete any existing DB. | |
| 87 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName); | |
| 88 deleteRequest.onSuccess.listen(expectAsync1(openDb)); | |
| 89 deleteRequest.onError.listen(fail); | |
| 90 }; | |
| 91 | 52 |
| 92 | 53 |
| 93 main() { | 54 main() { |
| 94 useHtmlConfiguration(); | 55 useHtmlConfiguration(); |
| 95 | 56 |
| 96 var obj1 = {'a': 100, 'b': 's'}; | 57 var obj1 = {'a': 100, 'b': 's'}; |
| 97 var obj2 = {'x': obj1, 'y': obj1}; // DAG. | 58 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
| 98 | 59 |
| 99 var obj3 = {}; | 60 var obj3 = {}; |
| 100 obj3['a'] = 100; | 61 obj3['a'] = 100; |
| 101 obj3['b'] = obj3; // Cycle. | 62 obj3['b'] = obj3; // Cycle. |
| 102 | 63 |
| 103 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. | 64 var obj4 = new SplayTreeMap<String, dynamic>(); // Different implementation. |
| 104 obj4['a'] = 100; | 65 obj4['a'] = 100; |
| 105 obj4['b'] = 's'; | 66 obj4['b'] = 's'; |
| 106 | 67 |
| 107 var cyclic_list = [1, 2, 3]; | 68 var cyclic_list = [1, 2, 3]; |
| 108 cyclic_list[1] = cyclic_list; | 69 cyclic_list[1] = cyclic_list; |
| 109 | 70 |
| 110 go(name, data) => test(name, testReadWrite(123, data, verifyGraph)); | 71 go(name, data) => futureTest(name, |
| 72 () => testReadWrite(123, data, verifyGraph)); |
| 111 | 73 |
| 112 test('test_verifyGraph', () { | 74 test('test_verifyGraph', () { |
| 113 // Nice to know verifyGraph is working before we rely on it. | 75 // Nice to know verifyGraph is working before we rely on it. |
| 114 verifyGraph(obj4, obj4); | 76 verifyGraph(obj4, obj4); |
| 115 verifyGraph(obj1, new Map.from(obj1)); | 77 verifyGraph(obj1, new Map.from(obj1)); |
| 116 verifyGraph(obj4, new Map.from(obj4)); | 78 verifyGraph(obj4, new Map.from(obj4)); |
| 117 | 79 |
| 118 var l1 = [1,2,3]; | 80 var l1 = [1,2,3]; |
| 119 var l2 = [const [1, 2, 3], const [1, 2, 3]]; | 81 var l2 = [const [1, 2, 3], const [1, 2, 3]]; |
| 120 verifyGraph([l1, l1], l2); | 82 verifyGraph([l1, l1], l2); |
| 121 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); | 83 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); |
| 122 | 84 |
| 123 verifyGraph(cyclic_list, cyclic_list); | 85 verifyGraph(cyclic_list, cyclic_list); |
| 124 }); | 86 }); |
| 125 | 87 |
| 126 // Don't bother with these tests if it's unsupported. | 88 // Don't bother with these tests if it's unsupported. |
| 127 // Support is tested in indexeddb_1_test | 89 // Support is tested in indexeddb_1_test |
| 128 if (idb.IdbFactory.supported) { | 90 if (idb.IdbFactory.supported) { |
| 129 go('test_simple', obj1); | 91 go('test_simple', obj1); |
| 130 go('test_DAG', obj2); | 92 go('test_DAG', obj2); |
| 131 go('test_cycle', obj3); | 93 go('test_cycle', obj3); |
| 132 go('test_simple_splay', obj4); | 94 go('test_simple_splay', obj4); |
| 133 go('const_array_1', const [const [1], const [2]]); | 95 go('const_array_1', const [const [1], const [2]]); |
| 134 go('const_array_dag', const [const [1], const [1]]); | 96 go('const_array_dag', const [const [1], const [1]]); |
| 135 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | 97 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
| 136 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | 98 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
| 137 go('cyclic_list', cyclic_list); | 99 go('cyclic_list', cyclic_list); |
| 138 } | 100 } |
| 139 } | 101 } |
| OLD | NEW |