idbcursor-request-source.any.js (4382B)
1 // META: global=window,worker 2 // META: title=IndexedDB: The source of requests made against cursors 3 // META: script=resources/support.js 4 5 // Spec: https://w3c.github.io/IndexedDB/#dom-idbrequest-source 6 7 'use strict'; 8 9 // Setup each test by populating an object store with an index for the cursor to 10 // iterate and manipulate. 11 function initializeDatabase(db) { 12 const store = db.createObjectStore('store', {autoIncrement: true}); 13 store.createIndex('index', /*keypath=*/ 'value'); 14 store.put({value: 'z'}); 15 store.put({value: 'y'}); 16 store.put({value: 'x'}); 17 store.put({value: 'w'}); 18 } 19 20 function isIndex(cursorSourceType) { 21 return cursorSourceType === 'IDBIndex'; 22 } 23 24 // Return the object store or index, depending on the test's `cursorSourceType`. 25 function getCursorSource(transaction, cursorSourceType) { 26 let cursorSource = transaction.objectStore('store'); 27 if (isIndex(cursorSourceType)) { 28 cursorSource = cursorSource.index('index'); 29 } 30 return cursorSource; 31 } 32 33 // Verify the request source after calling delete() or update() on the cursor. 34 function cursor_request_source_test( 35 cursorSourceType, createRequestFunctionName, createRequestFunctionArgs) { 36 indexeddb_test( 37 (t, db) => initializeDatabase(db), 38 (t, db) => { 39 const tx = db.transaction('store', 'readwrite'); 40 const cursorSource = getCursorSource(tx, cursorSourceType); 41 42 // Open the cursor. 43 const openCursorRequest = cursorSource.openCursor(); 44 openCursorRequest.onerror = 45 t.unreached_func('The cursor must not fail to open.'); 46 47 openCursorRequest.onsuccess = t.step_func(e => { 48 // Use the cursor to create a new request. 49 const cursor = e.target.result; 50 const request = 51 cursor[createRequestFunctionName](...createRequestFunctionArgs); 52 assert_equals( 53 request.source, cursor, 54 `The request's source must be the cursor itself.`); 55 t.done(); 56 }); 57 }, 58 `The source of the request from ${cursorSourceType}::${ 59 createRequestFunctionName}() is the cursor itself`); 60 } 61 62 // Verify the request source after calling openCursor() or openKeyCursor() and 63 // then using the cursor to iterate. 64 function open_cursor_request_source_test( 65 cursorSourceType, openCursorFunctionName) { 66 indexeddb_test( 67 (t, db) => initializeDatabase(db), 68 (t, db) => { 69 const tx = db.transaction('store', 'readonly'); 70 const cursorSource = getCursorSource(tx, cursorSourceType); 71 72 // Open the cursor. 73 const openCursorRequest = cursorSource[openCursorFunctionName](); 74 openCursorRequest.onerror = 75 t.unreached_func('The cursor must not fail to open or iterate.'); 76 77 assert_equals( 78 openCursorRequest.source, cursorSource, 79 'The request source must be the opener of the cursor.'); 80 81 // Verify the cursor's `request.source` after iterating with 82 // `advance()`, `continue()`, and `continuePrimaryKey()`. 83 let iterationCount = 0; 84 openCursorRequest.onsuccess = t.step_func(e => { 85 assert_equals( 86 openCursorRequest.source, cursorSource, 87 'The request source must be the opener of the cursor after iterating.'); 88 89 const cursor = e.target.result; 90 ++iterationCount; 91 92 if (iterationCount == 1) { 93 cursor.advance(1); 94 } else if (iterationCount == 2) { 95 cursor.continue(); 96 } else if (iterationCount == 3 && isIndex(cursorSourceType)) { 97 cursor.continuePrimaryKey('z', 0); 98 } else { 99 t.done(); 100 } 101 }); 102 }, 103 `${cursorSourceType}::${ 104 openCursorFunctionName}'s request source must be the ${ 105 cursorSourceType} instance that opened the cursor`); 106 } 107 108 open_cursor_request_source_test('IDBObjectStore', 'openCursor'); 109 open_cursor_request_source_test('IDBObjectStore', 'openKeyCursor'); 110 open_cursor_request_source_test('IDBIndex', 'openCursor'); 111 open_cursor_request_source_test('IDBIndex', 'openKeyCursor'); 112 113 cursor_request_source_test('IDBObjectStore', 'update', /*args=*/[0]); 114 cursor_request_source_test('IDBObjectStore', 'delete', /*args=*/[]); 115 cursor_request_source_test('IDBIndex', 'update', /*args=*/[0]); 116 cursor_request_source_test('IDBIndex', 'delete', /*args=*/[]);