summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/browser/browser_cache_pb_window.js
blob: bb4521079640689abf8c6818347c97f0a06aa50f (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
/* 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(true, "caches.match() should be successful");
          resolve();
        })
        .catch(function (err) {
          ok(false, "caches.match() should not throw error");
          reject();
        });
    });
  });
}

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

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

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

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

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 be successful from worker");
        isGood ? resolve() : reject();
      });
    });
  });
}

function test() {
  let privateWin, privateTab;
  waitForExplicitFinish();
  SpecialPowers.pushPrefEnv({
    set: [["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);
}