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

Side by Side Diff: tests/html/indexeddb_3_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('IndexedDB3Test'); 1 #library('IndexedDB3Test');
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 // Read with cursor. 6 // Read with cursor.
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) {
Siggi Cherem (dart-lang) 2012/05/21 22:20:47 remove tab, possibly merge with prev line
gram 2012/05/22 17:36:36 Done.
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 readAllViaCursor() { 61 readAllViaCursor() {
62 var doneCallback = expectAsync0((){});
57 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); 63 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
58 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); 64 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
59 IDBRequest cursorRequest = objectStore.openCursor(); 65 IDBRequest cursorRequest = objectStore.openCursor();
60 int itemCount = 0; 66 int itemCount = 0;
61 int sumKeys = 0; 67 int sumKeys = 0;
62 int lastKey = null; 68 int lastKey = null;
63 cursorRequest.on.success.add((e) { 69 cursorRequest.on.success.add((e) {
Siggi Cherem (dart-lang) 2012/05/21 22:20:47 replace doneCallback with count:100 here and below
gram 2012/05/22 17:36:36 Done.
70 guardAsync(() {
64 var cursor = e.target.result; 71 var cursor = e.target.result;
65 if (cursor != null) { 72 if (cursor != null) {
66 lastKey = cursor.key; 73 lastKey = cursor.key;
67 itemCount += 1; 74 itemCount += 1;
68 sumKeys += cursor.key; 75 sumKeys += cursor.key;
69 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); 76 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value);
70 cursor.continueFunction(); 77 cursor.continueFunction();
71 } else { 78 } else {
72 // Done 79 // Done
73 Expect.equals(99, lastKey); 80 Expect.equals(99, lastKey);
74 Expect.equals(100, itemCount); 81 Expect.equals(100, itemCount);
75 Expect.equals((100 * 99) ~/ 2, sumKeys); 82 Expect.equals((100 * 99) ~/ 2, sumKeys);
76 callbackDone(); 83 doneCallback();
77 } 84 }
78 }); 85 });
86 });
79 cursorRequest.on.error.add(fail('openCursor')); 87 cursorRequest.on.error.add(fail('openCursor'));
80 } 88 }
81 89
82 readAllReversedViaCursor() { 90 readAllReversedViaCursor() {
91 var doneCallback = expectAsync0((){});
83 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); 92 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY);
84 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); 93 IDBObjectStore objectStore = txn.objectStore(STORE_NAME);
85 // TODO: create a IDBKeyRange(0,100) 94 // TODO: create a IDBKeyRange(0,100)
86 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); 95 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV);
87 int itemCount = 0; 96 int itemCount = 0;
88 int sumKeys = 0; 97 int sumKeys = 0;
89 int lastKey = null; 98 int lastKey = null;
90 cursorRequest.on.success.add((e) { 99 cursorRequest.on.success.add((e) {
100 guardAsync(() {
91 var cursor = e.target.result; 101 var cursor = e.target.result;
92 if (cursor != null) { 102 if (cursor != null) {
93 lastKey = cursor.key; 103 lastKey = cursor.key;
94 itemCount += 1; 104 itemCount += 1;
95 sumKeys += cursor.key; 105 sumKeys += cursor.key;
96 Expect.equals('Item ${cursor.key}', cursor.value); 106 Expect.equals('Item ${cursor.key}', cursor.value);
97 cursor.continueFunction(); 107 cursor.continueFunction();
98 } else { 108 } else {
99 // Done 109 // Done
100 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). 110 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse).
101 Expect.equals(100, itemCount); 111 Expect.equals(100, itemCount);
102 Expect.equals((100 * 99) ~/ 2, sumKeys); 112 Expect.equals((100 * 99) ~/ 2, sumKeys);
103 callbackDone(); 113 doneCallback();
104 } 114 }
105 }); 115 });
116 });
106 cursorRequest.on.error.add(fail('openCursor')); 117 cursorRequest.on.error.add(fail('openCursor'));
107 } 118 }
108 } 119 }
109 120
110 main() { 121 main() {
111 useHtmlConfiguration(); 122 useHtmlConfiguration();
112 123
113 var test = new Test(); 124 var test_ = new Test();
114 asyncTest('prepare', 1, test.start); 125 test('prepare', test_.start);
115 asyncTest('readAll1', 1, test.readAllViaCursor); 126 test('readAll1', test_.readAllViaCursor);
116 asyncTest('readAll2', 1, test.readAllReversedViaCursor); 127 test('readAll2', test_.readAllReversedViaCursor);
117 } 128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698