Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tests/html/indexeddb_1_test.dart

Issue 12159005: Dartifying some IndexedDB APIs. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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:html' as html; 5 import 'dart:html' as html;
5 import 'dart:indexed_db' as idb; 6 import 'dart:indexed_db' as idb;
7 import 'utils.dart';
6 8
7 const String DB_NAME = 'Test'; 9 const String DB_NAME = 'Test';
8 const String STORE_NAME = 'TEST'; 10 const String STORE_NAME = 'TEST';
9 const int VERSION = 1; 11 const int VERSION = 1;
10 12
13 Future testUpgrade() {
14 var dbName = 'version_db';
15 var upgraded = false;
16
17 return chainSteps([
sra1 2013/02/08 02:30:28 This does not seem much of a win over an explicit
blois 2013/02/08 22:18:56 True- when I had the guardAsync around the methods
18 (_) {
19 // Delete any existing DBs.
20 return html.window.indexedDB.deleteDatabase(dbName);
21 },
22 (_) {
23 return html.window.indexedDB.open(dbName, version: 1,
24 onUpgradeNeeded: (e) {});
25 },
26 (db) {
27 db.close();
28 },
29 (_) {
30 return html.window.indexedDB.open(dbName, version: 2,
31 onUpgradeNeeded: (e) {
32 // Bug 8265, we're getting the wrong type here.
33 //expect(e.oldVersion, 1);
34 //expect(e.newVersion, 2);
35 upgraded = true;
36 });
37 },
38 (_) {
39 expect(upgraded, isTrue);
40 }
41 ]);
42 }
43
11 testReadWrite(key, value, matcher, 44 testReadWrite(key, value, matcher,
12 [dbName = DB_NAME, 45 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () {
13 storeName = STORE_NAME, 46 createObjectStore(e) {
14 version = VERSION]) => () { 47 var store = e.target.result.createObjectStore(storeName);
15 var db;
16
17 fail(e) {
18 guardAsync(() {
19 throw new Exception('IndexedDB failure');
20 });
21 }
22
23 createObjectStore(db) {
24 var store = db.createObjectStore(storeName);
25 expect(store, isNotNull); 48 expect(store, isNotNull);
26 } 49 }
27 50
28 step2(e) { 51 var db;
29 var transaction = db.transaction(storeName, 'readonly'); 52 return chainSteps([
30 var request = transaction.objectStore(storeName).getObject(key); 53 (_) {
31 request.onSuccess.listen(expectAsync1((e) { 54 // Delete any existing DBs.
32 var object = e.target.result; 55 return html.window.indexedDB.deleteDatabase(dbName);
56 },
57 (_) {
58 return html.window.indexedDB.open(dbName, version: version,
59 onUpgradeNeeded: createObjectStore);
60 },
61 (result) {
62 db = result;
63 var transaction = db.transaction([storeName], 'readwrite');
64 transaction.objectStore(storeName).put(value, key);
65 return transaction.completed;
66 },
67 (_) {
68 var transaction = db.transaction(storeName, 'readonly');
69 return transaction.objectStore(storeName).getObject(key);
70 },
71 (object) {
33 db.close(); 72 db.close();
34 expect(object, matcher); 73 expect(object, matcher);
35 }));
36 request.onError.listen(fail);
37 }
38
39 step1() {
40 var transaction = db.transaction([storeName], 'readwrite');
41 var request = transaction.objectStore(storeName).put(value, key);
42 request.onError.listen(fail);
43
44 transaction.onComplete.listen(expectAsync1(step2));
45 }
46
47 initDb(e) {
48 db = e.target.result;
49 if (version != db.version) {
50 // Legacy 'setVersion' upgrade protocol. Chrome 23 and earlier.
51 var request = db.setVersion('$version');
52 request.onSuccess.listen(
53 expectAsync1((e) {
54 createObjectStore(db);
55 var transaction = e.target.result;
56 transaction.onComplete.listen(expectAsync1((e) => step1()));
57 transaction.onError.listen(fail);
58 })
59 );
60 request.onError.listen(fail);
61 } else {
62 step1();
63 } 74 }
64 } 75 ]).whenComplete(() {
65 76 if (db != null) {
66 openDb(e) { 77 db.close();
67 var request = html.window.indexedDB.open(dbName, version);
68 expect(request, isNotNull);
69 request.onSuccess.listen(expectAsync1(initDb));
70 request.onError.listen(fail);
71 if (request is idb.OpenDBRequest) {
72 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
73 // setVersion instead. This path take by FireFox 15, Chrome 24.
74 request.onUpgradeNeeded.listen((e) {
75 guardAsync(() {
76 createObjectStore(e.target.result);
77 });
78 });
79 } 78 }
80 } 79 });
81
82 // Delete any existing DB.
83 var deleteRequest = html.window.indexedDB.deleteDatabase(dbName);
84 deleteRequest.onSuccess.listen(expectAsync1(openDb));
85 deleteRequest.onError.listen(fail);
86 }; 80 };
87 81
88 testReadWriteTyped(key, value, matcher, 82 testReadWriteTyped(key, value, matcher,
89 [dbName = DB_NAME, 83 [dbName = DB_NAME, storeName = STORE_NAME, version = VERSION]) => () {
90 storeName = STORE_NAME, 84 void createObjectStore(e) {
91 version = VERSION]) => () { 85 var store = e.target.result.createObjectStore(storeName);
92 idb.Database db;
93
94 fail(e) {
95 guardAsync(() {
96 throw new Exception('IndexedDB failure');
97 });
98 }
99
100 createObjectStore(db) {
101 idb.ObjectStore store = db.createObjectStore(storeName);
102 expect(store, isNotNull); 86 expect(store, isNotNull);
103 } 87 }
104 88
105 step2(e) { 89 idb.Database db;
106 idb.Transaction transaction = db.transaction(storeName, 'readonly'); 90 return chainSteps([
107 idb.Request request = transaction.objectStore(storeName).getObject(key); 91 (_) {
108 request.onSuccess.listen(expectAsync1((e) { 92 // Delete any existing DBs.
109 var object = e.target.result; 93 return html.window.indexedDB.deleteDatabase(dbName);
94 },
95 (_) {
96 return html.window.indexedDB.open(dbName, version: version,
97 onUpgradeNeeded: createObjectStore);
98 },
99 (idb.Database result) {
100 db = result;
101 idb.Transaction transaction = db.transaction([storeName], 'readwrite');
102 transaction.objectStore(storeName).put(value, key);
103
104 return transaction.completed;
105 },
106 (idb.Database result) {
107 idb.Transaction transaction = db.transaction(storeName, 'readonly');
108 return transaction.objectStore(storeName).getObject(key);
109 },
110 (object) {
110 db.close(); 111 db.close();
111 expect(object, matcher); 112 expect(object, matcher);
112 }));
113 request.onError.listen(fail);
114 }
115
116 step1() {
117 idb.Transaction transaction = db.transaction([storeName], 'readwrite');
118 idb.Request request = transaction.objectStore(storeName).put(value, key);
119 request.onError.listen(fail);
120
121 transaction.onComplete.listen(expectAsync1(step2));
122 }
123
124 initDb(e) {
125 db = e.target.result;
126 if (version != db.version) {
127 // Legacy 'setVersion' upgrade protocol.
128 idb.Request request = db.setVersion('$version');
129 request.onSuccess.listen(
130 expectAsync1((e) {
131 createObjectStore(db);
132 idb.Transaction transaction = e.target.result;
133 transaction.onComplete.listen(expectAsync1((e) => step1()));
134 transaction.onError.listen(fail);
135 })
136 );
137 request.onError.listen(fail);
138 } else {
139 step1();
140 } 113 }
141 } 114 ]).whenComplete(() {
142 115 if (db != null) {
143 openDb(e) { 116 db.close();
144 idb.Request request = html.window.indexedDB.open(dbName, version);
145 expect(request, isNotNull);
146 request.onSuccess.listen(expectAsync1(initDb));
147 request.onError.listen(fail);
148 if (request is idb.OpenDBRequest) {
149 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
150 // setVersion instead.
151 request.onUpgradeNeeded.listen((e) {
152 guardAsync(() {
153 createObjectStore(e.target.result);
154 });
155 });
156 } 117 }
157 } 118 });
158
159 // Delete any existing DB.
160 idb.Request deleteRequest = html.window.indexedDB.deleteDatabase(dbName);
161 deleteRequest.onSuccess.listen(expectAsync1(openDb));
162 deleteRequest.onError.listen(fail);
163 }; 119 };
164 120
165 tests_dynamic() { 121 tests_dynamic() {
166 test('test1', testReadWrite(123, 'Hoot!', equals('Hoot!'))); 122 futureTest('test1', testReadWrite(123, 'Hoot!', equals('Hoot!')));
167 test('test2', testReadWrite(123, 12345, equals(12345))); 123 futureTest('test2', testReadWrite(123, 12345, equals(12345)));
168 test('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3]))); 124 futureTest('test3', testReadWrite(123, [1, 2, 3], equals([1, 2, 3])));
169 test('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4]))); 125 futureTest('test4', testReadWrite(123, [2, 3, 4], equals([2, 3, 4])));
170 test('test4', testReadWrite(123, false, equals(false))); 126 futureTest('test4', testReadWrite(123, false, equals(false)));
171 } 127 }
172 128
173 tests_typed() { 129 tests_typed() {
174 test('test1', testReadWriteTyped(123, 'Hoot!', equals('Hoot!'))); 130 futureTest('test1', testReadWriteTyped(123, 'Hoot!', equals('Hoot!')));
175 test('test2', testReadWriteTyped(123, 12345, equals(12345))); 131 futureTest('test2', testReadWriteTyped(123, 12345, equals(12345)));
176 test('test3', testReadWriteTyped(123, [1, 2, 3], equals([1, 2, 3]))); 132 futureTest('test3', testReadWriteTyped(123, [1, 2, 3], equals([1, 2, 3])));
177 test('test4', testReadWriteTyped(123, [2, 3, 4], equals([2, 3, 4]))); 133 futureTest('test4', testReadWriteTyped(123, [2, 3, 4], equals([2, 3, 4])));
178 test('test4', testReadWriteTyped(123, false, equals(false))); 134 futureTest('test4', testReadWriteTyped(123, false, equals(false)));
179 } 135 }
180 136
181 main() { 137 main() {
182 useHtmlIndividualConfiguration(); 138 useHtmlIndividualConfiguration();
183 139
184 // Test that indexed_db is properly flagged as supported or not. 140 // Test that indexed_db is properly flagged as supported or not.
185 // Note that the rest of the indexed_db tests assume that this has been 141 // Note that the rest of the indexed_db tests assume that this has been
186 // checked. 142 // checked.
187 group('supported', () { 143 group('supported', () {
188 test('supported', () { 144 test('supported', () {
189 expect(idb.IdbFactory.supported, true); 145 expect(idb.IdbFactory.supported, true);
190 }); 146 });
191 }); 147 });
192 148
193 test('throws when unsupported', () { 149 group('functional', () {
194 var expectation = idb.IdbFactory.supported ? returnsNormally : throws; 150 test('throws when unsupported', () {
151 var expectation = idb.IdbFactory.supported ? returnsNormally : throws;
195 152
196 expect(() { 153 expect(() {
197 var db = html.window.indexedDB; 154 var db = html.window.indexedDB;
198 }, expectation); 155 }, expectation);
156 });
157
158 // Don't bother with these tests if it's unsupported.
159 if (idb.IdbFactory.supported) {
160 futureTest('upgrade', testUpgrade);
161 tests_dynamic();
162 tests_typed();
163 }
199 }); 164 });
200
201 // Don't bother with these tests if it's unsupported.
202 if (idb.IdbFactory.supported) {
203 tests_dynamic();
204 tests_typed();
205 }
206 } 165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698