Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #library('IndexedDB3Test'); | 1 #library('IndexedDB3Test'); |
| 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 #import('dart:coreimpl'); | 5 #import('dart:coreimpl'); |
| 6 | 6 |
| 7 // Read with cursor. | 7 // Read with cursor. |
| 8 | 8 |
| 9 final String DB_NAME = 'Test'; | 9 final String DB_NAME = 'Test'; |
| 10 final String STORE_NAME = 'TEST'; | 10 final String STORE_NAME = 'TEST'; |
| 11 final String VERSION = '1'; | 11 final String VERSION = '1'; |
| 12 | 12 |
| 13 class Test { | 13 class Test { |
| 14 var db; | 14 var db; |
| 15 | 15 |
| 16 start() { | 16 start() { |
| 17 var request = window.webkitIndexedDB.open(DB_NAME); | 17 var request = window.webkitIndexedDB.open(DB_NAME); |
| 18 Expect.isNotNull(request); | 18 Expect.isNotNull(request); |
| 19 request.addEventListener('success', initDb); | 19 request.addEventListener('success', expectAsync1(initDb)); |
| 20 request.addEventListener('error', fail('open')); | 20 request.addEventListener('error', fail('open')); |
| 21 } | 21 } |
| 22 | 22 |
| 23 initDb(e) { | 23 initDb(e) { |
| 24 db = e.target.result; | 24 db = e.target.result; |
| 25 // 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 |
| 26 // open call and listening to onversionchange. Can we feature-detect the | 26 // open call and listening to onversionchange. Can we feature-detect the |
| 27 // difference and make it work? | 27 // difference and make it work? |
| 28 var request = db.setVersion(VERSION); | 28 var request = db.setVersion(VERSION); |
| 29 request.addEventListener('success', (e) { | 29 request.addEventListener('success', |
| 30 expectAsync1((e) { | |
| 30 try { | 31 try { |
| 31 // Nuke object store if it already exists. | 32 // Nuke object store if it already exists. |
| 32 db.deleteObjectStore(STORE_NAME); | 33 db.deleteObjectStore(STORE_NAME); |
| 33 } catch (IDBDatabaseException e) { } | 34 } catch (IDBDatabaseException e) { } |
| 34 db.createObjectStore(STORE_NAME); | 35 db.createObjectStore(STORE_NAME); |
| 35 writeItems(0); | 36 writeItems(0); |
| 36 }); | 37 })); |
| 37 request.addEventListener('blocked', fail('setVersion blocked')); | 38 request.addEventListener('blocked', fail('setVersion blocked')); |
| 38 request.addEventListener('error', fail('setVersion error')); | 39 request.addEventListener('error', fail('setVersion error')); |
| 39 } | 40 } |
| 40 | 41 |
| 41 writeItems(int index) { | 42 writeItems(int index) { |
| 42 if (index < 100) { | 43 if (index < 100) { |
| 43 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); | 44 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); |
| 44 var request = transaction.objectStore(STORE_NAME) | 45 var request = transaction.objectStore(STORE_NAME) |
| 45 .put('Item $index', index); | 46 .put('Item $index', index); |
| 46 request.addEventListener('success', (e) { writeItems(index + 1); }); | 47 request.addEventListener('success', |
| 48 expectAsync1((e) { | |
| 49 writeItems(index + 1); | |
| 50 }) | |
| 51 ); | |
| 47 request.addEventListener('error', fail('put')); | 52 request.addEventListener('error', fail('put')); |
| 48 } else { | |
| 49 callbackDone(); | |
| 50 } | 53 } |
| 51 } | 54 } |
| 52 | 55 |
| 53 fail(message) => (e) { | 56 fail(message) => (e) { |
| 54 callbackDone(); | 57 guardAsync(() { |
| 55 Expect.fail('IndexedDB failure: $message'); | 58 Expect.fail('IndexedDB failure: $message'); |
| 59 }); | |
| 56 }; | 60 }; |
| 57 | 61 |
| 58 readAllViaCursor() { | 62 readAllViaCursor() { |
| 59 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | 63 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 60 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 64 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 61 IDBRequest cursorRequest = objectStore.openCursor(); | 65 IDBRequest cursorRequest = objectStore.openCursor(); |
| 62 int itemCount = 0; | 66 int itemCount = 0; |
| 63 int sumKeys = 0; | 67 int sumKeys = 0; |
| 64 int lastKey = null; | 68 int lastKey = null; |
| 69 var doneCallback = expectAsync0((){}); | |
| 65 cursorRequest.addEventListener("success", (e) { | 70 cursorRequest.addEventListener("success", (e) { |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
this is one you could do without the 'doneCallback
gram
2012/05/22 17:36:36
Done.
| |
| 71 guardAsync(() { | |
| 66 var cursor = e.target.result; | 72 var cursor = e.target.result; |
| 67 if (cursor != null) { | 73 if (cursor != null) { |
| 68 lastKey = cursor.key; | 74 lastKey = cursor.key; |
| 69 itemCount += 1; | 75 itemCount += 1; |
| 70 sumKeys += cursor.key; | 76 sumKeys += cursor.key; |
| 71 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); | 77 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); |
| 72 cursor.continueFunction(); | 78 cursor.continueFunction(); |
| 73 } else { | 79 } else { |
| 74 // Done | 80 // Done |
| 75 Expect.equals(99, lastKey); | 81 Expect.equals(99, lastKey); |
| 76 Expect.equals(100, itemCount); | 82 Expect.equals(100, itemCount); |
| 77 Expect.equals((100 * 99) ~/ 2, sumKeys); | 83 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 78 callbackDone(); | 84 doneCallback(); |
| 79 } | 85 } |
| 80 }); | 86 }); |
| 87 }); | |
| 81 cursorRequest.addEventListener('error', fail('openCursor')); | 88 cursorRequest.addEventListener('error', fail('openCursor')); |
| 82 } | 89 } |
| 83 | 90 |
| 84 readAllReversedViaCursor() { | 91 readAllReversedViaCursor() { |
| 85 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | 92 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 86 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 93 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 87 // TODO: create a IDBKeyRange(0,100) | 94 // TODO: create a IDBKeyRange(0,100) |
| 88 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); | 95 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); |
| 89 int itemCount = 0; | 96 int itemCount = 0; |
| 90 int sumKeys = 0; | 97 int sumKeys = 0; |
| 91 int lastKey = null; | 98 int lastKey = null; |
| 99 var doneCallback = expectAsync0((){}); | |
| 92 cursorRequest.addEventListener("success", (e) { | 100 cursorRequest.addEventListener("success", (e) { |
|
Siggi Cherem (dart-lang)
2012/05/21 22:20:47
same here
gram
2012/05/22 17:36:36
Done.
| |
| 101 guardAsync(() { | |
| 93 var cursor = e.target.result; | 102 var cursor = e.target.result; |
| 94 if (cursor != null) { | 103 if (cursor != null) { |
| 95 lastKey = cursor.key; | 104 lastKey = cursor.key; |
| 96 itemCount += 1; | 105 itemCount += 1; |
| 97 sumKeys += cursor.key; | 106 sumKeys += cursor.key; |
| 98 Expect.equals('Item ${cursor.key}', cursor.value); | 107 Expect.equals('Item ${cursor.key}', cursor.value); |
| 99 cursor.continueFunction(); | 108 cursor.continueFunction(); |
| 100 } else { | 109 } else { |
| 101 // Done | 110 // Done |
| 102 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | 111 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). |
| 103 Expect.equals(100, itemCount); | 112 Expect.equals(100, itemCount); |
| 104 Expect.equals((100 * 99) ~/ 2, sumKeys); | 113 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 105 callbackDone(); | 114 doneCallback(); |
| 106 } | 115 } |
| 107 }); | 116 }); |
| 117 }); | |
| 108 cursorRequest.addEventListener('error', fail('openCursor')); | 118 cursorRequest.addEventListener('error', fail('openCursor')); |
| 109 } | 119 } |
| 110 } | 120 } |
| 111 | 121 |
| 112 main() { | 122 main() { |
| 113 useDomConfiguration(); | 123 useDomConfiguration(); |
| 114 | 124 |
| 115 var test = new Test(); | 125 var test_ = new Test(); |
| 116 asyncTest('prepare', 1, test.start); | 126 test('prepare', test_.start); |
| 117 asyncTest('readAll1', 1, test.readAllViaCursor); | 127 test('readAll1', test_.readAllViaCursor); |
| 118 asyncTest('readAll2', 1, test.readAllReversedViaCursor); | 128 test('readAll2', test_.readAllReversedViaCursor); |
| 119 } | 129 } |
| OLD | NEW |