test_cursor_mutation.js (3352B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testGenerator */ 7 var testGenerator = testSteps(); 8 9 function* testSteps() { 10 const objectStoreData = [ 11 // This one will be removed. 12 { ss: "237-23-7732", name: "Bob" }, 13 14 // These will always be included. 15 { ss: "237-23-7733", name: "Ann" }, 16 { ss: "237-23-7734", name: "Ron" }, 17 { ss: "237-23-7735", name: "Sue" }, 18 { ss: "237-23-7736", name: "Joe" }, 19 20 // This one will be added. 21 { ss: "237-23-7737", name: "Pat" }, 22 ]; 23 24 // Post-add and post-remove data ordered by name. 25 const objectStoreDataNameSort = [1, 4, 5, 2, 3]; 26 27 let request = indexedDB.open( 28 this.window ? window.location.pathname : "Splendid Test", 29 1 30 ); 31 request.onerror = errorHandler; 32 request.onupgradeneeded = grabEventAndContinueHandler; 33 let event = yield undefined; 34 35 let db = event.target.result; 36 event.target.onsuccess = continueToNextStep; 37 38 let objectStore = db.createObjectStore("foo", { keyPath: "ss" }); 39 objectStore.createIndex("name", "name", { unique: true }); 40 41 for (let i = 0; i < objectStoreData.length - 1; i++) { 42 objectStore.add(objectStoreData[i]); 43 } 44 yield undefined; 45 46 let count = 0; 47 48 let sawAdded = false; 49 let sawRemoved = false; 50 51 db.transaction("foo").objectStore("foo").openCursor().onsuccess = function ( 52 event 53 ) { 54 event.target.transaction.oncomplete = continueToNextStep; 55 let cursor = event.target.result; 56 if (cursor) { 57 if (cursor.value.name == objectStoreData[0].name) { 58 sawRemoved = true; 59 } 60 if ( 61 cursor.value.name == objectStoreData[objectStoreData.length - 1].name 62 ) { 63 sawAdded = true; 64 } 65 cursor.continue(); 66 count++; 67 } 68 }; 69 yield undefined; 70 71 is(count, objectStoreData.length - 1, "Good initial count"); 72 is(sawAdded, false, "Didn't see item that is about to be added"); 73 is(sawRemoved, true, "Saw item that is about to be removed"); 74 75 count = 0; 76 sawAdded = false; 77 sawRemoved = false; 78 79 db 80 .transaction("foo", "readwrite") 81 .objectStore("foo") 82 .index("name") 83 .openCursor().onsuccess = function (event) { 84 event.target.transaction.oncomplete = continueToNextStep; 85 let cursor = event.target.result; 86 if (cursor) { 87 if (cursor.value.name == objectStoreData[0].name) { 88 sawRemoved = true; 89 } 90 if ( 91 cursor.value.name == objectStoreData[objectStoreData.length - 1].name 92 ) { 93 sawAdded = true; 94 } 95 96 is( 97 cursor.value.name, 98 objectStoreData[objectStoreDataNameSort[count++]].name, 99 "Correct name" 100 ); 101 102 if (count == 1) { 103 let objectStore = event.target.transaction.objectStore("foo"); 104 objectStore.delete(objectStoreData[0].ss).onsuccess = function () { 105 objectStore.add( 106 objectStoreData[objectStoreData.length - 1] 107 ).onsuccess = function () { 108 cursor.continue(); 109 }; 110 }; 111 } else { 112 cursor.continue(); 113 } 114 } 115 }; 116 yield undefined; 117 118 is(count, objectStoreData.length - 1, "Good final count"); 119 is(sawAdded, true, "Saw item that was added"); 120 is(sawRemoved, false, "Didn't see item that was removed"); 121 122 finishTest(); 123 124 objectStore = null; // Bug 943409 workaround. 125 }