summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_console_webextension.js
blob: b11a56bf27ce75687be3cb5723751c0c1927ab54 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that messages from WebExtension are logged in the Browser Console.

"use strict";

const TEST_URI =
  "https://example.com/browser/devtools/client/webconsole/" +
  "test/browser/test-console.html?" +
  Date.now();

add_task(async function () {
  await pushPref("devtools.browsertoolbox.scope", "everything");
  await addTab(TEST_URI);

  await testWebExtensionMessages(false);
  await testWebExtensionMessages(true);
});

async function testWebExtensionMessages(
  createWebExtensionBeforeOpeningBrowserConsole = false
) {
  let extension;
  if (createWebExtensionBeforeOpeningBrowserConsole) {
    extension = await loadExtension();
  }
  const hud = await BrowserConsoleManager.toggleBrowserConsole();
  if (!createWebExtensionBeforeOpeningBrowserConsole) {
    extension = await loadExtension();
  }

  // TODO: Re-enable this (See Bug 1699050).
  /*
  // Trigger the messages logged when opening the popup.
  const { AppUiTestDelegate } = ChromeUtils.importESModule(
    "resource://testing-common/AppUiTestDelegate.sys.mjs"
  );
  const onPopupReady = extension.awaitMessage(`popup-ready`);
  await AppUiTestDelegate.clickBrowserAction(window, extension.id);
  // Ensure the popup script ran before going further
  AppUiTestDelegate.awaitExtensionPanel(window, extension.id);
  await onPopupReady;
  */

  // Wait enough so any duplicated message would have the time to be rendered
  await wait(1000);

  await checkUniqueMessageExists(
    hud,
    "content console API message",
    ".console-api"
  );
  await checkUniqueMessageExists(
    hud,
    "background console API message",
    ".console-api"
  );

  await checkUniqueMessageExists(hud, "content error", ".error");
  await checkUniqueMessageExists(hud, "background error", ".error");

  // TODO: Re-enable those checks (See Bug 1699050).
  // await checkUniqueMessageExists(hud, "popup console API message", ".console-api");
  // await checkUniqueMessageExists(hud, "popup error", ".error");

  await clearOutput(hud);

  info("Close the Browser Console");
  await safeCloseBrowserConsole();

  await extension.unload();
}

async function loadExtension() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      background: { scripts: ["background.js"] },

      browser_action: {
        default_popup: "popup.html",
      },

      content_scripts: [
        {
          matches: [TEST_URI],
          js: ["content-script.js"],
        },
      ],
    },
    useAddonManager: "temporary",

    files: {
      "background.js": function () {
        console.log("background console API message");
        throw new Error("background error");
      },

      "popup.html": `
        <!DOCTYPE html>
        <html>
          <head>
            <meta charset="utf-8">
          </head>
          <body>Popup</body>
          <script src="popup.js"></script>
        </html>`,

      "popup.js": function () {
        console.log("popup console API message");
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(() => {
          throw new Error("popup error");
        }, 5);

        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        setTimeout(() => {
          // eslint-disable-next-line no-undef
          browser.test.sendMessage(`popup-ready`);
        }, 10);
      },

      "content-script.js": function () {
        console.log("content console API message");
        throw new Error("content error");
      },
    },
  });
  await extension.startup();
  return extension;
}