From 24dc48a4df093cb0092674331333ca0809d0fffe Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Fri, 3 Aug 2018 14:33:38 -0700 Subject: [PATCH] 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 --HG-- extra : rebase_source : 86c1fa4df070711f666dfee5487182afe28a7611 --- .../test/browser/browser_ext_find.js | 45 +++++++++---------- ...webNavigation_onCreatedNavigationTarget.js | 6 +-- ...n_onCreatedNavigationTarget_contextmenu.js | 4 +- ...rowser_messagemanager_loadprocessscript.js | 2 +- .../test/xpcshell/test_ext_permissions.js | 2 +- 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/browser/components/extensions/test/browser/browser_ext_find.js b/browser/components/extensions/test/browser/browser_ext_find.js index 64c4c51cb314..fa87ca69d3fe 100644 --- a/browser/components/extensions/test/browser/browser_ext_find.js +++ b/browser/components/extensions/test/browser/browser_ext_find.js @@ -2,29 +2,26 @@ "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) { @@ -144,7 +141,7 @@ add_task(async function testDuplicatePinnedTab() { 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"); diff --git a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js index 1728004fd9bb..144e968e8992 100644 --- a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js +++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget.js @@ -119,7 +119,7 @@ add_task(async function test_on_created_navigation_target_from_mouse_click_subfr 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 @@ -138,7 +138,7 @@ add_task(async function test_on_created_navigation_target_from_mouse_click_subfr 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 @@ -157,7 +157,7 @@ add_task(async function test_on_created_navigation_target_from_mouse_click_subfr 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 diff --git a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js index 55737a9cd238..23b5562863f6 100644 --- a/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js +++ b/browser/components/extensions/test/browser/browser_ext_webNavigation_onCreatedNavigationTarget_contextmenu.js @@ -114,7 +114,7 @@ add_task(async function test_on_created_navigation_target_from_context_menu_subf 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] @@ -136,7 +136,7 @@ add_task(async function test_on_created_navigation_target_from_context_menu_subf 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] diff --git a/dom/base/test/browser_messagemanager_loadprocessscript.js b/dom/base/test/browser_messagemanager_loadprocessscript.js index f76a45081be9..2af784dd5575 100644 --- a/dom/base/test/browser_messagemanager_loadprocessscript.js +++ b/dom/base/test/browser_messagemanager_loadprocessscript.js @@ -38,7 +38,7 @@ function processScript() { }); sendSyncMessage("ProcessTest:Loaded"); } -var processScriptURL = "data:,(" + processScript.toString() + ")()"; +var processScriptURL = "data:,(" + processScript.toString() + ").call(this)"; function initTestScript() { let init = initialProcessData; diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js b/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js index dcbc402464d4..c8961fe8363d 100644 --- a/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js +++ b/toolkit/components/extensions/test/xpcshell/test_ext_permissions.js @@ -36,7 +36,7 @@ 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); }