summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/browser/browser_cache_pb_window.js
blob: 40a6da7005e5447a765605497ae9df44cb1be11c (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* eslint-disable no-shadow */
var name = "pb-window-cache";

function testMatch(browser) {
  return SpecialPowers.spawn(browser, [name], function (name) {
    return new Promise((resolve, reject) => {
      content.caches
        .match("http://foo.com")
        .then(function (response) {
          ok(false, "caches.match() should not return success");
          reject();
        })
        .catch(function (err) {
          is(
            "SecurityError",
            err.name,
            "caches.match() should throw SecurityError"
          );
          resolve();
        });
    });
  });
}

function testHas(browser) {
  return SpecialPowers.spawn(browser, [name], function (name) {
    return new Promise(function (resolve, reject) {
      content.caches
        .has(name)
        .then(function (result) {
          ok(false, "caches.has() should not return success");
          reject();
        })
        .catch(function (err) {
          is(
            "SecurityError",
            err.name,
            "caches.has() should throw SecurityError"
          );
          resolve();
        });
    });
  });
}

function testOpen(browser) {
  return SpecialPowers.spawn(browser, [name], function (name) {
    return new Promise(function (resolve, reject) {
      content.caches
        .open(name)
        .then(function (c) {
          ok(false, "caches.open() should not return success");
          reject();
        })
        .catch(function (err) {
          is(
            "SecurityError",
            err.name,
            "caches.open() should throw SecurityError"
          );
          resolve();
        });
    });
  });
}

function testDelete(browser) {
  return SpecialPowers.spawn(browser, [name], function (name) {
    return new Promise(function (resolve, reject) {
      content.caches
        .delete(name)
        .then(function (result) {
          ok(false, "caches.delete() should not return success");
          reject();
        })
        .catch(function (err) {
          is(
            "SecurityError",
            err.name,
            "caches.delete() should throw SecurityError"
          );
          resolve();
        });
    });
  });
}

function testKeys(browser) {
  return SpecialPowers.spawn(browser, [name], function (name) {
    return new Promise(function (resolve, reject) {
      content.caches
        .keys()
        .then(function (names) {
          ok(false, "caches.keys() should not return success");
          reject();
        })
        .catch(function (err) {
          is(
            "SecurityError",
            err.name,
            "caches.keys() should throw SecurityError"
          );
          resolve();
        });
    });
  });
}

function testOpen_worker(browser) {
  return SpecialPowers.spawn(browser, [], function () {
    let workerFunctionString = function () {
      caches.open("pb-worker-cache").then(
        function (cacheObject) {
          postMessage(cacheObject.toString());
        },
        function (reason) {
          postMessage(reason.name);
        }
      );
    }.toString();
    let workerBlobURL = content.URL.createObjectURL(
      new Blob(["(", workerFunctionString, ")()"], {
        type: "application/javascript",
      })
    );
    let worker = new content.Worker(workerBlobURL);
    content.URL.revokeObjectURL(workerBlobURL);
    return new Promise(function (resolve, reject) {
      worker.addEventListener("message", function (e) {
        let isGood = e.data === "SecurityError";
        ok(isGood, "caches.open() should throw SecurityError from worker");
        isGood ? resolve() : reject();
      });
    });
  });
}

function test() {
  let privateWin, privateTab;
  waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({
    set: [
      ["dom.caches.enabled", true],
      ["dom.caches.testing.enabled", true],
    ],
  })
    .then(() => {
      return BrowserTestUtils.openNewBrowserWindow({ private: true });
    })
    .then(pw => {
      privateWin = pw;
      privateTab = BrowserTestUtils.addTab(pw.gBrowser, "http://example.com/");
      return BrowserTestUtils.browserLoaded(privateTab.linkedBrowser);
    })
    .then(tab => {
      return Promise.all([
        testMatch(privateTab.linkedBrowser),
        testHas(privateTab.linkedBrowser),
        testOpen(privateTab.linkedBrowser),
        testDelete(privateTab.linkedBrowser),
        testKeys(privateTab.linkedBrowser),
        testOpen_worker(privateTab.linkedBrowser),
      ]);
    })
    .then(() => {
      return BrowserTestUtils.closeWindow(privateWin);
    })
    .then(finish);
}