Bug 1330745 - Add 'No Container' when longPressBehaviour is 1 for containers new tab context menu. r?baku r?gijs draft
authorJonathan Kingston <jkt@mozilla.com>
Mon, 25 Sep 2017 14:34:13 +0100
changeset 669917 5500b8c6dba435e5e75e988f703e73cfc32d8f89
parent 669480 3828e44f5b0a56e7933b817d430cf32d41b03fe6
child 733087 1933d6678c570122803b12a43b4276bd4739b3a5
push id81467
push userbmo:jkt@mozilla.com
push dateMon, 25 Sep 2017 15:58:34 +0000
reviewersbaku, gijs
bugs1330745
milestone58.0a1
Bug 1330745 - Add 'No Container' when longPressBehaviour is 1 for containers new tab context menu. r?baku r?gijs MozReview-Commit-ID: I2ptF9Dh7yL
browser/base/content/tabbrowser.xml
browser/base/content/utilityOverlay.js
browser/components/contextualidentity/test/browser/browser_newtabButton.js
--- a/browser/base/content/tabbrowser.xml
+++ b/browser/base/content/tabbrowser.xml
@@ -8196,17 +8196,20 @@
           createUserContextMenu(event, {useAccessKeys: false});
           return;
         }
 
         let containersEnabled = Services.prefs.getBoolPref("privacy.userContext.enabled");
 
         if (event.target.getAttribute("anonid") == "newtab-popup" ||
             event.target.id == "newtab-popup") {
-          createUserContextMenu(event, {useAccessKeys: false});
+          createUserContextMenu(event, {
+            useAccessKeys: false,
+            showDefaultTab: Services.prefs.getIntPref("privacy.userContext.longPressBehavior") == 1
+          });
         } else {
           document.getElementById("alltabs-popup-separator-1").hidden = !containersEnabled;
           let containersTab = document.getElementById("alltabs_containersTab");
 
           containersTab.hidden = !containersEnabled;
           if (PrivateBrowsingUtils.isWindowPrivate(window)) {
             containersTab.setAttribute("disabled", "true");
           }
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -521,27 +521,28 @@ function checkForMiddleClick(node, event
   }
 }
 
 // Populate a menu with user-context menu items. This method should be called
 // by onpopupshowing passing the event as first argument.
 function createUserContextMenu(event, {
                                         isContextMenu = false,
                                         excludeUserContextId = 0,
+                                        showDefaultTab = false,
                                         useAccessKeys = true
                                       } = {}) {
   while (event.target.hasChildNodes()) {
     event.target.firstChild.remove();
   }
 
   let bundle = document.getElementById("bundle_browser");
   let docfrag = document.createDocumentFragment();
 
   // If we are excluding a userContextId, we want to add a 'no-container' item.
-  if (excludeUserContextId) {
+  if (excludeUserContextId || showDefaultTab) {
     let menuitem = document.createElement("menuitem");
     menuitem.setAttribute("data-usercontextid", "0");
     menuitem.setAttribute("label", bundle.getString("userContextNone.label"));
     menuitem.setAttribute("accesskey", bundle.getString("userContextNone.accesskey"));
 
     // We don't set an oncommand/command attribute because if we have
     // to exclude a userContextId we are generating the contextMenu and
     // isContextMenu will be true.
--- a/browser/components/contextualidentity/test/browser/browser_newtabButton.js
+++ b/browser/components/contextualidentity/test/browser/browser_newtabButton.js
@@ -43,31 +43,46 @@ add_task(async function test_menu_withou
 
   let newTab = document.getElementById("tabbrowser-tabs");
   let newTabButton = document.getAnonymousElementByAttribute(newTab, "anonid", "tabs-newtab-button");
   ok(newTabButton, "New tab button exists");
   ok(!newTabButton.hidden, "New tab button is visible");
   await BrowserTestUtils.waitForCondition(() => !!document.getAnonymousElementByAttribute(newTab, "anonid", "newtab-popup"), "Wait for popup to exist");
   let popup = document.getAnonymousElementByAttribute(newTab, "anonid", "newtab-popup");
 
-  for (let i = 1; i <= 4; i++) {
-    let popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown");
+  let popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown");
+  let popupHiddenPromise = BrowserTestUtils.waitForEvent(popup, "popuphidden");
+  EventUtils.synthesizeMouseAtCenter(newTabButton, {type: "mousedown"});
+  await popupShownPromise;
+  let contextIdItems = popup.querySelectorAll("menuitem");
+  // 4 + default + manage containers
+  is(contextIdItems.length, 6, "Has 6 menu items");
+  popup.hidePopup();
+  await popupHiddenPromise;
+
+  for (let i = 0; i <= 4; i++) {
+    popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown");
     EventUtils.synthesizeMouseAtCenter(newTabButton, {type: "mousedown"});
 
     await popupShownPromise;
     let contextIdItem = popup.querySelector(`menuitem[data-usercontextid="${i}"]`);
 
     ok(contextIdItem, `User context id ${i} exists`);
 
-    let waitForTabPromise = BrowserTestUtils.waitForNewTab(gBrowser);
+    // waitForNewTab doesn't work for default tabs due to a different code path that doesn't cause a load event
+    let waitForTabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
     EventUtils.synthesizeMouseAtCenter(contextIdItem, {});
 
-    let tab = await waitForTabPromise;
-
-    is(tab.getAttribute("usercontextid"), i, `New tab has UCI equal ${i}`);
+    let tabEvent = await waitForTabPromise;
+    let tab = tabEvent.target;
+    if (i > 0) {
+      is(tab.getAttribute("usercontextid"), i, `New tab has UCI equal ${i}`);
+    } else {
+      ok(!tab.hasAttribute("usercontextid"), `New tab has no UCI`);
+    }
     await BrowserTestUtils.removeTab(tab);
   }
 });
 
 add_task(async function test_no_menu() {
   await SpecialPowers.pushPrefEnv({"set": [
       ["privacy.userContext.enabled", true],
       ["privacy.userContext.longPressBehavior", 0]