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

Side by Side Diff: tests/html/indexeddb_3_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
OLDNEW
1 #library('IndexedDB3Test'); 1 #library('IndexedDB3Test');
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 // Read with cursor. 6 // Read with cursor.
7 7
8 const String DB_NAME = 'Test'; 8 const String DB_NAME = 'Test';
9 const String STORE_NAME = 'TEST'; 9 const String STORE_NAME = 'TEST';
10 const String VERSION = '1'; 10 const int VERSION = 1;
11 11
12 class Test { 12 class Test {
13 fail(message) => (e) {
14 guardAsync(() {
15 Expect.fail('IndexedDB failure: $message');
16 });
17 };
18
19 _createObjectStore(db) {
20 try {
21 // Nuke object store if it already exists.
22 db.deleteObjectStore(STORE_NAME);
23 }
24 on IDBDatabaseException catch(e) { } // Chrome
25 on DOMException catch(e) { } // Firefox
26 db.createObjectStore(STORE_NAME);
27 }
28
13 var db; 29 var db;
14 30
15 start() { 31 _openDb(afterOpen()) {
16 var request = window.indexedDB.open(DB_NAME); 32 var request = window.indexedDB.open(DB_NAME, VERSION);
17 Expect.isNotNull(request); 33 if (request is IDBOpenDBRequest) {
18 request.on.success.add(expectAsync1(initDb)); 34 // New upgrade protocol.
19 request.on.error.add(fail('open')); 35 request.on.success.add(expectAsync1((e) {
36 db = e.target.result;
37 afterOpen();
38 }));
39 request.on.upgradeNeeded.add((e) {
40 guardAsync(() {
41 _createObjectStore(e.target.result);
42 });
43 });
44 request.on.error.add(fail('open'));
45 } else {
46 // Legacy setVersion upgrade protocol.
47 request.on.success.add(expectAsync1((e) {
48 db = e.target.result;
49 if (db.version != '$VERSION') {
50 var setRequest = db.setVersion('$VERSION');
51 setRequest.on.success.add(
52 expectAsync1((e) {
53 _createObjectStore(db);
54 var transaction = e.target.result;
55 transaction.on.complete.add(
56 expectAsync1((e) => afterOpen()));
57 transaction.on.error.add(fail('Upgrade'));
58 }));
59 setRequest.on.error.add(fail('setVersion error'));
60 } else {
61 afterOpen();
62 }
63 }));
64 request.on.error.add(fail('open'));
65 }
20 } 66 }
21 67
22 initDb(e) { 68 _createAndOpenDb(afterOpen()) {
23 db = e.target.result; 69 var request = window.indexedDB.deleteDatabase(DB_NAME);
24 // TODO. Some browsers do this the w3 way - passing the VERSION to the 70 request.on.success.add(expectAsync1((e) { _openDb(afterOpen); }));
25 // open call and listening to onversionchange. Can we feature-detect the 71 request.on.error.add(fail('delete old Db'));
26 // difference and make it work?
27 var request = db.setVersion(VERSION);
28 request.on.success.add(
29 expectAsync1((e) {
30 try {
31 // Nuke object store if it already exists.
32 db.deleteObjectStore(STORE_NAME);
33 } on IDBDatabaseException catch(e) { }
34 db.createObjectStore(STORE_NAME);
35
36 var transaction = e.target.result;
37 transaction.on.complete.add(expectAsync1((e) => writeItems(0)));
38 transaction.on.error.add(fail);
39 })
40 );
41 request.on.error.add(fail('setVersion error'));
42 } 72 }
43 73
44 writeItems(int index) { 74 writeItems(int index) {
45 if (index < 100) { 75 if (index < 100) {
46 var transaction = db.transaction([STORE_NAME], 'readwrite'); 76 var transaction = db.transaction([STORE_NAME], 'readwrite');
47 var request = transaction.objectStore(STORE_NAME) 77 var request = transaction.objectStore(STORE_NAME)
48 .put('Item $index', index); 78 .put('Item $index', index);
49 request.on.success.add(expectAsync1((e) { 79 request.on.success.add(expectAsync1((e) {
50 writeItems(index + 1); 80 writeItems(index + 1);
51 } 81 }
52 )); 82 ));
53 request.on.error.add(fail('put')); 83 request.on.error.add(fail('put'));
54 } 84 }
55 } 85 }
56 86
57 fail(message) => (e) { 87 setupDb() { _createAndOpenDb(() => writeItems(0)); }
58 guardAsync(() {
59 Expect.fail('IndexedDB failure: $message');
60 });
61 };
62 88
63 readAllViaCursor() { 89 readAllViaCursor() {
64 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly'); 90 IDBTransaction txn = db.transaction(STORE_NAME, 'readonly');
65 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); 91 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
66 IDBRequest cursorRequest = objectStore.openCursor(); 92 IDBRequest cursorRequest = objectStore.openCursor();
67 int itemCount = 0; 93 int itemCount = 0;
68 int sumKeys = 0; 94 int sumKeys = 0;
69 int lastKey = null; 95 int lastKey = null;
70 cursorRequest.on.success.add(expectAsync1((e) { 96 cursorRequest.on.success.add(expectAsync1((e) {
71 var cursor = e.target.result; 97 var cursor = e.target.result;
72 if (cursor != null) { 98 if (cursor != null) {
73 lastKey = cursor.key; 99 lastKey = cursor.key;
74 itemCount += 1; 100 itemCount += 1;
75 sumKeys += cursor.key; 101 sumKeys += cursor.key;
102 window.console.log('${cursor.key} ${cursor.value}');
76 Expect.equals('Item ${cursor.key}', cursor.value); 103 Expect.equals('Item ${cursor.key}', cursor.value);
77 cursor.continueFunction(); 104 cursor.continueFunction();
78 } else { 105 } else {
79 // Done 106 // Done
80 Expect.equals(99, lastKey); 107 Expect.equals(99, lastKey);
81 Expect.equals(100, itemCount); 108 Expect.equals(100, itemCount);
82 Expect.equals((100 * 99) ~/ 2, sumKeys); 109 Expect.equals((100 * 99) ~/ 2, sumKeys);
83 } 110 }
84 }, count:101)); 111 }, count:101));
85 cursorRequest.on.error.add(fail('openCursor')); 112 cursorRequest.on.error.add(fail('openCursor'));
(...skipping 23 matching lines...) Expand all
109 } 136 }
110 }, count:101)); 137 }, count:101));
111 cursorRequest.on.error.add(fail('openCursor')); 138 cursorRequest.on.error.add(fail('openCursor'));
112 } 139 }
113 } 140 }
114 141
115 main() { 142 main() {
116 useHtmlConfiguration(); 143 useHtmlConfiguration();
117 144
118 var test_ = new Test(); 145 var test_ = new Test();
119 test('prepare', test_.start); 146 test('prepare', test_.setupDb);
120 test('readAll1', test_.readAllViaCursor); 147 test('readAll1', test_.readAllViaCursor);
121 test('readAll2', test_.readAllReversedViaCursor); 148 test('readAll2', test_.readAllReversedViaCursor);
122 } 149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698