1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/indexedDB/test/unit/test_autoIncrement.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,398 @@ 1.4 +/** 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.7 + */ 1.8 + 1.9 +if (!this.window) { 1.10 + this.runTest = function() { 1.11 + todo(false, "Test disabled in xpcshell test suite for now"); 1.12 + finishTest(); 1.13 + } 1.14 +} 1.15 + 1.16 +var testGenerator = testSteps(); 1.17 + 1.18 +function genCheck(key, value, test, options) { 1.19 + return function(event) { 1.20 + is(JSON.stringify(event.target.result), JSON.stringify(key), 1.21 + "correct returned key in " + test); 1.22 + if (options && options.store) { 1.23 + is(event.target.source, options.store, "correct store in " + test); 1.24 + } 1.25 + if (options && options.trans) { 1.26 + is(event.target.transaction, options.trans, "correct transaction in " + test); 1.27 + } 1.28 + 1.29 + event.target.source.get(key).onsuccess = function(event) { 1.30 + is(JSON.stringify(event.target.result), JSON.stringify(value), 1.31 + "correct stored value in " + test); 1.32 + continueToNextStepSync(); 1.33 + } 1.34 + } 1.35 +} 1.36 + 1.37 +function testSteps() 1.38 +{ 1.39 + const dbname = this.window ? window.location.pathname : "Splendid Test"; 1.40 + const RW = "readwrite"; 1.41 + let c1 = 1; 1.42 + let c2 = 1; 1.43 + 1.44 + let openRequest = indexedDB.open(dbname, 1); 1.45 + openRequest.onerror = errorHandler; 1.46 + openRequest.onupgradeneeded = grabEventAndContinueHandler; 1.47 + openRequest.onsuccess = unexpectedSuccessHandler; 1.48 + let event = yield undefined; 1.49 + let db = event.target.result; 1.50 + let trans = event.target.transaction; 1.51 + 1.52 + // Create test stores 1.53 + let store1 = db.createObjectStore("store1", { autoIncrement: true }); 1.54 + let store2 = db.createObjectStore("store2", { autoIncrement: true, keyPath: "id" }); 1.55 + let store3 = db.createObjectStore("store3", { autoIncrement: false }); 1.56 + is(store1.autoIncrement, true, "store1 .autoIncrement"); 1.57 + is(store2.autoIncrement, true, "store2 .autoIncrement"); 1.58 + is(store3.autoIncrement, false, "store3 .autoIncrement"); 1.59 + 1.60 + store1.createIndex("unique1", "unique", { unique: true }); 1.61 + store2.createIndex("unique1", "unique", { unique: true }); 1.62 + 1.63 + // Test simple inserts 1.64 + let test = " for test simple insert" 1.65 + store1.add({ foo: "value1" }).onsuccess = 1.66 + genCheck(c1++, { foo: "value1" }, "first" + test); 1.67 + store1.add({ foo: "value2" }).onsuccess = 1.68 + genCheck(c1++, { foo: "value2" }, "second" + test); 1.69 + 1.70 + yield undefined; 1.71 + yield undefined; 1.72 + 1.73 + store2.put({ bar: "value1" }).onsuccess = 1.74 + genCheck(c2, { bar: "value1", id: c2 }, "first in store2" + test, 1.75 + { store: store2 }); 1.76 + c2++; 1.77 + store1.put({ foo: "value3" }).onsuccess = 1.78 + genCheck(c1++, { foo: "value3" }, "third" + test, 1.79 + { store: store1 }); 1.80 + 1.81 + yield undefined; 1.82 + yield undefined; 1.83 + 1.84 + store2.get(IDBKeyRange.lowerBound(c2)).onsuccess = grabEventAndContinueHandler; 1.85 + event = yield undefined; 1.86 + is(event.target.result, undefined, "no such value" + test); 1.87 + 1.88 + // Close version_change transaction 1.89 + openRequest.onsuccess = grabEventAndContinueHandler; 1.90 + event = yield undefined; 1.91 + 1.92 + is(event.target, openRequest, "succeeded to open" + test); 1.93 + is(event.type, "success", "succeeded to open" + test); 1.94 + 1.95 + // Test inserting explicit keys 1.96 + test = " for test explicit keys"; 1.97 + trans = db.transaction("store1", RW); 1.98 + trans.objectStore("store1").add({ explicit: 1 }, 100).onsuccess = 1.99 + genCheck(100, { explicit: 1 }, "first" + test); 1.100 + c1 = 101; 1.101 + trans = db.transaction("store1", RW); 1.102 + trans.objectStore("store1").add({ explicit: 2 }).onsuccess = 1.103 + genCheck(c1++, { explicit: 2 }, "second" + test); 1.104 + yield undefined; yield undefined; 1.105 + 1.106 + trans = db.transaction("store1", RW); 1.107 + trans.objectStore("store1").add({ explicit: 3 }, 200).onsuccess = 1.108 + genCheck(200, { explicit: 3 }, "third" + test); 1.109 + c1 = 201; 1.110 + trans.objectStore("store1").add({ explicit: 4 }).onsuccess = 1.111 + genCheck(c1++, { explicit: 4 }, "fourth" + test); 1.112 + yield undefined; yield undefined; 1.113 + 1.114 + trans = db.transaction("store1", RW); 1.115 + trans.objectStore("store1").add({ explicit: 5 }, 150).onsuccess = 1.116 + genCheck(150, { explicit: 5 }, "fifth" + test); 1.117 + yield undefined; 1.118 + trans.objectStore("store1").add({ explicit: 6 }).onsuccess = 1.119 + genCheck(c1++, { explicit: 6 }, "sixth" + test); 1.120 + yield undefined; 1.121 + 1.122 + trans = db.transaction("store1", RW); 1.123 + trans.objectStore("store1").add({ explicit: 7 }, "key").onsuccess = 1.124 + genCheck("key", { explicit: 7 }, "seventh" + test); 1.125 + yield undefined; 1.126 + trans.objectStore("store1").add({ explicit: 8 }).onsuccess = 1.127 + genCheck(c1++, { explicit: 8 }, "eighth" + test); 1.128 + yield undefined; 1.129 + 1.130 + trans = db.transaction("store1", RW); 1.131 + trans.objectStore("store1").add({ explicit: 7 }, [100000]).onsuccess = 1.132 + genCheck([100000], { explicit: 7 }, "seventh" + test); 1.133 + yield undefined; 1.134 + trans.objectStore("store1").add({ explicit: 8 }).onsuccess = 1.135 + genCheck(c1++, { explicit: 8 }, "eighth" + test); 1.136 + yield undefined; 1.137 + 1.138 + trans = db.transaction("store1", RW); 1.139 + trans.objectStore("store1").add({ explicit: 9 }, -100000).onsuccess = 1.140 + genCheck(-100000, { explicit: 9 }, "ninth" + test); 1.141 + yield undefined; 1.142 + trans.objectStore("store1").add({ explicit: 10 }).onsuccess = 1.143 + genCheck(c1++, { explicit: 10 }, "tenth" + test); 1.144 + yield undefined; 1.145 + 1.146 + 1.147 + trans = db.transaction("store2", RW); 1.148 + trans.objectStore("store2").add({ explicit2: 1, id: 300 }).onsuccess = 1.149 + genCheck(300, { explicit2: 1, id: 300 }, "first store2" + test); 1.150 + c2 = 301; 1.151 + trans = db.transaction("store2", RW); 1.152 + trans.objectStore("store2").add({ explicit2: 2 }).onsuccess = 1.153 + genCheck(c2, { explicit2: 2, id: c2 }, "second store2" + test); 1.154 + c2++; 1.155 + yield undefined; yield undefined; 1.156 + 1.157 + trans = db.transaction("store2", RW); 1.158 + trans.objectStore("store2").add({ explicit2: 3, id: 400 }).onsuccess = 1.159 + genCheck(400, { explicit2: 3, id: 400 }, "third store2" + test); 1.160 + c2 = 401; 1.161 + trans.objectStore("store2").add({ explicit2: 4 }).onsuccess = 1.162 + genCheck(c2, { explicit2: 4, id: c2 }, "fourth store2" + test); 1.163 + c2++; 1.164 + yield undefined; yield undefined; 1.165 + 1.166 + trans = db.transaction("store2", RW); 1.167 + trans.objectStore("store2").add({ explicit: 5, id: 150 }).onsuccess = 1.168 + genCheck(150, { explicit: 5, id: 150 }, "fifth store2" + test); 1.169 + yield undefined; 1.170 + trans.objectStore("store2").add({ explicit: 6 }).onsuccess = 1.171 + genCheck(c2, { explicit: 6, id: c2 }, "sixth store2" + test); 1.172 + c2++; 1.173 + yield undefined; 1.174 + 1.175 + trans = db.transaction("store2", RW); 1.176 + trans.objectStore("store2").add({ explicit: 7, id: "key" }).onsuccess = 1.177 + genCheck("key", { explicit: 7, id: "key" }, "seventh store2" + test); 1.178 + yield undefined; 1.179 + trans.objectStore("store2").add({ explicit: 8 }).onsuccess = 1.180 + genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test); 1.181 + c2++; 1.182 + yield undefined; 1.183 + 1.184 + trans = db.transaction("store2", RW); 1.185 + trans.objectStore("store2").add({ explicit: 7, id: [100000] }).onsuccess = 1.186 + genCheck([100000], { explicit: 7, id: [100000] }, "seventh store2" + test); 1.187 + yield undefined; 1.188 + trans.objectStore("store2").add({ explicit: 8 }).onsuccess = 1.189 + genCheck(c2, { explicit: 8, id: c2 }, "eighth store2" + test); 1.190 + c2++; 1.191 + yield undefined; 1.192 + 1.193 + trans = db.transaction("store2", RW); 1.194 + trans.objectStore("store2").add({ explicit: 9, id: -100000 }).onsuccess = 1.195 + genCheck(-100000, { explicit: 9, id: -100000 }, "ninth store2" + test); 1.196 + yield undefined; 1.197 + trans.objectStore("store2").add({ explicit: 10 }).onsuccess = 1.198 + genCheck(c2, { explicit: 10, id: c2 }, "tenth store2" + test); 1.199 + c2++; 1.200 + yield undefined; 1.201 + 1.202 + 1.203 + // Test separate transactions doesn't generate overlapping numbers 1.204 + test = " for test non-overlapping counts"; 1.205 + trans = db.transaction("store1", RW); 1.206 + trans2 = db.transaction("store1", RW); 1.207 + trans2.objectStore("store1").put({ over: 2 }).onsuccess = 1.208 + genCheck(c1 + 1, { over: 2 }, "first" + test, 1.209 + { trans: trans2 }); 1.210 + trans.objectStore("store1").put({ over: 1 }).onsuccess = 1.211 + genCheck(c1, { over: 1 }, "second" + test, 1.212 + { trans: trans }); 1.213 + c1 += 2; 1.214 + yield undefined; yield undefined; 1.215 + 1.216 + trans = db.transaction("store2", RW); 1.217 + trans2 = db.transaction("store2", RW); 1.218 + trans2.objectStore("store2").put({ over: 2 }).onsuccess = 1.219 + genCheck(c2 + 1, { over: 2, id: c2 + 1 }, "third" + test, 1.220 + { trans: trans2 }); 1.221 + trans.objectStore("store2").put({ over: 1 }).onsuccess = 1.222 + genCheck(c2, { over: 1, id: c2 }, "fourth" + test, 1.223 + { trans: trans }); 1.224 + c2 += 2; 1.225 + yield undefined; yield undefined; 1.226 + 1.227 + // Test that error inserts doesn't increase generator 1.228 + test = " for test error inserts"; 1.229 + trans = db.transaction(["store1", "store2"], RW); 1.230 + trans.objectStore("store1").add({ unique: 1 }, -1); 1.231 + trans.objectStore("store2").add({ unique: 1, id: "unique" }); 1.232 + 1.233 + trans.objectStore("store1").add({ error: 1, unique: 1 }). 1.234 + addEventListener("error", new ExpectError("ConstraintError", true)); 1.235 + trans.objectStore("store1").add({ error: 2 }).onsuccess = 1.236 + genCheck(c1++, { error: 2 }, "first" + test); 1.237 + yield undefined; yield undefined; 1.238 + 1.239 + trans.objectStore("store2").add({ error: 3, unique: 1 }). 1.240 + addEventListener("error", new ExpectError("ConstraintError", true)); 1.241 + trans.objectStore("store2").add({ error: 4 }).onsuccess = 1.242 + genCheck(c2, { error: 4, id: c2 }, "second" + test); 1.243 + c2++; 1.244 + yield undefined; yield undefined; 1.245 + 1.246 + trans.objectStore("store1").add({ error: 5, unique: 1 }, 100000). 1.247 + addEventListener("error", new ExpectError("ConstraintError", true)); 1.248 + trans.objectStore("store1").add({ error: 6 }).onsuccess = 1.249 + genCheck(c1++, { error: 6 }, "third" + test); 1.250 + yield undefined; yield undefined; 1.251 + 1.252 + trans.objectStore("store2").add({ error: 7, unique: 1, id: 100000 }). 1.253 + addEventListener("error", new ExpectError("ConstraintError", true)); 1.254 + trans.objectStore("store2").add({ error: 8 }).onsuccess = 1.255 + genCheck(c2, { error: 8, id: c2 }, "fourth" + test); 1.256 + c2++; 1.257 + yield undefined; yield undefined; 1.258 + 1.259 + // Test that aborts doesn't increase generator 1.260 + test = " for test aborted transaction"; 1.261 + trans = db.transaction(["store1", "store2"], RW); 1.262 + trans.objectStore("store1").add({ abort: 1 }).onsuccess = 1.263 + genCheck(c1, { abort: 1 }, "first" + test); 1.264 + trans.objectStore("store2").put({ abort: 2 }).onsuccess = 1.265 + genCheck(c2, { abort: 2, id: c2 }, "second" + test); 1.266 + yield undefined; yield undefined; 1.267 + 1.268 + trans.objectStore("store1").add({ abort: 3 }, 500).onsuccess = 1.269 + genCheck(500, { abort: 3 }, "third" + test); 1.270 + trans.objectStore("store2").put({ abort: 4, id: 600 }).onsuccess = 1.271 + genCheck(600, { abort: 4, id: 600 }, "fourth" + test); 1.272 + yield undefined; yield undefined; 1.273 + 1.274 + trans.objectStore("store1").add({ abort: 5 }).onsuccess = 1.275 + genCheck(501, { abort: 5 }, "fifth" + test); 1.276 + trans.objectStore("store2").put({ abort: 6 }).onsuccess = 1.277 + genCheck(601, { abort: 6, id: 601 }, "sixth" + test); 1.278 + yield undefined; yield undefined; 1.279 + 1.280 + trans.abort(); 1.281 + trans.onabort = grabEventAndContinueHandler; 1.282 + event = yield 1.283 + is(event.type, "abort", "transaction aborted"); 1.284 + is(event.target, trans, "correct transaction aborted"); 1.285 + 1.286 + trans = db.transaction(["store1", "store2"], RW); 1.287 + trans.objectStore("store1").add({ abort: 1 }).onsuccess = 1.288 + genCheck(c1++, { abort: 1 }, "re-first" + test); 1.289 + trans.objectStore("store2").put({ abort: 2 }).onsuccess = 1.290 + genCheck(c2, { abort: 2, id: c2 }, "re-second" + test); 1.291 + c2++; 1.292 + yield undefined; yield undefined; 1.293 + 1.294 + // Test that delete doesn't decrease generator 1.295 + test = " for test delete items" 1.296 + trans = db.transaction(["store1", "store2"], RW); 1.297 + trans.objectStore("store1").add({ delete: 1 }).onsuccess = 1.298 + genCheck(c1++, { delete: 1 }, "first" + test); 1.299 + trans.objectStore("store2").put({ delete: 2 }).onsuccess = 1.300 + genCheck(c2, { delete: 2, id: c2 }, "second" + test); 1.301 + c2++; 1.302 + yield undefined; yield undefined; 1.303 + 1.304 + trans.objectStore("store1").delete(c1 - 1).onsuccess = 1.305 + grabEventAndContinueHandler; 1.306 + trans.objectStore("store2").delete(c2 - 1).onsuccess = 1.307 + grabEventAndContinueHandler; 1.308 + yield undefined; yield undefined; 1.309 + 1.310 + trans.objectStore("store1").add({ delete: 3 }).onsuccess = 1.311 + genCheck(c1++, { delete: 3 }, "first" + test); 1.312 + trans.objectStore("store2").put({ delete: 4 }).onsuccess = 1.313 + genCheck(c2, { delete: 4, id: c2 }, "second" + test); 1.314 + c2++; 1.315 + yield undefined; yield undefined; 1.316 + 1.317 + trans.objectStore("store1").delete(c1 - 1).onsuccess = 1.318 + grabEventAndContinueHandler; 1.319 + trans.objectStore("store2").delete(c2 - 1).onsuccess = 1.320 + grabEventAndContinueHandler; 1.321 + yield undefined; yield undefined; 1.322 + 1.323 + trans = db.transaction(["store1", "store2"], RW); 1.324 + trans.objectStore("store1").add({ delete: 5 }).onsuccess = 1.325 + genCheck(c1++, { delete: 5 }, "first" + test); 1.326 + trans.objectStore("store2").put({ delete: 6 }).onsuccess = 1.327 + genCheck(c2, { delete: 6, id: c2 }, "second" + test); 1.328 + c2++; 1.329 + yield undefined; yield undefined; 1.330 + 1.331 + // Test that clears doesn't decrease generator 1.332 + test = " for test clear stores"; 1.333 + trans = db.transaction(["store1", "store2"], RW); 1.334 + trans.objectStore("store1").add({ clear: 1 }).onsuccess = 1.335 + genCheck(c1++, { clear: 1 }, "first" + test); 1.336 + trans.objectStore("store2").put({ clear: 2 }).onsuccess = 1.337 + genCheck(c2, { clear: 2, id: c2 }, "second" + test); 1.338 + c2++; 1.339 + yield undefined; yield undefined; 1.340 + 1.341 + trans.objectStore("store1").clear().onsuccess = 1.342 + grabEventAndContinueHandler; 1.343 + trans.objectStore("store2").clear().onsuccess = 1.344 + grabEventAndContinueHandler; 1.345 + yield undefined; yield undefined; 1.346 + 1.347 + trans.objectStore("store1").add({ clear: 3 }).onsuccess = 1.348 + genCheck(c1++, { clear: 3 }, "third" + test); 1.349 + trans.objectStore("store2").put({ clear: 4 }).onsuccess = 1.350 + genCheck(c2, { clear: 4, id: c2 }, "forth" + test); 1.351 + c2++; 1.352 + yield undefined; yield undefined; 1.353 + 1.354 + trans.objectStore("store1").clear().onsuccess = 1.355 + grabEventAndContinueHandler; 1.356 + trans.objectStore("store2").clear().onsuccess = 1.357 + grabEventAndContinueHandler; 1.358 + yield undefined; yield undefined; 1.359 + 1.360 + trans = db.transaction(["store1", "store2"], RW); 1.361 + trans.objectStore("store1").add({ clear: 5 }).onsuccess = 1.362 + genCheck(c1++, { clear: 5 }, "fifth" + test); 1.363 + trans.objectStore("store2").put({ clear: 6 }).onsuccess = 1.364 + genCheck(c2, { clear: 6, id: c2 }, "sixth" + test); 1.365 + c2++; 1.366 + yield undefined; yield undefined; 1.367 + 1.368 + 1.369 + // Test that close/reopen doesn't decrease generator 1.370 + test = " for test clear stores"; 1.371 + trans = db.transaction(["store1", "store2"], RW); 1.372 + trans.objectStore("store1").clear().onsuccess = 1.373 + grabEventAndContinueHandler; 1.374 + trans.objectStore("store2").clear().onsuccess = 1.375 + grabEventAndContinueHandler; 1.376 + yield undefined; yield undefined; 1.377 + db.close(); 1.378 + 1.379 + SpecialPowers.gc(); 1.380 + 1.381 + openRequest = indexedDB.open(dbname, 2); 1.382 + openRequest.onerror = errorHandler; 1.383 + openRequest.onupgradeneeded = grabEventAndContinueHandler; 1.384 + openRequest.onsuccess = unexpectedSuccessHandler; 1.385 + event = yield undefined; 1.386 + db = event.target.result; 1.387 + trans = event.target.transaction; 1.388 + 1.389 + trans.objectStore("store1").add({ reopen: 1 }).onsuccess = 1.390 + genCheck(c1++, { reopen: 1 }, "first" + test); 1.391 + trans.objectStore("store2").put({ reopen: 2 }).onsuccess = 1.392 + genCheck(c2, { reopen: 2, id: c2 }, "second" + test); 1.393 + c2++; 1.394 + yield undefined; yield undefined; 1.395 + 1.396 + openRequest.onsuccess = grabEventAndContinueHandler; 1.397 + yield undefined; 1.398 + 1.399 + finishTest(); 1.400 + yield undefined; 1.401 +}