diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /dom/indexedDB/test/unit/test_create_index.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/indexedDB/test/unit/test_create_index.js')
-rw-r--r-- | dom/indexedDB/test/unit/test_create_index.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/dom/indexedDB/test/unit/test_create_index.js b/dom/indexedDB/test/unit/test_create_index.js new file mode 100644 index 0000000000..a925bd79d6 --- /dev/null +++ b/dom/indexedDB/test/unit/test_create_index.js @@ -0,0 +1,127 @@ +/** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +/* exported testGenerator */ +var testGenerator = testSteps(); + +function* testSteps() { + const name = this.window ? window.location.pathname : "Splendid Test"; + const objectStoreInfo = [ + { name: "a", options: { keyPath: "id", autoIncrement: true } }, + { name: "b", options: { keyPath: "id", autoIncrement: false } }, + ]; + const indexInfo = [ + { name: "1", keyPath: "unique_value", options: { unique: true } }, + { name: "2", keyPath: "value", options: { unique: false } }, + { name: "3", keyPath: "value", options: { unique: false } }, + { name: "", keyPath: "value", options: { unique: false } }, + { name: null, keyPath: "value", options: { unique: false } }, + { name: undefined, keyPath: "value", options: { unique: false } }, + ]; + + let request = indexedDB.open(name, 1); + request.onerror = errorHandler; + request.onupgradeneeded = grabEventAndContinueHandler; + request.onsuccess = unexpectedSuccessHandler; + let event = yield undefined; + let db = event.target.result; + + for (let i = 0; i < objectStoreInfo.length; i++) { + let info = objectStoreInfo[i]; + let objectStore = info.hasOwnProperty("options") + ? db.createObjectStore(info.name, info.options) + : db.createObjectStore(info.name); + + try { + request = objectStore.createIndex("Hola"); + ok(false, "createIndex with no keyPath should throw"); + } catch (e) { + ok(true, "createIndex with no keyPath should throw"); + } + + let ex; + try { + objectStore.createIndex("Hola", ["foo"], { multiEntry: true }); + } catch (e) { + ex = e; + } + ok(ex, "createIndex with array keyPath and multiEntry should throw"); + is(ex.name, "InvalidAccessError", "should throw right exception"); + ok(ex instanceof DOMException, "should throw right exception"); + is( + ex.code, + DOMException.INVALID_ACCESS_ERR, + "should throw right exception" + ); + + try { + objectStore.createIndex("foo", "bar", 10); + ok(false, "createIndex with bad options should throw"); + } catch (e) { + ok(true, "createIndex with bad options threw"); + } + + ok( + objectStore.createIndex("foo", "bar", { foo: "" }), + "createIndex with unknown options should not throw" + ); + objectStore.deleteIndex("foo"); + + // Test index creation, and that it ends up in indexNames. + let objectStoreName = info.name; + for (let j = 0; j < indexInfo.length; j++) { + let info = indexInfo[j]; + let count = objectStore.indexNames.length; + let index = info.hasOwnProperty("options") + ? objectStore.createIndex(info.name, info.keyPath, info.options) + : objectStore.createIndex(info.name, info.keyPath); + + let name = info.name; + if (name === null) { + name = "null"; + } else if (name === undefined) { + name = "undefined"; + } + + is(index.name, name, "correct name"); + is(index.keyPath, info.keyPath, "correct keyPath"); + is(index.unique, info.options.unique, "correct uniqueness"); + + is(objectStore.indexNames.length, count + 1, "indexNames grew in size"); + let found = false; + for (let k = 0; k < objectStore.indexNames.length; k++) { + if (objectStore.indexNames.item(k) == name) { + found = true; + break; + } + } + ok(found, "Name is on objectStore.indexNames"); + + ok(event.target.transaction, "event has a transaction"); + ok(event.target.transaction.db === db, "transaction has the right db"); + is( + event.target.transaction.mode, + "versionchange", + "transaction has the correct mode" + ); + is( + event.target.transaction.objectStoreNames.length, + i + 1, + "transaction only has one object store" + ); + ok( + event.target.transaction.objectStoreNames.contains(objectStoreName), + "transaction has the correct object store" + ); + } + } + + request.onsuccess = grabEventAndContinueHandler; + request.onupgradeneeded = unexpectedSuccessHandler; + + event = yield undefined; + + finishTest(); +} |