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