Bug 1529038 [wpt PR 15361] - KV storage: add schema checks when initializing, a=testonly
[gecko.git] / testing / web-platform / tests / kv-storage / cause-errors-via-idb.https.html
blobd77e56621e037b71fa6ae9bfb3e80a0ab85677fc
1 <!DOCTYPE html>
2 <meta charset="utf-8">
3 <title>KV Storage: causing errors by directly manipulating the IDB</title>
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7 <script src="/IndexedDB/support-promises.js"></script>
9 <script type="module">
10 import { testWithArea, testWithAreaNoCleanup } from "./helpers/kvs-tests.js";
12 const mustFail = {
13 "set()": area => area.set(1, "value 1"),
14 "get()": area => area.get(1),
15 "delete()": area => area.delete(1),
16 "keys()": area => {
17 const iter = area.keys();
18 return iter.next();
20 "values()": area => {
21 const iter = area.values();
22 return iter.next();
24 "entries()": area => {
25 const iter = area.entries();
26 return iter.next();
30 for (const [method, testFn] of Object.entries(mustFail)) {
31 testWithArea(async (area, t) => {
32 const { database, version } = area.backingStore;
33 const db = await migrateNamedDatabase(t, database, version + 1, () => {});
35 const result = testFn(area);
37 await promise_rejects(t, "VersionError", result);
38 }, `${method}: upgrading the database must cause a "VersionError" DOMException`);
40 testWithAreaNoCleanup(async (area, t) => {
41 const { database } = area.backingStore;
43 // Set up a new database with that name, but with no object stores!
44 // NB: this depends on the fact that createNameDatabase sets the initial version to 1, which is
45 // the same as the database version used/expected by KV Storage.
46 const db = await createNamedDatabase(t, database, () => {});
48 const result = testFn(area);
50 await promise_rejects(t, "InvalidStateError", result);
51 }, `${method}: creating a same-named database with no object store must cause an "InvalidStateError" DOMException`);
53 testWithAreaNoCleanup(async (area, t) => {
54 const { database } = area.backingStore;
56 const db = await createNamedDatabase(t, database, db => {
57 db.createObjectStore("wrongName");
58 });
60 const result = testFn(area);
62 await promise_rejects(t, "InvalidStateError", result);
63 }, `${method}: creating a same-named database with a single object store with the wrong name must cause an "InvalidStateError" DOMException`);
65 testWithAreaNoCleanup(async (area, t) => {
66 const { database, store } = area.backingStore;
68 const db = await createNamedDatabase(t, database, db => {
69 db.createObjectStore(store);
70 db.createObjectStore("wrongName");
71 });
73 const result = testFn(area);
75 await promise_rejects(t, "InvalidStateError", result);
76 }, `${method}: creating a same-named database with more than one object store must cause an "InvalidStateError" DOMException`);
78 testWithAreaNoCleanup(async (area, t) => {
79 const { database, store } = area.backingStore;
81 const db = await createNamedDatabase(t, database, db => {
82 db.createObjectStore(store, { autoIncrement: true });
83 });
85 const result = testFn(area);
87 await promise_rejects(t, "InvalidStateError", result);
88 }, `${method}: creating a same-named database the right object store but a bad schema (autoIncrement = true) must cause an "InvalidStateError" DOMException`);
90 testWithAreaNoCleanup(async (area, t) => {
91 const { database, store } = area.backingStore;
93 const db = await createNamedDatabase(t, database, db => {
94 db.createObjectStore(store, { keyPath: "somekey" });
95 });
97 const result = testFn(area);
99 await promise_rejects(t, "InvalidStateError", result);
100 }, `${method}: creating a same-named database the right object store but a bad schema (keyPath != null) must cause an "InvalidStateError" DOMException`);
102 testWithAreaNoCleanup(async (area, t) => {
103 const { database, store } = area.backingStore;
105 const db = await createNamedDatabase(t, database, db => {
106 const s = db.createObjectStore(store);
107 s.createIndex("index", "indexKey");
110 const result = testFn(area);
112 await promise_rejects(t, "InvalidStateError", result);
113 }, `${method}: creating a same-named database the right object store but a bad schema (has indices) must cause an "InvalidStateError" DOMException`);
115 </script>