summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/runtime/browser_consoleAPICalled.js
blob: eac774bd28fd6cc78279b8dd9d5b11ec29c3c42e (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Request a longer timeout as we have many tests which are longer
requestLongerTimeout(2);

const PAGE_CONSOLE_EVENTS =
  "https://example.com/browser/remote/cdp/test/browser/runtime/doc_console_events.html";
const PAGE_CONSOLE_EVENTS_ONLOAD =
  "https://example.com/browser/remote/cdp/test/browser/runtime/doc_console_events_onload.html";

add_task(async function noEventsWhenRuntimeDomainDisabled({ client }) {
  await runConsoleTest(client, 0, async () => {
    SpecialPowers.spawn(gBrowser.selectedBrowser, [], () =>
      content.console.log("foo")
    );
  });
});

add_task(async function noEventsAfterRuntimeDomainDisabled({ client }) {
  const { Runtime } = client;

  await Runtime.enable();
  await Runtime.disable();

  await runConsoleTest(client, 0, async () => {
    SpecialPowers.spawn(gBrowser.selectedBrowser, [], () =>
      content.console.log("foo")
    );
  });
});

add_task(async function noEventsForJavascriptErrors({ client }) {
  await loadURL(PAGE_CONSOLE_EVENTS);
  const context = await enableRuntime(client);

  await runConsoleTest(client, 0, async () => {
    evaluate(client, context.id, () => {
      document.getElementById("js-error").click();
    });
  });
});

add_task(async function consoleAPI({ client }) {
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, () => {
      console.log("foo");
    });
  });

  is(events[0].type, "log", "Got expected type");
  is(events[0].args[0].value, "foo", "Got expected argument value");
  is(
    events[0].executionContextId,
    context.id,
    "Got event from current execution context"
  );
});

add_task(async function consoleAPIBeforeEnable({ client }) {
  const { Runtime } = client;
  const timeBefore = Date.now();

  const check = async () => {
    const events = await runConsoleTest(
      client,
      1,
      async () => {
        await Runtime.enable();
      },
      // Set custom before timestamp as the event is before our callback
      { timeBefore }
    );

    is(events[0].type, "log", "Got expected type");
    is(events[0].args[0].value, "foo", "Got expected argument value");
  };

  // Load the page which runs a log on load
  await loadURL(PAGE_CONSOLE_EVENTS_ONLOAD);
  await check();

  // Disable and re-enable Runtime domain, should send event again
  await Runtime.disable();
  await check();
});

add_task(async function consoleAPITypes({ client }) {
  const expectedEvents = ["dir", "error", "log", "timeEnd", "trace", "warning"];
  const levels = ["dir", "error", "log", "time", "timeEnd", "trace", "warn"];

  const context = await enableRuntime(client);

  const events = await runConsoleTest(
    client,
    expectedEvents.length,
    async () => {
      for (const level of levels) {
        await evaluate(
          client,
          context.id,
          level => {
            console[level]("foo");
          },
          level
        );
      }
    }
  );

  events.forEach((event, index) => {
    console.log(`Check for event type "${expectedEvents[index]}"`);
    is(event.type, expectedEvents[index], "Got expected type");
  });
});

add_task(async function consoleAPIArgumentsCount({ client }) {
  const argumentList = [[], ["foo"], ["foo", "bar", "cheese"]];

  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, argumentList.length, async () => {
    for (const args of argumentList) {
      await evaluate(
        client,
        context.id,
        args => {
          console.log(...args);
        },
        args
      );
    }
  });

  events.forEach((event, index) => {
    console.log(`Check for event args "${argumentList[index]}"`);

    const argValues = event.args.map(arg => arg.value);
    Assert.deepEqual(argValues, argumentList[index], "Got expected args");
  });
});

add_task(async function consoleAPIArgumentsAsRemoteObject({ client }) {
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, () => {
      console.log("foo", Symbol("foo"));
    });
  });

  Assert.equal(events[0].args.length, 2, "Got expected amount of arguments");

  is(events[0].args[0].type, "string", "Got expected argument type");
  is(events[0].args[0].value, "foo", "Got expected argument value");

  is(events[0].args[1].type, "symbol", "Got expected argument type");
  is(
    events[0].args[1].description,
    "Symbol(foo)",
    "Got expected argument description"
  );
  ok(!!events[0].args[1].objectId, "Got objectId for argument");
});

add_task(async function consoleAPIByContentInteraction({ client }) {
  await loadURL(PAGE_CONSOLE_EVENTS);
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    evaluate(client, context.id, () => {
      document.getElementById("console-error").click();
    });
  });

  is(events[0].type, "error", "Got expected type");
  is(events[0].args[0].value, "foo", "Got expected argument value");
  is(
    events[0].executionContextId,
    context.id,
    "Got event from current execution context"
  );

  const { callFrames } = events[0].stackTrace;
  is(callFrames.length, 1, "Got expected amount of call frames");

  is(callFrames[0].functionName, "", "Got expected call frame function name");
  is(callFrames[0].lineNumber, 0, "Got expected call frame line number");
  is(callFrames[0].columnNumber, 8, "Got expected call frame column number");
  is(
    callFrames[0].url,
    "javascript:console.error('foo')",
    "Got expected call frame URL"
  );
});

add_task(async function consoleAPIByScript({ client }) {
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, function runLog() {
      console.trace("foo");
    });
  });

  const { callFrames } = events[0].stackTrace;
  is(callFrames.length, 1, "Got expected amount of call frames");

  is(
    callFrames[0].functionName,
    "runLog",
    "Got expected call frame function name"
  );
  is(callFrames[0].lineNumber, 1, "Got expected call frame line number");
  is(callFrames[0].columnNumber, 14, "Got expected call frame column number");
  is(callFrames[0].url, "debugger eval code", "Got expected call frame URL");
});

add_task(async function consoleAPIByScriptSubstack({ client }) {
  await loadURL(PAGE_CONSOLE_EVENTS);
  const context = await enableRuntime(client);

  const events = await runConsoleTest(client, 1, async () => {
    await evaluate(client, context.id, () => {
      document.getElementById("log-wrapper").click();
    });
  });

  const { callFrames } = events[0].stackTrace;
  is(callFrames.length, 5, "Got expected amount of call frames");

  is(
    callFrames[0].functionName,
    "runLogCaller",
    "Got expected call frame function name (frame 1)"
  );
  is(
    callFrames[0].lineNumber,
    13,
    "Got expected call frame line number (frame 1)"
  );
  is(
    callFrames[0].columnNumber,
    16,
    "Got expected call frame column number (frame 1)"
  );
  is(
    callFrames[0].url,
    PAGE_CONSOLE_EVENTS,
    "Got expected call frame UR (frame 1)"
  );

  is(
    callFrames[1].functionName,
    "runLogChild",
    "Got expected call frame function name (frame 2)"
  );
  is(
    callFrames[1].lineNumber,
    17,
    "Got expected call frame line number (frame 2)"
  );
  is(
    callFrames[1].columnNumber,
    8,
    "Got expected call frame column number (frame 2)"
  );
  is(
    callFrames[1].url,
    PAGE_CONSOLE_EVENTS,
    "Got expected call frame URL (frame 2)"
  );

  is(
    callFrames[2].functionName,
    "runLogParent",
    "Got expected call frame function name (frame 3)"
  );
  is(
    callFrames[2].lineNumber,
    20,
    "Got expected call frame line number (frame 3)"
  );
  is(
    callFrames[2].columnNumber,
    6,
    "Got expected call frame column number (frame 3)"
  );
  is(
    callFrames[2].url,
    PAGE_CONSOLE_EVENTS,
    "Got expected call frame URL (frame 3)"
  );

  is(
    callFrames[3].functionName,
    "onclick",
    "Got expected call frame function name (frame 4)"
  );
  is(
    callFrames[3].lineNumber,
    0,
    "Got expected call frame line number (frame 4)"
  );
  is(
    callFrames[3].columnNumber,
    0,
    "Got expected call frame column number (frame 4)"
  );
  is(
    callFrames[3].url,
    PAGE_CONSOLE_EVENTS,
    "Got expected call frame URL (frame 4)"
  );

  is(
    callFrames[4].functionName,
    "",
    "Got expected call frame function name (frame 5)"
  );
  is(
    callFrames[4].lineNumber,
    1,
    "Got expected call frame line number (frame 5)"
  );
  is(
    callFrames[4].columnNumber,
    45,
    "Got expected call frame column number (frame 5)"
  );
  is(
    callFrames[4].url,
    "debugger eval code",
    "Got expected call frame URL (frame 5)"
  );
});

async function runConsoleTest(client, eventCount, callback, options = {}) {
  let { timeBefore } = options;

  const { Runtime } = client;

  const EVENT_CONSOLE_API_CALLED = "Runtime.consoleAPICalled";

  const history = new RecordEvents(eventCount);
  history.addRecorder({
    event: Runtime.consoleAPICalled,
    eventName: EVENT_CONSOLE_API_CALLED,
    messageFn: payload =>
      `Received ${EVENT_CONSOLE_API_CALLED} for ${payload.type}`,
  });

  timeBefore ??= Date.now();
  await callback();

  const consoleAPIentries = await history.record();
  is(consoleAPIentries.length, eventCount, "Got expected amount of events");

  if (eventCount == 0) {
    return [];
  }

  const timeAfter = Date.now();

  // Check basic details for consoleAPICalled events
  consoleAPIentries.forEach(({ payload }) => {
    const timestamp = payload.timestamp;

    ok(
      timestamp >= timeBefore && timestamp <= timeAfter,
      `Timestamp ${timestamp} in expected range [${timeBefore} - ${timeAfter}]`
    );
  });

  return consoleAPIentries.map(event => event.payload);
}