summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_multiselect_tabs_duplicate.js
blob: d9f5e586696b94fc7e86bfcf36bc01633c696f55 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
async function openTabMenuFor(tab) {
  let tabMenu = tab.ownerDocument.getElementById("tabContextMenu");

  let tabMenuShown = BrowserTestUtils.waitForEvent(tabMenu, "popupshown");
  EventUtils.synthesizeMouseAtCenter(
    tab,
    { type: "contextmenu" },
    tab.ownerGlobal
  );
  await tabMenuShown;

  return tabMenu;
}

add_task(async function test() {
  let originalTab = gBrowser.selectedTab;
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let tab1 = await addTab("http://example.com/1");
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let tab2 = await addTab("http://example.com/2");
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  let tab3 = await addTab("http://example.com/3");

  let menuItemDuplicateTab = document.getElementById("context_duplicateTab");
  let menuItemDuplicateTabs = document.getElementById("context_duplicateTabs");

  is(gBrowser.multiSelectedTabsCount, 0, "Zero multiselected tabs");

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  await triggerClickOn(tab2, { ctrlKey: true });

  ok(tab1.multiselected, "Tab1 is multiselected");
  ok(tab2.multiselected, "Tab2 is multiselected");
  ok(!tab3.multiselected, "Tab3 is not multiselected");

  // Check the context menu with a multiselected tabs
  updateTabContextMenu(tab2);
  is(menuItemDuplicateTab.hidden, true, "Duplicate Tab is hidden");
  is(menuItemDuplicateTabs.hidden, false, "Duplicate Tabs is visible");

  // Check the context menu with a non-multiselected tab
  updateTabContextMenu(tab3);
  is(menuItemDuplicateTab.hidden, false, "Duplicate Tab is visible");
  is(menuItemDuplicateTabs.hidden, true, "Duplicate Tabs is hidden");

  let newTabOpened = BrowserTestUtils.waitForNewTab(
    gBrowser,
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    "http://example.com/3",
    true
  );
  {
    let menu = await openTabMenuFor(tab3);
    menu.activateItem(menuItemDuplicateTab);
  }
  let tab4 = await newTabOpened;

  is(
    getUrl(tab4),
    getUrl(tab3),
    "tab4 should have same URL as tab3, where it was duplicated from"
  );

  // Selection should be cleared after duplication
  ok(!tab1.multiselected, "Tab1 is not multiselected");
  ok(!tab2.multiselected, "Tab2 is not multiselected");
  ok(!tab3.multiselected, "Tab3 is not multiselected");
  ok(!tab4.multiselected, "Tab4 is not multiselected");

  is(gBrowser.selectedTab._tPos, tab4._tPos, "Tab4 should be selected");

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  await triggerClickOn(tab3, { ctrlKey: true });

  ok(tab1.multiselected, "Tab1 is multiselected");
  ok(!tab2.multiselected, "Tab2 is not multiselected");
  ok(tab3.multiselected, "Tab3 is multiselected");
  ok(!tab4.multiselected, "Tab4 is not multiselected");

  // Check the context menu with a non-multiselected tab
  updateTabContextMenu(tab3);
  is(menuItemDuplicateTab.hidden, true, "Duplicate Tab is hidden");
  is(menuItemDuplicateTabs.hidden, false, "Duplicate Tabs is visible");

  // 7 tabs because there was already one open when the test starts.
  // Can't use BrowserTestUtils.waitForNewTab because waitForNewTab only works
  // with one tab at a time.
  let newTabsOpened = TestUtils.waitForCondition(
    () => gBrowser.visibleTabs.length == 7,
    "Wait for two tabs to get created"
  );
  {
    let menu = await openTabMenuFor(tab3);
    menu.activateItem(menuItemDuplicateTabs);
  }
  await newTabsOpened;
  info("Two tabs opened");

  await TestUtils.waitForCondition(() => {
    return (
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      getUrl(gBrowser.visibleTabs[4]) == "http://example.com/1" &&
      // eslint-disable-next-line @microsoft/sdl/no-insecure-url
      getUrl(gBrowser.visibleTabs[5]) == "http://example.com/3"
    );
  });

  is(
    originalTab,
    gBrowser.visibleTabs[0],
    "Original tab should still be first"
  );
  is(tab1, gBrowser.visibleTabs[1], "tab1 should still be second");
  is(tab2, gBrowser.visibleTabs[2], "tab2 should still be third");
  is(tab3, gBrowser.visibleTabs[3], "tab3 should still be fourth");
  is(
    getUrl(gBrowser.visibleTabs[4]),
    getUrl(tab1),
    "the first duplicated tab should be placed next to tab3 and have URL of tab1"
  );
  is(
    getUrl(gBrowser.visibleTabs[5]),
    getUrl(tab3),
    "the second duplicated tab should have URL of tab3 and maintain same order"
  );
  is(
    tab4,
    gBrowser.visibleTabs[6],
    "tab4 should now be the still be the seventh tab"
  );

  let tabsToClose = gBrowser.visibleTabs.filter(t => t != originalTab);
  for (let tab of tabsToClose) {
    BrowserTestUtils.removeTab(tab);
  }
});