From 582474fa512db6df992a9a1edbbe07ff04f66b55 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Thu, 14 Sep 2017 02:11:29 +0200 Subject: [PATCH] Bug 1215376 - Always use old contextData upon refresh r=mixedpuppy MozReview-Commit-ID: K1wfFCYpMqB --HG-- extra : rebase_source : c82f9d01631e3d4160de0c49657dd7d8ca8c7a6a --- browser/components/extensions/ext-menus.js | 5 +- .../test/browser/browser_ext_menus_refresh.js | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/browser/components/extensions/ext-menus.js b/browser/components/extensions/ext-menus.js index 633cc36241ce..b0d285006b4f 100644 --- a/browser/components/extensions/ext-menus.js +++ b/browser/components/extensions/ext-menus.js @@ -63,9 +63,6 @@ var gMenuBuilder = { buildActionContextMenu(contextData) { const {menu} = contextData; - contextData.tab = tabTracker.activeTab; - contextData.pageUrl = contextData.tab.linkedBrowser.currentURI.spec; - const root = gRootItems.get(contextData.extension); if (!root) { return; @@ -429,6 +426,8 @@ var gMenuBuilder = { // Called from pageAction or browserAction popup. global.actionContextMenu = function(contextData) { + contextData.tab = tabTracker.activeTab; + contextData.pageUrl = contextData.tab.linkedBrowser.currentURI.spec; gMenuBuilder.buildActionContextMenu(contextData); }; diff --git a/browser/components/extensions/test/browser/browser_ext_menus_refresh.js b/browser/components/extensions/test/browser/browser_ext_menus_refresh.js index 7ba151cfd1f1..c2ac48da0925 100644 --- a/browser/components/extensions/test/browser/browser_ext_menus_refresh.js +++ b/browser/components/extensions/test/browser/browser_ext_menus_refresh.js @@ -288,3 +288,62 @@ add_task(async function refresh_menus_with_browser_action() { await extension.unload(); await other_extension.unload(); }); + +add_task(async function refresh_menus_during_navigation() { + const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE + "?1"); + let extension = loadExtensionWithMenusApi(); + await extension.startup(); + + await extension.callMenuApi("create", { + id: "item1", + title: "item1", + contexts: ["browser_action"], + documentUrlPatterns: ["*://*/*?1*"], + }); + + await extension.callMenuApi("create", { + id: "item2", + title: "item2", + contexts: ["browser_action"], + documentUrlPatterns: ["*://*/*?2*"], + }); + + await openActionContextMenu(extension, "browser"); + await extension.awaitMessage("onShown fired"); + + let elem = extension.getXULElementByMenuId("item1"); + is(elem.getAttribute("label"), "item1", "menu item 1 should be shown"); + elem = extension.getXULElementByMenuId("item2"); + is(elem, null, "menu item 2 should be hidden"); + + await BrowserTestUtils.loadURI(tab.linkedBrowser, PAGE + "?2"); + await BrowserTestUtils.browserStopped(tab.linkedBrowser); + + await extension.callMenuApi("refresh"); + + // The menu items in a context menu are based on the context at the time of + // opening the menu. Menus are not updated if the context changes, e.g. as a + // result of navigation events after the menu was shown. + // So when refresh() is called during the onShown event, then the original + // URL (before navigation) should be used to determine whether to show a + // URL-specific menu item, and NOT the current URL (after navigation). + elem = extension.getXULElementByMenuId("item1"); + is(elem.getAttribute("label"), "item1", "menu item 1 should still be shown"); + elem = extension.getXULElementByMenuId("item2"); + is(elem, null, "menu item 2 should still be hidden"); + + await closeActionContextMenu(); + await openActionContextMenu(extension, "browser"); + await extension.awaitMessage("onShown fired"); + + // Now after closing and re-opening the menu, the latest contextual info + // should be used. + elem = extension.getXULElementByMenuId("item1"); + is(elem, null, "menu item 1 should be hidden"); + elem = extension.getXULElementByMenuId("item2"); + is(elem.getAttribute("label"), "item2", "menu item 2 should be shown"); + + await closeActionContextMenu(); + await extension.unload(); + await BrowserTestUtils.removeTab(tab); +});