Chromium Code Reviews| Index: tests/html/indexeddb_3_test.dart |
| diff --git a/tests/html/indexeddb_3_test.dart b/tests/html/indexeddb_3_test.dart |
| index 436b12591897ef1d323778ac9dace6cd7592efca..e44c19576eed0ce52aa0df9cd39f35ecd7eec66b 100644 |
| --- a/tests/html/indexeddb_3_test.dart |
| +++ b/tests/html/indexeddb_3_test.dart |
| @@ -1,8 +1,10 @@ |
| library IndexedDB3Test; |
| import '../../pkg/unittest/lib/unittest.dart'; |
| import '../../pkg/unittest/lib/html_config.dart'; |
| -import 'dart:html'; |
| +import 'dart:async'; |
| +import 'dart:html' as html; |
| import 'dart:indexed_db'; |
| +import 'utils.dart'; |
| // Read with cursor. |
| @@ -10,135 +12,81 @@ const String DB_NAME = 'Test'; |
| const String STORE_NAME = 'TEST'; |
| const int VERSION = 1; |
| -class Test { |
| - fail(message) => (e) { |
| - guardAsync(() { |
| - expect(false, isTrue, reason: 'IndexedDB failure: $message'); |
| - }); |
| - }; |
| - |
| - _createObjectStore(db) { |
| - try { |
| - // Nuke object store if it already exists. |
| - db.deleteObjectStore(STORE_NAME); |
| - } |
| - // TODO: |
| - //on DomException catch(e) { } // Chrome and Firefox |
| - catch(e) { } // Chrome and Firefox |
| - db.createObjectStore(STORE_NAME); |
| - } |
| - var db; |
| - |
| - _openDb(afterOpen()) { |
| - var request = window.indexedDB.open(DB_NAME, VERSION); |
| - if (request is OpenDBRequest) { |
| - // New upgrade protocol. FireFox 15, Chrome 24, hopefully IE10. |
| - request.onSuccess.listen(expectAsync1((e) { |
| - db = e.target.result; |
| - afterOpen(); |
| - })); |
| - request.onUpgradeNeeded.listen((e) { |
| - guardAsync(() { |
| - _createObjectStore(e.target.result); |
| - }); |
| +Future<Database> createAndOpenDb() { |
| + return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) { |
| + return html.window.indexedDB.open(DB_NAME, version: VERSION, |
| + onUpgradeNeeded: (e) { |
| + var db = e.target.result; |
| + db.createObjectStore(STORE_NAME); |
| }); |
| - request.onError.listen(fail('open')); |
| - } else { |
| - // Legacy setVersion upgrade protocol. Chrome < 23. |
| - request.onSuccess.listen(expectAsync1((e) { |
| - db = e.target.result; |
| - if (db.version != '$VERSION') { |
| - var setRequest = db.setVersion('$VERSION'); |
| - setRequest.onSuccess.listen( |
| - expectAsync1((e) { |
| - _createObjectStore(db); |
| - var transaction = e.target.result; |
| - transaction.onComplete.listen( |
| - expectAsync1((e) => afterOpen())); |
| - transaction.onError.listen(fail('Upgrade')); |
| - })); |
| - setRequest.onError.listen(fail('setVersion error')); |
| - } else { |
| - afterOpen(); |
| - } |
| - })); |
| - request.onError.listen(fail('open')); |
| - } |
| - } |
| + }); |
| +} |
| - _createAndOpenDb(afterOpen()) { |
| - var request = window.indexedDB.deleteDatabase(DB_NAME); |
| - request.onSuccess.listen(expectAsync1((e) { _openDb(afterOpen); })); |
| - request.onError.listen(fail('delete old Db')); |
| +Future<Database> writeItems(Database db) { |
| + Future<Object> write(index) { |
| + var transaction = db.transaction([STORE_NAME], 'readwrite'); |
| + return transaction.objectStore(STORE_NAME).put('Item $index', index); |
| } |
| - writeItems(int index) { |
| - if (index < 100) { |
| - var transaction = db.transaction([STORE_NAME], 'readwrite'); |
| - var request = transaction.objectStore(STORE_NAME) |
| - .put('Item $index', index); |
| - request.onSuccess.listen(expectAsync1((e) { |
| - writeItems(index + 1); |
| - } |
| - )); |
| - request.onError.listen(fail('put')); |
| - } |
| + var future = write(0); |
| + for (var i = 1; i < 100; ++i) { |
| + future = future.then((_) => write(i)); |
| } |
| - setupDb() { _createAndOpenDb(() => writeItems(0)); } |
| - |
| - readAllViaCursor() { |
| - Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| - ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| - Request cursorRequest = objectStore.openCursor(); |
| - int itemCount = 0; |
| - int sumKeys = 0; |
| - int lastKey = null; |
| - cursorRequest.onSuccess.listen(expectAsync1((e) { |
| - var cursor = e.target.result; |
| - if (cursor != null) { |
| - lastKey = cursor.key; |
| - itemCount += 1; |
| - sumKeys += cursor.key; |
| - window.console.log('${cursor.key} ${cursor.value}'); |
| - expect(cursor.value, 'Item ${cursor.key}'); |
| - cursor.continueFunction(); |
| - } else { |
| - // Done |
| - expect(lastKey, 99); |
| - expect(itemCount, 100); |
| - expect(sumKeys, (100 * 99) ~/ 2); |
| - } |
| - }, count:101)); |
| - cursorRequest.onError.listen(fail('openCursor')); |
| - } |
| + // Chain on the DB so we return it at the end. |
| + return future.then((_) => db); |
| +} |
| - readAllReversedViaCursor() { |
| - Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| - ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| - // TODO: create a KeyRange(0,100) |
| - Request cursorRequest = objectStore.openCursor(null, 'prev'); |
| - int itemCount = 0; |
| - int sumKeys = 0; |
| - int lastKey = null; |
| - cursorRequest.onSuccess.listen(expectAsync1((e) { |
| - var cursor = e.target.result; |
| - if (cursor != null) { |
| - lastKey = cursor.key; |
| - itemCount += 1; |
| - sumKeys += cursor.key; |
| - expect(cursor.value, 'Item ${cursor.key}'); |
| - cursor.continueFunction(); |
| - } else { |
| - // Done |
| - expect(lastKey, 0); // i.e. first key (scanned in reverse). |
| - expect(itemCount, 100); |
| - expect(sumKeys, (100 * 99) ~/ 2); |
| - } |
| - }, count:101)); |
| - cursorRequest.onError.listen(fail('openCursor')); |
| - } |
| +Future<Database> setupDb() { |
| + return createAndOpenDb().then(writeItems); |
| +} |
| + |
| +Future<Database> readAllViaCursor(Database db) { |
| + Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| + ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| + int itemCount = 0; |
| + int sumKeys = 0; |
| + |
| + var cursors = objectStore.openCursor().asBroadcastStream(); |
| + cursors.listen( |
| + (cursor) { |
| + ++itemCount; |
| + sumKeys += cursor.key; |
| + expect(cursor.value, 'Item ${cursor.key}'); |
| + cursor.next(); |
| + }); |
| + cursors.last.then( |
| + (cursor) { |
| + expect(cursor.key, 99); |
| + expect(sumKeys, (100 * 99) ~/ 2); |
| + expect(itemCount, 100); |
| + }); |
| + |
| + return cursors.last.then((_) => db); |
| +} |
| + |
| +Future<Database> readAllReversedViaCursor(Database db) { |
| + Transaction txn = db.transaction(STORE_NAME, 'readonly'); |
| + ObjectStore objectStore = txn.objectStore(STORE_NAME); |
| + int itemCount = 0; |
| + int sumKeys = 0; |
| + |
| + var cursors = objectStore.openCursor(direction:'prev').asBroadcastStream(); |
|
nweiz
2013/02/07 20:38:10
Style nit: space after ":"
blois
2013/02/08 22:18:56
Done.
|
| + cursors.listen( |
| + (cursor) { |
| + ++itemCount; |
| + sumKeys += cursor.key; |
| + expect(cursor.value, 'Item ${cursor.key}'); |
| + cursor.next(); |
| + }); |
| + cursors.last.then( |
| + (cursor) { |
| + expect(cursor.key, 0); |
| + expect(sumKeys, (100 * 99) ~/ 2); |
| + expect(itemCount, 100); |
| + }); |
| + return cursors.last.then((_) => db); |
| } |
| main() { |
| @@ -147,9 +95,16 @@ main() { |
| // Don't bother with these tests if it's unsupported. |
| // Support is tested in indexeddb_1_test |
| if (IdbFactory.supported) { |
| - var test_ = new Test(); |
| - test('prepare', test_.setupDb); |
| - test('readAll1', test_.readAllViaCursor); |
| - test('readAll2', test_.readAllReversedViaCursor); |
| + var db; |
| + futureTest('prepare', () { |
| + return setupDb().then((result) { db = result; }); |
| + }); |
| + futureTest('readAll1', () { |
| + return readAllViaCursor(db); |
| + }); |
| + |
| + futureTest('readAll2', () { |
| + return readAllReversedViaCursor(db); |
| + }); |
| } |
| } |