test_indexes_funny_things.js (5022B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 var testGenerator = testSteps(); 7 8 function* testSteps() { 9 // Blob constructor is not implemented outside of windows yet (Bug 827723). 10 if (!this.window) { 11 finishTest(); 12 return; 13 } 14 15 const name = this.window ? window.location.pathname : "Splendid Test"; 16 17 const objectStoreName = "Things"; 18 19 const blob1 = new Blob(["foo", "bar"], { type: "text/plain" }); 20 const blob2 = new Blob(["foobazybar"], { type: "text/plain" }); 21 const blob3 = new Blob(["2"], { type: "bogus/" }); 22 const str = "The Book of Mozilla"; 23 str.type = blob1; 24 const arr = [1, 2, 3, 4, 5]; 25 26 const objectStoreData = [ 27 { key: "1", value: blob1 }, 28 { key: "2", value: blob2 }, 29 { key: "3", value: blob3 }, 30 { key: "4", value: str }, 31 { key: "5", value: arr }, 32 ]; 33 34 const indexData = [ 35 { name: "type", keyPath: "type", options: {} }, 36 { name: "length", keyPath: "length", options: { unique: true } }, 37 ]; 38 39 const objectStoreDataTypeSort = [ 40 { key: "3", value: blob3 }, 41 { key: "1", value: blob1 }, 42 { key: "2", value: blob2 }, 43 ]; 44 45 const objectStoreDataLengthSort = [ 46 { key: "5", value: arr }, 47 { key: "4", value: str }, 48 ]; 49 50 let request = indexedDB.open(name, 1); 51 request.onerror = errorHandler; 52 request.onupgradeneeded = grabEventAndContinueHandler; 53 request.onsuccess = grabEventAndContinueHandler; 54 let event = yield undefined; 55 let db = event.target.result; 56 57 let objectStore = db.createObjectStore(objectStoreName, { keyPath: null }); 58 59 // First, add all our data to the object store. 60 let addedData = 0; 61 for (let i in objectStoreData) { 62 request = objectStore.add(objectStoreData[i].value, objectStoreData[i].key); 63 request.onerror = errorHandler; 64 request.onsuccess = function (event) { 65 if (++addedData == objectStoreData.length) { 66 testGenerator.next(event); 67 } 68 }; 69 } 70 event = yield undefined; 71 // Now create the indexes. 72 for (let i in indexData) { 73 objectStore.createIndex( 74 indexData[i].name, 75 indexData[i].keyPath, 76 indexData[i].options 77 ); 78 } 79 is(objectStore.indexNames.length, indexData.length, "Good index count"); 80 yield undefined; 81 objectStore = db.transaction(objectStoreName).objectStore(objectStoreName); 82 83 // Check global properties to make sure they are correct. 84 is(objectStore.indexNames.length, indexData.length, "Good index count"); 85 for (let i in indexData) { 86 let found = false; 87 for (let j = 0; j < objectStore.indexNames.length; j++) { 88 if (objectStore.indexNames.item(j) == indexData[i].name) { 89 found = true; 90 break; 91 } 92 } 93 is(found, true, "objectStore has our index"); 94 let index = objectStore.index(indexData[i].name); 95 is(index.name, indexData[i].name, "Correct name"); 96 is(index.objectStore.name, objectStore.name, "Correct store name"); 97 is(index.keyPath, indexData[i].keyPath, "Correct keyPath"); 98 is(index.unique, !!indexData[i].options.unique, "Correct unique value"); 99 } 100 101 ok(true, "Test group 1"); 102 103 let keyIndex = 0; 104 105 request = objectStore.index("type").openKeyCursor(); 106 request.onerror = errorHandler; 107 request.onsuccess = function (event) { 108 let cursor = event.target.result; 109 if (cursor) { 110 is( 111 cursor.key, 112 objectStoreDataTypeSort[keyIndex].value.type, 113 "Correct key" 114 ); 115 is( 116 cursor.primaryKey, 117 objectStoreDataTypeSort[keyIndex].key, 118 "Correct primary key" 119 ); 120 ok(!("value" in cursor), "No value"); 121 122 cursor.continue(); 123 124 is( 125 cursor.key, 126 objectStoreDataTypeSort[keyIndex].value.type, 127 "Correct key" 128 ); 129 is( 130 cursor.primaryKey, 131 objectStoreDataTypeSort[keyIndex].key, 132 "Correct value" 133 ); 134 ok(!("value" in cursor), "No value"); 135 136 keyIndex++; 137 } else { 138 testGenerator.next(); 139 } 140 }; 141 yield undefined; 142 143 is(keyIndex, objectStoreDataTypeSort.length, "Saw all the expected keys"); 144 145 ok(true, "Test group 2"); 146 147 keyIndex = 0; 148 149 request = objectStore.index("length").openKeyCursor(null, "next"); 150 request.onerror = errorHandler; 151 request.onsuccess = function (event) { 152 let cursor = event.target.result; 153 if (cursor) { 154 is( 155 cursor.key, 156 objectStoreDataLengthSort[keyIndex].value.length, 157 "Correct key" 158 ); 159 is( 160 cursor.primaryKey, 161 objectStoreDataLengthSort[keyIndex].key, 162 "Correct value" 163 ); 164 165 cursor.continue(); 166 167 is( 168 cursor.key, 169 objectStoreDataLengthSort[keyIndex].value.length, 170 "Correct key" 171 ); 172 is( 173 cursor.primaryKey, 174 objectStoreDataLengthSort[keyIndex].key, 175 "Correct value" 176 ); 177 178 keyIndex++; 179 } else { 180 testGenerator.next(); 181 } 182 }; 183 yield undefined; 184 185 is(keyIndex, objectStoreDataLengthSort.length, "Saw all the expected keys"); 186 187 finishTest(); 188 }