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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tests/html/indexeddb_3_test.dart
diff --git a/tests/html/indexeddb_3_test.dart b/tests/html/indexeddb_3_test.dart
index 8413c4d9052ee971769355ecb8afd696db2449c0..feaac15504d6923245e12955eb8efd3549f00102 100644
--- a/tests/html/indexeddb_3_test.dart
+++ b/tests/html/indexeddb_3_test.dart
@@ -7,38 +7,68 @@
const String DB_NAME = 'Test';
const String STORE_NAME = 'TEST';
-const String VERSION = '1';
+const int VERSION = 1;
class Test {
- var db;
+ fail(message) => (e) {
+ guardAsync(() {
+ Expect.fail('IndexedDB failure: $message');
+ });
+ };
- start() {
- var request = window.indexedDB.open(DB_NAME);
- Expect.isNotNull(request);
- request.on.success.add(expectAsync1(initDb));
- request.on.error.add(fail('open'));
+ _createObjectStore(db) {
+ try {
+ // Nuke object store if it already exists.
+ db.deleteObjectStore(STORE_NAME);
+ }
+ on IDBDatabaseException catch(e) { } // Chrome
+ on DOMException catch(e) { } // Firefox
+ db.createObjectStore(STORE_NAME);
}
- initDb(e) {
- db = e.target.result;
- // TODO. Some browsers do this the w3 way - passing the VERSION to the
- // open call and listening to onversionchange. Can we feature-detect the
- // difference and make it work?
- var request = db.setVersion(VERSION);
- request.on.success.add(
- expectAsync1((e) {
- try {
- // Nuke object store if it already exists.
- db.deleteObjectStore(STORE_NAME);
- } on IDBDatabaseException catch(e) { }
- db.createObjectStore(STORE_NAME);
+ var db;
+
+ _openDb(afterOpen()) {
+ var request = window.indexedDB.open(DB_NAME, VERSION);
+ if (request is IDBOpenDBRequest) {
+ // New upgrade protocol.
+ request.on.success.add(expectAsync1((e) {
+ db = e.target.result;
+ afterOpen();
+ }));
+ request.on.upgradeNeeded.add((e) {
+ guardAsync(() {
+ _createObjectStore(e.target.result);
+ });
+ });
+ request.on.error.add(fail('open'));
+ } else {
+ // Legacy setVersion upgrade protocol.
+ request.on.success.add(expectAsync1((e) {
+ db = e.target.result;
+ if (db.version != '$VERSION') {
+ var setRequest = db.setVersion('$VERSION');
+ setRequest.on.success.add(
+ expectAsync1((e) {
+ _createObjectStore(db);
+ var transaction = e.target.result;
+ transaction.on.complete.add(
+ expectAsync1((e) => afterOpen()));
+ transaction.on.error.add(fail('Upgrade'));
+ }));
+ setRequest.on.error.add(fail('setVersion error'));
+ } else {
+ afterOpen();
+ }
+ }));
+ request.on.error.add(fail('open'));
+ }
+ }
- var transaction = e.target.result;
- transaction.on.complete.add(expectAsync1((e) => writeItems(0)));
- transaction.on.error.add(fail);
- })
- );
- request.on.error.add(fail('setVersion error'));
+ _createAndOpenDb(afterOpen()) {
+ var request = window.indexedDB.deleteDatabase(DB_NAME);
+ request.on.success.add(expectAsync1((e) { _openDb(afterOpen); }));
+ request.on.error.add(fail('delete old Db'));
}
writeItems(int index) {
@@ -54,11 +84,7 @@ class Test {
}
}
- fail(message) => (e) {
- guardAsync(() {
- Expect.fail('IndexedDB failure: $message');
- });
- };
+ setupDb() { _createAndOpenDb(() => writeItems(0)); }
readAllViaCursor() {
IDBTransaction txn = db.transaction(STORE_NAME, 'readonly');
@@ -73,6 +99,7 @@ class Test {
lastKey = cursor.key;
itemCount += 1;
sumKeys += cursor.key;
+ window.console.log('${cursor.key} ${cursor.value}');
Expect.equals('Item ${cursor.key}', cursor.value);
cursor.continueFunction();
} else {
@@ -116,7 +143,7 @@ main() {
useHtmlConfiguration();
var test_ = new Test();
- test('prepare', test_.start);
+ test('prepare', test_.setupDb);
test('readAll1', test_.readAllViaCursor);
test('readAll2', test_.readAllReversedViaCursor);
}

Powered by Google App Engine
This is Rietveld 408576698