dom/indexedDB/test/unit/test_add_put.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

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 let openRequest = indexedDB.open(name, 1);
michael@0 12 openRequest.onerror = errorHandler;
michael@0 13 openRequest.onupgradeneeded = grabEventAndContinueHandler;
michael@0 14 openRequest.onsuccess = unexpectedSuccessHandler;
michael@0 15 let event = yield undefined;
michael@0 16 let db = event.target.result;
michael@0 17 let trans = event.target.transaction;
michael@0 18
michael@0 19 for each (let autoincrement in [true, false]) {
michael@0 20 for each (let keypath in [false, true, "missing", "invalid"]) {
michael@0 21 for each (let method in ["put", "add"]) {
michael@0 22 for each (let explicit in [true, false, undefined, "invalid"]) {
michael@0 23 for each (let existing in [true, false]) {
michael@0 24 let speccedNoKey = (keypath == false || keypath == "missing") &&
michael@0 25 !explicit;
michael@0 26
michael@0 27 // We can't do 'existing' checks if we use autogenerated key
michael@0 28 if (speccedNoKey && autoincrement && existing) {
michael@0 29 continue;
michael@0 30 }
michael@0 31
michael@0 32 // Create store
michael@0 33 if (db.objectStoreNames.contains("mystore"))
michael@0 34 db.deleteObjectStore("mystore");
michael@0 35 let store = db.createObjectStore("mystore",
michael@0 36 { autoIncrement: autoincrement,
michael@0 37 keyPath: (keypath ? "id" : null) });
michael@0 38
michael@0 39 test = " for test " + JSON.stringify({ autoincrement: autoincrement,
michael@0 40 keypath: keypath,
michael@0 41 method: method,
michael@0 42 explicit: explicit === undefined ? "undefined" : explicit,
michael@0 43 existing: existing });
michael@0 44
michael@0 45 // Insert "existing" data if needed
michael@0 46 if (existing) {
michael@0 47 if (keypath)
michael@0 48 store.add({ existing: "data", id: 5 }).onsuccess = grabEventAndContinueHandler;
michael@0 49 else
michael@0 50 store.add({ existing: "data" }, 5).onsuccess = grabEventAndContinueHandler;
michael@0 51
michael@0 52 let e = yield undefined;
michael@0 53 is(e.type, "success", "success inserting existing" + test);
michael@0 54 is(e.target.result, 5, "inserted correct key" + test);
michael@0 55 }
michael@0 56
michael@0 57 // Set up value to be inserted
michael@0 58 let value = { theObj: true };
michael@0 59 if (keypath === true) {
michael@0 60 value.id = 5;
michael@0 61 }
michael@0 62 else if (keypath === "invalid") {
michael@0 63 value.id = /x/;
michael@0 64 }
michael@0 65
michael@0 66 // Which arguments are passed to function
michael@0 67 args = [value];
michael@0 68 if (explicit === true) {
michael@0 69 args.push(5);
michael@0 70 }
michael@0 71 else if (explicit === undefined) {
michael@0 72 args.push(undefined);
michael@0 73 }
michael@0 74 else if (explicit === "invalid") {
michael@0 75 args.push(/x/);
michael@0 76 }
michael@0 77
michael@0 78 let expected = expectedResult(method, keypath, explicit, autoincrement, existing);
michael@0 79
michael@0 80 let valueJSON = JSON.stringify(value);
michael@0 81
michael@0 82 ok(true, "making call" + test);
michael@0 83
michael@0 84 // Make function call for throwing functions
michael@0 85 if (expected === "throw") {
michael@0 86 try {
michael@0 87 store[method].apply(store, args);
michael@0 88 ok(false, "should have thrown" + test);
michael@0 89 }
michael@0 90 catch (ex) {
michael@0 91 ok(true, "did throw" + test);
michael@0 92 ok(ex instanceof DOMException, "Got a DOMException" + test);
michael@0 93 is(ex.name, "DataError", "expect a DataError" + test);
michael@0 94 is(ex.code, 0, "expect zero" + test);
michael@0 95 is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
michael@0 96 }
michael@0 97 continue;
michael@0 98 }
michael@0 99
michael@0 100 // Make non-throwing function call
michael@0 101 let req = store[method].apply(store, args);
michael@0 102 is(JSON.stringify(value), valueJSON, "call didn't modify value" + test);
michael@0 103
michael@0 104 req.onsuccess = req.onerror = grabEventAndContinueHandler;
michael@0 105 let e = yield undefined;
michael@0 106
michael@0 107 // Figure out what key we used
michael@0 108 let key = 5;
michael@0 109 if (autoincrement && speccedNoKey) {
michael@0 110 key = 1;
michael@0 111 }
michael@0 112
michael@0 113 // Adjust value if expected
michael@0 114 if (autoincrement && keypath && speccedNoKey) {
michael@0 115 value.id = key;
michael@0 116 }
michael@0 117
michael@0 118 // Check result
michael@0 119 if (expected === "error") {
michael@0 120 is(e.type, "error", "write should fail" + test);
michael@0 121 e.preventDefault();
michael@0 122 e.stopPropagation();
michael@0 123 continue;
michael@0 124 }
michael@0 125
michael@0 126 is(e.type, "success", "write should succeed" + test);
michael@0 127 is(e.target.result, key, "write should return correct key" + test);
michael@0 128
michael@0 129 store.get(key).onsuccess = grabEventAndContinueHandler;
michael@0 130 e = yield undefined;
michael@0 131 is(e.type, "success", "read back should succeed" + test);
michael@0 132 is(JSON.stringify(e.target.result),
michael@0 133 JSON.stringify(value),
michael@0 134 "read back should return correct value" + test);
michael@0 135 }
michael@0 136 }
michael@0 137 }
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141
michael@0 142 function expectedResult(method, keypath, explicit, autoincrement, existing) {
michael@0 143 if (keypath && explicit)
michael@0 144 return "throw";
michael@0 145 if (!keypath && !explicit && !autoincrement)
michael@0 146 return "throw";
michael@0 147 if (keypath == "invalid")
michael@0 148 return "throw";
michael@0 149 if (keypath == "missing" && !autoincrement)
michael@0 150 return "throw";
michael@0 151 if (explicit == "invalid")
michael@0 152 return "throw";
michael@0 153
michael@0 154 if (method == "add" && existing)
michael@0 155 return "error";
michael@0 156
michael@0 157 return "success";
michael@0 158 }
michael@0 159
michael@0 160 openRequest.onsuccess = grabEventAndContinueHandler;
michael@0 161 yield undefined;
michael@0 162
michael@0 163 finishTest();
michael@0 164 yield undefined;
michael@0 165 }

mercurial