Bug 1273251: Part 5 - Add tests for event listeners in nuked sandboxes. r=peterv
authorKris Maglione <maglione.k@gmail.com>
Thu, 17 Nov 2016 12:26:51 -0800
changeset 377338 d406d6d99be9294f04ee0f7090e8b93f6f882a96
parent 377337 27c422b6b825c6889e4ecb17737d5c16ef9fe859
child 377339 00519f64027bb8dabf6da2e08c8956eee0fe1d20
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspeterv
bugs1273251
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1273251: Part 5 - Add tests for event listeners in nuked sandboxes. r=peterv MozReview-Commit-ID: 1Ok8hhaOdp4
js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js
js/xpconnect/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_nuke_sandbox_event_listeners.js
@@ -0,0 +1,81 @@
+/* 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
+
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+function promiseEvent(target, event) {
+  return new Promise(resolve => {
+    target.addEventListener(event, resolve, {capture: true, once: true});
+  });
+}
+
+add_task(function*() {
+  let principal = Services.scriptSecurityManager
+    .createCodebasePrincipalFromOrigin("http://example.com/");
+
+  let webnav = Services.appShell.createWindowlessBrowser(false);
+
+  let docShell = webnav.QueryInterface(Ci.nsIInterfaceRequestor)
+                       .getInterface(Ci.nsIDocShell);
+
+  docShell.createAboutBlankContentViewer(principal);
+
+  let window = webnav.document.defaultView;
+  let sandbox = Cu.Sandbox(window, {sandboxPrototype: window});
+
+  function sandboxContent() {
+    window.addEventListener("FromTest", () => {
+      window.dispatchEvent(new CustomEvent("FromSandbox"));
+    }, true);
+  }
+
+  Cu.evalInSandbox(`(${sandboxContent})()`, sandbox);
+
+
+  let fromTestPromise = promiseEvent(window, "FromTest");
+  let fromSandboxPromise = promiseEvent(window, "FromSandbox");
+
+  do_print("Dispatch FromTest event");
+  window.dispatchEvent(new window.CustomEvent("FromTest"));
+
+  yield fromTestPromise;
+  do_print("Got event from test");
+
+  yield fromSandboxPromise;
+  do_print("Got response from sandbox");
+
+
+  window.addEventListener("FromSandbox", () => {
+    ok(false, "Got unexpected reply from sandbox");
+  }, true);
+
+  do_print("Nuke sandbox");
+  Cu.nukeSandbox(sandbox);
+
+
+  do_print("Dispatch FromTest event");
+  fromTestPromise = promiseEvent(window, "FromTest");
+  window.dispatchEvent(new window.CustomEvent("FromTest"));
+  yield fromTestPromise;
+  do_print("Got event from test");
+
+
+  // Force cycle collection, which should cause our callback reference
+  // to be dropped, and dredge up potential issues there.
+  Cu.forceCC();
+
+
+  do_print("Dispatch FromTest event");
+  fromTestPromise = promiseEvent(window, "FromTest");
+  window.dispatchEvent(new window.CustomEvent("FromTest"));
+  yield fromTestPromise;
+  do_print("Got event from test");
+
+
+  webnav.close();
+});
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -92,16 +92,17 @@ fail-if = os == "android"
 [test_attributes.js]
 [test_params.js]
 [test_tearoffs.js]
 [test_want_components.js]
 [test_components.js]
 [test_allowedDomains.js]
 [test_allowedDomainsXHR.js]
 [test_nuke_sandbox.js]
+[test_nuke_sandbox_event_listeners.js]
 [test_sandbox_metadata.js]
 [test_exportFunction.js]
 [test_promise.js]
 [test_returncode.js]
 [test_textDecoder.js]
 [test_url.js]
 [test_URLSearchParams.js]
 [test_fileReader.js]