summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js
blob: 2e304a8b72c4dd19f86afd185c703f8201287769 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// See https://bugzilla.mozilla.org/show_bug.cgi?id=1273251

function promiseEvent(target, event) {
  return new Promise(resolve => {
    target.addEventListener(event, resolve, {capture: true, once: true});
  });
}

add_task(async function() {
  let principal = Services.scriptSecurityManager
    .createContentPrincipalFromOrigin("http://example.com/");

  let webnav = Services.appShell.createWindowlessBrowser(false);

  let docShell = webnav.docShell;

  docShell.createAboutBlankContentViewer(principal, principal);

  let window = webnav.document.defaultView;
  let sandbox = Cu.Sandbox(window, {sandboxPrototype: window});

  function sandboxContent() {
    window.onload = function SandboxOnLoad() {};

    window.addEventListener("FromTest", () => {
      window.dispatchEvent(new CustomEvent("FromSandbox"));
    }, true);
  }

  Cu.evalInSandbox(`(${sandboxContent})()`, sandbox);


  let fromTestPromise = promiseEvent(window, "FromTest");
  let fromSandboxPromise = promiseEvent(window, "FromSandbox");

  equal(typeof window.onload, "function",
        "window.onload should contain sandbox event listener");
  equal(window.onload.name, "SandboxOnLoad",
        "window.onload have the correct function name");

  info("Dispatch FromTest event");
  window.dispatchEvent(new window.CustomEvent("FromTest"));

  await fromTestPromise;
  info("Got event from test");

  await fromSandboxPromise;
  info("Got response from sandbox");


  window.addEventListener("FromSandbox", () => {
    ok(false, "Got unexpected reply from sandbox");
  }, true);

  info("Nuke sandbox");
  Cu.nukeSandbox(sandbox);


  info("Dispatch FromTest event");
  fromTestPromise = promiseEvent(window, "FromTest");
  window.dispatchEvent(new window.CustomEvent("FromTest"));
  await fromTestPromise;
  info("Got event from test");


  // Force cycle collection, which should cause our callback reference
  // to be dropped, and dredge up potential issues there.
  Cu.forceGC();
  Cu.forceCC();

  ok(Cu.isDeadWrapper(window.onload),
     "window.onload should contain a dead wrapper after sandbox is nuked");

  info("Dispatch FromTest event");
  fromTestPromise = promiseEvent(window, "FromTest");
  window.dispatchEvent(new window.CustomEvent("FromTest"));
  await fromTestPromise;
  info("Got event from test");

  let listeners = Services.els.getListenerInfoFor(window);
  ok(!listeners.some(info => info.type == "FromTest"),
     "No 'FromTest' listeners returned for nuked sandbox");

  webnav.close();
});