summaryrefslogtreecommitdiffstats
path: root/dom/console/tests/xpcshell/test_failing_console_listener.js
blob: bb1b7cd46c3de8d641c8d0de19f7eb8462dedf21 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(async function () {
  Assert.ok("console" in this);

  // Add a first listener that synchronously throws.
  const removeListener1 = addConsoleListener(() => {
    throw new Error("Fail");
  });

  // Add a second listener updating a flag we can observe from the test.
  let secondListenerCalled = false;
  const removeListener2 = addConsoleListener(
    () => (secondListenerCalled = true)
  );

  console.log(42);
  Assert.ok(secondListenerCalled, "Second listener was called");

  // Cleanup listeners.
  removeListener1();
  removeListener2();
});

function addConsoleListener(callback) {
  const principal = Cc["@mozilla.org/systemprincipal;1"].createInstance(
    Ci.nsIPrincipal
  );
  ConsoleAPIStorage.addLogEventListener(callback, principal);

  return () => {
    ConsoleAPIStorage.removeLogEventListener(callback, principal);
  };
}