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

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

Issue 11052023: Update indexeddb tests to work with both methods of upgrading a database. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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
« no previous file with comments | « tests/html/html.status ('k') | tests/html/indexeddb_2_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 5
6 const String DB_NAME = 'Test'; 6 const String DB_NAME = 'Test';
7 const String STORE_NAME = 'TEST'; 7 const String STORE_NAME = 'TEST';
8 const String VERSION = '1'; 8 const int VERSION = 1;
9 9
10 testReadWrite(key, value, check, 10 testReadWrite(key, value, check,
11 [dbName = DB_NAME, 11 [dbName = DB_NAME,
12 storeName = STORE_NAME, 12 storeName = STORE_NAME,
13 version = VERSION]) => () { 13 version = VERSION]) => () {
14 var db; 14 var db;
15 15
16 fail(e) { 16 fail(e) {
17 guardAsync(() { 17 guardAsync(() {
18 Expect.fail('IndexedDB failure'); 18 throw const Exception('IndexedDB failure');
19 }); 19 });
20 } 20 }
21 21
22 createObjectStore() { 22 createObjectStore(db) {
23 var store = db.createObjectStore(storeName); 23 var store = db.createObjectStore(storeName);
24 Expect.isNotNull(store); 24 expect(store, isNotNull);
25 } 25 }
26 26
27 step2(e) { 27 step2(e) {
28 var transaction = db.transaction(storeName, 'readonly'); 28 var transaction = db.transaction(storeName, 'readonly');
29 var request = transaction.objectStore(storeName).getObject(key); 29 var request = transaction.objectStore(storeName).getObject(key);
30 request.on.success.add(expectAsync1((e) { 30 request.on.success.add(expectAsync1((e) {
31 var object = e.target.result; 31 var object = e.target.result;
32 db.close();
32 check(value, object); 33 check(value, object);
33 })); 34 }));
34 request.on.error.add(fail); 35 request.on.error.add(fail);
35 } 36 }
36 37
37 step1() { 38 step1() {
38 var transaction = db.transaction([storeName], 'readwrite'); 39 var transaction = db.transaction([storeName], 'readwrite');
39 var request = transaction.objectStore(storeName).put(value, key); 40 var request = transaction.objectStore(storeName).put(value, key);
40 request.on.success.add(expectAsync1(step2)); 41 request.on.success.add(expectAsync1(step2));
41 request.on.error.add(fail); 42 request.on.error.add(fail);
42 } 43 }
43 44
44 initDb(e) { 45 initDb(e) {
45 db = e.target.result; 46 db = e.target.result;
46 if (version != db.version) { 47 if (version != db.version) {
47 // TODO. Some browsers do this the w3 way - passing the version to the 48 // Legacy 'setVersion' upgrade protocol.
48 // open call and listening to onversionchange. Can we feature-detect the 49 var request = db.setVersion('$version');
49 // difference and make it work?
50 var request = db.setVersion(version);
51 request.on.success.add( 50 request.on.success.add(
52 expectAsync1((e) { 51 expectAsync1((e) {
53 createObjectStore(); 52 createObjectStore(db);
54
55 var transaction = e.target.result; 53 var transaction = e.target.result;
56 transaction.on.complete.add(expectAsync1((e) => step1())); 54 transaction.on.complete.add(expectAsync1((e) => step1()));
57 transaction.on.error.add(fail); 55 transaction.on.error.add(fail);
58 }) 56 })
59 ); 57 );
60 request.on.error.add(fail); 58 request.on.error.add(fail);
61 } else { 59 } else {
62 step1(); 60 step1();
63 } 61 }
64 } 62 }
65 63
66 var request = window.indexedDB.open(dbName); 64 openDb(e) {
67 Expect.isNotNull(request); 65 var request = window.indexedDB.open(dbName, version);
68 request.on.success.add(expectAsync1(initDb)); 66 expect(request, isNotNull);
69 request.on.error.add(fail); 67 request.on.success.add(expectAsync1(initDb));
68 request.on.error.add(fail);
69 if (request is IDBOpenDBRequest) {
vsm 2012/10/04 15:42:10 Can you add some comments on which browsers take w
70 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
71 // setVersion instead.
72 request.on.upgradeNeeded.add((e) {
73 guardAsync(() {
74 createObjectStore(e.target.result);
75 });
76 });
77 }
78 }
79
80 // Delete any existing DB.
81 var deleteRequest = window.indexedDB.deleteDatabase(dbName);
82 deleteRequest.on.success.add(expectAsync1(openDb));
83 deleteRequest.on.error.add(fail);
70 }; 84 };
71 85
72 testReadWriteTyped(key, value, check, 86 testReadWriteTyped(key, value, check,
73 [dbName = DB_NAME, 87 [dbName = DB_NAME,
74 storeName = STORE_NAME, 88 storeName = STORE_NAME,
75 version = VERSION]) => () { 89 version = VERSION]) => () {
76 IDBDatabase db; 90 IDBDatabase db;
77 91
78 fail(e) { 92 fail(e) {
79 guardAsync(() { 93 guardAsync(() {
80 Expect.fail('IndexedDB failure'); 94 throw const Exception('IndexedDB failure');
81 }); 95 });
82 } 96 }
83 97
84 createObjectStore() { 98 createObjectStore(db) {
85 IDBObjectStore store = db.createObjectStore(storeName); 99 IDBObjectStore store = db.createObjectStore(storeName);
86 Expect.isNotNull(store); 100 expect(store, isNotNull);
87 } 101 }
88 102
89 step2(e) { 103 step2(e) {
90 IDBTransaction transaction = db.transaction(storeName, 'readonly'); 104 IDBTransaction transaction = db.transaction(storeName, 'readonly');
91 IDBRequest request = transaction.objectStore(storeName).getObject(key); 105 IDBRequest request = transaction.objectStore(storeName).getObject(key);
92 request.on.success.add(expectAsync1((e) { 106 request.on.success.add(expectAsync1((e) {
93 var object = e.target.result; 107 var object = e.target.result;
108 db.close();
94 check(value, object); 109 check(value, object);
95 })); 110 }));
96 request.on.error.add(fail); 111 request.on.error.add(fail);
97 } 112 }
98 113
99 step1() { 114 step1() {
100 IDBTransaction transaction = 115 IDBTransaction transaction = db.transaction([storeName], 'readwrite');
101 db.transaction([storeName], 'readwrite');
102 IDBRequest request = transaction.objectStore(storeName).put(value, key); 116 IDBRequest request = transaction.objectStore(storeName).put(value, key);
103 request.on.success.add(expectAsync1(step2)); 117 request.on.success.add(expectAsync1(step2));
104 request.on.error.add(fail); 118 request.on.error.add(fail);
105 } 119 }
106 120
107 initDb(e) { 121 initDb(e) {
108 db = e.target.result; 122 db = e.target.result;
109 if (version != db.version) { 123 if (version != db.version) {
110 IDBRequest request = db.setVersion(version); 124 // Legacy 'setVersion' upgrade protocol.
125 IDBRequest request = db.setVersion('$version');
111 request.on.success.add( 126 request.on.success.add(
112 expectAsync1((e) { 127 expectAsync1((e) {
113 createObjectStore(); 128 createObjectStore(db);
114 step1(); 129 IDBTransaction transaction = e.target.result;
130 transaction.on.complete.add(expectAsync1((e) => step1()));
131 transaction.on.error.add(fail);
115 }) 132 })
116 ); 133 );
117 request.on.error.add(fail); 134 request.on.error.add(fail);
118 } else { 135 } else {
119 step1(); 136 step1();
120 } 137 }
121 } 138 }
122 139
123 IDBRequest request = window.indexedDB.open(dbName); 140 openDb(e) {
124 Expect.isNotNull(request); 141 IDBRequest request = window.indexedDB.open(dbName, version);
125 request.on.success.add(expectAsync1(initDb)); 142 expect(request, isNotNull);
126 request.on.error.add(fail); 143 request.on.success.add(expectAsync1(initDb));
144 request.on.error.add(fail);
145 if (request is IDBOpenDBRequest) {
146 // New upgrade protocol. Old API has no 'upgradeNeeded' and uses
147 // setVersion instead.
148 request.on.upgradeNeeded.add((e) {
149 guardAsync(() {
150 createObjectStore(e.target.result);
151 });
152 });
153 }
154 }
155
156 // Delete any existing DB.
157 IDBRequest deleteRequest = window.indexedDB.deleteDatabase(dbName);
158 deleteRequest.on.success.add(expectAsync1(openDb));
159 deleteRequest.on.error.add(fail);
127 }; 160 };
128 161
129 tests_dynamic() { 162 tests_dynamic() {
130 test('test1', testReadWrite(123, 'Hoot!', Expect.equals)); 163 test('test1', testReadWrite(123, 'Hoot!', Expect.equals));
131 test('test2', testReadWrite(123, 12345, Expect.equals)); 164 test('test2', testReadWrite(123, 12345, Expect.equals));
132 test('test3', testReadWrite(123, [1,2,3], Expect.listEquals)); 165 test('test3', testReadWrite(123, [1,2,3], Expect.listEquals));
133 test('test4', testReadWrite(123, const [2, 3, 4], Expect.listEquals)); 166 test('test4', testReadWrite(123, const [2, 3, 4], Expect.listEquals));
134 } 167 }
135 168
136 tests_typed() { 169 tests_typed() {
137 test('test1', testReadWriteTyped(123, 'Hoot!', Expect.equals)); 170 test('test1', testReadWriteTyped(123, 'Hoot!', Expect.equals));
138 test('test2', testReadWriteTyped(123, 12345, Expect.equals)); 171 test('test2', testReadWriteTyped(123, 12345, Expect.equals));
139 test('test3', testReadWriteTyped(123, [1,2,3], Expect.listEquals)); 172 test('test3', testReadWriteTyped(123, [1,2,3], Expect.listEquals));
140 test('test4', 173 test('test4',
141 testReadWriteTyped(123, const [2, 3, 4], Expect.listEquals)); 174 testReadWriteTyped(123, const [2, 3, 4], Expect.listEquals));
142 } 175 }
143 176
144 main() { 177 main() {
145 useHtmlConfiguration(); 178 useHtmlConfiguration();
146 179
147 tests_dynamic(); 180 tests_dynamic();
148 tests_typed(); 181 tests_typed();
149 } 182 }
OLDNEW
« no previous file with comments | « tests/html/html.status ('k') | tests/html/indexeddb_2_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698