summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/browser/browser_broadcastChannel.js
blob: 3882fee98f52680162adcb55739e9c7bed4b9b61 (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
// BroadcastChannel is not considered part of CookieJar. It's not allowed to
// communicate with other windows with different cookie jar settings.
"use strict";

CookiePolicyHelper.runTest("BroadcastChannel", {
  cookieJarAccessAllowed: async w => {
    new w.BroadcastChannel("hello");
    ok(true, "BroadcastChannel be used");
  },

  cookieJarAccessDenied: async w => {
    try {
      new w.BroadcastChannel("hello");
      ok(false, "BroadcastChannel cannot be used!");
    } catch (e) {
      ok(true, "BroadcastChannel cannot be used!");
      is(e.name, "SecurityError", "We want a security error message.");
    }
  },
});

CookiePolicyHelper.runTest("BroadcastChannel in workers", {
  cookieJarAccessAllowed: async w => {
    function nonBlockingCode() {
      new BroadcastChannel("hello");
      postMessage(true);
    }

    let blob = new w.Blob([
      nonBlockingCode.toString() + "; nonBlockingCode();",
    ]);
    ok(blob, "Blob has been created");

    let blobURL = w.URL.createObjectURL(blob);
    ok(blobURL, "Blob URL has been created");

    let worker = new w.Worker(blobURL);
    ok(worker, "Worker has been created");

    await new w.Promise((resolve, reject) => {
      worker.onmessage = function(e) {
        if (e) {
          resolve();
        } else {
          reject();
        }
      };
    });
  },

  cookieJarAccessDenied: async w => {
    function blockingCode() {
      try {
        new BroadcastChannel("hello");
        postMessage(false);
      } catch (e) {
        postMessage(e.name == "SecurityError");
      }
    }

    let blob = new w.Blob([blockingCode.toString() + "; blockingCode();"]);
    ok(blob, "Blob has been created");

    let blobURL = w.URL.createObjectURL(blob);
    ok(blobURL, "Blob URL has been created");

    let worker = new w.Worker(blobURL);
    ok(worker, "Worker has been created");

    await new w.Promise((resolve, reject) => {
      worker.onmessage = function(e) {
        if (e) {
          resolve();
        } else {
          reject();
        }
      };
    });
  },
});