summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/workerStorageAllowed.js
blob: 89e0a4b9ce2338e4339d7bb9b9ca54ec21a4c838 (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
// 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();
}

// 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"
);

// Make sure that we can access indexedDB
try {
  indexedDB;
  ok(true, "WORKER getting indexedDB didn't throw");
} catch (e) {
  ok(false, "WORKER getting indexedDB should not throw");
}

// Make sure that we can access caches
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 == "#inner") {
    // 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)"
    );
  });
}