summaryrefslogtreecommitdiffstats
path: root/dom/console/tests/test_jsm.xhtml
blob: 5c77ba196f6b2493d0f0bab45eb1a2f8045b2dbf (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
<?xml version="1.0"?>
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<window title="Console + JSM"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="test();">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

  <script type="application/javascript">
  <![CDATA[

const JSM = "chrome://mochitests/content/chrome/dom/console/tests/console.jsm";

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

let dumpCalled = 0;
function dumpFunction(msg) {
  ok(msg.includes("_PREFIX_"), "we have a prefix");
  dump("Message received: " + msg); // Just for debugging
  dumpCalled++;
}

function consoleListener() {
  Services.obs.addObserver(this, "console-api-log-event");
}

consoleListener.prototype  = {
  count: 0,

  observe: function(aSubject, aTopic, aData) {
    if (aTopic == "console-api-log-event") {
      var obj = aSubject.wrappedJSObject;
      ok(obj.chromeContext, "JSM is always a chrome context");

      if (obj.innerID == JSM) {
        is(obj.ID, "jsm", "ID and InnerID are correctly set.");
        is(obj.arguments[0], "Hello world!", "Message matches");
        is(obj.consoleID, "", "No consoleID for console API");
        is(obj.prefix, "", "prefix is empty by default");

        // We want to see 2 messages from this innerID, the first is generated
        // by console.log, the second one from createInstance().log();
        ++this.count;
      } else if (obj.innerID == "CUSTOM INNER") {
        is(obj.ID, "jsm", "ID and InnerID are correctly set.");
        is(obj.arguments[0], "Hello world!", "Message matches");
        is(obj.consoleID, "wow", "consoleID is set by consoleInstance");
        is(obj.prefix, "_PREFIX_", "prefix is set by consoleInstance");
        // We expect to see 2 messages from this innerID.
        ++this.count;
      } else if (obj.innerID == "LEVEL") {
        // Nothing special... just we don't want to see 'invisible' messages.
        is(obj.ID, "jsm", "ID and InnerID are correctly set.");
        is(obj.arguments[0], "Hello world!", "Message matches");
        is(obj.prefix, "", "prefix is empty by default");
        // We expect to see 2 messages from this innerID.
        ++this.count;
      } else if (obj.innerID == "NO PREF") {
        // Nothing special... just we don't want to see 'invisible' messages.
        is(obj.ID, "jsm", "ID and InnerID are correctly set.");
        is(obj.arguments[0], "Hello world!", "Message matches");
        is(obj.prefix, "", "prefix is empty by default");
        // We expect to see 2 messages from this innerID.
        ++this.count;
      }

      if (this.count == 8) {
        is(dumpCalled, 2, "Dump has been called!");
        Services.obs.removeObserver(this, "console-api-log-event");
        SimpleTest.finish();
      }
    }
  }
}
async function test() {
  SimpleTest.waitForExplicitFinish();

  var cl = new consoleListener();
  ChromeUtils.import(JSM, window);
  await SpecialPowers.pushPrefEnv({set: [["pref.test.console", "log"]]})
  ConsoleTest.go(dumpFunction);

  await SpecialPowers.pushPrefEnv({
    set: [["pref.test.console.notset", "Log"]],
  });
  ConsoleTest.go2();
}

  ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml">
  </body>
</window>