| 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; |
| 65 cursorRequest.addEventListener("success", (e) { | 69 cursorRequest.addEventListener("success", expectAsync1((e) { |
| 66 var cursor = e.target.result; | 70 var cursor = e.target.result; |
| 67 if (cursor != null) { | 71 if (cursor != null) { |
| 68 lastKey = cursor.key; | 72 lastKey = cursor.key; |
| 69 itemCount += 1; | 73 itemCount += 1; |
| 70 sumKeys += cursor.key; | 74 sumKeys += cursor.key; |
| 71 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); | 75 Expect.equals('Item ${cursor.key.toStringAsFixed(0)}', cursor.value); |
| 72 cursor.continueFunction(); | 76 cursor.continueFunction(); |
| 73 } else { | 77 } else { |
| 74 // Done | 78 // Done |
| 75 Expect.equals(99, lastKey); | 79 Expect.equals(99, lastKey); |
| 76 Expect.equals(100, itemCount); | 80 Expect.equals(100, itemCount); |
| 77 Expect.equals((100 * 99) ~/ 2, sumKeys); | 81 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 78 callbackDone(); | 82 } |
| 79 } | 83 }, 101)); |
| 80 }); | |
| 81 cursorRequest.addEventListener('error', fail('openCursor')); | 84 cursorRequest.addEventListener('error', fail('openCursor')); |
| 82 } | 85 } |
| 83 | 86 |
| 84 readAllReversedViaCursor() { | 87 readAllReversedViaCursor() { |
| 85 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); | 88 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 86 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); | 89 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 87 // TODO: create a IDBKeyRange(0,100) | 90 // TODO: create a IDBKeyRange(0,100) |
| 88 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); | 91 IDBRequest cursorRequest = objectStore.openCursor(null, IDBCursor.PREV); |
| 89 int itemCount = 0; | 92 int itemCount = 0; |
| 90 int sumKeys = 0; | 93 int sumKeys = 0; |
| 91 int lastKey = null; | 94 int lastKey = null; |
| 92 cursorRequest.addEventListener("success", (e) { | 95 cursorRequest.addEventListener("success", expectAsync1((e) { |
| 93 var cursor = e.target.result; | 96 var cursor = e.target.result; |
| 94 if (cursor != null) { | 97 if (cursor != null) { |
| 95 lastKey = cursor.key; | 98 lastKey = cursor.key; |
| 96 itemCount += 1; | 99 itemCount += 1; |
| 97 sumKeys += cursor.key; | 100 sumKeys += cursor.key; |
| 98 Expect.equals('Item ${cursor.key}', cursor.value); | 101 Expect.equals('Item ${cursor.key}', cursor.value); |
| 99 cursor.continueFunction(); | 102 cursor.continueFunction(); |
| 100 } else { | 103 } else { |
| 101 // Done | 104 // Done |
| 102 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). | 105 Expect.equals(0, lastKey); // i.e. first key (scanned in reverse). |
| 103 Expect.equals(100, itemCount); | 106 Expect.equals(100, itemCount); |
| 104 Expect.equals((100 * 99) ~/ 2, sumKeys); | 107 Expect.equals((100 * 99) ~/ 2, sumKeys); |
| 105 callbackDone(); | 108 } |
| 106 } | 109 }, 101)); |
| 107 }); | |
| 108 cursorRequest.addEventListener('error', fail('openCursor')); | 110 cursorRequest.addEventListener('error', fail('openCursor')); |
| 109 } | 111 } |
| 110 } | 112 } |
| 111 | 113 |
| 112 main() { | 114 main() { |
| 113 useDomConfiguration(); | 115 useDomConfiguration(); |
| 114 | 116 |
| 115 var test = new Test(); | 117 var test_ = new Test(); |
| 116 asyncTest('prepare', 1, test.start); | 118 test('prepare', test_.start); |
| 117 asyncTest('readAll1', 1, test.readAllViaCursor); | 119 test('readAll1', test_.readAllViaCursor); |
| 118 asyncTest('readAll2', 1, test.readAllReversedViaCursor); | 120 test('readAll2', test_.readAllReversedViaCursor); |
| 119 } | 121 } |
| OLD | NEW |