idbcursor_delete_index.any.js (5124B)
1 // META: global=window,worker 2 // META: title=IDBCursor.delete() - index 3 // META: script=resources/support.js 4 // @author Microsoft <https://www.microsoft.com> 5 // @author Intel <http://www.intel.com> 6 7 'use strict'; 8 9 function createObjectStoreWithIndexAndPopulate(db, records) { 10 let objStore = db.createObjectStore("test", { keyPath: "pKey" }); 11 objStore.createIndex("index", "iKey"); 12 for (let i = 0; i < records.length; i++) { 13 objStore.add(records[i]); 14 } 15 return objStore; 16 } 17 18 function setOnUpgradeNeeded(dbObj, records) { 19 return function (event) { 20 dbObj.db = event.target.result; 21 createObjectStoreWithIndexAndPopulate(dbObj.db, records); 22 }; 23 } 24 25 async_test(t => { 26 let dbObj = {}; 27 let count = 0; 28 29 const records = [ 30 { pKey: "primaryKey_0", iKey: "indexKey_0" }, 31 { pKey: "primaryKey_1", iKey: "indexKey_1" } 32 ]; 33 34 let open_rq = createdb(t); 35 open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records); 36 37 open_rq.onsuccess = t.step_func(CursorDeleteRecord); 38 39 function CursorDeleteRecord(e) { 40 let txn = dbObj.db.transaction("test", "readwrite"), 41 cursor_rq = txn.objectStore("test") 42 .index("index") 43 .openCursor(); 44 45 cursor_rq.onsuccess = t.step_func(function (e) { 46 let cursor = e.target.result; 47 48 assert_true(cursor instanceof IDBCursor, "cursor exist"); 49 cursor.delete(); 50 }); 51 52 txn.oncomplete = t.step_func(VerifyRecordWasDeleted); 53 } 54 55 56 function VerifyRecordWasDeleted(e) { 57 let cursor_rq = dbObj.db.transaction("test", "readonly") 58 .objectStore("test") 59 .openCursor(); 60 61 cursor_rq.onsuccess = t.step_func(function (e) { 62 let cursor = e.target.result; 63 64 if (!cursor) { 65 assert_equals(count, 1, 'count'); 66 t.done(); 67 } 68 69 assert_equals(cursor.value.pKey, records[1].pKey); 70 assert_equals(cursor.value.iKey, records[1].iKey); 71 cursor.continue(); 72 count++; 73 }); 74 } 75 }, "Remove a record from the object store"); 76 77 async_test(t => { 78 let dbObj = {}; 79 const records = [ 80 { pKey: "primaryKey_0", iKey: "indexKey_0" }, 81 { pKey: "primaryKey_1", iKey: "indexKey_1" } 82 ]; 83 84 let open_rq = createdb(t); 85 open_rq.onupgradeneeded = setOnUpgradeNeeded(dbObj, records); 86 87 open_rq.onsuccess = function (e) { 88 let cursor_rq = dbObj.db.transaction("test", "readonly") 89 .objectStore("test") 90 .index("index") 91 .openCursor(); 92 93 cursor_rq.onsuccess = t.step_func(function (e) { 94 let cursor = e.target.result; 95 96 assert_true(cursor instanceof IDBCursor, "cursor exist"); 97 assert_throws_dom('ReadOnlyError', function () { cursor.delete(); }); 98 t.done(); 99 }); 100 } 101 }, "Attempt to remove a record in a read-only transaction"); 102 103 async_test(t => { 104 let db; 105 const records = [ 106 { pKey: "primaryKey_0", iKey: "indexKey_0" }, 107 { pKey: "primaryKey_1", iKey: "indexKey_1" } 108 ]; 109 110 let open_rq = createdb(t); 111 open_rq.onupgradeneeded = function (e) { 112 db = e.target.result; 113 let objStore = createObjectStoreWithIndexAndPopulate(db, records); 114 115 let cursor_rq = objStore.index("index").openCursor(); 116 117 cursor_rq.onsuccess = t.step_func(function (e) { 118 let cursor = e.target.result; 119 assert_true(cursor instanceof IDBCursor, "cursor exist"); 120 self.cursor = cursor; 121 }); 122 123 e.target.transaction.oncomplete = t.step_func(function (e) { 124 assert_throws_dom('TransactionInactiveError', 125 function () { self.cursor.delete(); }) 126 t.done(); 127 }); 128 } 129 }, "Attempt to remove a record in an inactive transaction"); 130 131 async_test(t => { 132 let db; 133 const records = [ 134 { pKey: "primaryKey_0", iKey: "indexKey_0" }, 135 { pKey: "primaryKey_1", iKey: "indexKey_1" } 136 ]; 137 138 let open_rq = createdb(t); 139 open_rq.onupgradeneeded = function (event) { 140 db = event.target.result; 141 let objStore = createObjectStoreWithIndexAndPopulate(db, records); 142 143 let rq = objStore.index("index").openCursor(); 144 145 rq.onsuccess = t.step_func(function (event) { 146 let cursor = event.target.result; 147 assert_true(cursor instanceof IDBCursor, "cursor exist"); 148 149 db.deleteObjectStore("test"); 150 assert_throws_dom("InvalidStateError", 151 function () { cursor.delete(); }); 152 153 t.done(); 154 }); 155 } 156 }, "If the cursor's source or effective object store has been deleted, the implementation MUST throw a DOMException of type InvalidStateError"); 157 158 async_test(t => { 159 let db; 160 const records = [ 161 { pKey: "primaryKey_0", iKey: "indexKey_0" }, 162 { pKey: "primaryKey_1", iKey: "indexKey_1" } 163 ]; 164 165 let open_rq = createdb(t); 166 open_rq.onupgradeneeded = function (event) { 167 db = event.target.result; 168 let objStore = createObjectStoreWithIndexAndPopulate(db, records); 169 let rq = objStore.index("index").openCursor(); 170 rq.onsuccess = t.step_func(function (event) { 171 let cursor = event.target.result; 172 assert_true(cursor instanceof IDBCursor, "cursor exist"); 173 174 cursor.continue(); 175 assert_throws_dom("InvalidStateError", function () { 176 cursor.delete(); 177 }); 178 179 t.done(); 180 }); 181 } 182 }, "Throw InvalidStateError when the cursor is being iterated");