summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_create.js
blob: a3b6e783313ad7548e2caac41a8638a5358fa631 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function test_create_options() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:robots"
  );
  gBrowser.selectedTab = tab;

  // TODO: Multiple windows.

  await SpecialPowers.pushPrefEnv({
    set: [
      // Using pre-loaded new tab pages interferes with onUpdated events.
      // It probably shouldn't.
      ["browser.newtab.preload", false],
      // Some test cases below load http and check the behavior of https-first.
      ["dom.security.https_first", true],
    ],
  });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],

      background: { page: "bg/background.html" },
    },

    files: {
      "bg/blank.html": `<html><head><meta charset="utf-8"></head></html>`,

      "bg/background.html": `<html><head>
        <meta charset="utf-8">
        <script src="background.js"></script>
      </head></html>`,

      "bg/background.js": function () {
        let activeTab;
        let activeWindow;

        function runTests() {
          const DEFAULTS = {
            index: 2,
            windowId: activeWindow,
            active: true,
            pinned: false,
            url: "about:newtab",
            // 'selected' is marked as unsupported in schema, so we've removed it.
            // For more details, see bug 1337509
            selected: undefined,
            mutedInfo: {
              muted: false,
              extensionId: undefined,
              reason: undefined,
            },
          };

          let tests = [
            {
              create: { url: "https://example.com/" },
              result: { url: "https://example.com/" },
            },
            {
              create: { url: "view-source:https://example.com/" },
              result: { url: "view-source:https://example.com/" },
            },
            {
              create: { url: "blank.html" },
              result: { url: browser.runtime.getURL("bg/blank.html") },
            },
            {
              create: { url: "https://example.com/", openInReaderMode: true },
              result: {
                url: `about:reader?url=${encodeURIComponent(
                  "https://example.com/"
                )}`,
              },
            },
            {
              create: {},
              result: { url: "about:newtab" },
            },
            {
              create: { active: false },
              result: { active: false },
            },
            {
              create: { active: true },
              result: { active: true },
            },
            {
              create: { pinned: true },
              result: { pinned: true, index: 0 },
            },
            {
              create: { pinned: true, active: true },
              result: { pinned: true, active: true, index: 0 },
            },
            {
              create: { pinned: true, active: false },
              result: { pinned: true, active: false, index: 0 },
            },
            {
              create: { index: 1 },
              result: { index: 1 },
            },
            {
              create: { index: 1, active: false },
              result: { index: 1, active: false },
            },
            {
              create: { windowId: activeWindow },
              result: { windowId: activeWindow },
            },
            {
              create: { index: 9999 },
              result: { index: 2 },
            },
            {
              // https-first redirects http to https.
              create: { url: "http://example.com/" },
              result: { url: "https://example.com/" },
            },
            {
              // https-first redirects http to https.
              create: { url: "view-source:http://example.com/" },
              result: { url: "view-source:https://example.com/" },
            },
            {
              // Despite https-first, the about:reader URL does not change.
              create: { url: "http://example.com/", openInReaderMode: true },
              result: {
                url: `about:reader?url=${encodeURIComponent(
                  "http://example.com/"
                )}`,
              },
            },
            {
              create: { muted: true },
              result: {
                mutedInfo: {
                  muted: true,
                  extensionId: browser.runtime.id,
                  reason: "extension",
                },
              },
            },
            {
              create: { muted: false },
              result: {
                mutedInfo: {
                  muted: false,
                  extensionId: undefined,
                  reason: undefined,
                },
              },
            },
          ];

          async function nextTest() {
            if (!tests.length) {
              browser.test.notifyPass("tabs.create");
              return;
            }

            let test = tests.shift();
            let expected = Object.assign({}, DEFAULTS, test.result);

            browser.test.log(
              `Testing tabs.create(${JSON.stringify(
                test.create
              )}), expecting ${JSON.stringify(test.result)}`
            );

            let updatedPromise = new Promise(resolve => {
              let onUpdated = (changedTabId, changed) => {
                if (changed.url) {
                  browser.tabs.onUpdated.removeListener(onUpdated);
                  resolve({ tabId: changedTabId, url: changed.url });
                }
              };
              browser.tabs.onUpdated.addListener(onUpdated);
            });

            let createdPromise = new Promise(resolve => {
              let onCreated = tab => {
                browser.test.assertTrue(
                  "id" in tab,
                  `Expected tabs.onCreated callback to receive tab object`
                );
                resolve();
              };
              browser.tabs.onCreated.addListener(onCreated);
            });

            let [tab] = await Promise.all([
              browser.tabs.create(test.create),
              createdPromise,
            ]);
            let tabId = tab.id;

            for (let key of Object.keys(expected)) {
              if (key === "url") {
                // FIXME: This doesn't get updated until later in the load cycle.
                continue;
              }

              if (key === "mutedInfo") {
                for (let key of Object.keys(expected.mutedInfo)) {
                  browser.test.assertEq(
                    expected.mutedInfo[key],
                    tab.mutedInfo[key],
                    `Expected value for tab.mutedInfo.${key}`
                  );
                }
              } else {
                browser.test.assertEq(
                  expected[key],
                  tab[key],
                  `Expected value for tab.${key}`
                );
              }
            }

            let updated = await updatedPromise;
            browser.test.assertEq(
              tabId,
              updated.tabId,
              `Expected value for tab.id`
            );
            browser.test.assertEq(
              expected.url,
              updated.url,
              `Expected value for tab.url`
            );

            await browser.tabs.remove(tabId);
            await browser.tabs.update(activeTab, { active: true });

            nextTest();
          }

          nextTest();
        }

        browser.tabs.query({ active: true, currentWindow: true }, tabs => {
          activeTab = tabs[0].id;
          activeWindow = tabs[0].windowId;

          runTests();
        });
      },
    },
  });

  await extension.startup();
  await extension.awaitFinish("tabs.create");
  await extension.unload();

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_create_with_popup() {
  const extension = ExtensionTestUtils.loadExtension({
    async background() {
      let normalWin = await browser.windows.create();
      let lastFocusedNormalWin = await browser.windows.getLastFocused({});
      browser.test.assertEq(
        lastFocusedNormalWin.id,
        normalWin.id,
        "The normal window is the last focused window."
      );
      let popupWin = await browser.windows.create({ type: "popup" });
      let lastFocusedPopupWin = await browser.windows.getLastFocused({});
      browser.test.assertEq(
        lastFocusedPopupWin.id,
        popupWin.id,
        "The popup window is the last focused window."
      );
      let newtab = await browser.tabs.create({});
      browser.test.assertEq(
        normalWin.id,
        newtab.windowId,
        "New tab was created in last focused normal window."
      );
      await Promise.all([
        browser.windows.remove(normalWin.id),
        browser.windows.remove(popupWin.id),
      ]);
      browser.test.sendMessage("complete");
    },
  });

  await extension.startup();
  await extension.awaitMessage("complete");
  await extension.unload();
});