dom/indexedDB/test/unit/test_create_index.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 objectStoreInfo = [
michael@0 12 { name: "a", options: { keyPath: "id", autoIncrement: true } },
michael@0 13 { name: "b", options: { keyPath: "id", autoIncrement: false } },
michael@0 14 ];
michael@0 15 const indexInfo = [
michael@0 16 { name: "1", keyPath: "unique_value", options: { unique: true } },
michael@0 17 { name: "2", keyPath: "value", options: { unique: false } },
michael@0 18 { name: "3", keyPath: "value", options: { unique: false } },
michael@0 19 { name: "", keyPath: "value", options: { unique: false } },
michael@0 20 { name: null, keyPath: "value", options: { unique: false } },
michael@0 21 { name: undefined, keyPath: "value", options: { unique: false } },
michael@0 22 ];
michael@0 23
michael@0 24 let request = indexedDB.open(name, 1);
michael@0 25 request.onerror = errorHandler;
michael@0 26 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 27 request.onsuccess = unexpectedSuccessHandler;
michael@0 28 let event = yield undefined;
michael@0 29 let db = event.target.result;
michael@0 30
michael@0 31 for (let i = 0; i < objectStoreInfo.length; i++) {
michael@0 32 let info = objectStoreInfo[i];
michael@0 33 let objectStore = info.hasOwnProperty("options") ?
michael@0 34 db.createObjectStore(info.name, info.options) :
michael@0 35 db.createObjectStore(info.name);
michael@0 36
michael@0 37 try {
michael@0 38 request = objectStore.createIndex("Hola");
michael@0 39 ok(false, "createIndex with no keyPath should throw");
michael@0 40 }
michael@0 41 catch(e) {
michael@0 42 ok(true, "createIndex with no keyPath should throw");
michael@0 43 }
michael@0 44
michael@0 45 let ex;
michael@0 46 try {
michael@0 47 objectStore.createIndex("Hola", ["foo"], { multiEntry: true });
michael@0 48 }
michael@0 49 catch(e) {
michael@0 50 ex = e;
michael@0 51 }
michael@0 52 ok(ex, "createIndex with array keyPath and multiEntry should throw");
michael@0 53 is(ex.name, "InvalidAccessError", "should throw right exception");
michael@0 54 ok(ex instanceof DOMException, "should throw right exception");
michael@0 55 is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
michael@0 56
michael@0 57 try {
michael@0 58 objectStore.createIndex("foo", "bar", 10);
michael@0 59 ok(false, "createIndex with bad options should throw");
michael@0 60 }
michael@0 61 catch(e) {
michael@0 62 ok(true, "createIndex with bad options threw");
michael@0 63 }
michael@0 64
michael@0 65 ok(objectStore.createIndex("foo", "bar", { foo: "" }),
michael@0 66 "createIndex with unknown options should not throw");
michael@0 67 objectStore.deleteIndex("foo");
michael@0 68
michael@0 69 // Test index creation, and that it ends up in indexNames.
michael@0 70 let objectStoreName = info.name;
michael@0 71 for (let j = 0; j < indexInfo.length; j++) {
michael@0 72 let info = indexInfo[j];
michael@0 73 let count = objectStore.indexNames.length;
michael@0 74 let index = info.hasOwnProperty("options") ?
michael@0 75 objectStore.createIndex(info.name, info.keyPath,
michael@0 76 info.options) :
michael@0 77 objectStore.createIndex(info.name, info.keyPath);
michael@0 78
michael@0 79 let name = info.name;
michael@0 80 if (name === null) {
michael@0 81 name = "null";
michael@0 82 }
michael@0 83 else if (name === undefined) {
michael@0 84 name = "undefined";
michael@0 85 }
michael@0 86
michael@0 87 is(index.name, name, "correct name");
michael@0 88 is(index.keyPath, info.keyPath, "correct keyPath");
michael@0 89 is(index.unique, info.options.unique, "correct uniqueness");
michael@0 90
michael@0 91 is(objectStore.indexNames.length, count + 1,
michael@0 92 "indexNames grew in size");
michael@0 93 let found = false;
michael@0 94 for (let k = 0; k < objectStore.indexNames.length; k++) {
michael@0 95 if (objectStore.indexNames.item(k) == name) {
michael@0 96 found = true;
michael@0 97 break;
michael@0 98 }
michael@0 99 }
michael@0 100 ok(found, "Name is on objectStore.indexNames");
michael@0 101
michael@0 102 ok(event.target.transaction, "event has a transaction");
michael@0 103 ok(event.target.transaction.db === db,
michael@0 104 "transaction has the right db");
michael@0 105 is(event.target.transaction.mode, "versionchange",
michael@0 106 "transaction has the correct mode");
michael@0 107 is(event.target.transaction.objectStoreNames.length, i + 1,
michael@0 108 "transaction only has one object store");
michael@0 109 ok(event.target.transaction.objectStoreNames.contains(objectStoreName),
michael@0 110 "transaction has the correct object store");
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 request.onsuccess = grabEventAndContinueHandler;
michael@0 115 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 116
michael@0 117 event = yield undefined;
michael@0 118
michael@0 119 finishTest();
michael@0 120 yield undefined;
michael@0 121 }

mercurial