Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /** |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | var testGenerator = testSteps(); |
michael@0 | 7 | |
michael@0 | 8 | function testSteps() |
michael@0 | 9 | { |
michael@0 | 10 | const name = this.window ? window.location.pathname : "Splendid Test"; |
michael@0 | 11 | const objectStoreName = "People"; |
michael@0 | 12 | |
michael@0 | 13 | const objectStoreData = [ |
michael@0 | 14 | { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
michael@0 | 15 | { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
michael@0 | 16 | { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, |
michael@0 | 17 | { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
michael@0 | 18 | { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
michael@0 | 19 | { key: "237-23-7737", value: { name: "Pat", height: 65 } } |
michael@0 | 20 | ]; |
michael@0 | 21 | |
michael@0 | 22 | const indexData = [ |
michael@0 | 23 | { name: "name", keyPath: "name", options: { unique: true } }, |
michael@0 | 24 | { name: "height", keyPath: "height", options: { unique: false } }, |
michael@0 | 25 | { name: "weight", keyPath: "weight", options: { unique: false } } |
michael@0 | 26 | ]; |
michael@0 | 27 | |
michael@0 | 28 | const objectStoreDataNameSort = [ |
michael@0 | 29 | { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
michael@0 | 30 | { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
michael@0 | 31 | { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
michael@0 | 32 | { key: "237-23-7737", value: { name: "Pat", height: 65 } }, |
michael@0 | 33 | { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, |
michael@0 | 34 | { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } } |
michael@0 | 35 | ]; |
michael@0 | 36 | |
michael@0 | 37 | const objectStoreDataWeightSort = [ |
michael@0 | 38 | { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
michael@0 | 39 | { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
michael@0 | 40 | { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
michael@0 | 41 | { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
michael@0 | 42 | { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } |
michael@0 | 43 | ]; |
michael@0 | 44 | |
michael@0 | 45 | const objectStoreDataHeightSort = [ |
michael@0 | 46 | { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
michael@0 | 47 | { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
michael@0 | 48 | { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
michael@0 | 49 | { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
michael@0 | 50 | { key: "237-23-7737", value: { name: "Pat", height: 65 } }, |
michael@0 | 51 | { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } |
michael@0 | 52 | ]; |
michael@0 | 53 | |
michael@0 | 54 | let request = indexedDB.open(name, 1); |
michael@0 | 55 | request.onerror = errorHandler; |
michael@0 | 56 | request.onupgradeneeded = grabEventAndContinueHandler; |
michael@0 | 57 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 58 | let event = yield undefined; |
michael@0 | 59 | |
michael@0 | 60 | let db = event.target.result; |
michael@0 | 61 | |
michael@0 | 62 | let objectStore = db.createObjectStore(objectStoreName, {}); |
michael@0 | 63 | |
michael@0 | 64 | // First, add all our data to the object store. |
michael@0 | 65 | let addedData = 0; |
michael@0 | 66 | for (let i in objectStoreData) { |
michael@0 | 67 | request = objectStore.add(objectStoreData[i].value, |
michael@0 | 68 | objectStoreData[i].key); |
michael@0 | 69 | request.onerror = errorHandler; |
michael@0 | 70 | request.onsuccess = function(event) { |
michael@0 | 71 | if (++addedData == objectStoreData.length) { |
michael@0 | 72 | testGenerator.send(event); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | event = yield undefined; |
michael@0 | 77 | |
michael@0 | 78 | // Now create the indexes. |
michael@0 | 79 | for (let i in indexData) { |
michael@0 | 80 | objectStore.createIndex(indexData[i].name, indexData[i].keyPath, |
michael@0 | 81 | indexData[i].options); |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | is(objectStore.indexNames.length, indexData.length, "Good index count"); |
michael@0 | 85 | yield undefined; |
michael@0 | 86 | |
michael@0 | 87 | objectStore = db.transaction(objectStoreName) |
michael@0 | 88 | .objectStore(objectStoreName); |
michael@0 | 89 | |
michael@0 | 90 | request = objectStore.index("height").mozGetAll(65); |
michael@0 | 91 | request.onerror = errorHandler; |
michael@0 | 92 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 93 | event = yield undefined; |
michael@0 | 94 | |
michael@0 | 95 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 96 | is(event.target.result.length, 2, "Correct length"); |
michael@0 | 97 | |
michael@0 | 98 | for (let i in event.target.result) { |
michael@0 | 99 | let result = event.target.result[i]; |
michael@0 | 100 | let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
michael@0 | 101 | |
michael@0 | 102 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 103 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 104 | |
michael@0 | 105 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 106 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 107 | } |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | request = objectStore.index("height").mozGetAll(65, 0); |
michael@0 | 111 | request.onerror = errorHandler; |
michael@0 | 112 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 113 | event = yield undefined; |
michael@0 | 114 | |
michael@0 | 115 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 116 | is(event.target.result.length, 2, "Correct length"); |
michael@0 | 117 | |
michael@0 | 118 | for (let i in event.target.result) { |
michael@0 | 119 | let result = event.target.result[i]; |
michael@0 | 120 | let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
michael@0 | 121 | |
michael@0 | 122 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 123 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 124 | |
michael@0 | 125 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 126 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | request = objectStore.index("height").mozGetAll(65, null); |
michael@0 | 131 | request.onerror = errorHandler; |
michael@0 | 132 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 133 | event = yield undefined; |
michael@0 | 134 | |
michael@0 | 135 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 136 | is(event.target.result.length, 2, "Correct length"); |
michael@0 | 137 | |
michael@0 | 138 | for (let i in event.target.result) { |
michael@0 | 139 | let result = event.target.result[i]; |
michael@0 | 140 | let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
michael@0 | 141 | |
michael@0 | 142 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 143 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 144 | |
michael@0 | 145 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 146 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 147 | } |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | request = objectStore.index("height").mozGetAll(65, undefined); |
michael@0 | 151 | request.onerror = errorHandler; |
michael@0 | 152 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 153 | event = yield undefined; |
michael@0 | 154 | |
michael@0 | 155 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 156 | is(event.target.result.length, 2, "Correct length"); |
michael@0 | 157 | |
michael@0 | 158 | for (let i in event.target.result) { |
michael@0 | 159 | let result = event.target.result[i]; |
michael@0 | 160 | let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
michael@0 | 161 | |
michael@0 | 162 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 163 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 164 | |
michael@0 | 165 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 166 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | request = objectStore.index("height").mozGetAll(); |
michael@0 | 171 | request.onerror = errorHandler; |
michael@0 | 172 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 173 | event = yield undefined; |
michael@0 | 174 | |
michael@0 | 175 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 176 | is(event.target.result.length, objectStoreDataHeightSort.length, |
michael@0 | 177 | "Correct length"); |
michael@0 | 178 | |
michael@0 | 179 | for (let i in event.target.result) { |
michael@0 | 180 | let result = event.target.result[i]; |
michael@0 | 181 | let testObj = objectStoreDataHeightSort[i].value; |
michael@0 | 182 | |
michael@0 | 183 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 184 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 185 | |
michael@0 | 186 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 187 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 188 | } |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | request = objectStore.index("height").mozGetAll(null, 4); |
michael@0 | 192 | request.onerror = errorHandler; |
michael@0 | 193 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 194 | event = yield undefined; |
michael@0 | 195 | |
michael@0 | 196 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 197 | is(event.target.result.length, 4, "Correct length"); |
michael@0 | 198 | |
michael@0 | 199 | for (let i in event.target.result) { |
michael@0 | 200 | let result = event.target.result[i]; |
michael@0 | 201 | let testObj = objectStoreDataHeightSort[i].value; |
michael@0 | 202 | |
michael@0 | 203 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 204 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 205 | |
michael@0 | 206 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 207 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | request = objectStore.index("height").mozGetAll(65, 1); |
michael@0 | 212 | request.onerror = errorHandler; |
michael@0 | 213 | request.onsuccess = grabEventAndContinueHandler; |
michael@0 | 214 | event = yield undefined; |
michael@0 | 215 | |
michael@0 | 216 | is(event.target.result instanceof Array, true, "Got an array object"); |
michael@0 | 217 | is(event.target.result.length, 1, "Correct length"); |
michael@0 | 218 | |
michael@0 | 219 | for (let i in event.target.result) { |
michael@0 | 220 | let result = event.target.result[i]; |
michael@0 | 221 | let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
michael@0 | 222 | |
michael@0 | 223 | is(result.name, testObj.name, "Correct name"); |
michael@0 | 224 | is(result.height, testObj.height, "Correct height"); |
michael@0 | 225 | |
michael@0 | 226 | if (testObj.hasOwnProperty("weight")) { |
michael@0 | 227 | is(result.weight, testObj.weight, "Correct weight"); |
michael@0 | 228 | } |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | finishTest(); |
michael@0 | 232 | yield undefined; |
michael@0 | 233 | } |