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