idbcursor-continuePrimaryKey.any.js (5666B)
1 // META: global=window,worker 2 // META: title=IndexedDB: IDBCursor method continuePrimaryKey() 3 // META: script=resources/support.js 4 5 // Spec: http://w3c.github.io/IndexedDB/#dom-idbcursor-continueprimarykey 6 7 'use strict'; 8 9 indexeddb_test( 10 (t, db, txn) => { 11 const store = db.createObjectStore('store'); 12 const index = store.createIndex('index', 'indexKey', {multiEntry: true}); 13 14 store.put({indexKey: ['a', 'b']}, 1); 15 store.put({indexKey: ['a', 'b']}, 2); 16 store.put({indexKey: ['a', 'b']}, 3); 17 store.put({indexKey: ['b']}, 4); 18 19 const expectedIndexEntries = [ 20 {key: 'a', primaryKey: 1}, 21 {key: 'a', primaryKey: 2}, 22 {key: 'a', primaryKey: 3}, 23 {key: 'b', primaryKey: 1}, 24 {key: 'b', primaryKey: 2}, 25 {key: 'b', primaryKey: 3}, 26 {key: 'b', primaryKey: 4}, 27 ]; 28 29 const request = index.openCursor(); 30 request.onerror = t.unreached_func('IDBIndex.openCursor should not fail'); 31 request.onsuccess = t.step_func(() => { 32 const cursor = request.result; 33 const expectedEntry = expectedIndexEntries.shift(); 34 if (expectedEntry) { 35 assert_equals( 36 cursor.key, expectedEntry.key, 37 'The index entry keys should reflect the object store contents'); 38 assert_equals( 39 cursor.primaryKey, expectedEntry.primaryKey, 40 'The index entry primary keys should reflect the object store ' + 41 'contents'); 42 cursor.continue(); 43 } else { 44 assert_equals( 45 cursor, null, 46 'The index should not have entries that do not reflect the ' + 47 'object store contents'); 48 } 49 }); 50 }, 51 (t, db) => { 52 const testCases = [ 53 // Continuing index key. 54 { 55 call: cursor => { 56 cursor.continue(); 57 }, 58 result: {key: 'a', primaryKey: 2} 59 }, 60 { 61 call: cursor => { 62 cursor.continue('a'); 63 }, 64 exception: 'DataError' 65 }, 66 { 67 call: cursor => { 68 cursor.continue('b'); 69 }, 70 result: {key: 'b', primaryKey: 1} 71 }, 72 { 73 call: cursor => { 74 cursor.continue('c'); 75 }, 76 result: null 77 }, 78 79 // Called w/ index key and primary key. 80 { 81 call: cursor => { 82 cursor.continuePrimaryKey('a', 3); 83 }, 84 result: {key: 'a', primaryKey: 3} 85 }, 86 { 87 call: cursor => { 88 cursor.continuePrimaryKey('a', 4); 89 }, 90 result: {key: 'b', primaryKey: 1} 91 }, 92 { 93 call: cursor => { 94 cursor.continuePrimaryKey('b', 1); 95 }, 96 result: {key: 'b', primaryKey: 1} 97 }, 98 { 99 call: cursor => { 100 cursor.continuePrimaryKey('b', 4); 101 }, 102 result: {key: 'b', primaryKey: 4} 103 }, 104 { 105 call: cursor => { 106 cursor.continuePrimaryKey('b', 5); 107 }, 108 result: null 109 }, 110 { 111 call: cursor => { 112 cursor.continuePrimaryKey('c', 1); 113 }, 114 result: null 115 }, 116 117 // Called w/ primary key but w/o index key. 118 { 119 call: cursor => { 120 cursor.continuePrimaryKey(null, 1); 121 }, 122 exception: 'DataError' 123 }, 124 { 125 call: cursor => { 126 cursor.continuePrimaryKey(null, 2); 127 }, 128 exception: 'DataError' 129 }, 130 { 131 call: cursor => { 132 cursor.continuePrimaryKey(null, 3); 133 }, 134 exception: 'DataError' 135 }, 136 { 137 call: cursor => { 138 cursor.continuePrimaryKey(null, 4); 139 }, 140 exception: 'DataError' 141 }, 142 { 143 call: cursor => { 144 cursor.continuePrimaryKey(null, 5); 145 }, 146 exception: 'DataError' 147 }, 148 149 // Called w/ index key but w/o primary key. 150 { 151 call: cursor => { 152 cursor.continuePrimaryKey('a', null); 153 }, 154 exception: 'DataError' 155 }, 156 ]; 157 158 const verifyContinueCalls = () => { 159 if (!testCases.length) { 160 t.done(); 161 return; 162 } 163 164 const testCase = testCases.shift(); 165 166 const txn = db.transaction('store', 'readonly'); 167 txn.oncomplete = t.step_func(verifyContinueCalls); 168 169 const request = txn.objectStore('store').index('index').openCursor(); 170 let calledContinue = false; 171 request.onerror = 172 t.unreached_func('IDBIndex.openCursor should not fail'); 173 request.onsuccess = t.step_func(() => { 174 const cursor = request.result; 175 if (calledContinue) { 176 if (testCase.result) { 177 assert_equals( 178 cursor.key, testCase.result.key, 179 `${testCase.call.toString()} - result key`); 180 assert_equals( 181 cursor.primaryKey, testCase.result.primaryKey, 182 `${testCase.call.toString()} - result primary key`); 183 } else { 184 assert_equals(cursor, null); 185 } 186 } else { 187 calledContinue = true; 188 if ('exception' in testCase) { 189 assert_throws_dom(testCase.exception, () => { 190 testCase.call(cursor); 191 }, testCase.call.toString()); 192 } else { 193 testCase.call(cursor); 194 } 195 } 196 }); 197 }; 198 verifyContinueCalls(); 199 });