summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_executeScript_no_create.js
blob: e8e1f1255f112a6aedeec4cae5304c25179afbd4 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function testExecuteScriptAtOnUpdated() {
  const BASE =
    "http://mochi.test:8888/browser/browser/components/extensions/test/browser/";
  const URL = BASE + "file_iframe_document.html";
  // This is a regression test for bug 1325830.
  // The bug (executeScript not completing any more) occurred when executeScript
  // was called early at the onUpdated event, unless the tabs.create method is
  // called. So this test does not use tabs.create to open new tabs.
  // Note that if this test is run together with other tests that do call
  // tabs.create, then this test case does not properly test the conditions of
  // the regression any more. To verify that the regression has been resolved,
  // this test must be run in isolation.

  function background() {
    // Using variables to prevent listeners from running more than once, instead
    // of removing the listener. This is to minimize any IPC, since the bug that
    // is being tested is sensitive to timing.
    let ignore = false;
    let url;
    browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
      if (ignore) {
        return;
      }
      if (url && changeInfo.status === "loading" && tab.url === url) {
        ignore = true;
        browser.tabs
          .executeScript(tabId, {
            code: "document.URL",
          })
          .then(
            results => {
              browser.test.assertEq(
                url,
                results[0],
                "Content script should run"
              );
              browser.test.notifyPass("executeScript-at-onUpdated");
            },
            error => {
              browser.test.fail(`Unexpected error: ${error} :: ${error.stack}`);
              browser.test.notifyFail("executeScript-at-onUpdated");
            }
          );
        // (running this log call after executeScript to minimize IPC between
        //  onUpdated and executeScript.)
        browser.test.log(`Found expected navigation to ${url}`);
      } else {
        // The bug occurs when executeScript is called before a tab is
        // initialized.
        browser.tabs.executeScript(tabId, { code: "" });
      }
    });
    browser.test.onMessage.addListener(testUrl => {
      url = testUrl;
      browser.test.sendMessage("open-test-tab");
    });
  }

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

  await extension.startup();
  extension.sendMessage(URL);
  await extension.awaitMessage("open-test-tab");
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL, true);

  await extension.awaitFinish("executeScript-at-onUpdated");

  await extension.unload();

  BrowserTestUtils.removeTab(tab);
});