| OLD | NEW |
| (Empty) | |
| 1 #library('IndexedDB4Test'); |
| 2 #import('../../lib/unittest/unittest.dart'); |
| 3 #import('../../lib/unittest/html_config.dart'); |
| 4 #import('dart:html'); |
| 5 |
| 6 // Test for IDBKeyRange and IDBCursor. |
| 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 testRange(range, expectedFirst, expectedLast) { |
| 57 IDBTransaction txn = db.transaction(STORE_NAME, IDBTransaction.READ_ONLY); |
| 58 IDBObjectStore objectStore = txn.objectStore(STORE_NAME); |
| 59 IDBRequest cursorRequest = objectStore.openCursor(range); |
| 60 int itemCount = 0; |
| 61 int firstKey = null; |
| 62 int lastKey = null; |
| 63 cursorRequest.on.success.add((e) { |
| 64 var cursor = e.target.result; |
| 65 if (cursor != null) { |
| 66 if (firstKey == null) firstKey = cursor.key; |
| 67 lastKey = cursor.key; |
| 68 itemCount += 1; |
| 69 Expect.equals('Item ${cursor.key}', cursor.value); |
| 70 cursor.continueFunction(); |
| 71 } else { |
| 72 // Done |
| 73 Expect.equals(expectedFirst, firstKey); |
| 74 Expect.equals(expectedLast, lastKey); |
| 75 if (expectedFirst == null) { |
| 76 Expect.equals(0, itemCount); |
| 77 } else { |
| 78 Expect.equals(expectedLast - expectedFirst + 1, itemCount); |
| 79 } |
| 80 callbackDone(); |
| 81 } |
| 82 }); |
| 83 cursorRequest.on.error.add(fail('openCursor')); |
| 84 } |
| 85 |
| 86 only1() => testRange(new IDBKeyRange.only(55), 55, 55); |
| 87 only2() => testRange(new IDBKeyRange.only(100), null, null); |
| 88 only3() => testRange(new IDBKeyRange.only(-1), null, null); |
| 89 |
| 90 lower1() => testRange(new IDBKeyRange.lowerBound(40), 40, 99); |
| 91 lower2() => testRange(new IDBKeyRange.lowerBound(40, open: true), 41, 99); |
| 92 lower3() => testRange(new IDBKeyRange.lowerBound(40, open: false), 40, 99); |
| 93 |
| 94 upper1() => testRange(new IDBKeyRange.upperBound(40), 0, 40); |
| 95 upper2() => testRange(new IDBKeyRange.upperBound(40, open: true), 0, 39); |
| 96 upper3() => testRange(new IDBKeyRange.upperBound(40, open: false), 0, 40); |
| 97 |
| 98 bound1() => testRange(new IDBKeyRange.bound(20, 30), 20, 30); |
| 99 |
| 100 bound2() => testRange(new IDBKeyRange.bound(-100, 200), 0, 99); |
| 101 |
| 102 bound3() => |
| 103 testRange(new IDBKeyRange.bound(20, 30, upperOpen: true), |
| 104 20, 29); |
| 105 |
| 106 bound4() => |
| 107 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true), |
| 108 21, 30); |
| 109 |
| 110 bound5() => |
| 111 testRange(new IDBKeyRange.bound(20, 30, lowerOpen: true, upperOpen: true), |
| 112 21, 29); |
| 113 |
| 114 } |
| 115 |
| 116 main() { |
| 117 useHtmlConfiguration(); |
| 118 |
| 119 var test = new Test(); |
| 120 asyncTest('prepare', 1, test.start); |
| 121 |
| 122 asyncTest('only1', 1, test.only1); |
| 123 asyncTest('only2', 1, test.only2); |
| 124 asyncTest('only3', 1, test.only3); |
| 125 |
| 126 asyncTest('lower1', 1, test.lower1); |
| 127 asyncTest('lower2', 1, test.lower2); |
| 128 asyncTest('lower3', 1, test.lower3); |
| 129 |
| 130 asyncTest('upper1', 1, test.upper1); |
| 131 asyncTest('upper2', 1, test.upper2); |
| 132 asyncTest('upper3', 1, test.upper3); |
| 133 |
| 134 asyncTest('bound1', 1, test.bound1); |
| 135 asyncTest('bound2', 1, test.bound2); |
| 136 asyncTest('bound3', 1, test.bound3); |
| 137 asyncTest('bound4', 1, test.bound4); |
| 138 asyncTest('bound5', 1, test.bound5); |
| 139 |
| 140 } |
| OLD | NEW |