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

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

Issue 10352021: 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/indexeddb_3_test.dart ('k') | no next file » | 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('IndexedDB4Test');
2 #import('../../lib/unittest/unittest.dart');
3 #import('../../lib/unittest/html_config.dart');
4 #import('dart:html');
5
6 // Test for IDBKeyRange and IDBCursor.
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 testRange(range, expectedFirst, expectedLast) {
58 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
59 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
60 IDBRequest cursorRequest = objectStore.openCursor(range);
61 int itemCount = 0;
62 int firstKey = null;
63 int lastKey = null;
64 cursorRequest.on.success.add((e) {
65 var cursor = e.target.result;
66 if (cursor != null) {
67 if (firstKey == null) firstKey = cursor.key;
68 lastKey = cursor.key;
69 itemCount += 1;
70 Expect.equals('Item ${cursor.key}', cursor.value);
71 cursor.continueFunction();
72 } else {
73 // Done
74 Expect.equals(expectedFirst, firstKey);
75 Expect.equals(expectedLast, lastKey);
76 if (expectedFirst == null) {
77 Expect.equals(0, itemCount);
78 } else {
79 Expect.equals(expectedLast - expectedFirst + 1, itemCount);
80 }
81 callbackDone();
82 }
83 });
84 cursorRequest.on.error.add(fail('openCursor'));
85 }
86
87 only1() => testRange(new IDBKeyRange.only(55), 55, 55);
88 only2() => testRange(new IDBKeyRange.only(100), null, null);
89 only3() => testRange(new IDBKeyRange.only(-1), null, null);
90
91 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99);
92 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99);
93 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99);
94
95 upper1() => testRange(new IDBKeyRange.upperBound(40), 0, 40);
96 upper2() => testRange(new IDBKeyRange.upperBound(40, open: true), 0, 39);
97 upper3() => testRange(new IDBKeyRange.upperBound(40, open: false), 0, 40);
98
99 bound1() => testRange(new IDBKeyRange.bound(20, 30), 20, 30);
100
101 bound2() => testRange(new IDBKeyRange.bound(-100, 200), 0, 99);
102
103 bound3() =>
104 testRange(new IDBKeyRange.bound(20, 30, upperOpen: true),
105 20, 29);
106
107 bound4() =>
108 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true),
109 21, 30);
110
111 bound5() =>
112 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true),
113 21, 29);
114
115 }
116
117 main() {
118 useHtmlConfiguration();
119
120 var test = new Test();
121 asyncTest('prepare', 1, test.start);
122
123 asyncTest('only1', 1, test.only1);
124 asyncTest('only2', 1, test.only2);
125 asyncTest('only3', 1, test.only3);
126
127 asyncTest('lower1', 1, test.lower1);
128 asyncTest('lower2', 1, test.lower2);
129 asyncTest('lower3', 1, test.lower3);
130
131 asyncTest('upper1', 1, test.upper1);
132 asyncTest('upper2', 1, test.upper2);
133 asyncTest('upper3', 1, test.upper3);
134
135 asyncTest('bound1', 1, test.bound1);
136 asyncTest('bound2', 1, test.bound2);
137 asyncTest('bound3', 1, test.bound3);
138 asyncTest('bound4', 1, test.bound4);
139 asyncTest('bound5', 1, test.bound5);
140
141 }
OLDNEW
« no previous file with comments | « tests/html/indexeddb_3_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698