summaryrefslogtreecommitdiffstats
path: root/devtools/shared/webconsole/test/chrome/test_console_worker.html
blob: 330c083f6b1c567712fcafd521837ca292a8b2a1 (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
<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf8">
  <title>Test for the Console API and Workers</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 the Console API and Workers</p>

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

SimpleTest.waitForExplicitFinish();

const expectedCachedConsoleCalls = [{
  message:{
    level: "log",
    filename: /console-test-worker/,
    arguments: ['Log from worker init'],
  }
}];

const expectedConsoleAPICalls = [{
  message: {
    level: "log",
    arguments: ['Log was requested from worker'],
  }
}];

window.onload = async function () {
  const {state} = await attachConsoleToWorker(["ConsoleAPI"]);

  await testCachedMessages(state);
  await testConsoleAPI(state);

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

const testCachedMessages = async function (state) {
  info("testCachedMessages entered");
  return new Promise(resolve => {
    const onCachedConsoleAPI = (response) => {
      const consoleCalls = response.messages;

      info('Received cached response. Checking console calls.');
      checkConsoleAPICalls(consoleCalls, expectedCachedConsoleCalls);
      resolve();
    };
    state.webConsoleFront.getCachedMessages(["ConsoleAPI"]).then(onCachedConsoleAPI);
  })
};

const testConsoleAPI = async function (state) {
  info("testConsoleAPI: adding listener for consoleAPICall");
  const onConsoleApiMessage = state.webConsoleFront.once("consoleAPICall");
  state._worker_ref.postMessage({
    type: "log",
    message: "Log was requested from worker"
  });
  const packet = await onConsoleApiMessage;
  info("received message level: " + packet.message.level);
  checkConsoleAPICalls([packet], expectedConsoleAPICalls);
};

</script>
</body>
</html>