summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_history_recently_closed_middleclick.js
blob: 309554ce237a8b18063cc27b94adc052fc5bb4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Verifies that middle-clicking "Recently Closed Tabs" in both history
// menus works as expected.

const URLS = [
  "http://example.com/",
  "http://example.org/",
  "http://example.net/",
];

async function setupTest() {
  // Navigate the initial tab to ensure that it won't be reused for the tab
  // that will be reopened.
  let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  BrowserTestUtils.loadURIString(
    gBrowser.selectedBrowser,
    "https://example.com"
  );
  await loadPromise;

  // Populate the recently closed tabs list.
  for (let url of URLS) {
    await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
  }
  for (let i = 0; i < URLS.length; i++) {
    gBrowser.removeTab(gBrowser.selectedTab);
  }

  return gBrowser.tabs.length;
}

add_task(async function testMenubar() {
  if (AppConstants.platform === "macosx") {
    ok(true, "Can't open menu items on macOS");
    return;
  }

  let nOpenTabs = await setupTest();

  // Open the "History" menu.
  let menu = document.getElementById("history-menu");
  let popupPromise = BrowserTestUtils.waitForEvent(menu, "popupshown");
  menu.open = true;
  await popupPromise;
  ok(true, "Opened 'History' menu");

  // Open the "Recently Closed Tabs" submenu.
  let undoMenu = document.getElementById("historyUndoMenu");
  popupPromise = BrowserTestUtils.waitForEvent(undoMenu, "popupshown");
  undoMenu.open = true;
  let popupEvent = await popupPromise;
  ok(true, "Opened 'Recently Closed Tabs' menu");

  // And now middle-click the first item in that menu, and ensure that we're
  // only opening a single new tab.
  let menuitems = popupEvent.target.querySelectorAll("menuitem");
  let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
  popupEvent.target.activateItem(menuitems[0], { button: 1 });

  let newTab = await newTabPromise;
  is(newTab.linkedBrowser.currentURI.spec, URLS[0], "Opened correct URL");
  is(gBrowser.tabs.length, nOpenTabs + 1, "Only opened 1 new tab");

  gBrowser.removeTab(newTab);
});

add_task(async function testHistoryPanel() {
  let nOpenTabs = await setupTest();

  // Setup history panel.
  CustomizableUI.addWidgetToArea(
    "history-panelmenu",
    CustomizableUI.AREA_FIXED_OVERFLOW_PANEL
  );
  registerCleanupFunction(() => CustomizableUI.reset());
  await openHistoryPanel();

  // Open the "Recently closed tabs" panel.
  let recentlyClosedTabs = document.getElementById("appMenuRecentlyClosedTabs");
  recentlyClosedTabs.click();

  let recentlyClosedTabsPanel = document.getElementById(
    "appMenu-library-recentlyClosedTabs"
  );
  await BrowserTestUtils.waitForEvent(recentlyClosedTabsPanel, "ViewShown");
  ok(true, "Opened 'Recently closed tabs' panel");

  let panelBody = recentlyClosedTabsPanel.querySelector(".panel-subview-body");
  let toolbarButtons = panelBody.querySelectorAll("toolbarbutton");

  // Middle-click the first toolbar button in the panel.
  let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, null, true);
  EventUtils.sendMouseEvent(
    { type: "click", button: 1 },
    toolbarButtons[0],
    window
  );

  let newTab = await newTabPromise;
  is(newTab.linkedBrowser.currentURI.spec, URLS[0], "Opened correct URL");
  is(gBrowser.tabs.length, nOpenTabs + 1, "Only opened 1 new tab");

  gBrowser.removeTab(newTab);
});