summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/workerStorageAllowed.js
blob: 93c4c94c4884474ec6bff53ea19a97aabfcc6250 (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
// Unfortunately, workers can't share the code from storagePermissionsUtils.
// These are basic mechanisms for communicating to the test runner.

function ok(condition, text) {
  if (!condition) {
    self.postMessage("FAILURE: " + text);
  } else {
    self.postMessage(text);
  }
}

function finishTest() {
  self.postMessage("done");
  self.close();
}

// Make sure that we can access indexedDB
function idbTest() {
  try {
    indexedDB;

    const idbcycle = new Promise((resolve, reject) => {
      const begin = indexedDB.open("door");
      begin.onerror = e => {
        reject(e);
      };
      begin.onsuccess = () => {
        indexedDB
          .databases()
          .then(dbs => {
            ok(
              dbs.some(elem => elem.name === "door"),
              "WORKER just created database should be found"
            );
            const end = indexedDB.deleteDatabase("door");
            end.onerror = e => {
              reject(e);
            };
            end.onsuccess = () => {
              resolve();
            };
          })
          .catch(err => {
            reject(err);
          });
      };
    });

    idbcycle.then(
      () => {
        ok(true, "WORKER getting indexedDB didn't throw");
        cacheTest();
      },
      e => {
        ok(false, "WORKER getting indexedDB threw " + e.message);
        cacheTest();
      }
    );
  } catch (e) {
    ok(false, "WORKER getting indexedDB should not throw");
    cacheTest();
  }
}

// Make sure that we can access caches
function cacheTest() {
  try {
    var promise = caches.keys();
    ok(true, "WORKER getting caches didn't throw");

    promise.then(
      function () {
        ok(
          location.protocol == "https:",
          "WORKER The promise was not rejected"
        );
        workerTest();
      },
      function () {
        ok(
          location.protocol !== "https:",
          "WORKER The promise should not have been rejected"
        );
        workerTest();
      }
    );
  } catch (e) {
    ok(
      location.protocol !== "https:",
      "WORKER getting caches should not have thrown"
    );
    workerTest();
  }
}

// Try to spawn an inner worker, and make sure that it can also access storage
function workerTest() {
  if (location.hash != "#outer") {
    // Don't recurse infinitely, if we are the inner worker, don't spawn another
    finishTest();
    return;
  }
  // Create the inner worker, and listen for test messages from it
  var worker = new Worker("workerStorageAllowed.js#inner");
  worker.addEventListener("message", function (e) {
    if (e.data == "done") {
      finishTest();
      return;
    }

    ok(
      !e.data.match(/^FAILURE/),
      e.data + " (WORKER = workerStorageAllowed.js#inner)"
    );
  });

  worker.addEventListener("error", function (e) {
    ok(false, e.data + " (WORKER = workerStorageAllowed.js#inner)");

    finishTest();
  });
}

try {
  // Workers don't have access to localstorage or sessionstorage
  ok(
    typeof self.localStorage == "undefined",
    "localStorage should be undefined"
  );
  ok(
    typeof self.sessionStorage == "undefined",
    "sessionStorage should be undefined"
  );

  idbTest();
} catch (e) {
  ok(false, "WORKER Unwelcome exception received");
  finishTest();
}