summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_errors.js
blob: a9daa9d7aba15b8c7d0b012da5a5fc2b9f5018fa (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
"use strict";

const server = createHttpServer();
server.registerDirectory("/data/", do_get_file("data"));

const BASE_URL = `http://localhost:${server.identity.primaryPort}/data`;
const TEST_URL_1 = `${BASE_URL}/file_sample.html`;
const TEST_URL_2 = `${BASE_URL}/file_content_script_errors.html`;

// ExtensionContent.jsm needs to know when it's running from xpcshell,
// to use the right timeout for content scripts executed at document_idle.
ExtensionTestUtils.mockAppInfo();

add_task(async function test_cached_contentscript_on_document_start() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        // Use distinct content scripts as some will throw and would prevent executing the next script
        {
          matches: ["http://*/*/file_content_script_errors.html"],
          js: ["script1.js"],
          run_at: "document_start",
        },
        {
          matches: ["http://*/*/file_content_script_errors.html"],
          js: ["script2.js"],
          run_at: "document_start",
        },
        {
          matches: ["http://*/*/file_content_script_errors.html"],
          js: ["script3.js"],
          run_at: "document_start",
        },
        {
          matches: ["http://*/*/file_content_script_errors.html"],
          js: ["script4.js"],
          run_at: "document_start",
        },
        {
          matches: ["http://*/*/file_content_script_errors.html"],
          js: ["script5.js"],
          run_at: "document_start",
        },
      ],
    },

    files: {
      "script1.js": `
        throw new Error("Object exception");
      `,
      "script2.js": `
        throw "String exception";
      `,
      "script3.js": `
        undefinedSymbol();
      `,
      "script4.js": `
        )
      `,
      "script5.js": `
        Promise.reject("rejected promise");

        (async () => {
          /* make the async, really async */
          await new Promise(r => setTimeout(r, 0));
          throw new Error("async function exception");
        })();

        setTimeout(() => {
          asyncUndefinedSymbol();
        });

        /* Use a delay in order to resume test execution after these async errors */
        setTimeout(() => {
          browser.test.sendMessage("content-script-loaded");
        }, 500);
      `,
    },
  });

  await extension.startup();

  // Load a first page in order to be able to register a console listener in the content process.
  // This has to be done in the same domain of the second page to stay in the same process.
  let contentPage = await ExtensionTestUtils.loadContentPage(TEST_URL_1);

  // Listen to the errors logged in the content process.
  ContentTask.spawn(contentPage.browser, {}, () => {
    this.collectedErrors = [];

    this.consoleErrorListener = error => {
      error.QueryInterface(Ci.nsIScriptError);
      // Ignore errors from ExtensionContent.jsm
      if (error.innerWindowID) {
        this.collectedErrors.push({
          innerWindowID: error.innerWindowID,
          message: error.errorMessage,
        });
      }
    };

    Services.console.registerListener(this.consoleErrorListener);
  });

  // Reload the page and check that the cached content script is still able to
  // run on document_start.
  await contentPage.loadURL(TEST_URL_2);

  await extension.awaitMessage("content-script-loaded");

  const errors = await ContentTask.spawn(contentPage.browser, {}, () => {
    Services.console.unregisterListener(this.consoleErrorListener);
    return this.collectedErrors;
  });
  equal(errors.length, 7);
  for (const { innerWindowID, message } of errors) {
    equal(
      innerWindowID,
      contentPage.browser.innerWindowID,
      `Message ${message} has the innerWindowID set`
    );
  }

  await extension.unload();

  await contentPage.close();
});