dom/indexedDB/test/unit/test_create_objectStore.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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: "1", options: { keyPath: null } },
michael@0 13 { name: "2", options: { keyPath: null, autoIncrement: true } },
michael@0 14 { name: "3", options: { keyPath: null, autoIncrement: false } },
michael@0 15 { name: "4", options: { keyPath: null } },
michael@0 16 { name: "5", options: { keyPath: "foo" } },
michael@0 17 { name: "6" },
michael@0 18 { name: "7", options: null },
michael@0 19 { name: "8", options: { autoIncrement: true } },
michael@0 20 { name: "9", options: { autoIncrement: false } },
michael@0 21 { name: "10", options: { keyPath: "foo", autoIncrement: false } },
michael@0 22 { name: "11", options: { keyPath: "foo", autoIncrement: true } },
michael@0 23 { name: "" },
michael@0 24 { name: null },
michael@0 25 { name: undefined }
michael@0 26 ];
michael@0 27
michael@0 28 let request = indexedDB.open(name, 1);
michael@0 29 request.onerror = errorHandler;
michael@0 30 request.onupgradeneeded = grabEventAndContinueHandler;
michael@0 31 request.onsuccess = unexpectedSuccessHandler;
michael@0 32 let event = yield undefined;
michael@0 33
michael@0 34 let db = event.target.result;
michael@0 35
michael@0 36 let count = db.objectStoreNames.length;
michael@0 37 is(count, 0, "correct objectStoreNames length");
michael@0 38
michael@0 39 try {
michael@0 40 db.createObjectStore("foo", "bar");
michael@0 41 ok(false, "createObjectStore with bad options should throw");
michael@0 42 }
michael@0 43 catch(e) {
michael@0 44 ok(true, "createObjectStore with bad options");
michael@0 45 }
michael@0 46
michael@0 47 ok(db.createObjectStore("foo", { foo: "" }),
michael@0 48 "createObjectStore with unknown options should not throw");
michael@0 49 db.deleteObjectStore("foo");
michael@0 50
michael@0 51 for (let index in objectStoreInfo) {
michael@0 52 index = parseInt(index);
michael@0 53 const info = objectStoreInfo[index];
michael@0 54
michael@0 55 let objectStore = info.hasOwnProperty("options") ?
michael@0 56 db.createObjectStore(info.name, info.options) :
michael@0 57 db.createObjectStore(info.name);
michael@0 58
michael@0 59 is(db.objectStoreNames.length, index + 1,
michael@0 60 "updated objectStoreNames list");
michael@0 61
michael@0 62 let name = info.name;
michael@0 63 if (name === null) {
michael@0 64 name = "null";
michael@0 65 }
michael@0 66 else if (name === undefined) {
michael@0 67 name = "undefined";
michael@0 68 }
michael@0 69
michael@0 70 let found = false;
michael@0 71 for (let i = 0; i <= index; i++) {
michael@0 72 if (db.objectStoreNames.item(i) == name) {
michael@0 73 found = true;
michael@0 74 break;
michael@0 75 }
michael@0 76 }
michael@0 77 is(found, true, "objectStoreNames contains name");
michael@0 78
michael@0 79 is(objectStore.name, name, "Bad name");
michael@0 80 is(objectStore.keyPath, info.options && info.options.keyPath ?
michael@0 81 info.options.keyPath : null,
michael@0 82 "Bad keyPath");
michael@0 83 if(objectStore.indexNames.length, 0, "Bad indexNames");
michael@0 84
michael@0 85 ok(event.target.transaction, "event has a transaction");
michael@0 86 ok(event.target.transaction.db === db, "transaction has the right db");
michael@0 87 is(event.target.transaction.mode, "versionchange",
michael@0 88 "transaction has the correct mode");
michael@0 89 is(event.target.transaction.objectStoreNames.length, index + 1,
michael@0 90 "transaction has correct objectStoreNames list");
michael@0 91 found = false;
michael@0 92 for (let j = 0; j < event.target.transaction.objectStoreNames.length;
michael@0 93 j++) {
michael@0 94 if (event.target.transaction.objectStoreNames.item(j) == name) {
michael@0 95 found = true;
michael@0 96 break;
michael@0 97 }
michael@0 98 }
michael@0 99 is(found, true, "transaction has correct objectStoreNames list");
michael@0 100 }
michael@0 101
michael@0 102 // Can't handle autoincrement and empty keypath
michael@0 103 let ex;
michael@0 104 try {
michael@0 105 db.createObjectStore("storefail", { keyPath: "", autoIncrement: true });
michael@0 106 }
michael@0 107 catch(e) {
michael@0 108 ex = e;
michael@0 109 }
michael@0 110 ok(ex, "createObjectStore with empty keyPath and autoIncrement should throw");
michael@0 111 is(ex.name, "InvalidAccessError", "should throw right exception");
michael@0 112 ok(ex instanceof DOMException, "should throw right exception");
michael@0 113 is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
michael@0 114
michael@0 115 // Can't handle autoincrement and array keypath
michael@0 116 let ex;
michael@0 117 try {
michael@0 118 db.createObjectStore("storefail", { keyPath: ["a"], autoIncrement: true });
michael@0 119 }
michael@0 120 catch(e) {
michael@0 121 ex = e;
michael@0 122 }
michael@0 123 ok(ex, "createObjectStore with array keyPath and autoIncrement should throw");
michael@0 124 is(ex.name, "InvalidAccessError", "should throw right exception");
michael@0 125 ok(ex instanceof DOMException, "should throw right exception");
michael@0 126 is(ex.code, DOMException.INVALID_ACCESS_ERR, "should throw right exception");
michael@0 127
michael@0 128 request.onsuccess = grabEventAndContinueHandler;
michael@0 129 request.onupgradeneeded = unexpectedSuccessHandler;
michael@0 130
michael@0 131 event = yield undefined;
michael@0 132
michael@0 133 finishTest();
michael@0 134 yield undefined;
michael@0 135 }

mercurial