Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #library('IndexedDB1Test'); | |
|
vsm
2012/04/16 19:41:39
DB1->DB3 in lib name
| |
| 2 #import('../../../../lib/unittest/unittest.dart'); | |
| 3 #import('../../../../lib/unittest/dom_config.dart'); | |
| 4 #import('dart:dom'); | |
| 5 #import('dart:coreimpl'); | |
| 6 | |
| 7 // Read with cursor. | |
| 8 | |
| 9 final String DB_NAME = 'Test'; | |
| 10 final String STORE_NAME = 'TEST'; | |
| 11 final String VERSION = '1'; | |
| 12 | |
| 13 class Test { | |
| 14 var db; | |
| 15 | |
| 16 start() { | |
| 17 var request = window.webkitIndexedDB.open(DB_NAME); | |
| 18 Expect.isNotNull(request); | |
| 19 request.addEventListener('success', initDb); | |
| 20 request.addEventListener('error', fail('open')); | |
| 21 } | |
| 22 | |
| 23 initDb(e) { | |
| 24 db = e.target.result; | |
| 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 | |
| 27 // difference and make it work? | |
| 28 var request = db.setVersion(VERSION); | |
| 29 request.addEventListener('success', (e) { | |
| 30 try { | |
| 31 // Nuke object store if it already exists. | |
| 32 db.deleteObjectStore(STORE_NAME); | |
| 33 print('Deleted store $STORE_NAME'); | |
|
vsm
2012/04/16 19:41:39
Extraneous prints sometimes screw up our tests.
| |
| 34 } catch (IDBDatabaseException e) { } | |
| 35 db.createObjectStore(STORE_NAME); | |
| 36 writeItems(0); | |
| 37 }); | |
| 38 request.addEventListener('blocked', fail('setVersion blocked')); | |
| 39 request.addEventListener('error', fail('setVersion error')); | |
| 40 } | |
| 41 | |
| 42 writeItems(int index) { | |
| 43 if (index < 100) { | |
| 44 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); | |
| 45 var request = transaction.objectStore(STORE_NAME) | |
| 46 .put('Item $index', index); | |
| 47 request.addEventListener('success', (e) { writeItems(index + 1); }); | |
| 48 request.addEventListener('error', fail('put')); | |
| 49 } else { | |
| 50 callbackDone(); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 fail(message) => (e) { | |
| 55 callbackDone(); | |
| 56 Expect.fail('IndexedDB failure: $message'); | |
| 57 }; | |
| 58 | |
| 59 readAllViaCursor() { | |
| 60 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | |
| 61 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | |
| 62 IDBRequest cursorRequest = objectStore.openCursor(); | |
| 63 int itemCount = 0; | |
| 64 int sumKeys = 0; | |
| 65 int lastKey = null; | |
| 66 cursorRequest.addEventListener("success", (e) { | |
| 67 var cursor = e.target.result; | |
| 68 if (cursor != null) { | |
| 69 lastKey = cursor.key; | |
| 70 itemCount += 1; | |
| 71 sumKeys += cursor.key; | |
| 72 Expect.equals('Item ${cursor.key}', cursor.value); | |
| 73 cursor.continueFunction(); | |
| 74 } else { | |
| 75 // Done | |
| 76 Expect.equals(99, lastKey); | |
| 77 Expect.equals(100, itemCount); | |
| 78 Expect.equals((100 * 99) ~/ 2, sumKeys); | |
| 79 callbackDone(); | |
| 80 } | |
| 81 }); | |
| 82 cursorRequest.addEventListener('error', fail('openCursor')); | |
| 83 } | |
| 84 | |
| 85 readAllReversedViaCursor() { | |
| 86 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | |
| 87 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | |
| 88 // TODO: create a IDBKeyRange(0,100) | |
| 89 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); | |
| 90 int itemCount = 0; | |
| 91 int sumKeys = 0; | |
| 92 int lastKey = null; | |
| 93 cursorRequest.addEventListener("success", (e) { | |
| 94 var cursor = e.target.result; | |
| 95 if (cursor != null) { | |
| 96 lastKey = cursor.key; | |
| 97 itemCount += 1; | |
| 98 sumKeys += cursor.key; | |
| 99 Expect.equals('Item ${cursor.key}', cursor.value); | |
| 100 cursor.continueFunction(); | |
| 101 } else { | |
| 102 // Done | |
| 103 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | |
| 104 Expect.equals(100, itemCount); | |
| 105 Expect.equals((100 * 99) ~/ 2, sumKeys); | |
| 106 callbackDone(); | |
| 107 } | |
| 108 }); | |
| 109 cursorRequest.addEventListener('error', fail('openCursor')); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 main() { | |
| 114 useDomConfiguration(); | |
| 115 | |
| 116 var test = new Test(); | |
| 117 asyncTest('prepare', 1, test.start); | |
| 118 asyncTest('readAll1', 1, test.readAllViaCursor); | |
| 119 asyncTest('readAll2', 1, test.readAllReversedViaCursor); | |
| 120 } | |
| OLD | NEW |