Bug 1373170 - Closing the extension popup tab should select the parent tab only if the popup tab was selected. draft
authorLuca Greco <lgreco@mozilla.com>
Wed, 01 Nov 2017 04:45:50 +0100
changeset 692096 0e04e4d3f771f9cd34d71b35edc36744090a5957
parent 692089 40a14ca1cf04499f398e4cb8ba359b39eae4e216
child 692097 3542e1014d6b9a59050686ba9af90c9bbcaa53ae
push id87393
push userluca.greco@alcacoop.it
push dateThu, 02 Nov 2017 14:56:26 +0000
bugs1373170
milestone58.0a1
Bug 1373170 - Closing the extension popup tab should select the parent tab only if the popup tab was selected. MozReview-Commit-ID: KgSTdblvmYe
mobile/android/components/extensions/ext-utils.js
mobile/android/components/extensions/test/mochitest/chrome.ini
mobile/android/components/extensions/test/mochitest/test_ext_popup_behavior.html
--- a/mobile/android/components/extensions/ext-utils.js
+++ b/mobile/android/components/extensions/ext-utils.js
@@ -249,16 +249,18 @@ global.WindowEventManager = class extend
 };
 
 class TabTracker extends TabTrackerBase {
   constructor() {
     super();
 
     // Keep track of the extension popup tab.
     this._extensionPopupTabWeak = null;
+    // Keep track of the last selected tabId
+    this._lastSelectedTabId = null;
   }
 
   init() {
     if (this.initialized) {
       return;
     }
     this.initialized = true;
 
@@ -366,16 +368,18 @@ class TabTracker extends TabTrackerBase 
    * @param {string} event The event which fired.
    * @param {object} data Information about the event which fired.
    */
   onEvent(event, data) {
     const {BrowserApp} = windowTracker.topWindow;
 
     switch (event) {
       case "Tab:Selected": {
+        this._lastSelectedTabId = data.id;
+
         // If a new tab has been selected while an extension popup tab is still open,
         // close it immediately.
         const nativeTab = BrowserApp.getTabForId(data.id);
 
         const popupTab = tabTracker.extensionPopupTab;
         if (popupTab && popupTab !== nativeTab) {
           BrowserApp.closeTab(popupTab);
         }
@@ -408,16 +412,22 @@ class TabTracker extends TabTrackerBase 
    */
   emitRemoved(nativeTab, isWindowClosing) {
     let windowId = windowTracker.getId(nativeTab.browser.ownerGlobal);
     let tabId = this.getId(nativeTab);
 
     if (this.extensionPopupTab && this.extensionPopupTab === nativeTab) {
       this._extensionPopupTabWeak = null;
 
+      // Do not switch to the parent tab of the extension popup tab
+      // if the popup tab is not the last selected tab.
+      if (this._lastSelectedTabId !== tabId) {
+        return;
+      }
+
       // Select the parent tab when the closed tab was an extension popup tab.
       const {BrowserApp} = windowTracker.topWindow;
       const popupParentTab = BrowserApp.getTabForId(nativeTab.parentId);
       if (popupParentTab) {
         BrowserApp.selectTab(popupParentTab);
       }
     }
 
--- a/mobile/android/components/extensions/test/mochitest/chrome.ini
+++ b/mobile/android/components/extensions/test/mochitest/chrome.ini
@@ -11,8 +11,9 @@ tags = webextensions
 [test_ext_browsingData_cookies_cache.html]
 [test_ext_browsingData_downloads.html]
 [test_ext_browsingData_formdata.html]
 [test_ext_browsingData_settings.html]
 [test_ext_options_ui.html]
 [test_ext_pageAction_show_hide.html]
 [test_ext_pageAction_getPopup_setPopup.html]
 skip-if = os == 'android' # bug 1373170
+[test_ext_popup_behavior.html]
new file mode 100644
--- /dev/null
+++ b/mobile/android/components/extensions/test/mochitest/test_ext_popup_behavior.html
@@ -0,0 +1,173 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>PageAction Test</title>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
+  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <script type="text/javascript" src="head.js"></script>
+  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
+</head>
+<body>
+
+<script type="text/javascript">
+"use strict";
+
+var {BrowserActions} = SpecialPowers.Cu.import("resource://gre/modules/BrowserActions.jsm", {});
+var {Services} = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm", {});
+
+function pageLoadedContentScript() {
+  browser.test.sendMessage("page-loaded", window.location.href);
+}
+
+function promiseDispatchedWindowEvent(eventName) {
+  return new Promise(resolve => {
+    let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+    let WindowEventDispatcher = chromeWin.WindowEventDispatcher;
+
+    let listener = (event) => {
+      WindowEventDispatcher.unregisterListener(listener, eventName);
+      resolve();
+    };
+
+    WindowEventDispatcher.registerListener(listener, eventName);
+  });
+}
+
+add_task(async function test_popup_behavior() {
+  const chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
+  const BrowserApp = chromeWin.BrowserApp;
+
+  async function background() {
+    const tab1 = await browser.tabs.create({url: "http://example.com#test_popup_behavior_1"});
+    const tab2 = await browser.tabs.create({url: "http://example.com#test_popup_behavior_2"});
+
+    browser.test.sendMessage("background_page.ready", {
+      tabId1: tab1.id,
+      tabId2: tab2.id,
+    });
+  }
+
+  async function popupScript() {
+    browser.test.sendMessage("popup_script.loaded");
+  }
+
+  let popupHtml = `<!DOCTYPE html>
+    <html>
+      <head>
+        <meta charset="utf-8">
+      </head>
+      <body>
+        <h1>Extension Popup</h1>
+        <script src="popup.js"><\/script>
+      </body>
+    </html>
+  `;
+
+  let extension = ExtensionTestUtils.loadExtension({
+    background,
+    manifest: {
+      "name": "BrowserAction Extension",
+      "browser_action": {
+        "default_title": "Browser Action",
+        "default_popup": "popup.html",
+        "default_icon": {
+          "18": "extension.png",
+        },
+      },
+      "content_scripts": [
+        {
+          "js": ["page_loaded.js"],
+          "matches": ["http://example.com/*"],
+          "run_at": "document_end",
+        },
+      ],
+      "permissions": ["activeTab"],
+    },
+    files: {
+      "extension.png": TEST_ICON_ARRAYBUFFER,
+      "page_loaded.js": pageLoadedContentScript,
+      "popup.html": popupHtml,
+      "popup.js": popupScript,
+    },
+  });
+
+  await extension.startup();
+
+  const {
+    tabId1,
+    tabId2,
+  } = await extension.awaitMessage("background_page.ready");
+
+  const uuid = `{${extension.uuid}}`;
+
+  ok(BrowserActions.isShown(uuid), "browser action is shown");
+
+  info("Wait the new tabs to be loaded");
+
+  await extension.awaitMessage("page-loaded");
+  await extension.awaitMessage("page-loaded");
+
+  is(BrowserApp.selectedTab.id, tabId2, "The new second tab has been selected");
+
+  BrowserActions.synthesizeClick(uuid);
+  await extension.awaitMessage("popup_script.loaded");
+
+  // Check that while the extension popup tab is selected the active tab is still the tab
+  // from which the user has opened the extension popup.
+  ok(BrowserApp.selectedBrowser.currentURI.spec.endsWith("popup.html"),
+     "The first popup tab has been selected");
+
+  let popupParentTabId = BrowserApp.selectedTab.parentId;
+  is(popupParentTabId, tabId2, "The parent of the first popup tab is the second tab");
+
+  // Close the popup and test that its parent tab is selected.
+  let onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
+  BrowserApp.closeTab(BrowserApp.selectedTab);
+  await onceTabClosed;
+
+  const tab1 = BrowserApp.getTabForId(tabId1);
+  const tab2 = BrowserApp.getTabForId(tabId2);
+
+  BrowserActions.synthesizeClick(uuid);
+  await extension.awaitMessage("popup_script.loaded");
+
+  ok(BrowserApp.selectedBrowser.currentURI.spec.endsWith("popup.html"),
+     "The second popup tab has been selected");
+
+  popupParentTabId = BrowserApp.selectedTab.parentId;
+  is(popupParentTabId, tabId2, "The parent of the second popup tab is the second tab");
+
+  // Switch to the second tab and expect it to still be the selected tab
+  // after the popup has been closed.
+  onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
+  BrowserApp.selectTab(tab1);
+  await onceTabClosed;
+
+  // Selecting a Firefox for Android tab is asynchronous,
+  // wait for 500ms (or once an unexpected "Tab:Selected" event has been
+  // received).
+  await Promise.race([
+    new Promise(resolve => setTimeout(resolve, 500)),
+    promiseDispatchedWindowEvent("Tab:Selected"),
+  ]);
+
+  is(BrowserApp.selectedTab.id, tabId1,
+     "The first tab is still the currently selected tab");
+
+  // Close the tab that opened the extension popup before exiting the test.
+  onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
+  BrowserApp.closeTab(tab1);
+  await onceTabClosed;
+
+  onceTabClosed = promiseDispatchedWindowEvent("Tab:Closed");
+  BrowserApp.closeTab(tab2);
+  await onceTabClosed;
+
+  await extension.unload();
+});
+
+</script>
+
+</body>
+</html>