OLD | NEW |
1 #library('IndexedDB1Test'); | 1 #library('IndexedDB1Test'); |
2 #import('../../pkg/unittest/unittest.dart'); | 2 #import('../../pkg/unittest/unittest.dart'); |
3 #import('../../pkg/unittest/html_config.dart'); | 3 #import('../../pkg/unittest/html_config.dart'); |
4 #import('dart:html'); | 4 #import('dart:html'); |
5 #import('dart:coreimpl'); | 5 #import('dart:coreimpl'); |
6 #import('utils.dart'); | 6 #import('utils.dart'); |
7 | 7 |
8 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. | 8 // Write and re-read Maps: simple Maps; Maps with DAGs; Maps with cycles. |
9 | 9 |
10 const String DB_NAME = 'Test'; | 10 const String DB_NAME = 'Test'; |
11 const String STORE_NAME = 'TEST'; | 11 const String STORE_NAME = 'TEST'; |
12 const String VERSION = '1'; | 12 const int VERSION = 1; |
13 | 13 |
14 testReadWrite(key, value, check, | 14 testReadWrite(key, value, check, |
15 [dbName = DB_NAME, | 15 [dbName = DB_NAME, |
16 storeName = STORE_NAME, | 16 storeName = STORE_NAME, |
17 version = VERSION]) => () { | 17 version = VERSION]) => () { |
18 var db; | 18 var db; |
19 | 19 |
20 fail(e) { | 20 fail(e) { |
21 guardAsync(() { | 21 guardAsync(() { |
22 Expect.fail('IndexedDB failure'); | 22 throw const Exception('IndexedDB failure'); |
23 }); | 23 }); |
24 } | 24 } |
25 | 25 |
26 createObjectStore() { | 26 createObjectStore(db) { |
27 var store = db.createObjectStore(storeName); | 27 var store = db.createObjectStore(storeName); |
28 Expect.isNotNull(store); | 28 expect(store, isNotNull); |
29 } | 29 } |
30 | 30 |
31 step2(e) { | 31 step2(e) { |
32 var transaction = db.transaction(storeName, 'readonly'); | 32 var transaction = db.transaction(storeName, 'readonly'); |
33 var request = transaction.objectStore(storeName).getObject(key); | 33 var request = transaction.objectStore(storeName).getObject(key); |
34 request.on.success.add(expectAsync1((e) { | 34 request.on.success.add(expectAsync1((e) { |
35 var object = e.target.result; | 35 var object = e.target.result; |
36 check(value, object); | 36 db.close(); |
37 }) | 37 check(value, object); |
38 ); | 38 })); |
39 request.on.error.add(fail); | 39 request.on.error.add(fail); |
40 } | 40 } |
41 | 41 |
42 step1() { | 42 step1() { |
43 var transaction = db.transaction([storeName], 'readwrite'); | 43 var transaction = db.transaction([storeName], 'readwrite'); |
44 var request = transaction.objectStore(storeName).put(value, key); | 44 var request = transaction.objectStore(storeName).put(value, key); |
45 request.on.success.add(expectAsync1(step2)); | 45 request.on.success.add(expectAsync1(step2)); |
46 request.on.error.add(fail); | 46 request.on.error.add(fail); |
47 } | 47 } |
48 | 48 |
49 initDb(e) { | 49 initDb(e) { |
50 db = e.target.result; | 50 db = e.target.result; |
51 if (version != db.version) { | 51 if (version != db.version) { |
52 // TODO. Some browsers do this the w3 way - passing the version to the | 52 // Legacy 'setVersion' upgrade protocol. |
53 // open call and listening to onversionchange. Can we feature-detect the | 53 var request = db.setVersion('$version'); |
54 // difference and make it work? | |
55 var request = db.setVersion(version); | |
56 request.on.success.add( | 54 request.on.success.add( |
57 expectAsync1((e) { | 55 expectAsync1((e) { |
58 createObjectStore(); | 56 createObjectStore(db); |
59 | |
60 var transaction = e.target.result; | 57 var transaction = e.target.result; |
61 transaction.on.complete.add(expectAsync1((e) => step1())); | 58 transaction.on.complete.add(expectAsync1((e) => step1())); |
62 transaction.on.error.add(fail); | 59 transaction.on.error.add(fail); |
63 }) | 60 }) |
64 ); | 61 ); |
65 request.on.error.add(fail); | 62 request.on.error.add(fail); |
66 } else { | 63 } else { |
67 step1(); | 64 step1(); |
68 } | 65 } |
69 } | 66 } |
70 | 67 |
71 var request = window.indexedDB.open(dbName); | 68 openDb(e) { |
72 Expect.isNotNull(request); | 69 var request = window.indexedDB.open(dbName, version); |
73 request.on.success.add(expectAsync1(initDb)); | 70 expect(request, isNotNull); |
74 request.on.error.add(fail); | 71 request.on.success.add(expectAsync1(initDb)); |
| 72 request.on.error.add(fail); |
| 73 if (request is IDBOpenDBRequest) { |
| 74 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses |
| 75 // setVersion instead. |
| 76 request.on.upgradeNeeded.add((e) { |
| 77 guardAsync(() { |
| 78 createObjectStore(e.target.result); |
| 79 }); |
| 80 }); |
| 81 } |
| 82 } |
| 83 |
| 84 // Delete any existing DB. |
| 85 var deleteRequest = window.indexedDB.deleteDatabase(dbName); |
| 86 deleteRequest.on.success.add(expectAsync1(openDb)); |
| 87 deleteRequest.on.error.add(fail); |
75 }; | 88 }; |
76 | 89 |
77 | 90 |
78 main() { | 91 main() { |
79 useHtmlConfiguration(); | 92 useHtmlConfiguration(); |
80 | 93 |
81 var obj1 = {'a': 100, 'b': 's'}; | 94 var obj1 = {'a': 100, 'b': 's'}; |
82 var obj2 = {'x': obj1, 'y': obj1}; // DAG. | 95 var obj2 = {'x': obj1, 'y': obj1}; // DAG. |
83 | 96 |
84 var obj3 = {}; | 97 var obj3 = {}; |
85 obj3['a'] = 100; | 98 obj3['a'] = 100; |
86 obj3['b'] = obj3; // Cycle. | 99 obj3['b'] = obj3; // Cycle. |
87 | 100 |
88 var obj4 = new SplayTreeMap<String, Dynamic>(); // Different implementation. | 101 var obj4 = new SplayTreeMap<String, Dynamic>(); // Different implementation. |
89 obj4['a'] = 100; | 102 obj4['a'] = 100; |
90 obj4['b'] = 's'; | 103 obj4['b'] = 's'; |
91 | 104 |
92 var cyclic_list = [1, 2, 3]; | 105 var cyclic_list = [1, 2, 3]; |
93 cyclic_list[1] = cyclic_list; | 106 cyclic_list[1] = cyclic_list; |
94 | 107 |
95 go(name, data) => test(name, testReadWrite(123, data, verifyGraph)); | 108 go(name, data) => test(name, testReadWrite(123, data, verifyGraph)); |
96 | 109 |
97 test('test_verifyGraph', () { | 110 test('test_verifyGraph', () { |
98 // Nice to know verifyGraph is working before we rely on it. | 111 // Nice to know verifyGraph is working before we rely on it. |
99 Expect.mapEquals(obj4, obj4); | |
100 verifyGraph(obj4, obj4); | 112 verifyGraph(obj4, obj4); |
101 verifyGraph(obj1, new Map.from(obj1)); | 113 verifyGraph(obj1, new Map.from(obj1)); |
102 verifyGraph(obj4, new Map.from(obj4)); | 114 verifyGraph(obj4, new Map.from(obj4)); |
103 | 115 |
104 var l1 = [1,2,3]; | 116 var l1 = [1,2,3]; |
105 var l2 = [const [1, 2, 3], const [1, 2, 3]]; | 117 var l2 = [const [1, 2, 3], const [1, 2, 3]]; |
106 verifyGraph([l1, l1], l2); | 118 verifyGraph([l1, l1], l2); |
107 Expect.throws(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2)); | 119 expect(() => verifyGraph([[1, 2, 3], [1, 2, 3]], l2), throws); |
108 | 120 |
109 verifyGraph(cyclic_list, cyclic_list); | 121 verifyGraph(cyclic_list, cyclic_list); |
110 }); | 122 }); |
111 | 123 |
112 go('test_simple', obj1); | 124 go('test_simple', obj1); |
113 go('test_DAG', obj2); | 125 go('test_DAG', obj2); |
114 go('test_cycle', obj3); | 126 go('test_cycle', obj3); |
115 go('test_simple_splay', obj4); | 127 go('test_simple_splay', obj4); |
116 go('const_array_1', const [const [1], const [2]]); | 128 go('const_array_1', const [const [1], const [2]]); |
117 go('const_array_dag', const [const [1], const [1]]); | 129 go('const_array_dag', const [const [1], const [1]]); |
118 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); | 130 go('array_deferred_copy', [1,2,3, obj3, obj3, 6]); |
119 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); | 131 go('array_deferred_copy_2', [1,2,3, [4, 5, obj3], [obj3, 6]]); |
120 go('cyclic_list', cyclic_list); | 132 go('cyclic_list', cyclic_list); |
121 } | 133 } |
OLD | NEW |