| 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/html_config.dart'); |
| 4 #import('dart:dom'); | 4 #import('dart:html'); |
| 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.on.success.add(initDb); |
| 20 request.addEventListener('error', fail('open')); | 20 request.on.error.add(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.on.success.add((e) { |
| 30 try { | 30 try { |
| 31 // Nuke object store if it already exists. | 31 // Nuke object store if it already exists. |
| 32 db.deleteObjectStore(STORE_NAME); | 32 db.deleteObjectStore(STORE_NAME); |
| 33 } catch (IDBDatabaseException e) { } | 33 } catch (IDBDatabaseException e) { } |
| 34 db.createObjectStore(STORE_NAME); | 34 db.createObjectStore(STORE_NAME); |
| 35 writeItems(0); | 35 writeItems(0); |
| 36 }); | 36 }); |
| 37 request.addEventListener('blocked', fail('setVersion blocked')); | 37 request.on.blocked.add(fail('setVersion blocked')); |
| 38 request.addEventListener('error', fail('setVersion error')); | 38 request.on.error.add(fail('setVersion error')); |
| 39 } | 39 } |
| 40 | 40 |
| 41 writeItems(int index) { | 41 writeItems(int index) { |
| 42 if (index < 100) { | 42 if (index < 100) { |
| 43 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); | 43 var transaction = db.transaction([STORE_NAME], IDBTransaction.READ_WRITE); |
| 44 var request = transaction.objectStore(STORE_NAME) | 44 var request = transaction.objectStore(STORE_NAME) |
| 45 .put('Item $index', index); | 45 .put('Item $index', index); |
| 46 request.addEventListener('success', (e) { writeItems(index + 1); }); | 46 request.on.success.add((e) { writeItems(index + 1); }); |
| 47 request.addEventListener('error', fail('put')); | 47 request.on.error.add(fail('put')); |
| 48 } else { | 48 } else { |
| 49 callbackDone(); | 49 callbackDone(); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 fail(message) => (e) { | 53 fail(message) => (e) { |
| 54 callbackDone(); | 54 callbackDone(); |
| 55 Expect.fail('IndexedDB failure: $message'); | 55 Expect.fail('IndexedDB failure: $message'); |
| 56 }; | 56 }; |
| 57 | 57 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 71 Expect.equals('Item ${cursor.key}', cursor.value); | 71 Expect.equals('Item ${cursor.key}', cursor.value); |
| 72 cursor.continueFunction(); | 72 cursor.continueFunction(); |
| 73 } else { | 73 } else { |
| 74 // Done | 74 // Done |
| 75 Expect.equals(99, lastKey); | 75 Expect.equals(99, lastKey); |
| 76 Expect.equals(100, itemCount); | 76 Expect.equals(100, itemCount); |
| 77 Expect.equals((100 * 99) ~/ 2, sumKeys); | 77 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 78 callbackDone(); | 78 callbackDone(); |
| 79 } | 79 } |
| 80 }); | 80 }); |
| 81 cursorRequest.addEventListener('error', fail('openCursor')); | 81 cursorRequest.on.error.add(fail('openCursor')); |
| 82 } | 82 } |
| 83 | 83 |
| 84 readAllReversedViaCursor() { | 84 readAllReversedViaCursor() { |
| 85 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | 85 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 86 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 86 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 87 // TODO: create a IDBKeyRange(0,100) | 87 // TODO: create a IDBKeyRange(0,100) |
| 88 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); | 88 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); |
| 89 int itemCount = 0; | 89 int itemCount = 0; |
| 90 int sumKeys = 0; | 90 int sumKeys = 0; |
| 91 int lastKey = null; | 91 int lastKey = null; |
| 92 cursorRequest.addEventListener("success", (e) { | 92 cursorRequest.addEventListener("success", (e) { |
| 93 var cursor = e.target.result; | 93 var cursor = e.target.result; |
| 94 if (cursor != null) { | 94 if (cursor != null) { |
| 95 lastKey = cursor.key; | 95 lastKey = cursor.key; |
| 96 itemCount += 1; | 96 itemCount += 1; |
| 97 sumKeys += cursor.key; | 97 sumKeys += cursor.key; |
| 98 Expect.equals('Item ${cursor.key}', cursor.value); | 98 Expect.equals('Item ${cursor.key}', cursor.value); |
| 99 cursor.continueFunction(); | 99 cursor.continueFunction(); |
| 100 } else { | 100 } else { |
| 101 // Done | 101 // Done |
| 102 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | 102 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). |
| 103 Expect.equals(100, itemCount); | 103 Expect.equals(100, itemCount); |
| 104 Expect.equals((100 * 99) ~/ 2, sumKeys); | 104 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 105 callbackDone(); | 105 callbackDone(); |
| 106 } | 106 } |
| 107 }); | 107 }); |
| 108 cursorRequest.addEventListener('error', fail('openCursor')); | 108 cursorRequest.on.error.add(fail('openCursor')); |
| 109 } | 109 } |
| 110 } | 110 } |
| 111 | 111 |
| 112 main() { | 112 main() { |
| 113 useDomConfiguration(); | 113 useHtmlConfiguration(); |
| 114 | 114 |
| 115 var test = new Test(); | 115 var test = new Test(); |
| 116 asyncTest('prepare', 1, test.start); | 116 asyncTest('prepare', 1, test.start); |
| 117 asyncTest('readAll1', 1, test.readAllViaCursor); | 117 asyncTest('readAll1', 1, test.readAllViaCursor); |
| 118 asyncTest('readAll2', 1, test.readAllReversedViaCursor); | 118 asyncTest('readAll2', 1, test.readAllReversedViaCursor); |
| 119 } | 119 } |
| OLD | NEW |