9f09ef0af3
Event type parameters instead of dynamic.
- Fixed missing interceptor in dart2js for MutationEvent (even though its deprecated
and should not be used). The MutationEvent is created when a node is deleted. Enabled
Dart MutationEvent class generation.
- Fixed/re-enabled all indexed_db tests to run and pass successfully.
Stephen please take a look - target in VersionChangeEvent is a final field, per your suggestion,
instead of a getter. The MutationEvent is returned in the browser and without the MutationEvent
Dart class the error:
Uncaught TypeError: t1.get$target is not a function
R=vsm@google.com,gabrielchow@google.com,sra@google.com
Change-Id: Ic36aea8c33b4263a32ff9d047ba3e6b979e03b10
Reviewed-on: https://dart-review.googlesource.com/41569
Reviewed-by: Stephen Adams <sra@google.com>
Reviewed-by: Vijay Menon <vsm@google.com>
Commit-Queue: Terry Lucas <terry@google.com>
113 lines
2.8 KiB
Dart
113 lines
2.8 KiB
Dart
library IndexedDB3Test;
|
|
|
|
import 'package:unittest/unittest.dart';
|
|
import 'package:unittest/html_config.dart';
|
|
import 'dart:async';
|
|
import 'dart:html' as html;
|
|
import 'dart:indexed_db';
|
|
|
|
// Read with cursor.
|
|
|
|
const String DB_NAME = 'Test3';
|
|
const String STORE_NAME = 'TEST';
|
|
const int VERSION = 1;
|
|
|
|
Future<Database> createAndOpenDb() {
|
|
return html.window.indexedDB.deleteDatabase(DB_NAME).then((_) {
|
|
return html.window.indexedDB.open(DB_NAME, version: VERSION,
|
|
onUpgradeNeeded: (VersionChangeEvent e) {
|
|
var db = e.target.result;
|
|
db.createObjectStore(STORE_NAME);
|
|
});
|
|
});
|
|
}
|
|
|
|
Future<Database> writeItems(Database db) {
|
|
Future<Object> write(index) {
|
|
var transaction = db.transaction(STORE_NAME, 'readwrite');
|
|
transaction.objectStore(STORE_NAME).put('Item $index', index);
|
|
return transaction.completed;
|
|
}
|
|
|
|
var future = write(0);
|
|
for (var i = 1; i < 100; ++i) {
|
|
future = future.then((_) => write(i));
|
|
}
|
|
|
|
// Chain on the DB so we return it at the end.
|
|
return future.then((_) => db);
|
|
}
|
|
|
|
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 lastKey = null;
|
|
|
|
var cursors = objectStore.openCursor().asBroadcastStream();
|
|
cursors.listen((cursor) {
|
|
++itemCount;
|
|
lastKey = cursor.key;
|
|
sumKeys += cursor.key;
|
|
expect(cursor.value, 'Item ${cursor.key}');
|
|
cursor.next();
|
|
});
|
|
cursors.last.then((cursor) {
|
|
expect(lastKey, 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 lastKey = null;
|
|
|
|
var cursors = objectStore.openCursor(direction: 'prev').asBroadcastStream();
|
|
cursors.listen((cursor) {
|
|
++itemCount;
|
|
lastKey = cursor.key;
|
|
sumKeys += cursor.key;
|
|
expect(cursor.value, 'Item ${cursor.key}');
|
|
cursor.next();
|
|
});
|
|
cursors.last.then((cursor) {
|
|
expect(lastKey, 0);
|
|
expect(sumKeys, (100 * 99) ~/ 2);
|
|
expect(itemCount, 100);
|
|
});
|
|
return cursors.last.then((_) => db);
|
|
}
|
|
|
|
main() {
|
|
useHtmlConfiguration();
|
|
|
|
// Don't bother with these tests if it's unsupported.
|
|
// Support is tested in indexeddb_1_test
|
|
if (IdbFactory.supported) {
|
|
var db;
|
|
test('prepare', () {
|
|
return setupDb().then((result) {
|
|
db = result;
|
|
});
|
|
});
|
|
test('readAll1', () {
|
|
return readAllViaCursor(db);
|
|
});
|
|
|
|
test('readAll2', () {
|
|
return readAllReversedViaCursor(db);
|
|
});
|
|
}
|
|
}
|