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

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

Issue 10378003: Revert "Add IDBKeyRange to dart:html, port two indexeddb tests" (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 7 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_4_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
(Empty)
1 #library('IndexedDB3Test');
2 #import('../../lib/unittest/unittest.dart');
3 #import('../../lib/unittest/html_config.dart');
4 #import('dart:html');
5
6 // Read with cursor.
7
8 final String DB_NAME = 'Test';
9 final String STORE_NAME = 'TEST';
10 final String VERSION = '1';
11
12 class Test {
13 var db;
14
15 start() {
16 var request = window.webkitIndexedDB.open(DB_NAME);
17 Expect.isNotNull(request);
18 request.on.success.add(initDb);
19 request.on.error.add(fail('open'));
20 }
21
22 initDb(e) {
23 db = e.target.result;
24 // TODO. Some browsers do this the w3 way - passing the VERSION to the
25 // open call and listening to onversionchange. Can we feature-detect the
26 // difference and make it work?
27 var request = db.setVersion(VERSION);
28 request.on.success.add((e) {
29 try {
30 // Nuke object store if it already exists.
31 db.deleteObjectStore(STORE_NAME);
32 } catch (IDBDatabaseException e) { }
33 db.createObjectStore(STORE_NAME);
34 writeItems(0);
35 });
36 //request.on.blocked.add(fail('setVersion blocked'));
37 request.on.error.add(fail('setVersion error'));
38 }
39
40 writeItems(int index) {
41 if (index < 100) {
42 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE);
43 var request = transaction.objectStore(STORE_NAME)
44 .put('Item $index', index);
45 request.on.success.add((e) { writeItems(index + 1); });
46 request.on.error.add(fail('put'));
47 } else {
48 callbackDone();
49 }
50 }
51
52 fail(message) => (e) {
53 callbackDone();
54 Expect.fail('IndexedDB failure: $message');
55 };
56
57 readAllViaCursor() {
58 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
59 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
60 IDBRequest cursorRequest = objectStore.openCursor();
61 int itemCount = 0;
62 int sumKeys = 0;
63 int lastKey = null;
64 cursorRequest.on.success.add((e) {
65 var cursor = e.target.result;
66 if (cursor != null) {
67 lastKey = cursor.key;
68 itemCount += 1;
69 sumKeys += cursor.key;
70 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value);
71 cursor.continueFunction();
72 } else {
73 // Done
74 Expect.equals(99, lastKey);
75 Expect.equals(100, itemCount);
76 Expect.equals((100 * 99) ~/ 2, sumKeys);
77 callbackDone();
78 }
79 });
80 cursorRequest.on.error.add(fail('openCursor'));
81 }
82
83 readAllReversedViaCursor() {
84 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
85 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
86 // TODO: create a IDBKeyRange(0,100)
87 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV);
88 int itemCount = 0;
89 int sumKeys = 0;
90 int lastKey = null;
91 cursorRequest.on.success.add((e) {
92 var cursor = e.target.result;
93 if (cursor != null) {
94 lastKey = cursor.key;
95 itemCount += 1;
96 sumKeys += cursor.key;
97 Expect.equals('Item ${cursor.key}', cursor.value);
98 cursor.continueFunction();
99 } else {
100 // Done
101 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse).
102 Expect.equals(100, itemCount);
103 Expect.equals((100 * 99) ~/ 2, sumKeys);
104 callbackDone();
105 }
106 });
107 cursorRequest.on.error.add(fail('openCursor'));
108 }
109 }
110
111 main() {
112 useHtmlConfiguration();
113
114 var test = new Test();
115 asyncTest('prepare', 1, test.start);
116 asyncTest('readAll1', 1, test.readAllViaCursor);
117 asyncTest('readAll2', 1, test.readAllReversedViaCursor);
118 }
OLDNEW
« no previous file with comments | « tests/html/html.status ('k') | tests/html/indexeddb_4_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698