summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/test/browser/browser_partitionedIndexedDB.js
blob: 683f90e8356373de59f6b03d7e8c03779667bc51 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
PartitionedStorageHelper.runTest(
  "IndexedDB",
  async (win3rdParty, win1stParty) => {
    await new Promise(resolve => {
      let a = win1stParty.indexedDB.open("test", 1);
      ok(!!a, "IDB should not be blocked in 1st party contexts");

      a.onsuccess = e => {
        let db = e.target.result;
        is(db.objectStoreNames.length, 1, "We have 1 objectStore");
        is(db.objectStoreNames[0], "foobar", "We have 'foobar' objectStore");
        resolve();
      };

      a.onupgradeneeded = e => {
        let db = e.target.result;
        is(db.objectStoreNames.length, 0, "We have 0 objectStores");
        db.createObjectStore("foobar", { keyPath: "test" });
      };
    });

    await new Promise(resolve => {
      let a = win3rdParty.indexedDB.open("test", 1);
      ok(!!a, "IDB should not be blocked in 3rd party contexts");

      a.onsuccess = e => {
        let db = e.target.result;

        is(db.objectStoreNames.length, 0, "We have 0 objectStore");
        resolve();
      };
    });
  },

  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  }
);

PartitionedStorageHelper.runPartitioningTest(
  "Partitioned tabs - IndexedDB",
  "indexeddb",

  // getDataCallback
  async win => {
    return new Promise(resolve => {
      let a = win.indexedDB.open("test", 1);

      a.onupgradeneeded = e => {
        let db = e.target.result;
        db.createObjectStore("foobar", { keyPath: "id" });
      };

      a.onsuccess = e => {
        let db = e.target.result;
        db.transaction("foobar").objectStore("foobar").get(1).onsuccess =
          ee => {
            resolve(
              ee.target.result === undefined ? "" : ee.target.result.value
            );
          };
      };
    });
  },

  // addDataCallback
  async (win, value) => {
    return new Promise(resolve => {
      let a = win.indexedDB.open("test", 1);

      a.onsuccess = e => {
        let db = e.target.result;
        db
          .transaction("foobar", "readwrite")
          .objectStore("foobar")
          .put({ id: 1, value }).onsuccess = _ => {
          resolve(true);
        };
      };
    });
  },

  // cleanup
  async _ => {
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () =>
        resolve()
      );
    });
  }
);