summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/browser_worker_use_counters.js
blob: 6f115916d818eb4154d89d6ae935ca5b5d9308d5 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const gHttpTestRoot = "https://example.com/browser/dom/workers/test/";

function grabHistogramsFromContent(
  use_counter_name,
  worker_type,
  counter_before = null
) {
  let telemetry = Cc["@mozilla.org/base/telemetry;1"].getService(
    Ci.nsITelemetry
  );
  let gather = () => {
    let snapshots;
    if (Services.appinfo.browserTabsRemoteAutostart) {
      snapshots = telemetry.getSnapshotForHistograms("main", false).content;
    } else {
      snapshots = telemetry.getSnapshotForHistograms("main", false).parent;
    }
    let checkedGet = probe => {
      return snapshots[probe] ? snapshots[probe].sum : 0;
    };
    return [
      checkedGet(`USE_COUNTER2_${use_counter_name}_${worker_type}_WORKER`),
      checkedGet(`${worker_type}_WORKER_DESTROYED`),
    ];
  };
  return BrowserTestUtils.waitForCondition(() => {
    return counter_before != gather()[0];
  }).then(gather, gather);
}

var check_use_counter_worker = async function (
  use_counter_name,
  worker_type,
  content_task
) {
  info(`checking ${use_counter_name} use counters for ${worker_type} worker`);

  let newTab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  gBrowser.selectedTab = newTab;
  newTab.linkedBrowser.stop();

  // Hold on to the current values of the telemetry histograms we're
  // interested in.
  let [histogram_before, destructions_before] = await grabHistogramsFromContent(
    use_counter_name,
    worker_type
  );

  BrowserTestUtils.loadURIString(
    gBrowser.selectedBrowser,
    gHttpTestRoot + "file_use_counter_worker.html"
  );
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  await content_task(gBrowser.selectedBrowser);

  // Tear down the page.
  let tabClosed = BrowserTestUtils.waitForTabClosing(newTab);
  gBrowser.removeTab(newTab);
  await tabClosed;

  // Grab histograms again and compare.
  let [histogram_after, destructions_after] = await grabHistogramsFromContent(
    use_counter_name,
    worker_type,
    histogram_before
  );

  is(
    histogram_after,
    histogram_before + 1,
    `histogram ${use_counter_name} counts for ${worker_type} worker are correct`
  );
  // There might be other workers created by prior tests get destroyed during
  // this tests.
  ok(
    destructions_after > destructions_before,
    `${worker_type} worker counts are correct`
  );
};

add_task(async function test_dedicated_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "DEDICATED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.Worker("file_use_counter_worker.js");
        worker.onmessage = function (e) {
          if (e.data === "DONE") {
            worker.terminate();
            resolve();
          }
        };
      });
    });
  });
});

add_task(async function test_shared_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "SHARED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.SharedWorker(
          "file_use_counter_shared_worker.js"
        );
        worker.port.onmessage = function (e) {
          if (e.data === "DONE") {
            resolve();
          }
        };
        worker.port.postMessage("RUN");
      });
    });
  });
});

add_task(async function test_shared_worker_microtask() {
  await check_use_counter_worker("CONSOLE_LOG", "SHARED", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      return new Promise(resolve => {
        let worker = new content.SharedWorker(
          "file_use_counter_shared_worker_microtask.js"
        );
        worker.port.onmessage = function (e) {
          if (e.data === "DONE") {
            resolve();
          }
        };
        worker.port.postMessage("RUN");
      });
    });
  });
});

add_task(async function test_service_worker() {
  await check_use_counter_worker("CONSOLE_LOG", "SERVICE", async browser => {
    await ContentTask.spawn(browser, {}, function () {
      let waitForActivated = async function (registration) {
        return new Promise(resolve => {
          let worker =
            registration.installing ||
            registration.waiting ||
            registration.active;
          if (worker.state === "activated") {
            resolve(worker);
            return;
          }

          worker.addEventListener("statechange", function onStateChange() {
            if (worker.state === "activated") {
              worker.removeEventListener("statechange", onStateChange);
              resolve(worker);
            }
          });
        });
      };

      return new Promise(resolve => {
        content.navigator.serviceWorker
          .register("file_use_counter_service_worker.js")
          .then(async registration => {
            content.navigator.serviceWorker.onmessage = function (e) {
              if (e.data === "DONE") {
                registration.unregister().then(resolve);
              }
            };
            let worker = await waitForActivated(registration);
            worker.postMessage("RUN");
          });
      });
    });
  });
});