Commit 6809804a authored by henry's avatar henry Committed by Pier Angelo Vendrame
Browse files

Bug 1919363 - Only show one app menu "new window" item in permanent private browsing. r=mconley

We also update the browser_private_browsing_window.js test.
The previous test was limited because it was referring to non-existent
"appmenu_newNavigator" and "appmenu_newPrivateWindow".

Differential Revision: https://phabricator.services.mozilla.com/D222507
parent 6f44909d
Loading
Loading
Loading
Loading
+28 −9
Original line number Diff line number Diff line
@@ -6798,15 +6798,34 @@ var gPrivateBrowsingUI = {
    }

    if (PrivateBrowsingUtils.permanentPrivateBrowsing) {
      // Adjust the New Window menu entries
      let newWindow = document.getElementById("menu_newNavigator");
      let newPrivateWindow = document.getElementById("menu_newPrivateWindow");
      if (newWindow && newPrivateWindow) {
        newPrivateWindow.hidden = true;
        newWindow.label = newPrivateWindow.label;
        newWindow.accessKey = newPrivateWindow.accessKey;
        newWindow.command = newPrivateWindow.command;
      }
      let hideNewWindowItem = (windowItem, privateWindowItem) => {
        // In permanent browsing mode command "cmd_newNavigator" should act the
        // same as "Tools:PrivateBrowsing".
        // So we hide the redundant private window item. But we also rename the
        // "new window" item to be "new private window".
        // NOTE: We choose to hide privateWindowItem rather than windowItem so
        // that we still show the "key" for "cmd_newNavigator" (Ctrl+N) rather
        // than (Ctrl+Shift+P).
        privateWindowItem.hidden = true;
        windowItem.setAttribute(
          "data-l10n-id",
          privateWindowItem.getAttribute("data-l10n-id")
        );
      };

      // Adjust the File menu items.
      hideNewWindowItem(
        document.getElementById("menu_newNavigator"),
        document.getElementById("menu_newPrivateWindow")
      );
      // Adjust the App menu items.
      hideNewWindowItem(
        PanelMultiView.getViewNode(document, "appMenu-new-window-button2"),
        PanelMultiView.getViewNode(
          document,
          "appMenu-new-private-window-button2"
        )
      );
    }
  },
};
+203 −118
Original line number Diff line number Diff line
// Make sure that we can open private browsing windows
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function test() {
  waitForExplicitFinish();
  var nonPrivateWin = OpenBrowserWindow();
  ok(
    !PrivateBrowsingUtils.isWindowPrivate(nonPrivateWin),
add_task(async function testOpenBrowserWindow() {
  let win = OpenBrowserWindow();
  Assert.ok(
    !PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow() should open a normal window"
  );
  nonPrivateWin.close();
  await BrowserTestUtils.closeWindow(win);

  var privateWin = OpenBrowserWindow({ private: true });
  ok(
    PrivateBrowsingUtils.isWindowPrivate(privateWin),
  win = OpenBrowserWindow({ private: true });
  Assert.ok(
    PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow({private: true}) should open a private window"
  );
  await BrowserTestUtils.closeWindow(win);

  nonPrivateWin = OpenBrowserWindow({ private: false });
  ok(
    !PrivateBrowsingUtils.isWindowPrivate(nonPrivateWin),
  win = OpenBrowserWindow({ private: false });
  Assert.ok(
    !PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow({private: false}) should open a normal window"
  );
  nonPrivateWin.close();
  await BrowserTestUtils.closeWindow(win);

  whenDelayedStartupFinished(privateWin, function () {
    nonPrivateWin = privateWin.OpenBrowserWindow({ private: false });
    ok(
      !PrivateBrowsingUtils.isWindowPrivate(nonPrivateWin),
      "privateWin.OpenBrowserWindow({private: false}) should open a normal window"
  // In permanent browsing mode.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.privatebrowsing.autostart", true]],
  });

  win = OpenBrowserWindow();
  Assert.ok(
    PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow() in PBM should open a private window"
  );
  await BrowserTestUtils.closeWindow(win);

    nonPrivateWin.close();
  win = OpenBrowserWindow({ private: true });
  Assert.ok(
    PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow({private: true}) in PBM should open a private window"
  );
  await BrowserTestUtils.closeWindow(win);

    [
      {
        normal: "menu_newNavigator",
        private: "menu_newPrivateWindow",
        accesskey: true,
      },
      {
        normal: "appmenu_newNavigator",
        private: "appmenu_newPrivateWindow",
        accesskey: false,
      },
    ].forEach(function (menu) {
      let newWindow = privateWin.document.getElementById(menu.normal);
      let newPrivateWindow = privateWin.document.getElementById(menu.private);
      if (newWindow && newPrivateWindow) {
        ok(
          !newPrivateWindow.hidden,
          "New Private Window menu item should be hidden"
        );
        isnot(
          newWindow.label,
          newPrivateWindow.label,
          "New Window's label shouldn't be overwritten"
        );
        if (menu.accesskey) {
          isnot(
            newWindow.accessKey,
            newPrivateWindow.accessKey,
            "New Window's accessKey shouldn't be overwritten"
  win = OpenBrowserWindow({ private: false });
  Assert.ok(
    PrivateBrowsingUtils.isWindowPrivate(win),
    "OpenBrowserWindow({private: false}) in PBM should open a private window"
  );
        }
        isnot(
          newWindow.command,
          newPrivateWindow.command,
          "New Window's command shouldn't be overwritten"
  await BrowserTestUtils.closeWindow(win);

  await SpecialPowers.popPrefEnv();
});

/**
 * Check that the "new window" menu items have the expected properties.
 *
 * @param {Element} newWindowItem - The "new window" item to check.
 * @param {Element} privateWindowItem - The "new private window" item to check.
 * @param {Object} expect - The expected properties.
 * @param {boolean} expect.privateVisible - Whether we expect the private item
 *   to be visible or not.
 * @param {string} expect.newWindowL10nId - The expected string ID used by the
 *   "new window" item.
 * @param {string} expect.privateWindowL10nId - The expected string ID used by
 *   the "new private window" item.
 * @param {boolean} [useIsVisible=true] - Whether to test the "true" visibility
 *   of the item. Otherwise only the "hidden" attribute is checked.
 */
function assertMenuItems(
  newWindowItem,
  privateWindowItem,
  expect,
  useIsVisible = true
) {
  Assert.ok(newWindowItem);
  Assert.ok(privateWindowItem);

  if (useIsVisible) {
    Assert.ok(
      BrowserTestUtils.isVisible(newWindowItem),
      "New window item should be visible"
    );
  } else {
    // The application menu is not accessible on macOS, just check the hidden
    // attribute.
    Assert.ok(!newWindowItem.hidden, "New window item should be visible");
  }
    });

    is(
      privateWin.gBrowser.tabs[0].label,
      "New Private Tab",
      "New tabs in the private browsing windows should have 'New Private Tab' as the title."
  Assert.equal(
    newWindowItem.getAttribute("key"),
    "key_newNavigator",
    "New window item should use the same key"
  );
  Assert.equal(
    newWindowItem.getAttribute("data-l10n-id"),
    expect.newWindowL10nId
  );

    privateWin.close();
  if (!expect.privateVisible) {
    if (useIsVisible) {
      Assert.ok(
        BrowserTestUtils.isHidden(privateWindowItem),
        "Private window item should be hidden"
      );
    } else {
      Assert.ok(
        privateWindowItem.hidden,
        "Private window item should be hidden"
      );
    }
    // Don't check attributes since hidden.
  } else {
    if (useIsVisible) {
      Assert.ok(
        BrowserTestUtils.isVisible(privateWindowItem),
        "Private window item should be visible"
      );
    } else {
      Assert.ok(
        !privateWindowItem.hidden,
        "Private window item should be visible"
      );
    }
    Assert.equal(
      privateWindowItem.getAttribute("key"),
      "key_privatebrowsing",
      "Private window item should use the same key"
    );
    Assert.equal(
      privateWindowItem.getAttribute("data-l10n-id"),
      expect.privateWindowL10nId
    );
  }
}

    Services.prefs.setBoolPref("browser.privatebrowsing.autostart", true);
    privateWin = OpenBrowserWindow({ private: true });
    whenDelayedStartupFinished(privateWin, function () {
      [
/**
 * Check that a window has the expected "new window" items in the "File" and app
 * menus.
 *
 * @param {Window} win - The window to check.
 * @param {boolean} expectBoth - Whether we expect the window to contain both
 *   "new window" and "new private window" as separate controls.
 */
async function checkWindowMenus(win, expectBoth) {
  // Check the File menu.
  assertMenuItems(
    win.document.getElementById("menu_newNavigator"),
    win.document.getElementById("menu_newPrivateWindow"),
    {
          normal: "menu_newNavigator",
          private: "menu_newPrivateWindow",
          accessKey: true,
      privateVisible: expectBoth,
      // If in permanent private browsing, expect the new window item to use the
      // "New private window" string.
      newWindowL10nId: expectBoth
        ? "menu-file-new-window"
        : "menu-file-new-private-window",
      privateWindowL10nId: "menu-file-new-private-window",
    },
        {
          normal: "appmenu_newNavigator",
          private: "appmenu_newPrivateWindow",
          accessKey: false,
        },
      ].forEach(function (menu) {
        let newWindow = privateWin.document.getElementById(menu.normal);
        let newPrivateWindow = privateWin.document.getElementById(menu.private);
        if (newWindow && newPrivateWindow) {
          ok(
            newPrivateWindow.hidden,
            "New Private Window menu item should be hidden"
          );
          is(
            newWindow.label,
            newPrivateWindow.label,
            "New Window's label should be overwritten"
          );
          if (menu.accesskey) {
            is(
              newWindow.accessKey,
              newPrivateWindow.accessKey,
              "New Window's accessKey should be overwritten"
    // The file menu is difficult to open cross-platform, so we do not open it
    // for this test.
    false
  );

  // Open the app menu.
  let appMenuButton = win.document.getElementById("PanelUI-menu-button");
  let appMenu = win.document.getElementById("appMenu-popup");
  let menuShown = BrowserTestUtils.waitForEvent(appMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(appMenuButton, {}, win);
  await menuShown;

  // Check the app menu.
  assertMenuItems(
    win.document.getElementById("appMenu-new-window-button2"),
    win.document.getElementById("appMenu-new-private-window-button2"),
    {
      privateVisible: expectBoth,
      // If in permanent private browsing, expect the new window item to use the
      // "New private window" string.
      newWindowL10nId: expectBoth
        ? "appmenuitem-new-window"
        : "appmenuitem-new-private-window",
      privateWindowL10nId: "appmenuitem-new-private-window",
    }
          is(
            newWindow.command,
            newPrivateWindow.command,
            "New Window's command should be overwritten"
  );

  appMenu.hidePopup();
}

add_task(async function testNewWindowMenuItems() {
  // In non-private window, expect both menu items.
  let win = await BrowserTestUtils.openNewBrowserWindow({
    private: false,
  });
  await checkWindowMenus(win, true);
  Assert.equal(win.gBrowser.currentURI.spec, "about:blank");
  await BrowserTestUtils.closeWindow(win);

      is(
        privateWin.gBrowser.tabs[0].label,
        "New Tab",
        "Normal tab title is used also in the permanent private browsing mode."
      );
      privateWin.close();
      Services.prefs.clearUserPref("browser.privatebrowsing.autostart");
      finish();
  // In non-permanent private window, still expect both menu items.
  win = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  await checkWindowMenus(win, true);
  Assert.equal(win.gBrowser.currentURI.spec, "about:privatebrowsing");
  await BrowserTestUtils.closeWindow(win);

  // In permanent private browsing, expect only one menu item.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.privatebrowsing.autostart", true]],
  });

  win = await BrowserTestUtils.openNewBrowserWindow();
  await checkWindowMenus(win, false);
  Assert.equal(win.gBrowser.currentURI.spec, "about:blank");
  await BrowserTestUtils.closeWindow(win);

  await SpecialPowers.popPrefEnv();
});
}