Bug 1288284 - Add tests for eval and friends in content script
MozReview-Commit-ID: Be2c2EqX9G0
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -42,16 +42,17 @@ skip-if = os == 'android' # Android does
[test_ext_simple.html]
[test_ext_geturl.html]
[test_ext_content_security_policy.html]
[test_ext_contentscript.html]
skip-if = buildapp == 'b2g' # runat != document_idle is not supported.
[test_ext_contentscript_api_injection.html]
[test_ext_contentscript_create_iframe.html]
[test_ext_contentscript_devtools_metadata.html]
+[test_ext_contentscript_eval.html]
[test_ext_contentscript_css.html]
[test_ext_downloads.html]
[test_ext_exclude_include_globs.html]
[test_ext_i18n_css.html]
[test_ext_generate.html]
[test_ext_idle.html]
[test_ext_localStorage.html]
[test_ext_onmessage_removelistener.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_contentscript_eval.html
@@ -0,0 +1,104 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for content script</title>
+ <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+ <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+ <script type="text/javascript" src="head.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
+</head>
+<body>
+
+<script>
+"use strict";
+
+add_task(function* test_contentscript_eval_and_friends() {
+ function testEval() {
+ /* eslint-disable no-unused-vars */
+ let someVar = "LOCAL"; // Shadows global "someVar".
+ /* eslint-enable no-unused-vars */
+
+ /* eslint-disable no-eval */
+ let result = eval("window.localEvaled = true; someVar");
+ browser.test.assertTrue(window.localEvaled,
+ "eval() should run in the content script context");
+ browser.test.assertEq("LOCAL", result, "eval should run in local scope");
+
+ result = window.eval("window.globalEvaled = true; someVar");
+ browser.test.assertTrue(window.globalEvaled,
+ "window.eval() should run in the content script context");
+ browser.test.assertEq("GLOBAL", result,
+ "window.eval should run in global scope");
+ }
+
+ function testFunctionConstructor() {
+ new Function("window.localFunc = true")();
+ browser.test.assertTrue(window.localFunc,
+ "Function(...)() should run in the content script context");
+
+ new window.Function("window.globalFunc = true");
+ browser.test.assertTrue(window.globalFunc,
+ "window.Function(...)() should run in the content script context");
+ }
+
+ function testSetTimeout() {
+ let timersFired = new Promise(resolve => {
+ let fired = 0;
+ window.addEventListener("message", function listener() {
+ if (++fired === 2) {
+ window.removeEventListener("message", listener);
+ resolve();
+ }
+ });
+ });
+ /* eslint-disable no-implied-eval */
+ setTimeout("window.localTimer = true;postMessage('timer1', '*')");
+ window.setTimeout("window.globalTimer = true;postMessage('timer2', '*')");
+ timersFired.then(() => {
+ browser.test.assertTrue(window.localTimer,
+ "setTimeout(...) should run in the content script context");
+ browser.test.assertTrue(window.globalTimer,
+ "window.setTimeout(...) should run in the content script context");
+
+ browser.test.notifyPass("finished eval in content script test");
+ }, 10);
+ }
+
+ let extensionData = {
+ manifest: {
+ content_scripts: [{
+ js: ["contentscript.js"],
+ matches: ["http://mochi.test/*/file_sample.html"],
+ }],
+ },
+ files: {
+ "contentscript.js": `
+ // Global variable for eval test:
+ var someVar = "GLOBAL";
+ {
+ let s = document.createElement('script');
+ s.textContent = "var someVar = 'PAGE-GLOBAL';";
+ document.body.appendChild(s);
+ }
+ (${testEval})();
+ (${testFunctionConstructor})();
+ (${testSetTimeout})();
+ `,
+ },
+ };
+
+ let extension = ExtensionTestUtils.loadExtension(extensionData);
+
+ yield extension.startup();
+
+ let win = window.open("file_sample.html");
+
+ yield extension.awaitFinish("finished eval in content script test");
+ win.close();
+
+ yield extension.unload();
+});
+</script>
+</body>
+</html>