test_schema18upgrade.js (8349B)
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 testName = "schema18upgrade"; 11 const testKeys = [ 12 -1 / 0, 13 -1.7e308, 14 -10000, 15 -2, 16 -1.5, 17 -1, 18 -1.00001e-200, 19 -1e-200, 20 0, 21 1e-200, 22 1.00001e-200, 23 1, 24 2, 25 10000, 26 1.7e308, 27 1 / 0, 28 new Date("1750-01-02"), 29 new Date("1800-12-31T12:34:56.001Z"), 30 new Date(-1000), 31 new Date(-10), 32 new Date(-1), 33 new Date(0), 34 new Date(1), 35 new Date(2), 36 new Date(1000), 37 new Date("1971-01-01"), 38 new Date("1971-01-01T01:01:01Z"), 39 new Date("1971-01-01T01:01:01.001Z"), 40 new Date("1971-01-01T01:01:01.01Z"), 41 new Date("1971-01-01T01:01:01.1Z"), 42 new Date("1980-02-02"), 43 new Date("3333-03-19T03:33:33.333Z"), 44 "", 45 "\x00", 46 "\x00\x00", 47 "\x00\x01", 48 "\x01", 49 "\x02", 50 "\x03", 51 "\x04", 52 "\x07", 53 "\x08", 54 "\x0F", 55 "\x10", 56 "\x1F", 57 "\x20", 58 "01234", 59 "\x3F", 60 "\x40", 61 "A", 62 "A\x00", 63 "A1", 64 "ZZZZ", 65 "a", 66 "a\x00", 67 "aa", 68 "azz", 69 "}", 70 "\x7E", 71 "\x7F", 72 "\x80", 73 "\xFF", 74 "\u0100", 75 "\u01FF", 76 "\u0200", 77 "\u03FF", 78 "\u0400", 79 "\u07FF", 80 "\u0800", 81 "\u0FFF", 82 "\u1000", 83 "\u1FFF", 84 "\u2000", 85 "\u3FFF", 86 "\u4000", 87 "\u7FFF", 88 "\u8000", 89 "\uD800", 90 "\uD800a", 91 "\uD800\uDC01", 92 "\uDBFF", 93 "\uDC00", 94 "\uDFFF\uD800", 95 "\uFFFE", 96 "\uFFFF", 97 "\uFFFF\x00", 98 "\uFFFFZZZ", 99 [], 100 [-1 / 0], 101 [-1], 102 [0], 103 [1], 104 [1, "a"], 105 [1, []], 106 [1, [""]], 107 [2, 3], 108 [2, 3.0000000000001], 109 [12, [[]]], 110 [12, [[[]]]], 111 [12, [[[""]]]], 112 [12, [[["foo"]]]], 113 [12, [[[[[3]]]]]], 114 [12, [[[[[[3]]]]]]], 115 [12, [[[[[[3], [[[[[4.2]]]]]]]]]]], 116 [new Date(-1)], 117 [new Date(1)], 118 [""], 119 ["", [[]]], 120 ["", [[[]]]], 121 ["abc"], 122 ["abc", "def"], 123 ["abc\x00"], 124 ["abc\x00", "\x00\x01"], 125 ["abc\x00", "\x00def"], 126 ["abc\x00\x00def"], 127 ["x", [[]]], 128 ["x", [[[]]]], 129 [[]], 130 [[], "foo"], 131 [[], []], 132 [[[]]], 133 [[[]], []], 134 [[[]], [[]]], 135 [[[]], [[1]]], 136 [[[]], [[[]]]], 137 [[[1]]], 138 [[[[]], []]], 139 ]; 140 const testString = 141 "abcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_+=,<.>/?\\|"; 142 143 clearAllDatabases(continueToNextStepSync); 144 yield undefined; 145 146 info("Installing profile"); 147 148 installPackagedProfile(testName + "_profile"); 149 150 info("Opening database with no version"); 151 152 let request = indexedDB.open(testName); 153 request.onerror = errorHandler; 154 request.onupgradeneeded = unexpectedSuccessHandler; 155 request.onsuccess = grabEventAndContinueHandler; 156 let event = yield undefined; 157 158 let db = event.target.result; 159 160 is(db.version, 1, "Correct db version"); 161 162 let transaction = db.transaction(testName); 163 transaction.oncomplete = grabEventAndContinueHandler; 164 165 let objectStore = transaction.objectStore(testName); 166 let index = objectStore.index("uniqueIndex"); 167 168 info("Starting 'uniqueIndex' cursor"); 169 170 let keyIndex = 0; 171 index.openCursor().onsuccess = event => { 172 let cursor = event.target.result; 173 if (cursor) { 174 info( 175 "Comparing " + 176 JSON.stringify(cursor.primaryKey) + 177 " to " + 178 JSON.stringify(testKeys[cursor.key]) + 179 " [" + 180 cursor.key + 181 "]" 182 ); 183 is( 184 indexedDB.cmp(cursor.primaryKey, testKeys[cursor.key]), 185 0, 186 "Keys compare equally via 'indexedDB.cmp'" 187 ); 188 is( 189 compareKeys(cursor.primaryKey, testKeys[cursor.key]), 190 true, 191 "Keys compare equally via 'compareKeys'" 192 ); 193 194 let indexProperty = cursor.value.index; 195 is(Array.isArray(indexProperty), true, "index property is Array"); 196 is(indexProperty[0], cursor.key, "index property first item correct"); 197 is( 198 indexProperty[1], 199 cursor.key + 1, 200 "index property second item correct" 201 ); 202 203 is(cursor.key, keyIndex, "Cursor key property is correct"); 204 205 is(cursor.value.testString, testString, "Test string compared equally"); 206 207 keyIndex++; 208 cursor.continue(); 209 } 210 }; 211 yield undefined; 212 213 is(keyIndex, testKeys.length, "Saw all keys"); 214 215 transaction = db.transaction(testName, "readwrite"); 216 transaction.oncomplete = grabEventAndContinueHandler; 217 218 objectStore = transaction.objectStore(testName); 219 index = objectStore.index("index"); 220 221 info("Getting all 'index' keys"); 222 223 index.getAllKeys().onsuccess = grabEventAndContinueHandler; 224 event = yield undefined; 225 226 is(event.target.result.length, testKeys.length * 2, "Got all keys"); 227 228 info("Starting objectStore cursor"); 229 230 objectStore.openCursor().onsuccess = event => { 231 let cursor = event.target.result; 232 if (cursor) { 233 let value = cursor.value; 234 is(value.testString, testString, "Test string compared equally"); 235 236 delete value.index; 237 cursor.update(value); 238 239 cursor.continue(); 240 } else { 241 continueToNextStepSync(); 242 } 243 }; 244 yield undefined; 245 246 info("Getting all 'index' keys"); 247 248 index.getAllKeys().onsuccess = grabEventAndContinueHandler; 249 event = yield undefined; 250 251 is(event.target.result.length, 0, "Removed all keys"); 252 yield undefined; 253 254 db.close(); 255 256 info("Opening database with new version"); 257 258 request = indexedDB.open(testName, 2); 259 request.onerror = errorHandler; 260 request.onupgradeneeded = grabEventAndContinueHandler; 261 request.onsuccess = grabEventAndContinueHandler; 262 event = yield undefined; 263 264 info("Deleting indexes"); 265 266 objectStore = event.target.transaction.objectStore(testName); 267 objectStore.deleteIndex("index"); 268 objectStore.deleteIndex("uniqueIndex"); 269 270 event = yield undefined; 271 272 db = event.target.result; 273 274 transaction = db.transaction(testName, "readwrite"); 275 transaction.oncomplete = grabEventAndContinueHandler; 276 277 info("Starting objectStore cursor"); 278 279 objectStore = transaction.objectStore(testName); 280 objectStore.openCursor().onsuccess = event => { 281 let cursor = event.target.result; 282 if (cursor) { 283 let value = cursor.value; 284 is(value.testString, testString, "Test string compared equally"); 285 286 value.index = value.keyPath; 287 cursor.update(value); 288 289 cursor.continue(); 290 } 291 }; 292 event = yield undefined; 293 294 db.close(); 295 296 info("Opening database with new version"); 297 298 request = indexedDB.open(testName, 3); 299 request.onerror = errorHandler; 300 request.onupgradeneeded = grabEventAndContinueHandler; 301 request.onsuccess = grabEventAndContinueHandler; 302 event = yield undefined; 303 304 info("Creating indexes"); 305 306 objectStore = event.target.transaction.objectStore(testName); 307 objectStore.createIndex("index", "index"); 308 309 event = yield undefined; 310 311 db = event.target.result; 312 313 transaction = db.transaction(testName); 314 transaction.oncomplete = grabEventAndContinueHandler; 315 316 objectStore = transaction.objectStore(testName); 317 index = objectStore.index("index"); 318 319 info("Starting 'index' cursor"); 320 321 keyIndex = 0; 322 index.openCursor().onsuccess = event => { 323 let cursor = event.target.result; 324 if (cursor) { 325 is( 326 indexedDB.cmp(cursor.primaryKey, testKeys[keyIndex]), 327 0, 328 "Keys compare equally via 'indexedDB.cmp'" 329 ); 330 is( 331 compareKeys(cursor.primaryKey, testKeys[keyIndex]), 332 true, 333 "Keys compare equally via 'compareKeys'" 334 ); 335 is( 336 indexedDB.cmp(cursor.key, testKeys[keyIndex]), 337 0, 338 "Keys compare equally via 'indexedDB.cmp'" 339 ); 340 is( 341 compareKeys(cursor.key, testKeys[keyIndex]), 342 true, 343 "Keys compare equally via 'compareKeys'" 344 ); 345 346 let indexProperty = cursor.value.index; 347 is( 348 indexedDB.cmp(indexProperty, testKeys[keyIndex]), 349 0, 350 "Keys compare equally via 'indexedDB.cmp'" 351 ); 352 is( 353 compareKeys(indexProperty, testKeys[keyIndex]), 354 true, 355 "Keys compare equally via 'compareKeys'" 356 ); 357 358 is(cursor.value.testString, testString, "Test string compared equally"); 359 360 keyIndex++; 361 cursor.continue(); 362 } 363 }; 364 yield undefined; 365 366 is(keyIndex, testKeys.length, "Added all keys again"); 367 368 finishTest(); 369 yield undefined; 370 }