Chromium Code Reviews| OLD | NEW |
|---|---|
| 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) { |
| 62 var doneCallback = expectAsync0((){}); | |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
ditto
gram
2012/05/22 17:36:36
Done.
| |
| 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(range); | 65 IDBRequest cursorRequest = objectStore.openCursor(range); |
| 60 int itemCount = 0; | 66 int itemCount = 0; |
| 61 int firstKey = null; | 67 int firstKey = null; |
| 62 int lastKey = null; | 68 int lastKey = null; |
| 63 cursorRequest.on.success.add((e) { | 69 cursorRequest.on.success.add((e) { |
| 70 guardAsync(() { | |
| 64 var cursor = e.target.result; | 71 var cursor = e.target.result; |
| 65 if (cursor != null) { | 72 if (cursor != null) { |
| 66 if (firstKey == null) firstKey = cursor.key; | 73 if (firstKey == null) firstKey = cursor.key; |
| 67 lastKey = cursor.key; | 74 lastKey = cursor.key; |
| 68 itemCount += 1; | 75 itemCount += 1; |
| 69 Expect.equals('Item ${cursor.key}', cursor.value); | 76 Expect.equals('Item ${cursor.key}', cursor.value); |
| 70 cursor.continueFunction(); | 77 cursor.continueFunction(); |
| 71 } else { | 78 } else { |
| 72 // Done | 79 // Done |
| 73 Expect.equals(expectedFirst, firstKey); | 80 Expect.equals(expectedFirst, firstKey); |
| 74 Expect.equals(expectedLast, lastKey); | 81 Expect.equals(expectedLast, lastKey); |
| 75 if (expectedFirst == null) { | 82 if (expectedFirst == null) { |
| 76 Expect.equals(0, itemCount); | 83 Expect.equals(0, itemCount); |
| 77 } else { | 84 } else { |
| 78 Expect.equals(expectedLast - expectedFirst + 1, itemCount); | 85 Expect.equals(expectedLast - expectedFirst + 1, itemCount); |
| 79 } | 86 } |
| 80 callbackDone(); | 87 doneCallback(); |
| 81 } | 88 } |
| 82 }); | 89 }); |
| 90 }); | |
| 83 cursorRequest.on.error.add(fail('openCursor')); | 91 cursorRequest.on.error.add(fail('openCursor')); |
| 84 } | 92 } |
| 85 | 93 |
| 86 only1() => testRange(new IDBKeyRange.only(55), 55, 55); | 94 only1() => testRange(new IDBKeyRange.only(55), 55, 55); |
| 87 only2() => testRange(new IDBKeyRange.only(100), null, null); | 95 only2() => testRange(new IDBKeyRange.only(100), null, null); |
| 88 only3() => testRange(new IDBKeyRange.only(-1), null, null); | 96 only3() => testRange(new IDBKeyRange.only(-1), null, null); |
| 89 | 97 |
| 90 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); | 98 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); |
| 91 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); | 99 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); |
| 92 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); | 100 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 109 | 117 |
| 110 bound5() => | 118 bound5() => |
| 111 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), | 119 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), |
| 112 21, 29); | 120 21, 29); |
| 113 | 121 |
| 114 } | 122 } |
| 115 | 123 |
| 116 main() { | 124 main() { |
| 117 useHtmlConfiguration(); | 125 useHtmlConfiguration(); |
| 118 | 126 |
| 119 var test = new Test(); | 127 var test_ = new Test(); |
| 120 asyncTest('prepare', 1, test.start); | 128 test('prepare', test_.start); |
| 121 | 129 |
| 122 asyncTest('only1', 1, test.only1); | 130 test('only1', test_.only1); |
| 123 asyncTest('only2', 1, test.only2); | 131 test('only2', test_.only2); |
| 124 asyncTest('only3', 1, test.only3); | 132 test('only3', test_.only3); |
| 125 | 133 |
| 126 asyncTest('lower1', 1, test.lower1); | 134 test('lower1', test_.lower1); |
| 127 asyncTest('lower2', 1, test.lower2); | 135 test('lower2', test_.lower2); |
| 128 asyncTest('lower3', 1, test.lower3); | 136 test('lower3', test_.lower3); |
| 129 | 137 |
| 130 asyncTest('upper1', 1, test.upper1); | 138 test('upper1', test_.upper1); |
| 131 asyncTest('upper2', 1, test.upper2); | 139 test('upper2', test_.upper2); |
| 132 asyncTest('upper3', 1, test.upper3); | 140 test('upper3', test_.upper3); |
| 133 | 141 |
| 134 asyncTest('bound1', 1, test.bound1); | 142 test('bound1', test_.bound1); |
| 135 asyncTest('bound2', 1, test.bound2); | 143 test('bound2', test_.bound2); |
| 136 asyncTest('bound3', 1, test.bound3); | 144 test('bound3', test_.bound3); |
| 137 asyncTest('bound4', 1, test.bound4); | 145 test('bound4', test_.bound4); |
| 138 asyncTest('bound5', 1, test.bound5); | 146 test('bound5', test_.bound5); |
| 139 | 147 |
| 140 } | 148 } |
| OLD | NEW |