summaryrefslogtreecommitdiffstats
path: root/browser/components/contextualidentity/test/browser/browser_reopenIn.js
blob: 9bda63b201c208fd98c0671f770eddc8a5977a13 (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
"use strict";

const TEST_HOST = "example.com";
const TEST_URL =
  "http://" +
  TEST_HOST +
  "/browser/browser/components/contextualidentity/test/browser/";

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;
}

async function openReopenMenuForTab(tab) {
  await openTabMenuFor(tab);

  let reopenItem = tab.ownerDocument.getElementById(
    "context_reopenInContainer"
  );
  ok(!reopenItem.hidden, "Reopen in Container item should be shown");

  let reopenMenu = reopenItem.getElementsByTagName("menupopup")[0];
  let reopenMenuShown = BrowserTestUtils.waitForEvent(reopenMenu, "popupshown");
  reopenItem.openMenu(true);
  await reopenMenuShown;

  return reopenMenu;
}

function checkMenuItem(reopenMenu, shown, hidden) {
  for (let id of shown) {
    ok(
      reopenMenu.querySelector(`menuitem[data-usercontextid="${id}"]`),
      `User context id ${id} should exist`
    );
  }
  for (let id of hidden) {
    ok(
      !reopenMenu.querySelector(`menuitem[data-usercontextid="${id}"]`),
      `User context id ${id} shouldn't exist`
    );
  }
}

function openTabInContainer(gBrowser, reopenMenu, id) {
  let tabPromise = BrowserTestUtils.waitForNewTab(gBrowser, TEST_URL, true);
  let menuitem = reopenMenu.querySelector(
    `menuitem[data-usercontextid="${id}"]`
  );
  reopenMenu.activateItem(menuitem);
  return tabPromise;
}

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

  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_URL,
  });
  ok(
    !tab.hasAttribute("usercontextid"),
    "Tab with No Container should be opened"
  );

  let reopenMenu = await openReopenMenuForTab(tab);
  checkMenuItem(reopenMenu, [1, 2, 3, 4], [0]);

  let containerTab = await openTabInContainer(gBrowser, reopenMenu, "1");

  is(
    containerTab.getAttribute("usercontextid"),
    "1",
    "Tab with UCI=1 should be opened"
  );
  is(
    containerTab.linkedBrowser.currentURI.spec,
    TEST_URL,
    "Same page should be opened"
  );

  reopenMenu = await openReopenMenuForTab(containerTab);
  checkMenuItem(reopenMenu, [0, 2, 3, 4], [1]);

  let noContainerTab = await openTabInContainer(gBrowser, reopenMenu, "0");

  ok(
    !noContainerTab.hasAttribute("usercontextid"),
    "Tab with no UCI should be opened"
  );
  is(
    noContainerTab.linkedBrowser.currentURI.spec,
    TEST_URL,
    "Same page should be opened"
  );

  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(containerTab);
  BrowserTestUtils.removeTab(noContainerTab);
});

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

  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: TEST_URL,
  });
  ok(
    !tab.hasAttribute("usercontextid"),
    "Tab with No Container should be opened"
  );

  let tabMenu = await openTabMenuFor(tab);
  let reopenItem = document.getElementById("context_reopenInContainer");
  ok(reopenItem.hidden, "Reopen in Container item should be hidden");

  // Close the tab menu.
  tabMenu.hidePopup();

  BrowserTestUtils.removeTab(tab);
});

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

  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser: privateWindow.gBrowser,
    url: TEST_URL,
  });
  ok(
    !tab.hasAttribute("usercontextid"),
    "Tab with No Container should be opened"
  );

  let tabMenu = await openTabMenuFor(tab);
  let reopenItem = privateWindow.document.getElementById(
    "context_reopenInContainer"
  );
  ok(reopenItem.hidden, "Reopen in Container item should be hidden");

  // Close the tab menu.
  tabMenu.hidePopup();

  await BrowserTestUtils.closeWindow(privateWindow);
});