Bug 1353150: Add tests for WebExtension Xray named element property access. r=bz,mixedpuppy draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 06 Apr 2017 15:20:24 -0700
changeset 557680 883039a115295b8bd61990324d4a02411e3039ba
parent 557679 339ecf06aede37cd19501e4df6b78ba4741d5303
child 623113 0ef55769d68664f53d5350fc75dc4c27187c386d
push id52781
push usermaglione.k@gmail.com
push dateFri, 07 Apr 2017 04:33:23 +0000
reviewersbz, mixedpuppy
bugs1353150
milestone55.0a1
Bug 1353150: Add tests for WebExtension Xray named element property access. r=bz,mixedpuppy MozReview-Commit-ID: ARyek92K5qR
toolkit/components/extensions/test/xpcshell/data/file_sample.html
toolkit/components/extensions/test/xpcshell/test_ext_contentscript_xrays.js
toolkit/components/extensions/test/xpcshell/xpcshell-content.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/data/file_sample.html
@@ -0,0 +1,12 @@
+<!DOCTYPE HTML>
+
+<html>
+<head>
+<meta charset="utf-8">
+</head>
+<body>
+
+<div id="test">Sample text</div>
+
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_xrays.js
@@ -0,0 +1,72 @@
+"use strict";
+
+Cu.import("resource://gre/modules/Preferences.jsm");
+
+const server = createHttpServer();
+server.registerDirectory("/data/", do_get_file("data"));
+
+const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
+const XRAY_PREF = "dom.allow_named_properties_object_for_xrays";
+
+add_task(async function test_contentscript_xrays() {
+  async function contentScript() {
+    let deferred;
+    browser.test.onMessage.addListener(msg => {
+      if (msg === "pref-set") {
+        deferred.resolve();
+      }
+    });
+    function setPref(val) {
+      browser.test.sendMessage("set-pref", val);
+      return new Promise(resolve => { deferred = {resolve}; });
+    }
+
+    let unwrapped = window.wrappedJSObject;
+
+    await setPref(0);
+    browser.test.assertEq("object", typeof test, "Should have named X-ray property access with pref=0");
+    browser.test.assertEq("object", typeof window.test, "Should have named X-ray property access with pref=0");
+    browser.test.assertEq("object", typeof unwrapped.test, "Should always have non-X-ray named property access");
+
+    await setPref(1);
+    browser.test.assertEq("undefined", typeof test, "Should not have named X-ray property access with pref=1");
+    browser.test.assertEq(undefined, window.test, "Should not have named X-ray property access with pref=1");
+    browser.test.assertEq("object", typeof unwrapped.test, "Should always have non-X-ray named property access");
+
+    await setPref(2);
+    browser.test.assertEq("undefined", typeof test, "Should not have named X-ray property access with pref=2");
+    browser.test.assertEq(undefined, window.test, "Should not have named X-ray property access with pref=2");
+    browser.test.assertEq("object", typeof unwrapped.test, "Should always have non-X-ray named property access");
+
+    browser.test.notifyPass("contentScriptXrays");
+  }
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      content_scripts: [{
+        "matches": ["http://*/*/file_sample.html"],
+        "js": ["content_script.js"],
+      }],
+    },
+
+    files: {
+      "content_script.js": contentScript,
+    },
+  });
+
+  extension.onMessage("set-pref", value => {
+    Preferences.set(XRAY_PREF, value);
+    extension.sendMessage("pref-set");
+  });
+
+  equal(Preferences.get(XRAY_PREF), 1, "Should have pref=1 by default");
+
+  await extension.startup();
+  let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/file_sample.html`);
+
+  await extension.awaitFinish("contentScriptXrays");
+
+  await contentPage.close();
+  await extension.unload();
+});
+
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-content.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-content.ini
@@ -1,3 +1,4 @@
 [test_ext_i18n.js]
 [test_ext_i18n_css.js]
 [test_ext_contentscript.js]
+[test_ext_contentscript_xrays.js]