summaryrefslogtreecommitdiffstats
path: root/devtools/shared/webconsole/test/test_console_serviceworker_cached.html
blob: 5aab64d7ff96a4789901fdca9015a2337da5c74f (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
<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf8">
  <title>Test for getCachedMessages and Service Workers</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript;version=1.8" src="common.js"></script>
  <!-- Any copyright is dedicated to the Public Domain.
     - http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for getCachedMessages and Service Workers</p>

<script class="testbody" type="text/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();

let BASE_URL = "https://example.com/chrome/devtools/shared/webconsole/test/";
let SERVICE_WORKER_URL = BASE_URL + "helper_serviceworker.js";
let FRAME_URL = BASE_URL + "sandboxed_iframe.html";

let firstTabExpectedCalls = [
  {
    level: "log",
    filename: /helper_serviceworker/,
    arguments: ['script evaluation'],
  },
  {
    level: "log",
    filename: /helper_serviceworker/,
    arguments: ['install event'],
  },
  {
    level: "log",
    filename: /helper_serviceworker/,
    arguments: ['activate event'],
  },
];

let secondTabExpectedCalls = [
  {
    level: "log",
    filename: /helper_serviceworker/,
    arguments: ['fetch event: ' + FRAME_URL],
  }
];

let startTest = Task.async(function*() {
  removeEventListener("load", startTest);

  yield new Promise(resolve => {
    SpecialPowers.pushPrefEnv({"set": [
      ["dom.serviceWorkers.enabled", true],
      ["devtools.webconsole.filter.serviceworkers", true]
    ]}, resolve);
  });

  info("Adding a tab and attaching a service worker");
  let tab1 = yield addTab(FRAME_URL);
  let swr = yield withActiveServiceWorker(tab1.linkedBrowser.contentWindow,
                                SERVICE_WORKER_URL);

  yield new Promise(resolve => {
    info("Attaching console to tab 1");
    attachConsoleToTab(["ConsoleAPI"], function(state) {
      state.client.getCachedMessages(["ConsoleAPI"], function(calls) {
        checkConsoleAPICalls(calls.messages, firstTabExpectedCalls);
        closeDebugger(state, resolve);
      });
    });
  });

  // Because this tab is being added after the original messages happened,
  // they shouldn't show up in a call to getCachedMessages.
  // However, there is a fetch event which is logged due to loading the tab.
  info("Adding a new tab at the same URL");
  let tab2 = yield addTab(FRAME_URL);
  yield new Promise(resolve => {
    info("Attaching console to tab 2");
    attachConsoleToTab(["ConsoleAPI"], function(state) {
      state.client.getCachedMessages(["ConsoleAPI"], function(calls) {
        checkConsoleAPICalls(calls.messages, secondTabExpectedCalls);
        closeDebugger(state, resolve);
      });
    });
  });

  yield swr.unregister();

  SimpleTest.finish();
});
addEventListener("load", startTest);

// This test needs to add tabs that are controlled by a service worker
// so use some special powers to dig around and find gBrowser
let {gBrowser} = SpecialPowers._getTopChromeWindow(SpecialPowers.window.get());

SimpleTest.registerCleanupFunction(() => {
  while (gBrowser.tabs.length > 1) {
    gBrowser.removeCurrentTab();
  }
});

function addTab(url) {
  info("Adding a new tab with URL: '" + url + "'");
  return new Promise(resolve => {
    let tab = gBrowser.selectedTab = gBrowser.addTab(url);
    gBrowser.selectedBrowser.addEventListener("load", function onload() {
      gBrowser.selectedBrowser.removeEventListener("load", onload, true);
      info("URL '" + url + "' loading complete");
      resolve(tab);
    }, true);
  });
}

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