summaryrefslogtreecommitdiffstats
path: root/devtools/client/storage/test/storage-complex-keys.html
blob: b037190dea0ef2d7d9e72d4137ad26893db94ed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Storage inspector test for correct keys in the sidebar</title>
</head>
<body>
<script type="application/javascript">
"use strict";

// Some local storage items ...
localStorage.setItem("", "1");
localStorage.setItem("键", "2");
// ... and finally some session storage items too
sessionStorage.setItem("Key with spaces", "3");
sessionStorage.setItem("Key#with~special$characters", "4");
// long string
const longKey = "a".repeat(1000);
sessionStorage.setItem(longKey, "5");

const idbGenerator = async function () {
  const request = indexedDB.open("idb", 1);
  request.onerror = function() {
    throw new Error("Error opening database connection");
  };

  const db = await new Promise(done => {
    request.onupgradeneeded = event => {
      const _db = event.target.result;
      const store = _db.createObjectStore("obj", { keyPath: "id" });
      store.createIndex("name", "name", { unique: false });
      store.transaction.oncomplete = () => {
        done(_db);
      };
    };
  });

  // Prevents AbortError
  await new Promise(done => {
    request.onsuccess = done;
  });

  const transaction = db.transaction("obj", "readwrite");
  const store = transaction.objectStore("obj");

  store.add({id: "", name: "foo"});
  store.add({id: "键", name: "foo2"});
  store.add({id: "Key with spaces", name: "foo3"});
  store.add({id: "Key#with~special$characters", name: "foo4"});
  store.add({id: longKey, name: "foo5"});

  db.close();

  console.log("Added local and session storage items and indexedDB");
};

function deleteDB(dbName) {
  return new Promise(resolve => {
    dump("removing database " + dbName + " from " + document.location + "\n");
    indexedDB.deleteDatabase(dbName).onsuccess = resolve;
  });
}

window.setup = async function () {
  await idbGenerator();
};

window.clear = async function () {
  localStorage.clear();
  sessionStorage.clear();

  await deleteDB("idb");

  dump("Removed data from " + document.location + "\n");
};
</script>
</body>
</html>