summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_chrome_ext_background_page.html
blob: 471c5339df23c2f58c24ed04021d9547906c90da (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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="chrome_head.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

Cu.import("resource://testing-common/TestUtils.jsm");

/* eslint-disable mozilla/balanced-listeners */

add_task(function* testAlertNotShownInBackgroundWindow() {
  ok(!Services.wm.getEnumerator("alert:alert").hasMoreElements(),
     "Alerts should not be present at the start of the test.");

  let consoleOpened = TestUtils.topicObserved("web-console-created");


  let extension = ExtensionTestUtils.loadExtension({
    background: function() {
      browser.test.log("background script executed");

      alert("I am an alert in the background.");

      browser.test.notifyPass("alertCalled");
    },
  });

  yield extension.startup();

  info("startup complete loaded");

  yield extension.awaitFinish("alertCalled");


  let alertWindows = Services.wm.getEnumerator("alert:alert");
  ok(!alertWindows.hasMoreElements(), "Should not show alert");


  // Make sure the message we output to the console is seen.
  // This message is in ext-backgroundPage.js
  let events = Cc["@mozilla.org/consoleAPI-storage;1"]
    .getService(Ci.nsIConsoleAPIStorage).getEvents();

  // This is the warning that is output after the first `alert()` call is made.
  let alertWarningEvent = events[events.length - 2];
  is(alertWarningEvent.arguments[0], "alert() is not supported in background windows; please use console.log instead.");

  // This is the actual alert text that should be present in the console
  // instead of as an `alert`.
  let alertEvent = events[events.length - 1];
  is(alertEvent.arguments[0], "I am an alert in the background.");


  // Wait for the browser console window to open.
  yield consoleOpened;

  let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
  require("devtools/client/framework/devtools-browser");
  let {HUDService} = require("devtools/client/webconsole/hudservice");

  // And then double check that we have an actual browser console.
  let haveConsole = !!HUDService.getBrowserConsole();
  ok(haveConsole, "Expected browser console to be open");

  if (haveConsole) {
    yield HUDService.toggleBrowserConsole();
  }

  yield extension.unload();
});
</script>

</body>
</html>