idbcursor-direction-index-keyrange.any.js (1830B)
1 // META: global=window,worker 2 // META: title=IDBCursor direction - index with keyrange 3 // META: script=resources/support.js 4 5 // Spec: https://w3c.github.io/IndexedDB/#cursor-iteration-operation 6 7 'use strict'; 8 9 let records = [1337, 'Alice', 'Bob', 'Bob', 'Greg', 'Åke', ['Anne']]; 10 let cases = [ 11 {dir: 'next', expect: ['Alice:1', 'Bob:2', 'Bob:3', 'Greg:4']}, 12 {dir: 'prev', expect: ['Greg:4', 'Bob:3', 'Bob:2', 'Alice:1']}, 13 {dir: 'nextunique', expect: ['Alice:1', 'Bob:2', 'Greg:4']}, 14 {dir: 'prevunique', expect: ['Greg:4', 'Bob:2', 'Alice:1']} 15 ]; 16 17 cases.forEach(function(testcase) { 18 let dir = testcase.dir; 19 let expect = testcase.expect; 20 indexeddb_test( 21 function(t, db, tx) { 22 let objStore = db.createObjectStore('test'); 23 objStore.createIndex('idx', 'name'); 24 25 for (let i = 0; i < records.length; i++) { 26 objStore.add({name: records[i]}, i); 27 } 28 }, 29 function(t, db) { 30 let count = 0; 31 let rq = db.transaction('test', 'readonly') 32 .objectStore('test') 33 .index('idx') 34 .openCursor(IDBKeyRange.bound('AA', 'ZZ'), dir); 35 rq.onsuccess = t.step_func(function(e) { 36 let cursor = e.target.result; 37 if (!cursor) { 38 assert_equals(count, expect.length, 'cursor runs'); 39 t.done(); 40 return; 41 } 42 assert_equals( 43 cursor.value.name + ':' + cursor.primaryKey, expect[count], 44 'cursor.value'); 45 count++; 46 cursor.continue(); 47 }); 48 rq.onerror = t.step_func(function(e) { 49 e.preventDefault(); 50 e.stopPropagation(); 51 assert_unreached('rq.onerror - ' + e.message); 52 }); 53 }, 54 'IDBCursor direction - index with keyrange - ' + dir); 55 });