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