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:html' as html; | 4 import 'dart:html' as html; |
5 import 'dart:indexed_db' as idb; | 5 import 'dart:indexed_db' as idb; |
6 | 6 |
7 const String DB_NAME = 'Test'; | 7 const String DB_NAME = 'Test'; |
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 10 matching lines...) Expand all Loading... |
21 } | 21 } |
22 | 22 |
23 createObjectStore(db) { | 23 createObjectStore(db) { |
24 var store = db.createObjectStore(storeName); | 24 var store = db.createObjectStore(storeName); |
25 expect(store, isNotNull); | 25 expect(store, isNotNull); |
26 } | 26 } |
27 | 27 |
28 step2(e) { | 28 step2(e) { |
29 var transaction = db.transaction(storeName, 'readonly'); | 29 var transaction = db.transaction(storeName, 'readonly'); |
30 var request = transaction.objectStore(storeName).getObject(key); | 30 var request = transaction.objectStore(storeName).getObject(key); |
31 request.on.success.add(expectAsync1((e) { | 31 request.onSuccess.listen(expectAsync1((e) { |
32 var object = e.target.result; | 32 var object = e.target.result; |
33 db.close(); | 33 db.close(); |
34 expect(object, matcher); | 34 expect(object, matcher); |
35 })); | 35 })); |
36 request.on.error.add(fail); | 36 request.onError.listen(fail); |
37 } | 37 } |
38 | 38 |
39 step1() { | 39 step1() { |
40 var transaction = db.transaction([storeName], 'readwrite'); | 40 var transaction = db.transaction([storeName], 'readwrite'); |
41 var request = transaction.objectStore(storeName).put(value, key); | 41 var request = transaction.objectStore(storeName).put(value, key); |
42 request.on.success.add(expectAsync1(step2)); | 42 request.onSuccess.listen(expectAsync1(step2)); |
43 request.on.error.add(fail); | 43 request.onError.listen(fail); |
44 } | 44 } |
45 | 45 |
46 initDb(e) { | 46 initDb(e) { |
47 db = e.target.result; | 47 db = e.target.result; |
48 if (version != db.version) { | 48 if (version != db.version) { |
49 // Legacy 'setVersion' upgrade protocol. Chrome 23 and earlier. | 49 // Legacy 'setVersion' upgrade protocol. Chrome 23 and earlier. |
50 var request = db.setVersion('$version'); | 50 var request = db.setVersion('$version'); |
51 request.on.success.add( | 51 request.onSuccess.listen( |
52 expectAsync1((e) { | 52 expectAsync1((e) { |
53 createObjectStore(db); | 53 createObjectStore(db); |
54 var transaction = e.target.result; | 54 var transaction = e.target.result; |
55 transaction.on.complete.add(expectAsync1((e) => step1())); | 55 transaction.onComplete.listen(expectAsync1((e) => step1())); |
56 transaction.on.error.add(fail); | 56 transaction.onError.listen(fail); |
57 }) | 57 }) |
58 ); | 58 ); |
59 request.on.error.add(fail); | 59 request.onError.listen(fail); |
60 } else { | 60 } else { |
61 step1(); | 61 step1(); |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 openDb(e) { | 65 openDb(e) { |
66 var request = html.window.indexedDB.open(dbName, version); | 66 var request = html.window.indexedDB.open(dbName, version); |
67 expect(request, isNotNull); | 67 expect(request, isNotNull); |
68 request.on.success.add(expectAsync1(initDb)); | 68 request.onSuccess.listen(expectAsync1(initDb)); |
69 request.on.error.add(fail); | 69 request.onError.listen(fail); |
70 if (request is idb.OpenDBRequest) { | 70 if (request is idb.OpenDBRequest) { |
71 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses | 71 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses |
72 // setVersion instead. This path take by FireFox 15, Chrome 24. | 72 // setVersion instead. This path take by FireFox 15, Chrome 24. |
73 request.on.upgradeNeeded.add((e) { | 73 request.onUpgradeNeeded.listen((e) { |
74 guardAsync(() { | 74 guardAsync(() { |
75 createObjectStore(e.target.result); | 75 createObjectStore(e.target.result); |
76 }); | 76 }); |
77 }); | 77 }); |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 // Delete any existing DB. | 81 // Delete any existing DB. |
82 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName); | 82 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName); |
83 deleteRequest.on.success.add(expectAsync1(openDb)); | 83 deleteRequest.onSuccess.listen(expectAsync1(openDb)); |
84 deleteRequest.on.error.add(fail); | 84 deleteRequest.onError.listen(fail); |
85 }; | 85 }; |
86 | 86 |
87 testReadWriteTyped(key, value, matcher, | 87 testReadWriteTyped(key, value, matcher, |
88 [dbName = DB_NAME, | 88 [dbName = DB_NAME, |
89 storeName = STORE_NAME, | 89 storeName = STORE_NAME, |
90 version = VERSION]) => () { | 90 version = VERSION]) => () { |
91 idb.Database db; | 91 idb.Database db; |
92 | 92 |
93 fail(e) { | 93 fail(e) { |
94 guardAsync(() { | 94 guardAsync(() { |
95 throw new Exception('IndexedDB failure'); | 95 throw new Exception('IndexedDB failure'); |
96 }); | 96 }); |
97 } | 97 } |
98 | 98 |
99 createObjectStore(db) { | 99 createObjectStore(db) { |
100 idb.ObjectStore store = db.createObjectStore(storeName); | 100 idb.ObjectStore store = db.createObjectStore(storeName); |
101 expect(store, isNotNull); | 101 expect(store, isNotNull); |
102 } | 102 } |
103 | 103 |
104 step2(e) { | 104 step2(e) { |
105 idb.Transaction transaction = db.transaction(storeName, 'readonly'); | 105 idb.Transaction transaction = db.transaction(storeName, 'readonly'); |
106 idb.Request request = transaction.objectStore(storeName).getObject(key); | 106 idb.Request request = transaction.objectStore(storeName).getObject(key); |
107 request.on.success.add(expectAsync1((e) { | 107 request.onSuccess.listen(expectAsync1((e) { |
108 var object = e.target.result; | 108 var object = e.target.result; |
109 db.close(); | 109 db.close(); |
110 expect(object, matcher); | 110 expect(object, matcher); |
111 })); | 111 })); |
112 request.on.error.add(fail); | 112 request.onError.listen(fail); |
113 } | 113 } |
114 | 114 |
115 step1() { | 115 step1() { |
116 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); | 116 idb.Transaction transaction = db.transaction([storeName], 'readwrite'); |
117 idb.Request request = transaction.objectStore(storeName).put(value, key); | 117 idb.Request request = transaction.objectStore(storeName).put(value, key); |
118 request.on.success.add(expectAsync1(step2)); | 118 request.onSuccess.listen(expectAsync1(step2)); |
119 request.on.error.add(fail); | 119 request.onError.listen(fail); |
120 } | 120 } |
121 | 121 |
122 initDb(e) { | 122 initDb(e) { |
123 db = e.target.result; | 123 db = e.target.result; |
124 if (version != db.version) { | 124 if (version != db.version) { |
125 // Legacy 'setVersion' upgrade protocol. | 125 // Legacy 'setVersion' upgrade protocol. |
126 idb.Request request = db.setVersion('$version'); | 126 idb.Request request = db.setVersion('$version'); |
127 request.on.success.add( | 127 request.onSuccess.listen( |
128 expectAsync1((e) { | 128 expectAsync1((e) { |
129 createObjectStore(db); | 129 createObjectStore(db); |
130 idb.Transaction transaction = e.target.result; | 130 idb.Transaction transaction = e.target.result; |
131 transaction.on.complete.add(expectAsync1((e) => step1())); | 131 transaction.onComplete.listen(expectAsync1((e) => step1())); |
132 transaction.on.error.add(fail); | 132 transaction.onError.listen(fail); |
133 }) | 133 }) |
134 ); | 134 ); |
135 request.on.error.add(fail); | 135 request.onError.listen(fail); |
136 } else { | 136 } else { |
137 step1(); | 137 step1(); |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 openDb(e) { | 141 openDb(e) { |
142 idb.Request request = html.window.indexedDB.open(dbName, version); | 142 idb.Request request = html.window.indexedDB.open(dbName, version); |
143 expect(request, isNotNull); | 143 expect(request, isNotNull); |
144 request.on.success.add(expectAsync1(initDb)); | 144 request.onSuccess.listen(expectAsync1(initDb)); |
145 request.on.error.add(fail); | 145 request.onError.listen(fail); |
146 if (request is idb.OpenDBRequest) { | 146 if (request is idb.OpenDBRequest) { |
147 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses | 147 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses |
148 // setVersion instead. | 148 // setVersion instead. |
149 request.on.upgradeNeeded.add((e) { | 149 request.onUpgradeNeeded.listen((e) { |
150 guardAsync(() { | 150 guardAsync(() { |
151 createObjectStore(e.target.result); | 151 createObjectStore(e.target.result); |
152 }); | 152 }); |
153 }); | 153 }); |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 // Delete any existing DB. | 157 // Delete any existing DB. |
158 idb.Request deleteRequest = html.window.indexedDB.deleteDatabase(dbName); | 158 idb.Request deleteRequest = html.window.indexedDB.deleteDatabase(dbName); |
159 deleteRequest.on.success.add(expectAsync1(openDb)); | 159 deleteRequest.onSuccess.listen(expectAsync1(openDb)); |
160 deleteRequest.on.error.add(fail); | 160 deleteRequest.onError.listen(fail); |
161 }; | 161 }; |
162 | 162 |
163 tests_dynamic() { | 163 tests_dynamic() { |
164 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); | 164 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); |
165 test('test2', testReadWrite(123, 12345, equals(12345))); | 165 test('test2', testReadWrite(123, 12345, equals(12345))); |
166 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); | 166 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); |
167 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); | 167 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); |
168 test('test4', testReadWrite(123, false, equals(false))); | 168 test('test4', testReadWrite(123, false, equals(false))); |
169 } | 169 } |
170 | 170 |
(...skipping 24 matching lines...) Expand all Loading... |
195 var db = html.window.indexedDB; | 195 var db = html.window.indexedDB; |
196 }, expectation); | 196 }, expectation); |
197 }); | 197 }); |
198 | 198 |
199 // Don't bother with these tests if it's unsupported. | 199 // Don't bother with these tests if it's unsupported. |
200 if (idb.IdbFactory.supported) { | 200 if (idb.IdbFactory.supported) { |
201 tests_dynamic(); | 201 tests_dynamic(); |
202 tests_typed(); | 202 tests_typed(); |
203 } | 203 } |
204 } | 204 } |
OLD | NEW |