summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/utilityOverlay/browser_openWebLinkIn.js
blob: 53f11e49c09299e1ad52524d0ddc1cb6cd425882 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

// Stolen from https://searchfox.org/mozilla-central/source/browser/base/content/test/popups/browser_popup_close_main_window.js
// When calling this function, the main window where the test runs will be
// hidden from various APIs, so that they won't be able to find it. This makes
// it possible to test some behaviors when only private windows are present.
function concealMainWindow() {
  info("Concealing main window.");
  let oldWinType = document.documentElement.getAttribute("windowtype");
  // Check if we've already done this to allow calling multiple times:
  if (oldWinType != "navigator:testrunner") {
    // Make the main test window not count as a browser window any longer
    document.documentElement.setAttribute("windowtype", "navigator:testrunner");
    BrowserWindowTracker.untrackForTestsOnly(window);

    registerCleanupFunction(() => {
      info("Unconcealing the main window in the cleanup phase.");
      BrowserWindowTracker.track(window);
      document.documentElement.setAttribute("windowtype", oldWinType);
    });
  }
}

const EXAMPLE_URL = "https://example.org/";
add_task(async function test_open_tab() {
  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    EXAMPLE_URL
  );
  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    openWebLinkIn(EXAMPLE_URL, "tab", {
      resolveOnContentBrowserCreated,
    })
  );

  const tab = await waitForTabPromise;
  is(
    contentBrowser,
    tab.linkedBrowser,
    "We get a content browser that is the tab's linked browser as the result of opening a tab"
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_open_window() {
  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow();
  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    openWebLinkIn(EXAMPLE_URL, "window", {
      resolveOnContentBrowserCreated,
    })
  );

  const win = await waitForWindowPromise;
  is(
    contentBrowser,
    win.gBrowser.selectedBrowser,
    "We get the content browser for the newly opened window as a result of opening a window"
  );

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_open_private_window() {
  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow();
  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    openWebLinkIn(EXAMPLE_URL, "window", {
      resolveOnContentBrowserCreated,
      private: true,
    })
  );

  const win = await waitForWindowPromise;
  ok(
    PrivateBrowsingUtils.isBrowserPrivate(win),
    "The new window is a private window."
  );
  is(
    contentBrowser,
    win.gBrowser.selectedBrowser,
    "We get the content browser for the newly opened window as a result of opening a private window"
  );

  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_open_private_tab_from_private_window() {
  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
    privateWindow.gBrowser,
    EXAMPLE_URL
  );
  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
      resolveOnContentBrowserCreated,
    })
  );

  const tab = await waitForTabPromise;
  ok(
    PrivateBrowsingUtils.isBrowserPrivate(tab),
    "The new tab was opened in a private browser."
  );
  is(
    contentBrowser,
    tab.linkedBrowser,
    "We get a content browser that is the tab's linked browser as the result of opening a private tab in a private window"
  );

  await BrowserTestUtils.closeWindow(privateWindow);
});

add_task(async function test_open_non_private_tab_from_private_window() {
  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  // Opening this tab from the private window should open it in the non-private window.
  const waitForTabPromise = BrowserTestUtils.waitForNewTab(
    gBrowser,
    EXAMPLE_URL
  );

  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
      forceNonPrivate: true,
      resolveOnContentBrowserCreated,
    })
  );

  const nonPrivateTab = await waitForTabPromise;
  ok(
    !PrivateBrowsingUtils.isBrowserPrivate(nonPrivateTab),
    "The new window isn't a private window."
  );
  is(
    contentBrowser,
    nonPrivateTab.linkedBrowser,
    "We get a content browser that is the non private tab's linked browser as the result of opening a non private tab from a private window"
  );

  BrowserTestUtils.removeTab(nonPrivateTab);
  await BrowserTestUtils.closeWindow(privateWindow);
});

add_task(async function test_open_non_private_tab_from_only_private_window() {
  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  // In this test we'll hide the existing window from window trackers, because
  // we want to test that we open a new window when there's only a private
  // window.
  concealMainWindow();

  // Opening this tab from the private window should open it in a new non-private window.
  const waitForWindowPromise = BrowserTestUtils.waitForNewWindow({
    url: EXAMPLE_URL,
  });
  const contentBrowser = await new Promise(resolveOnContentBrowserCreated =>
    privateWindow.openWebLinkIn(EXAMPLE_URL, "tab", {
      forceNonPrivate: true,
      resolveOnContentBrowserCreated,
    })
  );

  const nonPrivateWindow = await waitForWindowPromise;
  ok(
    !PrivateBrowsingUtils.isBrowserPrivate(nonPrivateWindow),
    "The new window isn't a private window."
  );
  is(
    contentBrowser,
    nonPrivateWindow.gBrowser.selectedBrowser,
    "We get the content browser for the newly opened non private window from a private window, as a result of opening a non private tab."
  );

  await BrowserTestUtils.closeWindow(nonPrivateWindow);
  await BrowserTestUtils.closeWindow(privateWindow);
});