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

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

Issue 10412015: aChange all asyncTest/callbackDone style tests to use the new expectAsync/ (Closed) Base URL: http://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
OLDNEW
1 #library('IndexedDB4Test'); 1 #library('IndexedDB4Test');
2 #import('../../lib/unittest/unittest.dart'); 2 #import('../../lib/unittest/unittest.dart');
3 #import('../../lib/unittest/html_config.dart'); 3 #import('../../lib/unittest/html_config.dart');
4 #import('dart:html'); 4 #import('dart:html');
5 5
6 // Test for IDBKeyRange and IDBCursor. 6 // Test for IDBKeyRange and IDBCursor.
7 7
8 final String DB_NAME = 'Test'; 8 final String DB_NAME = 'Test';
9 final String STORE_NAME = 'TEST'; 9 final String STORE_NAME = 'TEST';
10 final String VERSION = '1'; 10 final String VERSION = '1';
11 11
12 class Test { 12 class Test {
13 var db; 13 var db;
14 14
15 start() { 15 start() {
16 var request = window.webkitIndexedDB.open(DB_NAME); 16 var request = window.webkitIndexedDB.open(DB_NAME);
17 Expect.isNotNull(request); 17 Expect.isNotNull(request);
18 request.on.success.add(initDb); 18 request.on.success.add(expectAsync1(initDb));
19 request.on.error.add(fail('open')); 19 request.on.error.add(fail('open'));
20 } 20 }
21 21
22 initDb(e) { 22 initDb(e) {
23 db = e.target.result; 23 db = e.target.result;
24 // TODO. Some browsers do this the w3 way - passing the VERSION to the 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 25 // open call and listening to onversionchange. Can we feature-detect the
26 // difference and make it work? 26 // difference and make it work?
27 var request = db.setVersion(VERSION); 27 var request = db.setVersion(VERSION);
28 request.on.success.add((e) { 28 request.on.success.add(
29 expectAsync1((e) {
29 try { 30 try {
30 // Nuke object store if it already exists. 31 // Nuke object store if it already exists.
31 db.deleteObjectStore(STORE_NAME); 32 db.deleteObjectStore(STORE_NAME);
32 } catch (IDBDatabaseException e) { } 33 } catch (IDBDatabaseException e) { }
33 db.createObjectStore(STORE_NAME); 34 db.createObjectStore(STORE_NAME);
34 writeItems(0); 35 writeItems(0);
35 }); 36 })
37 );
36 request.on.error.add(fail('setVersion error')); 38 request.on.error.add(fail('setVersion error'));
37 } 39 }
38 40
39 writeItems(int index) { 41 writeItems(int index) {
40 if (index < 100) { 42 if (index < 100) {
41 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); 43 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE);
42 var request = transaction.objectStore(STORE_NAME) 44 var request = transaction.objectStore(STORE_NAME)
43 .put('Item $index', index); 45 .put('Item $index', index);
44 request.on.success.add((e) { writeItems(index + 1); }); 46 request.on.success.add(
47 expectAsync1((e) {
48 writeItems(index + 1);
49 })
50 );
45 request.on.error.add(fail('put')); 51 request.on.error.add(fail('put'));
46 } else {
47 callbackDone();
48 } 52 }
49 } 53 }
50 54
51 fail(message) => (e) { 55 fail(message) => (e) {
52 callbackDone(); 56 guardAsync(() {
53 Expect.fail('IndexedDB failure: $message'); 57 Expect.fail('IndexedDB failure: $message');
58 });
54 }; 59 };
55 60
56 testRange(range, expectedFirst, expectedLast) { 61 testRange(range, expectedFirst, expectedLast) {
57 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); 62 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
58 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); 63 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
59 IDBRequest cursorRequest = objectStore.openCursor(range); 64 IDBRequest cursorRequest = objectStore.openCursor(range);
60 int itemCount = 0; 65 int itemCount = 0;
61 int firstKey = null; 66 int firstKey = null;
62 int lastKey = null; 67 int lastKey = null;
63 cursorRequest.on.success.add((e) { 68 cursorRequest.on.success.add(expectAsync1((e) {
64 var cursor = e.target.result; 69 var cursor = e.target.result;
65 if (cursor != null) { 70 if (cursor != null) {
66 if (firstKey == null) firstKey = cursor.key; 71 if (firstKey == null) firstKey = cursor.key;
67 lastKey = cursor.key; 72 lastKey = cursor.key;
68 itemCount += 1; 73 itemCount += 1;
69 Expect.equals('Item ${cursor.key}', cursor.value); 74 Expect.equals('Item ${cursor.key}', cursor.value);
70 cursor.continueFunction(); 75 cursor.continueFunction();
76 } else {
77 // Done
78 Expect.equals(expectedFirst, firstKey);
79 Expect.equals(expectedLast, lastKey);
80 if (expectedFirst == null) {
81 Expect.equals(0, itemCount);
71 } else { 82 } else {
72 // Done 83 Expect.equals(expectedLast - expectedFirst + 1, itemCount);
73 Expect.equals(expectedFirst, firstKey);
74 Expect.equals(expectedLast, lastKey);
75 if (expectedFirst == null) {
76 Expect.equals(0, itemCount);
77 } else {
78 Expect.equals(expectedLast - expectedFirst + 1, itemCount);
79 }
80 callbackDone();
81 } 84 }
82 }); 85 }
86 },
87 1 + ((expectedFirst == null) ? 0 : (expectedLast - expectedFirst + 1))));
83 cursorRequest.on.error.add(fail('openCursor')); 88 cursorRequest.on.error.add(fail('openCursor'));
84 } 89 }
85 90
86 only1() => testRange(new IDBKeyRange.only(55), 55, 55); 91 only1() => testRange(new IDBKeyRange.only(55), 55, 55);
87 only2() => testRange(new IDBKeyRange.only(100), null, null); 92 only2() => testRange(new IDBKeyRange.only(100), null, null);
88 only3() => testRange(new IDBKeyRange.only(-1), null, null); 93 only3() => testRange(new IDBKeyRange.only(-1), null, null);
89 94
90 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); 95 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99);
91 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); 96 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99);
92 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); 97 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99);
(...skipping 16 matching lines...) Expand all
109 114
110 bound5() => 115 bound5() =>
111 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), 116 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true),
112 21, 29); 117 21, 29);
113 118
114 } 119 }
115 120
116 main() { 121 main() {
117 useHtmlConfiguration(); 122 useHtmlConfiguration();
118 123
119 var test = new Test(); 124 var test_ = new Test();
120 asyncTest('prepare', 1, test.start); 125 test('prepare', test_.start);
121 126
122 asyncTest('only1', 1, test.only1); 127 test('only1', test_.only1);
123 asyncTest('only2', 1, test.only2); 128 test('only2', test_.only2);
124 asyncTest('only3', 1, test.only3); 129 test('only3', test_.only3);
125 130
126 asyncTest('lower1', 1, test.lower1); 131 test('lower1', test_.lower1);
127 asyncTest('lower2', 1, test.lower2); 132 test('lower2', test_.lower2);
128 asyncTest('lower3', 1, test.lower3); 133 test('lower3', test_.lower3);
129 134
130 asyncTest('upper1', 1, test.upper1); 135 test('upper1', test_.upper1);
131 asyncTest('upper2', 1, test.upper2); 136 test('upper2', test_.upper2);
132 asyncTest('upper3', 1, test.upper3); 137 test('upper3', test_.upper3);
133 138
134 asyncTest('bound1', 1, test.bound1); 139 test('bound1', test_.bound1);
135 asyncTest('bound2', 1, test.bound2); 140 test('bound2', test_.bound2);
136 asyncTest('bound3', 1, test.bound3); 141 test('bound3', test_.bound3);
137 asyncTest('bound4', 1, test.bound4); 142 test('bound4', test_.bound4);
138 asyncTest('bound5', 1, test.bound5); 143 test('bound5', test_.bound5);
139 144
140 } 145 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698