summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_onUpdated.js
blob: a59fa21f8ae279057a15b6381d6850a2dc6c3366 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

requestLongerTimeout(2);

add_task(async function () {
  let win1 = await BrowserTestUtils.openNewBrowserWindow();

  await focusWindow(win1);

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
      content_scripts: [
        {
          matches: ["http://mochi.test/*/context_tabs_onUpdated_page.html"],
          js: ["content-script.js"],
          run_at: "document_start",
        },
      ],
    },

    background: function () {
      let pageURL =
        "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html";

      let expectedSequence = [
        { status: "loading" },
        { status: "loading", url: pageURL },
        { status: "complete" },
      ];
      let collectedSequence = [];

      browser.tabs.onUpdated.addListener(function (tabId, updatedInfo) {
        // onUpdated also fires with updatedInfo.faviconUrl, so explicitly
        // check for updatedInfo.status before recording the event.
        if ("status" in updatedInfo) {
          collectedSequence.push(updatedInfo);
        }
      });

      browser.runtime.onMessage.addListener(function () {
        if (collectedSequence.length !== expectedSequence.length) {
          browser.test.assertEq(
            JSON.stringify(expectedSequence),
            JSON.stringify(collectedSequence),
            "got unexpected number of updateInfo data"
          );
        } else {
          for (let i = 0; i < expectedSequence.length; i++) {
            browser.test.assertEq(
              expectedSequence[i].status,
              collectedSequence[i].status,
              "check updatedInfo status"
            );
            if (expectedSequence[i].url || collectedSequence[i].url) {
              browser.test.assertEq(
                expectedSequence[i].url,
                collectedSequence[i].url,
                "check updatedInfo url"
              );
            }
          }
        }

        browser.test.notifyPass("tabs.onUpdated");
      });

      browser.tabs.create({ url: pageURL });
    },
    files: {
      "content-script.js": `
        window.addEventListener("message", function(evt) {
          if (evt.data == "frame-updated") {
            browser.runtime.sendMessage("load-completed");
          }
        }, true);
      `,
    },
  });

  await Promise.all([
    extension.startup(),
    extension.awaitFinish("tabs.onUpdated"),
  ]);

  await extension.unload();

  await BrowserTestUtils.closeWindow(win1);
});

async function do_test_update(background, withPermissions = true) {
  let win1 = await BrowserTestUtils.openNewBrowserWindow();

  await focusWindow(win1);

  let manifest = {};
  if (withPermissions) {
    manifest.permissions = ["tabs", "http://mochi.test/"];
  }
  let extension = ExtensionTestUtils.loadExtension({ manifest, background });

  await extension.startup();
  await extension.awaitFinish("finish");

  await extension.unload();

  await BrowserTestUtils.closeWindow(win1);
}

add_task(async function test_pinned() {
  await do_test_update(function background() {
    // Create a new tab for testing update.
    browser.tabs.create({}, function (tab) {
      browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
        // Check callback
        browser.test.assertEq(tabId, tab.id, "Check tab id");
        browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
        if ("pinned" in changeInfo) {
          browser.test.assertTrue(changeInfo.pinned, "Check changeInfo.pinned");
          browser.tabs.onUpdated.removeListener(onUpdated);
          // Remove created tab.
          browser.tabs.remove(tabId);
          browser.test.notifyPass("finish");
        }
      });
      browser.tabs.update(tab.id, { pinned: true });
    });
  });
});

add_task(async function test_unpinned() {
  await do_test_update(function background() {
    // Create a new tab for testing update.
    browser.tabs.create({ pinned: true }, function (tab) {
      browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
        // Check callback
        browser.test.assertEq(tabId, tab.id, "Check tab id");
        browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
        if ("pinned" in changeInfo) {
          browser.test.assertFalse(
            changeInfo.pinned,
            "Check changeInfo.pinned"
          );
          browser.tabs.onUpdated.removeListener(onUpdated);
          // Remove created tab.
          browser.tabs.remove(tabId);
          browser.test.notifyPass("finish");
        }
      });
      browser.tabs.update(tab.id, { pinned: false });
    });
  });
});

add_task(async function test_url() {
  await do_test_update(function background() {
    // Create a new tab for testing update.
    browser.tabs.create({ url: "about:blank?initial_url=1" }, function (tab) {
      const expectedUpdatedURL = "about:blank?updated_url=1";

      browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
        // Wait for the tabs.onUpdated events related to the updated url (because
        // there is a good chance that we may still be receiving events related to
        // the browser.tabs.create API call above before we are able to start
        // loading the new url from the browser.tabs.update API call below).
        if ("url" in changeInfo && changeInfo.url === expectedUpdatedURL) {
          browser.test.assertEq(
            expectedUpdatedURL,
            changeInfo.url,
            "Got tabs.onUpdated event for the expected url"
          );
          browser.tabs.onUpdated.removeListener(onUpdated);
          // Remove created tab.
          browser.tabs.remove(tabId);
          browser.test.notifyPass("finish");
        }
        // Check callback
        browser.test.assertEq(tabId, tab.id, "Check tab id");
        browser.test.log("onUpdate: " + JSON.stringify(changeInfo));
      });

      browser.tabs.update(tab.id, { url: expectedUpdatedURL });
    });
  });
});

add_task(async function test_title() {
  await do_test_update(async function background() {
    const url =
      "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html";
    const tab = await browser.tabs.create({ url });

    browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
      browser.test.assertEq(tabId, tab.id, "Check tab id");
      browser.test.log(`onUpdated: ${JSON.stringify(changeInfo)}`);
      if ("title" in changeInfo && changeInfo.title === "New Message (1)") {
        browser.test.log("changeInfo.title is correct");
        browser.tabs.onUpdated.removeListener(onUpdated);
        browser.tabs.remove(tabId);
        browser.test.notifyPass("finish");
      }
    });

    browser.tabs.executeScript(tab.id, {
      code: "document.title = 'New Message (1)'",
    });
  });
});

add_task(async function test_without_tabs_permission() {
  await do_test_update(async function background() {
    const url =
      "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html";
    let tab = null;
    let count = 0;

    browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
      // An attention change can happen during tabs.create, so
      // we can't compare against tab yet.
      if (!("attention" in changeInfo)) {
        browser.test.assertEq(tabId, tab.id, "Check tab id");
      }
      browser.test.log(`onUpdated: ${JSON.stringify(changeInfo)}`);

      browser.test.assertFalse(
        "url" in changeInfo,
        "url should not be included without tabs permission"
      );
      browser.test.assertFalse(
        "favIconUrl" in changeInfo,
        "favIconUrl should not be included without tabs permission"
      );
      browser.test.assertFalse(
        "title" in changeInfo,
        "title should not be included without tabs permission"
      );

      if (changeInfo.status == "complete") {
        count++;
        if (count === 1) {
          browser.tabs.reload(tabId);
        } else {
          browser.test.log("Reload complete");
          browser.tabs.onUpdated.removeListener(onUpdated);
          browser.tabs.remove(tabId);
          browser.test.notifyPass("finish");
        }
      }
    });

    tab = await browser.tabs.create({ url });
  }, false /* withPermissions */);
});

add_task(async function test_onUpdated_after_onRemoved() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
    },
    async background() {
      const url =
        "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context_tabs_onUpdated_page.html";
      let removed = false;
      let tab;

      // If remove happens fast and we never receive onUpdated, that is ok, but
      // we never want to receive onUpdated after onRemoved.
      browser.tabs.onUpdated.addListener(function onUpdated(tabId, changeInfo) {
        if (!tab || tab.id !== tabId) {
          return;
        }
        browser.test.assertFalse(
          removed,
          "tab has not been removed before onUpdated"
        );
      });

      browser.tabs.onRemoved.addListener((tabId, removedInfo) => {
        if (!tab || tab.id !== tabId) {
          return;
        }
        removed = true;
        browser.test.notifyPass("onRemoved");
      });

      tab = await browser.tabs.create({ url });
      browser.tabs.remove(tab.id);
    },
  });
  await extension.startup();
  await extension.awaitFinish("onRemoved");
  await extension.unload();
});

// Regression test for Bug 1852391.
add_task(async function test_pin_discarded_tab() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
    },
    async background() {
      const url = "http://mochi.test:8888";
      const newTab = await browser.tabs.create({
        url,
        active: false,
        discarded: true,
      });
      browser.tabs.onUpdated.addListener(
        async (tabId, changeInfo) => {
          browser.test.assertEq(
            tabId,
            newTab.id,
            "Expect onUpdated to be fired for the expected tab"
          );
          browser.test.assertEq(
            changeInfo.pinned,
            true,
            "Expect pinned to be set to true"
          );
          await browser.tabs.remove(newTab.id);
          browser.test.notifyPass("onPinned");
        },
        { properties: ["pinned"] }
      );
      await browser.tabs.update(newTab.id, { pinned: true }).catch(err => {
        browser.test.fail(`Got unexpected rejection from tabs.update: ${err}`);
        browser.test.notifyFail("onPinned");
      });
    },
  });

  await extension.startup();
  await extension.awaitFinish("onPinned");
  await extension.unload();
});

add_task(forceGC);