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 | // Test object stores |
michael@0 | 11 | |
michael@0 | 12 | let name = this.window ? window.location.pathname : "Splendid Test"; |
michael@0 | 13 | let openRequest = indexedDB.open(name, 1); |
michael@0 | 14 | openRequest.onerror = errorHandler; |
michael@0 | 15 | openRequest.onupgradeneeded = grabEventAndContinueHandler; |
michael@0 | 16 | openRequest.onsuccess = unexpectedSuccessHandler; |
michael@0 | 17 | let event = yield undefined; |
michael@0 | 18 | let db = event.target.result; |
michael@0 | 19 | db.onerror = errorHandler; |
michael@0 | 20 | let tests = |
michael@0 | 21 | [{ add: { x: 1, id: 1 }, |
michael@0 | 22 | indexes:[{ v: 1, k: 1 }] }, |
michael@0 | 23 | { add: { x: [2, 3], id: 2 }, |
michael@0 | 24 | indexes:[{ v: 1, k: 1 }, |
michael@0 | 25 | { v: 2, k: 2 }, |
michael@0 | 26 | { v: 3, k: 2 }] }, |
michael@0 | 27 | { put: { x: [2, 4], id: 1 }, |
michael@0 | 28 | indexes:[{ v: 2, k: 1 }, |
michael@0 | 29 | { v: 2, k: 2 }, |
michael@0 | 30 | { v: 3, k: 2 }, |
michael@0 | 31 | { v: 4, k: 1 }] }, |
michael@0 | 32 | { add: { x: [5, 6, 5, -2, 3], id: 3 }, |
michael@0 | 33 | indexes:[{ v:-2, k: 3 }, |
michael@0 | 34 | { v: 2, k: 1 }, |
michael@0 | 35 | { v: 2, k: 2 }, |
michael@0 | 36 | { v: 3, k: 2 }, |
michael@0 | 37 | { v: 3, k: 3 }, |
michael@0 | 38 | { v: 4, k: 1 }, |
michael@0 | 39 | { v: 5, k: 3 }, |
michael@0 | 40 | { v: 6, k: 3 }] }, |
michael@0 | 41 | { delete: IDBKeyRange.bound(1, 3), |
michael@0 | 42 | indexes:[] }, |
michael@0 | 43 | { put: { x: ["food", {}, false, undefined, /x/, [73, false]], id: 2 }, |
michael@0 | 44 | indexes:[{ v: "food", k: 2 }] }, |
michael@0 | 45 | { add: { x: [{}, /x/, -12, "food", null, [false], undefined], id: 3 }, |
michael@0 | 46 | indexes:[{ v: -12, k: 3 }, |
michael@0 | 47 | { v: "food", k: 2 }, |
michael@0 | 48 | { v: "food", k: 3 }] }, |
michael@0 | 49 | { put: { x: [], id: 2 }, |
michael@0 | 50 | indexes:[{ v: -12, k: 3 }, |
michael@0 | 51 | { v: "food", k: 3 }] }, |
michael@0 | 52 | { put: { x: { y: 3 }, id: 3 }, |
michael@0 | 53 | indexes:[] }, |
michael@0 | 54 | { add: { x: false, id: 7 }, |
michael@0 | 55 | indexes:[] }, |
michael@0 | 56 | { delete: IDBKeyRange.lowerBound(0), |
michael@0 | 57 | indexes:[] }, |
michael@0 | 58 | ]; |
michael@0 | 59 | |
michael@0 | 60 | let store = db.createObjectStore("mystore", { keyPath: "id" }); |
michael@0 | 61 | let index = store.createIndex("myindex", "x", { multiEntry: true }); |
michael@0 | 62 | is(index.multiEntry, true, "index created with multiEntry"); |
michael@0 | 63 | |
michael@0 | 64 | let i; |
michael@0 | 65 | for (i = 0; i < tests.length; ++i) { |
michael@0 | 66 | let test = tests[i]; |
michael@0 | 67 | let testName = " for " + JSON.stringify(test); |
michael@0 | 68 | let req; |
michael@0 | 69 | if (test.add) { |
michael@0 | 70 | req = store.add(test.add); |
michael@0 | 71 | } |
michael@0 | 72 | else if (test.put) { |
michael@0 | 73 | req = store.put(test.put); |
michael@0 | 74 | } |
michael@0 | 75 | else if (test.delete) { |
michael@0 | 76 | req = store.delete(test.delete); |
michael@0 | 77 | } |
michael@0 | 78 | else { |
michael@0 | 79 | ok(false, "borked test"); |
michael@0 | 80 | } |
michael@0 | 81 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 82 | let e = yield undefined; |
michael@0 | 83 | |
michael@0 | 84 | req = index.openKeyCursor(); |
michael@0 | 85 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 86 | for (let j = 0; j < test.indexes.length; ++j) { |
michael@0 | 87 | e = yield undefined; |
michael@0 | 88 | is(req.result.key, test.indexes[j].v, "found expected index key at index " + j + testName); |
michael@0 | 89 | is(req.result.primaryKey, test.indexes[j].k, "found expected index primary key at index " + j + testName); |
michael@0 | 90 | req.result.continue(); |
michael@0 | 91 | } |
michael@0 | 92 | e = yield undefined; |
michael@0 | 93 | is(req.result, undefined, "exhausted indexes"); |
michael@0 | 94 | |
michael@0 | 95 | let tempIndex = store.createIndex("temp index", "x", { multiEntry: true }); |
michael@0 | 96 | req = tempIndex.openKeyCursor(); |
michael@0 | 97 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 98 | for (let j = 0; j < test.indexes.length; ++j) { |
michael@0 | 99 | e = yield undefined; |
michael@0 | 100 | is(req.result.key, test.indexes[j].v, "found expected temp index key at index " + j + testName); |
michael@0 | 101 | is(req.result.primaryKey, test.indexes[j].k, "found expected temp index primary key at index " + j + testName); |
michael@0 | 102 | req.result.continue(); |
michael@0 | 103 | } |
michael@0 | 104 | e = yield undefined; |
michael@0 | 105 | is(req.result, undefined, "exhausted temp index"); |
michael@0 | 106 | store.deleteIndex("temp index"); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // Unique indexes |
michael@0 | 110 | tests = |
michael@0 | 111 | [{ add: { x: 1, id: 1 }, |
michael@0 | 112 | indexes:[{ v: 1, k: 1 }] }, |
michael@0 | 113 | { add: { x: [2, 3], id: 2 }, |
michael@0 | 114 | indexes:[{ v: 1, k: 1 }, |
michael@0 | 115 | { v: 2, k: 2 }, |
michael@0 | 116 | { v: 3, k: 2 }] }, |
michael@0 | 117 | { put: { x: [2, 4], id: 3 }, |
michael@0 | 118 | fail: true }, |
michael@0 | 119 | { put: { x: [1, 4], id: 1 }, |
michael@0 | 120 | indexes:[{ v: 1, k: 1 }, |
michael@0 | 121 | { v: 2, k: 2 }, |
michael@0 | 122 | { v: 3, k: 2 }, |
michael@0 | 123 | { v: 4, k: 1 }] }, |
michael@0 | 124 | { add: { x: [5, 0, 5, 5, 5], id: 3 }, |
michael@0 | 125 | indexes:[{ v: 0, k: 3 }, |
michael@0 | 126 | { v: 1, k: 1 }, |
michael@0 | 127 | { v: 2, k: 2 }, |
michael@0 | 128 | { v: 3, k: 2 }, |
michael@0 | 129 | { v: 4, k: 1 }, |
michael@0 | 130 | { v: 5, k: 3 }] }, |
michael@0 | 131 | { delete: IDBKeyRange.bound(1, 2), |
michael@0 | 132 | indexes:[{ v: 0, k: 3 }, |
michael@0 | 133 | { v: 5, k: 3 }] }, |
michael@0 | 134 | { add: { x: [0, 6], id: 8 }, |
michael@0 | 135 | fail: true }, |
michael@0 | 136 | { add: { x: 5, id: 8 }, |
michael@0 | 137 | fail: true }, |
michael@0 | 138 | { put: { x: 0, id: 8 }, |
michael@0 | 139 | fail: true }, |
michael@0 | 140 | ]; |
michael@0 | 141 | |
michael@0 | 142 | store.deleteIndex("myindex"); |
michael@0 | 143 | index = store.createIndex("myindex", "x", { multiEntry: true, unique: true }); |
michael@0 | 144 | is(index.multiEntry, true, "index created with multiEntry"); |
michael@0 | 145 | |
michael@0 | 146 | let i; |
michael@0 | 147 | let indexes; |
michael@0 | 148 | for (i = 0; i < tests.length; ++i) { |
michael@0 | 149 | let test = tests[i]; |
michael@0 | 150 | let testName = " for " + JSON.stringify(test); |
michael@0 | 151 | let req; |
michael@0 | 152 | if (test.add) { |
michael@0 | 153 | req = store.add(test.add); |
michael@0 | 154 | } |
michael@0 | 155 | else if (test.put) { |
michael@0 | 156 | req = store.put(test.put); |
michael@0 | 157 | } |
michael@0 | 158 | else if (test.delete) { |
michael@0 | 159 | req = store.delete(test.delete); |
michael@0 | 160 | } |
michael@0 | 161 | else { |
michael@0 | 162 | ok(false, "borked test"); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | if (!test.fail) { |
michael@0 | 166 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 167 | let e = yield undefined; |
michael@0 | 168 | indexes = test.indexes; |
michael@0 | 169 | } |
michael@0 | 170 | else { |
michael@0 | 171 | req.onsuccess = unexpectedSuccessHandler; |
michael@0 | 172 | req.onerror = grabEventAndContinueHandler; |
michael@0 | 173 | ok(true, "waiting for error"); |
michael@0 | 174 | let e = yield undefined; |
michael@0 | 175 | ok(true, "got error: " + e.type); |
michael@0 | 176 | e.preventDefault(); |
michael@0 | 177 | e.stopPropagation(); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | let e; |
michael@0 | 181 | req = index.openKeyCursor(); |
michael@0 | 182 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 183 | for (let j = 0; j < indexes.length; ++j) { |
michael@0 | 184 | e = yield undefined; |
michael@0 | 185 | is(req.result.key, indexes[j].v, "found expected index key at index " + j + testName); |
michael@0 | 186 | is(req.result.primaryKey, indexes[j].k, "found expected index primary key at index " + j + testName); |
michael@0 | 187 | req.result.continue(); |
michael@0 | 188 | } |
michael@0 | 189 | e = yield undefined; |
michael@0 | 190 | is(req.result, undefined, "exhausted indexes"); |
michael@0 | 191 | |
michael@0 | 192 | let tempIndex = store.createIndex("temp index", "x", { multiEntry: true, unique: true }); |
michael@0 | 193 | req = tempIndex.openKeyCursor(); |
michael@0 | 194 | req.onsuccess = grabEventAndContinueHandler; |
michael@0 | 195 | for (let j = 0; j < indexes.length; ++j) { |
michael@0 | 196 | e = yield undefined; |
michael@0 | 197 | is(req.result.key, indexes[j].v, "found expected temp index key at index " + j + testName); |
michael@0 | 198 | is(req.result.primaryKey, indexes[j].k, "found expected temp index primary key at index " + j + testName); |
michael@0 | 199 | req.result.continue(); |
michael@0 | 200 | } |
michael@0 | 201 | e = yield undefined; |
michael@0 | 202 | is(req.result, undefined, "exhausted temp index"); |
michael@0 | 203 | store.deleteIndex("temp index"); |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | openRequest.onsuccess = grabEventAndContinueHandler; |
michael@0 | 208 | yield undefined; |
michael@0 | 209 | |
michael@0 | 210 | let trans = db.transaction(["mystore"], "readwrite"); |
michael@0 | 211 | store = trans.objectStore("mystore"); |
michael@0 | 212 | index = store.index("myindex"); |
michael@0 | 213 | is(index.multiEntry, true, "index still is multiEntry"); |
michael@0 | 214 | trans.oncomplete = grabEventAndContinueHandler; |
michael@0 | 215 | yield undefined; |
michael@0 | 216 | |
michael@0 | 217 | finishTest(); |
michael@0 | 218 | yield undefined; |
michael@0 | 219 | } |