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/dom_config.dart'); | 3 #import('../../lib/unittest/dom_config.dart'); |
| 4 #import('dart:dom_deprecated'); | 4 #import('dart:dom_deprecated'); |
| 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.addEventListener('success', initDb); | 18 request.addEventListener('success', |
| 19 expectAsync1(initDb)); | |
| 19 request.addEventListener('error', fail('open')); | 20 request.addEventListener('error', fail('open')); |
| 20 } | 21 } |
| 21 | 22 |
| 22 initDb(e) { | 23 initDb(e) { |
| 23 db = e.target.result; | 24 db = e.target.result; |
| 24 // TODO. Some browsers do this the w3 way - passing the VERSION to the | 25 // 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 // open call and listening to onversionchange. Can we feature-detect the |
| 26 // difference and make it work? | 27 // difference and make it work? |
| 27 var request = db.setVersion(VERSION); | 28 var request = db.setVersion(VERSION); |
| 28 request.addEventListener('success', (e) { | 29 request.addEventListener('success', |
| 30 expectAsync1( (e) { | |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
remove extra space ( (
gram
2012/05/22 17:36:36
Done.
| |
| 29 try { | 31 try { |
| 30 // Nuke object store if it already exists. | 32 // Nuke object store if it already exists. |
| 31 db.deleteObjectStore(STORE_NAME); | 33 db.deleteObjectStore(STORE_NAME); |
| 32 } catch (IDBDatabaseException e) { } | 34 } catch (IDBDatabaseException e) { } |
| 33 db.createObjectStore(STORE_NAME); | 35 db.createObjectStore(STORE_NAME); |
| 34 writeItems(0); | 36 writeItems(0); |
| 35 }); | 37 })); |
| 36 request.addEventListener('blocked', fail('setVersion blocked')); | 38 request.addEventListener('blocked', fail('setVersion blocked')); |
| 37 request.addEventListener('error', fail('setVersion error')); | 39 request.addEventListener('error', fail('setVersion error')); |
| 38 } | 40 } |
| 39 | 41 |
| 40 writeItems(int index) { | 42 writeItems(int index) { |
| 41 if (index < 100) { | 43 if (index < 100) { |
| 42 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); | 44 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); |
| 43 var request = transaction.objectStore(STORE_NAME) | 45 var request = transaction.objectStore(STORE_NAME) |
| 44 .put('Item $index', index); | 46 .put('Item $index', index); |
| 45 request.addEventListener('success', (e) { writeItems(index + 1); }); | 47 request.addEventListener('success', |
| 48 expectAsync1( (e) { writeItems(index + 1); })); | |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
ditto
gram
2012/05/22 17:36:36
Done.
| |
| 46 request.addEventListener('error', fail('put')); | 49 request.addEventListener('error', fail('put')); |
| 47 } else { | |
| 48 callbackDone(); | |
| 49 } | 50 } |
| 50 } | 51 } |
| 51 | 52 |
| 52 fail(message) => (e) { | 53 fail(message) => (e) { |
| 53 callbackDone(); | 54 guardAsync(() { |
| 54 Expect.fail('IndexedDB failure: $message'); | 55 Expect.fail('IndexedDB failure: $message'); |
| 56 }); | |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 testRange(range, expectedFirst, expectedLast) { | 59 testRange(range, expectedFirst, expectedLast) { |
| 58 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | 60 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 59 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 61 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 60 IDBRequest cursorRequest = objectStore.openCursor(range); | 62 IDBRequest cursorRequest = objectStore.openCursor(range); |
| 61 int itemCount = 0; | 63 int itemCount = 0; |
| 62 num firstKey = null; | 64 num firstKey = null; |
| 63 num lastKey = null; | 65 num lastKey = null; |
| 64 cursorRequest.addEventListener("success", (e) { | 66 var doneCallback = expectAsync0((){}); |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
here also you could do without doneCallback:
expec
gram
2012/05/22 17:36:36
Done.
| |
| 65 var cursor = e.target.result; | 67 cursorRequest.addEventListener("success", |
| 66 if (cursor != null) { | 68 (e) { |
| 67 if (firstKey == null) firstKey = cursor.key; | 69 guardAsync(() { |
| 68 lastKey = cursor.key; | 70 var cursor = e.target.result; |
| 69 itemCount += 1; | 71 if (cursor != null) { |
| 70 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); | 72 if (firstKey == null) firstKey = cursor.key; |
| 71 cursor.continueFunction(); | 73 lastKey = cursor.key; |
| 72 } else { | 74 itemCount += 1; |
| 73 // Done | 75 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', |
| 74 Expect.equals(expectedFirst, firstKey); | 76 cursor.value); |
| 75 Expect.equals(expectedLast, lastKey); | 77 cursor.continueFunction(); |
| 76 if (expectedFirst == null) { | |
| 77 Expect.equals(0, itemCount); | |
| 78 } else { | 78 } else { |
| 79 Expect.equals(expectedLast - expectedFirst + 1, itemCount); | 79 // Done |
| 80 Expect.equals(expectedFirst, firstKey); | |
| 81 Expect.equals(expectedLast, lastKey); | |
| 82 if (expectedFirst == null) { | |
| 83 Expect.equals(0, itemCount); | |
| 84 } else { | |
| 85 Expect.equals(expectedLast - expectedFirst + 1, itemCount); | |
| 86 } | |
| 87 doneCallback(); | |
| 80 } | 88 } |
| 81 callbackDone(); | 89 }); |
| 82 } | 90 } |
| 83 }); | 91 ); |
| 84 cursorRequest.addEventListener('error', fail('openCursor')); | 92 cursorRequest.addEventListener('error', fail('openCursor')); |
| 85 } | 93 } |
| 86 | 94 |
| 87 only1() => testRange(new IDBKeyRange.only(55), 55, 55); | 95 only1() => testRange(new IDBKeyRange.only(55), 55, 55); |
| 88 only2() => testRange(new IDBKeyRange.only(100), null, null); | 96 only2() => testRange(new IDBKeyRange.only(100), null, null); |
| 89 only3() => testRange(new IDBKeyRange.only(-1), null, null); | 97 only3() => testRange(new IDBKeyRange.only(-1), null, null); |
| 90 | 98 |
| 91 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); | 99 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); |
| 92 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); | 100 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); |
| 93 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); | 101 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 110 | 118 |
| 111 bound5() => | 119 bound5() => |
| 112 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), | 120 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), |
| 113 21, 29); | 121 21, 29); |
| 114 | 122 |
| 115 } | 123 } |
| 116 | 124 |
| 117 main() { | 125 main() { |
| 118 useDomConfiguration(); | 126 useDomConfiguration(); |
| 119 | 127 |
| 120 var test = new Test(); | 128 var test_ = new Test(); |
| 121 asyncTest('prepare', 1, test.start); | 129 test('prepare', test_.start); |
| 122 | 130 |
| 123 asyncTest('only1', 1, test.only1); | 131 test('only1', test_.only1); |
| 124 asyncTest('only2', 1, test.only2); | 132 test('only2', test_.only2); |
| 125 asyncTest('only3', 1, test.only3); | 133 test('only3', test_.only3); |
| 126 | 134 |
| 127 asyncTest('lower1', 1, test.lower1); | 135 test('lower1', test_.lower1); |
| 128 asyncTest('lower2', 1, test.lower2); | 136 test('lower2', test_.lower2); |
| 129 asyncTest('lower3', 1, test.lower3); | 137 test('lower3', test_.lower3); |
| 130 | 138 |
| 131 asyncTest('upper1', 1, test.upper1); | 139 test('upper1', test_.upper1); |
| 132 asyncTest('upper2', 1, test.upper2); | 140 test('upper2', test_.upper2); |
| 133 asyncTest('upper3', 1, test.upper3); | 141 test('upper3', test_.upper3); |
| 134 | 142 |
| 135 asyncTest('bound1', 1, test.bound1); | 143 test('bound1', test_.bound1); |
| 136 asyncTest('bound2', 1, test.bound2); | 144 test('bound2', test_.bound2); |
| 137 asyncTest('bound3', 1, test.bound3); | 145 test('bound3', test_.bound3); |
| 138 asyncTest('bound4', 1, test.bound4); | 146 test('bound4', test_.bound4); |
| 139 asyncTest('bound5', 1, test.bound5); | 147 test('bound5', test_.bound5); |
| 140 | 148 |
| 141 } | 149 } |
| OLD | NEW |