summaryrefslogtreecommitdiffstats
path: root/devtools/shared/webconsole/test/chrome/test_cached_messages.html
blob: 12d5069c7d49686fa8b18b323bdd83196bf95c60 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf8">
  <title>Test for cached messages</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="common.js"></script>
  <!-- Any copyright is dedicated to the Public Domain.
     - http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for cached messages</p>

<script class="testbody" type="application/javascript">
"use strict";

const { MESSAGE_CATEGORY } = require("devtools/shared/constants");

const previousEnabled = window.docShell.cssErrorReportingEnabled;
window.docShell.cssErrorReportingEnabled = true;

SimpleTest.registerCleanupFunction(() => {
  window.docShell.cssErrorReportingEnabled = previousEnabled;
});

var ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
                          .getService(Ci.nsIConsoleAPIStorage);
let expectedConsoleCalls = [];
let expectedPageErrors = [];

function doPageErrors() {
  Services.console.reset();

  expectedPageErrors = [
    {
      errorMessage: /fooColor/,
      sourceName: /.+/,
      category: MESSAGE_CATEGORY.CSS_PARSER,
      timeStamp: FRACTIONAL_NUMBER_REGEX,
      error: false,
      warning: true,
    },
    {
      errorMessage: /doTheImpossible/,
      sourceName: /.+/,
      category: "chrome javascript",
      timeStamp: FRACTIONAL_NUMBER_REGEX,
      error: true,
      warning: false,
    },
  ];

  let container = document.createElement("script");
  document.body.appendChild(container);
  container.textContent = "document.body.style.color = 'fooColor';";
  document.body.removeChild(container);

  SimpleTest.expectUncaughtException();

  container = document.createElement("script");
  document.body.appendChild(container);
  container.textContent = "document.doTheImpossible();";
  document.body.removeChild(container);
}

function doConsoleCalls() {
  ConsoleAPIStorage.clearEvents();

  top.console.log("foobarBaz-log", undefined);
  top.console.info("foobarBaz-info", null);
  top.console.warn("foobarBaz-warn", document.body);

  expectedConsoleCalls = [
    {
      level: "log",
      filename: /test_cached_messages/,
      timeStamp: FRACTIONAL_NUMBER_REGEX,
      arguments: ["foobarBaz-log", { type: "undefined" }],
    },
    {
      level: "info",
      filename: /test_cached_messages/,
      timeStamp: FRACTIONAL_NUMBER_REGEX,
      arguments: ["foobarBaz-info", { type: "null" }],
    },
    {
      level: "warn",
      filename: /test_cached_messages/,
      timeStamp: FRACTIONAL_NUMBER_REGEX,
      arguments: ["foobarBaz-warn", { type: "object", actor: /[a-z]/ }],
    },
  ];
}
</script>

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

SimpleTest.waitForExplicitFinish();
var {ConsoleServiceListener} =
  require("devtools/server/actors/webconsole/listeners/console-service");
var {ConsoleAPIListener} =
  require("devtools/server/actors/webconsole/listeners/console-api");

let consoleAPIListener, consoleServiceListener;
let consoleAPICalls = 0;
let pageErrors = 0;

async function onConsoleAPICall(message) {
  for (const msg of expectedConsoleCalls) {
    if (msg.functionName == message.functionName &&
        msg.filename.test(message.filename)) {
      consoleAPICalls++;
      break;
    }
  }
  if (consoleAPICalls == expectedConsoleCalls.length) {
    await checkConsoleAPICache();
  }
}

async function onConsoleServiceMessage(message) {
  if (!(message instanceof Ci.nsIScriptError)) {
    return;
  }
  for (const msg of expectedPageErrors) {
    if (msg.category == message.category &&
        msg.errorMessage.test(message.errorMessage)) {
      pageErrors++;
      break;
    }
  }
  if (pageErrors == expectedPageErrors.length) {
    await testPageErrors();
  }
}

function startTest() {
  removeEventListener("load", startTest);

  consoleAPIListener = new ConsoleAPIListener(top, onConsoleAPICall);
  consoleAPIListener.init();

  doConsoleCalls();
}

async function checkConsoleAPICache() {
  consoleAPIListener.destroy();
  consoleAPIListener = null;
  const {state} = await attachConsole(["ConsoleAPI"]);
  const response = await state.webConsoleFront.getCachedMessages(["ConsoleAPI"]);
  onCachedConsoleAPI(state, response);
}

function onCachedConsoleAPI(state, response) {
  const msgs = response.messages;
  info("cached console messages: " + msgs.length);

  ok(msgs.length >= expectedConsoleCalls.length,
     "number of cached console messages");

  for (const {message} of msgs) {
    for (const expected of expectedConsoleCalls) {
      if (expected.filename.test(message.filename)) {
        expectedConsoleCalls.splice(expectedConsoleCalls.indexOf(expected));
        checkConsoleAPICall(message, expected);
        break;
      }
    }
  }

  is(expectedConsoleCalls.length, 0, "all expected messages have been found");

  closeDebugger(state, function() {
    consoleServiceListener = new ConsoleServiceListener(null, onConsoleServiceMessage);
    consoleServiceListener.init();
    doPageErrors();
  });
}

async function testPageErrors() {
  consoleServiceListener.destroy();
  consoleServiceListener = null;
  const {state} = await attachConsole(["PageError"]);
  const response = await state.webConsoleFront.getCachedMessages(["PageError"]);
  onCachedPageErrors(state, response);
}

function onCachedPageErrors(state, response) {
  const msgs = response.messages;
  info("cached page errors: " + msgs.length);

  ok(msgs.length >= expectedPageErrors.length,
     "number of cached page errors");

  for (const {pageError} of msgs) {
    for (const expected of expectedPageErrors) {
      if (expected.category == pageError.category &&
          expected.errorMessage.test(pageError.errorMessage)) {
        expectedPageErrors.splice(expectedPageErrors.indexOf(expected));
        checkObject(pageError, expected);
        break;
      }
    }
  }

  is(expectedPageErrors.length, 0, "all expected messages have been found");

  closeDebugger(state, function() {
    SimpleTest.finish();
  });
}

addEventListener("load", startTest);
</script>
</body>
</html>