summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/mochitest_console.js
blob: 582e12b48fbb12526e2616df2dd1d2cb6dc40f5d (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
/* eslint-env mozilla/chrome-script */

"use strict";

const { addMessageListener, sendAsyncMessage } = this;

// Much of the console monitoring code is copied from TestUtils but simplified
// to our needs.
function monitorConsole(msgs) {
  function msgMatches(msg, pat) {
    for (let k in pat) {
      if (!(k in msg)) {
        return false;
      }
      if (pat[k] instanceof RegExp && typeof msg[k] === "string") {
        if (!pat[k].test(msg[k])) {
          return false;
        }
      } else if (msg[k] !== pat[k]) {
        return false;
      }
    }
    return true;
  }

  let counter = 0;
  function listener(msg) {
    if (msgMatches(msg, msgs[counter])) {
      counter++;
    }
  }
  addMessageListener("waitForConsole", () => {
    sendAsyncMessage("consoleDone", {
      ok: counter >= msgs.length,
      message: `monitorConsole | messages left expected at least ${msgs.length} got ${counter}`,
    });
    Services.console.unregisterListener(listener);
  });

  Services.console.registerListener(listener);
}

addMessageListener("consoleStart", messages => {
  for (let msg of messages) {
    // Message might be a RegExp object from a different compartment, but
    // instanceof RegExp will fail.  If we have an object, lets just make
    // sure.
    let message = msg.message;
    if (typeof message == "object" && !(message instanceof RegExp)) {
      msg.message = new RegExp(message);
    }
  }
  monitorConsole(messages);
});