Commit bd217ea8 authored by Jonathan Sudiaman's avatar Jonathan Sudiaman
Browse files

Bug 1801734 - Change withFirefoxView helper so it doesn't open a new window by...

Bug 1801734 - Change withFirefoxView helper so it doesn't open a new window by default r=fxview-reviewers,sfoster

Updates `withFirefoxView()` signature to accept a boolean flag indicating whether a new window should be opened, rather than having to pass in a `Window` object or opening a new window by default. The new default behavior is to use the global `window` by default. Update tests to only use this setting if it is needed to pass.

https://treeherder.mozilla.org/jobs?revision=f49e65c14c5eacdaedfb86c81d61450e3274a7c5&repo=try

Differential Revision: https://phabricator.services.mozilla.com/D173014
parent 22666f82
Loading
Loading
Loading
Loading
+21 −11
Original line number Diff line number Diff line
@@ -27,10 +27,6 @@ async function assertFirefoxViewTabSelected(win) {
}

async function openFirefoxViewTab(win) {
  Assert.ok(
    !win.FirefoxViewHandler.tab,
    "Firefox View tab doesn't exist prior to clicking the button"
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#firefox-view-button",
    { type: "mousedown" },
@@ -55,15 +51,29 @@ function closeFirefoxViewTab(win) {
  );
}

/**
 * Run a task with Firefox View open.
 *
 * @param {Object} options
 *   Options object.
 * @param {boolean} [options.openNewWindow]
 *   Whether to run the task in a new window. If false, the current window will
 *   be used.
 * @param {boolean} [options.resetFlowManager]
 *   Whether to reset the internal state of TabsSetupFlowManager before running
 *   the task.
 * @param {function(MozBrowser)} taskFn
 *   The task to run. It can be asynchronous.
 * @returns {any}
 *   The value returned by the task.
 */
async function withFirefoxView(
  { resetFlowManager = true, win = null },
  { openNewWindow = false, resetFlowManager = true },
  taskFn
) {
  let shouldCloseWin = false;
  if (!win) {
    win = await BrowserTestUtils.openNewBrowserWindow();
    shouldCloseWin = true;
  }
  const win = openNewWindow
    ? await BrowserTestUtils.openNewBrowserWindow()
    : Services.wm.getMostRecentBrowserWindow();
  if (resetFlowManager) {
    const { TabsSetupFlowManager } = ChromeUtils.importESModule(
      "resource:///modules/firefox-view-tabs-setup-manager.sys.mjs"
@@ -92,7 +102,7 @@ async function withFirefoxView(
    );
  }
  await win.SpecialPowers.popPrefEnv();
  if (shouldCloseWin) {
  if (openNewWindow) {
    await BrowserTestUtils.closeWindow(win);
  }
  return result;
+3 −3
Original line number Diff line number Diff line
@@ -94,7 +94,7 @@ add_task(async function aria_attributes() {
});

add_task(async function load_opens_new_tab() {
  await withFirefoxView({}, async browser => {
  await withFirefoxView({ openNewWindow: true }, async browser => {
    let win = browser.ownerGlobal;
    ok(win.FirefoxViewHandler.tab.selected, "Firefox View tab is selected");
    win.gURLBar.focus();
@@ -117,7 +117,7 @@ add_task(async function load_opens_new_tab() {
});

add_task(async function homepage_new_tab() {
  await withFirefoxView({}, async browser => {
  await withFirefoxView({ openNewWindow: true }, async browser => {
    let win = browser.ownerGlobal;
    ok(win.FirefoxViewHandler.tab.selected, "Firefox View tab is selected");
    let newTabOpened = BrowserTestUtils.waitForEvent(
@@ -265,7 +265,7 @@ add_task(async function testFirstTabFocusableWhenFxViewOpen() {

// Test that Firefox View tab is not multiselectable
add_task(async function testFxViewNotMultiselect() {
  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    let win = browser.ownerGlobal;
    Assert.ok(
      win.FirefoxViewHandler.tab.selected,
+4 −2
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ add_task(async function test_keyboard_focus() {
    set: [["accessibility.tabfocus", 7]],
  });

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    const sandbox = setupRecentDeviceListMocks();
@@ -57,7 +57,9 @@ add_task(async function test_keyboard_focus() {

    window.FirefoxViewHandler.openTab();

    let recentlyClosedEle = document.querySelector(".closed-tab-li-main");
    let recentlyClosedEle = await TestUtils.waitForCondition(() =>
      document.querySelector(".closed-tab-li-main")
    );
    document.querySelectorAll(".page-section-header")[1].focus();

    EventUtils.synthesizeKey("KEY_Tab");
+44 −50
Original line number Diff line number Diff line
@@ -6,10 +6,8 @@
/**
 * The recently closed tab list is populated on a per-window basis.
 *
 * By default, the withFirefoxView helper opens a new window.
 * When using this helper for the tests in this file, we pass a
 * { win: window } option to skip that step and open fx view in
 * the current window. This ensures that the add_new_tab, close_tab,
 * By default, the withFirefoxView helper opens fx view in the current window.
 * This ensures that the add_new_tab, close_tab,
 * and open_then_close functions are creating sessionstore entries
 * associated with the correct window where the tests are run.
 */
@@ -55,7 +53,7 @@ async function dismiss_tab(tab, content) {
add_task(async function test_empty_list() {
  clearHistory();

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    let container = document.querySelector("#collapsible-tabs-container");
    ok(
@@ -133,7 +131,7 @@ add_task(async function test_list_ordering() {
  await close_tab(tab1);
  await closedObjectsChanged();

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const tabsList = document.querySelector("ol.closed-tabs-list");
    await BrowserTestUtils.waitForMutationCondition(
@@ -236,7 +234,7 @@ add_task(async function test_max_list_items() {
  // above.
  let mockMaxTabsLength = 3;

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    // override this value for testing purposes
@@ -332,16 +330,13 @@ add_task(async function test_time_updates_correctly() {
    "Closed tab count after setting browser state"
  );

  await withFirefoxView(
    {
      win: window,
    },
    async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const numOfListItems = document.querySelector("ol.closed-tabs-list")
      .children.length;
      const lastListItem = document.querySelector("ol.closed-tabs-list")
        .children[numOfListItems - 1];
    const lastListItem = document.querySelector("ol.closed-tabs-list").children[
      numOfListItems - 1
    ];
    const timeLabel = lastListItem.querySelector("span.closed-tab-li-time");
    let initialTimeText = timeLabel.textContent;
    Assert.stringContains(
@@ -367,8 +362,7 @@ add_task(async function test_time_updates_correctly() {
    );

    await SpecialPowers.popPrefEnv();
    }
  );
  });
  // Cleanup recently closed tab data.
  clearHistory();
});
@@ -393,7 +387,7 @@ add_task(async function test_list_maintains_focus_when_restoring_tab() {
  await open_then_close(URLs[1]);
  await open_then_close(URLs[2]);

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    let gBrowser = browser.getTabBrowser();
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
@@ -414,7 +408,7 @@ add_task(async function test_list_maintains_focus_when_restoring_tab() {

  clearHistory();
  await open_then_close(URLs[2]);
  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    let gBrowser = browser.getTabBrowser();
    const { document } = browser.contentWindow;
    let expectedFocusedElement = document.getElementById(
@@ -443,7 +437,7 @@ add_task(async function test_switch_before_closing() {

  const INITIAL_URL = "https://example.org/iwilldisappear";
  const FINAL_URL = "https://example.com/ishouldappear";
  await withFirefoxView({ win: window }, async function(browser) {
  await withFirefoxView({}, async function(browser) {
    let gBrowser = browser.getTabBrowser();
    let newTab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
@@ -496,7 +490,7 @@ add_task(async function test_alt_click_no_launch() {

  await open_then_close(URLs[0]);

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    let gBrowser = browser.getTabBrowser();
    let originalTabsLength = gBrowser.tabs.length;
    await BrowserTestUtils.synthesizeMouseAtCenter(
@@ -662,7 +656,7 @@ add_task(async function test_dismiss_tab() {
  );
  await clearAllParentTelemetryEvents();

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    const closedObjectsChanged = () =>
@@ -801,7 +795,7 @@ add_task(async function test_button_role() {
    "Closed tab count after purging session history"
  );

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    clearHistory();
+4 −4
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ add_task(async function test_keyboard_navigation() {
  setupCompleteStub.returns(true);
  await open_then_close(URLs[0]);

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
@@ -109,7 +109,7 @@ add_task(async function test_keyboard_navigation() {
  await open_then_close(URLs[0]);
  await open_then_close(URLs[1]);

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
@@ -158,7 +158,7 @@ add_task(async function test_keyboard_navigation() {
  await open_then_close(URLs[1]);
  await open_then_close(URLs[2]);

  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
@@ -211,7 +211,7 @@ add_task(async function test_dismiss_tab_keyboard() {
    0,
    "Closed tab count after purging session history"
  );
  await withFirefoxView({ win: window }, async browser => {
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    await open_then_close(URLs[0]);
Loading