test_index_object_cursors.js (4283B)
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 { name: "", options: { keyPath: "id", autoIncrement: true } }, 12 { name: null, options: { keyPath: "ss" } }, 13 { name: undefined, options: {} }, 14 { name: "4", options: { autoIncrement: true } }, 15 ]; 16 17 const indexData = [ 18 { name: "", keyPath: "name", options: { unique: true } }, 19 { name: null, keyPath: "height", options: {} }, 20 ]; 21 22 const data = [ 23 { ss: "237-23-7732", name: "Ann", height: 60 }, 24 { ss: "237-23-7733", name: "Bob", height: 65 }, 25 ]; 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 db.onerror = errorHandler; 37 38 event.target.onsuccess = continueToNextStep; 39 40 for (let objectStoreIndex in objectStoreData) { 41 const objectStoreInfo = objectStoreData[objectStoreIndex]; 42 let objectStore = db.createObjectStore( 43 objectStoreInfo.name, 44 objectStoreInfo.options 45 ); 46 for (let indexIndex in indexData) { 47 const indexInfo = indexData[indexIndex]; 48 objectStore.createIndex( 49 indexInfo.name, 50 indexInfo.keyPath, 51 indexInfo.options 52 ); 53 } 54 } 55 yield undefined; 56 57 ok(true, "Initial setup"); 58 59 for (let objectStoreIndex in objectStoreData) { 60 const info = objectStoreData[objectStoreIndex]; 61 62 for (let indexIndex in indexData) { 63 const objectStoreName = objectStoreData[objectStoreIndex].name; 64 const indexName = indexData[indexIndex].name; 65 66 let objectStore = db 67 .transaction(objectStoreName, "readwrite") 68 .objectStore(objectStoreName); 69 ok(true, "Got objectStore " + objectStoreName); 70 71 for (let dataIndex in data) { 72 const obj = data[dataIndex]; 73 let key; 74 if (!info.options.keyPath && !info.options.autoIncrement) { 75 key = obj.ss; 76 } 77 objectStore.add(obj, key); 78 } 79 80 let index = objectStore.index(indexName); 81 ok(true, "Got index " + indexName); 82 83 let keyIndex = 0; 84 85 index.openCursor().onsuccess = function (event) { 86 let cursor = event.target.result; 87 if (!cursor) { 88 continueToNextStep(); 89 return; 90 } 91 92 is( 93 cursor.key, 94 data[keyIndex][indexData[indexIndex].keyPath], 95 "Good key" 96 ); 97 is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); 98 is(cursor.value.name, data[keyIndex].name, "Correct name"); 99 is(cursor.value.height, data[keyIndex].height, "Correct height"); 100 101 if (!keyIndex) { 102 let obj = cursor.value; 103 obj.updated = true; 104 105 cursor.update(obj).onsuccess = function () { 106 ok(true, "Object updated"); 107 cursor.continue(); 108 keyIndex++; 109 }; 110 return; 111 } 112 113 cursor.delete().onsuccess = function () { 114 ok(true, "Object deleted"); 115 cursor.continue(); 116 keyIndex++; 117 }; 118 }; 119 yield undefined; 120 121 is(keyIndex, 2, "Saw all the items"); 122 123 keyIndex = 0; 124 125 db 126 .transaction(objectStoreName) 127 .objectStore(objectStoreName) 128 .openCursor().onsuccess = function (event) { 129 let cursor = event.target.result; 130 if (!cursor) { 131 continueToNextStep(); 132 return; 133 } 134 135 is(cursor.value.ss, data[keyIndex].ss, "Correct ss"); 136 is(cursor.value.name, data[keyIndex].name, "Correct name"); 137 is(cursor.value.height, data[keyIndex].height, "Correct height"); 138 is(cursor.value.updated, true, "Correct updated flag"); 139 140 cursor.continue(); 141 keyIndex++; 142 }; 143 yield undefined; 144 145 is(keyIndex, 1, "Saw all the items"); 146 147 db 148 .transaction(objectStoreName, "readwrite") 149 .objectStore(objectStoreName) 150 .clear().onsuccess = continueToNextStep; 151 yield undefined; 152 153 objectStore = index = null; // Bug 943409 workaround. 154 } 155 } 156 157 finishTest(); 158 }