summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_create_url.js
blob: 91cafa6e7e16310abc135eba90e2c92747883c16 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

async function runWithDisabledPrivateBrowsing(callback) {
  const { EnterprisePolicyTesting, PoliciesPrefTracker } =
    ChromeUtils.importESModule(
      "resource://testing-common/EnterprisePolicyTesting.sys.mjs"
    );

  PoliciesPrefTracker.start();
  await EnterprisePolicyTesting.setupPolicyEngineWithJson({
    policies: { DisablePrivateBrowsing: true },
  });

  try {
    await callback();
  } finally {
    await EnterprisePolicyTesting.setupPolicyEngineWithJson("");
    EnterprisePolicyTesting.resetRunOnceState();
    PoliciesPrefTracker.stop();
  }
}

add_task(async function test_urlbar_focus() {
  // Disable preloaded new tab because the urlbar is automatically focused when
  // a preloaded new tab is opened, while this test is supposed to test that the
  // implementation of tabs.create automatically focuses the urlbar of new tabs.
  await SpecialPowers.pushPrefEnv({
    set: [["browser.newtab.preload", false]],
  });

  const extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.tabs.onUpdated.addListener(function onUpdated(_, info, tab) {
        if (info.status === "complete" && tab.url !== "about:blank") {
          browser.test.sendMessage("complete");
          browser.tabs.onUpdated.removeListener(onUpdated);
        }
      });
      browser.test.onMessage.addListener(async (cmd, ...args) => {
        const result = await browser.tabs[cmd](...args);
        browser.test.sendMessage("result", result);
      });
    },
  });

  await extension.startup();

  // Test content is focused after opening a regular url
  extension.sendMessage("create", { url: "https://example.com" });
  const [tab1] = await Promise.all([
    extension.awaitMessage("result"),
    extension.awaitMessage("complete"),
  ]);

  is(
    document.activeElement.tagName,
    "browser",
    "Content focused after opening a web page"
  );

  extension.sendMessage("remove", tab1.id);
  await extension.awaitMessage("result");

  // Test urlbar is focused after opening an empty tab
  extension.sendMessage("create", {});
  const tab2 = await extension.awaitMessage("result");

  const active = document.activeElement;
  info(
    `Active element: ${active.tagName}, id: ${active.id}, class: ${active.className}`
  );

  const parent = active.parentNode;
  info(
    `Parent element: ${parent.tagName}, id: ${parent.id}, class: ${parent.className}`
  );

  info(`After opening an empty tab, gURLBar.focused: ${gURLBar.focused}`);

  is(active.tagName, "html:input", "Input element focused");
  is(active.id, "urlbar-input", "Urlbar focused");

  extension.sendMessage("remove", tab2.id);
  await extension.awaitMessage("result");

  await extension.unload();
});

add_task(async function default_url() {
  const extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      permissions: ["tabs"],
    },
    background() {
      function promiseNonBlankTab() {
        return new Promise(resolve => {
          browser.tabs.onUpdated.addListener(function listener(
            tabId,
            changeInfo,
            tab
          ) {
            if (changeInfo.status === "complete" && tab.url !== "about:blank") {
              browser.tabs.onUpdated.removeListener(listener);
              resolve(tab);
            }
          });
        });
      }

      browser.test.onMessage.addListener(
        async (msg, { incognito, expectedNewWindowUrl, expectedNewTabUrl }) => {
          browser.test.assertEq(
            "start",
            msg,
            `Start test, incognito=${incognito}`
          );

          let tabPromise = promiseNonBlankTab();
          let win;
          try {
            win = await browser.windows.create({ incognito });
            browser.test.assertEq(
              1,
              win.tabs.length,
              "Expected one tab in the new window."
            );
          } catch (e) {
            browser.test.assertEq(
              expectedNewWindowUrl,
              e.message,
              "Expected error"
            );
            browser.test.sendMessage("done");
            return;
          }
          let tab = await tabPromise;
          browser.test.assertEq(
            expectedNewWindowUrl,
            tab.url,
            "Expected default URL of new window"
          );

          tabPromise = promiseNonBlankTab();
          await browser.tabs.create({ windowId: win.id });
          tab = await tabPromise;
          browser.test.assertEq(
            expectedNewTabUrl,
            tab.url,
            "Expected default URL of new tab"
          );

          await browser.windows.remove(win.id);
          browser.test.sendMessage("done");
        }
      );
    },
  });

  await extension.startup();

  extension.sendMessage("start", {
    incognito: false,
    expectedNewWindowUrl: "about:home",
    expectedNewTabUrl: "about:newtab",
  });
  await extension.awaitMessage("done");
  extension.sendMessage("start", {
    incognito: true,
    expectedNewWindowUrl: "about:privatebrowsing",
    expectedNewTabUrl: "about:privatebrowsing",
  });
  await extension.awaitMessage("done");

  info("Testing with multiple homepages.");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.startup.homepage", "about:robots|about:blank|about:home"]],
  });
  extension.sendMessage("start", {
    incognito: false,
    expectedNewWindowUrl: "about:robots",
    expectedNewTabUrl: "about:newtab",
  });
  await extension.awaitMessage("done");
  extension.sendMessage("start", {
    incognito: true,
    expectedNewWindowUrl: "about:privatebrowsing",
    expectedNewTabUrl: "about:privatebrowsing",
  });
  await extension.awaitMessage("done");
  await SpecialPowers.popPrefEnv();

  info("Testing with perma-private browsing mode.");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.privatebrowsing.autostart", true]],
  });
  extension.sendMessage("start", {
    incognito: false,
    expectedNewWindowUrl: "about:home",
    expectedNewTabUrl: "about:newtab",
  });
  await extension.awaitMessage("done");
  extension.sendMessage("start", {
    incognito: true,
    expectedNewWindowUrl: "about:home",
    expectedNewTabUrl: "about:newtab",
  });
  await extension.awaitMessage("done");
  await SpecialPowers.popPrefEnv();

  info("Testing with disabled private browsing mode.");
  await runWithDisabledPrivateBrowsing(async () => {
    extension.sendMessage("start", {
      incognito: false,
      expectedNewWindowUrl: "about:home",
      expectedNewTabUrl: "about:newtab",
    });
    await extension.awaitMessage("done");
    extension.sendMessage("start", {
      incognito: true,
      expectedNewWindowUrl:
        "`incognito` cannot be used if incognito mode is disabled",
    });
    await extension.awaitMessage("done");
  });

  await extension.unload();
});