summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_newtabButton.js
blob: 02f50f27d61b3cb8553ae503f237b28d2f74a7b9 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";

// Testing that when the user opens the add tab menu and clicks menu items
// the correct context id is opened

function findPopup(browser = gBrowser) {
  return browser.tabContainer.querySelector(".new-tab-popup");
}

function findContextPopup() {
  return document.querySelector("#new-tab-button-popup");
}

add_task(async function test_containers_no_left_click() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      ["privacy.userContext.newTabContainerOnLeftClick.enabled", false],
    ],
  });

  let newTabButton = gBrowser.tabContainer.newTabButton;
  ok(newTabButton, "New tab button exists");
  ok(!newTabButton.hidden, "New tab button is visible");
  let popup = findPopup();
  ok(popup, "new tab should have a popup");

  // Test context menu
  let contextMenu = findContextPopup();
  is(contextMenu.state, "closed", "Context menu is initally closed.");

  let popupshown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");

  let target = gBrowser.tabContainer.newTabButton;
  EventUtils.synthesizeMouseAtCenter(target, { type: "contextmenu" });
  await popupshown;

  is(contextMenu.state, "open", "Context menu is open.");
  let contextIdItems = contextMenu.querySelectorAll("menuitem");
  // 4 + default + manage containers
  is(contextIdItems.length, 6, "Has 6 menu items");
  contextMenu.hidePopup();
});

add_task(async function test_containers_with_left_click() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      ["privacy.userContext.newTabContainerOnLeftClick.enabled", true],
    ],
  });

  let newTabButton = gBrowser.tabContainer.newTabButton;
  ok(newTabButton, "New tab button exists");
  ok(!newTabButton.hidden, "New tab button is visible");

  await BrowserTestUtils.waitForCondition(
    () => !!findPopup(),
    "Wait for popup to exist"
  );
  let popup = findPopup();

  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`);

    // 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 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`);
    }
    BrowserTestUtils.removeTab(tab);
  }

  // Test context menu
  let contextMenu = findContextPopup();
  is(contextMenu.state, "closed", "Context menu is initally closed.");

  let popupshown = BrowserTestUtils.waitForEvent(contextMenu, "popupshown");

  let target = gBrowser.tabContainer.newTabButton;
  EventUtils.synthesizeMouseAtCenter(target, { type: "contextmenu" });
  await popupshown;

  is(contextMenu.state, "open", "Context menu is open.");
  contextIdItems = contextMenu.querySelectorAll("menuitem");
  // 4 + default + manage containers
  is(contextIdItems.length, 6, "Has 6 menu items");
  contextMenu.hidePopup();
});

add_task(async function test_no_containers_no_left_click() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", false],
      ["privacy.userContext.newTabContainerOnLeftClick.enabled", false],
    ],
  });

  let newTabButton = gBrowser.tabContainer.newTabButton;
  ok(newTabButton, "New tab button exists");
  ok(!newTabButton.hidden, "New tab button is visible");
  let popup = findPopup();
  ok(!popup, "new tab should not have a popup");

  // Test context menu
  let contextMenu = findContextPopup();
  let target = gBrowser.tabContainer.newTabButton;
  EventUtils.synthesizeMouseAtCenter(target, { type: "contextmenu" });
  is(contextMenu.state, "closed", "Context menu does not open.");
});

add_task(async function test_private_mode() {
  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  let privateDocument = privateWindow.document;
  let { tabContainer } = privateWindow.gBrowser;
  let newTab = tabContainer.newTabButton;
  let newTab2 = privateDocument.getElementById("new-tab-button");
  // Check to ensure we are talking about the right button
  ok(!!newTab.clientWidth, "new tab button should not be hidden");
  ok(!newTab2.clientWidth, "overflow new tab button should be hidden");
  let popup = findPopup(privateWindow.gBrowser);
  ok(!popup, "new tab should not have a popup");
  await BrowserTestUtils.closeWindow(privateWindow);
});

add_task(async function test_opening_container_tab_context() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["privacy.userContext.enabled", true],
      ["privacy.userContext.newTabContainerOnLeftClick.enabled", true],
    ],
  });

  let newTabButton = gBrowser.tabContainer.newTabButton;
  ok(newTabButton, "New tab button exists");
  ok(!newTabButton.hidden, "New tab button is visible");

  let popup = findContextPopup();

  let popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown");
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(popup, "popuphidden");
  EventUtils.synthesizeMouseAtCenter(newTabButton, { type: "contextmenu" });
  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: "contextmenu" });

    await popupShownPromise;
    let contextIdItem = popup.querySelector(
      `menuitem[data-usercontextid="${i}"]`
    );

    ok(contextIdItem, `User context id ${i} exists`);

    // 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"
    );
    popup.activateItem(contextIdItem);

    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`);
    }
    BrowserTestUtils.removeTab(tab);
  }
});