Bug 1480244: Part 3a - Fix non-strict-mode test code which expects `this` to be bound to its global. r=aswan
A lot of the ad-hoc frame scripts we execute for tests does not run in strict
mode, and therefore has its functions' `this` objects set to the global when
they are called without a target object.
At the moment, this gives them a MessageManager global. Once message managers
become non-global objects, however, it will give them the shared JSM global,
which is not what they expect.
This patches changes scripts which rely on this to explicitly capture or set
the appropriate `this` object for their calls.
MozReview-Commit-ID: DY8DDb0xE1K
--- a/browser/components/extensions/test/browser/browser_ext_find.js
+++ b/browser/components/extensions/test/browser/browser_ext_find.js
@@ -1,35 +1,32 @@
/* global browser */
"use strict";
function frameScript() {
- function getSelectedText() {
- let frame = this.content.frames[0].frames[1];
- let docShell = frame.docShell;
- let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
- .getInterface(Ci.nsISelectionDisplay)
- .QueryInterface(Ci.nsISelectionController);
- let selection = controller.getSelection(controller.SELECTION_FIND);
- let range = selection.getRangeAt(0);
- let scope = {};
- ChromeUtils.import("resource://gre/modules/FindContent.jsm", scope);
- let highlighter = (new scope.FindContent(docShell)).highlighter;
- let r1 = frame.parent.frameElement.getBoundingClientRect();
- let f1 = highlighter._getFrameElementOffsets(frame.parent);
- let r2 = frame.frameElement.getBoundingClientRect();
- let f2 = highlighter._getFrameElementOffsets(frame);
- let r3 = range.getBoundingClientRect();
- let rect = {
- top: (r1.top + r2.top + r3.top + f1.y + f2.y),
- left: (r1.left + r2.left + r3.left + f1.x + f2.x),
- };
- this.sendAsyncMessage("test:find:selectionTest", {text: selection.toString(), rect});
- }
- getSelectedText();
+ let frame = this.content.frames[0].frames[1];
+ let docShell = frame.docShell;
+ let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsISelectionDisplay)
+ .QueryInterface(Ci.nsISelectionController);
+ let selection = controller.getSelection(controller.SELECTION_FIND);
+ let range = selection.getRangeAt(0);
+ let scope = {};
+ ChromeUtils.import("resource://gre/modules/FindContent.jsm", scope);
+ let highlighter = (new scope.FindContent(docShell)).highlighter;
+ let r1 = frame.parent.frameElement.getBoundingClientRect();
+ let f1 = highlighter._getFrameElementOffsets(frame.parent);
+ let r2 = frame.frameElement.getBoundingClientRect();
+ let f2 = highlighter._getFrameElementOffsets(frame);
+ let r3 = range.getBoundingClientRect();
+ let rect = {
+ top: (r1.top + r2.top + r3.top + f1.y + f2.y),
+ left: (r1.left + r2.left + r3.left + f1.x + f2.x),
+ };
+ this.sendAsyncMessage("test:find:selectionTest", {text: selection.toString(), rect});
}
function waitForMessage(messageManager, topic) {
return new Promise(resolve => {
messageManager.addMessageListener(topic, function messageListener(message) {
messageManager.removeMessageListener(topic, messageListener);
resolve(message);
});
@@ -139,17 +136,17 @@ add_task(async function testDuplicatePin
await extension.startup();
let rectData = await extension.awaitMessage("test:find:WebExtensionFinished");
let {top, left} = rectData[5].rectsAndTexts.rectList[0];
await extension.unload();
let {selectedBrowser} = gBrowser;
- let frameScriptUrl = `data:,(${frameScript})()`;
+ let frameScriptUrl = `data:,(${frameScript}).call(this)`;
selectedBrowser.messageManager.loadFrameScript(frameScriptUrl, false);
let message = await waitForMessage(selectedBrowser.messageManager, "test:find:selectionTest");
info("Test that text was highlighted properly.");
is(message.data.text, "bananA", `The text that was highlighted: - Expected: bananA, Actual: ${message.data.text}`);
info("Test that rectangle data returned from the search matches the highlighted result.");
is(message.data.rect.top, top, `rect.top: - Expected: ${message.data.rect.top}, Actual: ${top}`);
--- a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js
+++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js
@@ -114,17 +114,17 @@ add_task(async function test_on_created_
const expectedSourceTab = await extension.awaitMessage("expectedSourceTab");
info("Open a subframe link in a new tab using Ctrl-click");
await runCreatedNavigationTargetTest({
extension,
openNavTarget() {
- BrowserTestUtils.synthesizeMouseAtCenter(function() {
+ BrowserTestUtils.synthesizeMouseAtCenter(() => {
// This code runs as a framescript in the child process and it returns the
// target link in the subframe.
return this.content.frames[0].document
.querySelector("#test-create-new-tab-from-mouse-click-subframe");
}, {ctrlKey: true, metaKey: true}, tab.linkedBrowser);
},
expectedWebNavProps: {
sourceTabId: expectedSourceTab.sourceTabId,
@@ -133,17 +133,17 @@ add_task(async function test_on_created_
},
});
info("Open a subframe link in a new window using Shift-click");
await runCreatedNavigationTargetTest({
extension,
openNavTarget() {
- BrowserTestUtils.synthesizeMouseAtCenter(function() {
+ BrowserTestUtils.synthesizeMouseAtCenter(() => {
// This code runs as a framescript in the child process and it returns the
// target link in the subframe.
return this.content.frames[0].document
.querySelector("#test-create-new-window-from-mouse-click-subframe");
}, {shiftKey: true}, tab.linkedBrowser);
},
expectedWebNavProps: {
sourceTabId: expectedSourceTab.sourceTabId,
@@ -152,17 +152,17 @@ add_task(async function test_on_created_
},
});
info("Open a subframe link with target=\"_blank\" in a new tab using click");
await runCreatedNavigationTargetTest({
extension,
openNavTarget() {
- BrowserTestUtils.synthesizeMouseAtCenter(function() {
+ BrowserTestUtils.synthesizeMouseAtCenter(() => {
// This code runs as a framescript in the child process and it returns the
// target link in the subframe.
return this.content.frames[0].document
.querySelector("#test-create-new-tab-from-targetblank-click-subframe");
}, {}, tab.linkedBrowser);
},
expectedWebNavProps: {
sourceTabId: expectedSourceTab.sourceTabId,
--- a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js
+++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js
@@ -109,17 +109,17 @@ add_task(async function test_on_created_
const expectedSourceTab = await extension.awaitMessage("expectedSourceTab");
info("Open a subframe link in a new tab from the context menu");
await runCreatedNavigationTargetTest({
extension,
async openNavTarget() {
await clickContextMenuItem({
- pageElementSelector: function() {
+ pageElementSelector: () => {
// This code runs as a framescript in the child process and it returns the
// target link in the subframe.
return this.content.frames[0]
.document.querySelector("#test-create-new-tab-from-context-menu-subframe");
},
contextMenuItemLabel: "Open Link in New Tab",
});
},
@@ -131,17 +131,17 @@ add_task(async function test_on_created_
});
info("Open a subframe link in a new window from the context menu");
await runCreatedNavigationTargetTest({
extension,
async openNavTarget() {
await clickContextMenuItem({
- pageElementSelector: function() {
+ pageElementSelector: () => {
// This code runs as a framescript in the child process and it returns the
// target link in the subframe.
return this.content.frames[0]
.document.querySelector("#test-create-new-window-from-context-menu-subframe");
},
contextMenuItemLabel: "Open Link in New Window",
});
},
--- a/dom/base/test/browser_messagemanager_loadprocessscript.js
+++ b/dom/base/test/browser_messagemanager_loadprocessscript.js
@@ -33,17 +33,17 @@ function processScript() {
this.cpmm = Services.cpmm;
addMessageListener("ProcessTest:Reply", function listener(msg) {
removeMessageListener("ProcessTest:Reply", listener);
sendAsyncMessage("ProcessTest:Finished");
});
sendSyncMessage("ProcessTest:Loaded");
}
-var processScriptURL = "data:,(" + processScript.toString() + ")()";
+var processScriptURL = "data:,(" + processScript.toString() + ").call(this)";
function initTestScript() {
let init = initialProcessData;
if (init.test123 != "hello") {
dump("Initial data incorrect\n");
return;
}
--- a/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js
@@ -31,17 +31,17 @@ function frameScript() {
},
});
}
async function withHandlingUserInput(extension, fn) {
let {messageManager} = extension.extension.groupFrameLoader;
if (!extensionHandlers.has(extension)) {
- messageManager.loadFrameScript(`data:,(${frameScript})(this)`, false);
+ messageManager.loadFrameScript(`data:,(${frameScript}).call(this)`, false);
extensionHandlers.add(extension);
}
await MessageChannel.sendMessage(messageManager, "ExtensionTest:HandleUserInput", true);
await fn();
await MessageChannel.sendMessage(messageManager, "ExtensionTest:HandleUserInput", false);
}