summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing.js
blob: 152f64f835d11100a47fb8ff0bf525912395ca6e (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Test that enabling Service Workers testing option enables the
// mServiceWorkersTestingEnabled attribute added to nsPIDOMWindow.

// We explicitly want to test that service worker testing allows to use service
// workers on non-https, so we use mochi.test:8888 to avoid the automatic upgrade
// to https when dom.security.https_first is true.
const TEST_URI =
  URL_ROOT_MOCHI_8888 +
  "browser_toolbox_options_enable_serviceworkers_testing.html";
const ELEMENT_ID = "devtools-enable-serviceWorkersTesting";

add_task(async function () {
  await pushPref("dom.serviceWorkers.exemptFromPerDomainMax", true);
  await pushPref("dom.serviceWorkers.enabled", true);
  await pushPref("dom.serviceWorkers.testing.enabled", false);
  // Force the test to start without service worker testing enabled
  await pushPref("devtools.serviceWorkers.testing.enabled", false);

  const tab = await addTab(TEST_URI);
  const toolbox = await openToolboxForTab(tab, "options");

  let data = await register();
  is(data.success, false, "Register should fail with security error");

  const panel = toolbox.getCurrentPanel();
  const cbx = panel.panelDoc.getElementById(ELEMENT_ID);
  is(cbx.checked, false, "The checkbox shouldn't be checked");

  info(`Checking checkbox to enable service workers testing`);
  cbx.scrollIntoView();
  cbx.click();

  await reloadBrowser();

  data = await register();
  is(data.success, true, "Register should success");

  await unregister();
  data = await registerAndUnregisterInFrame();
  is(data.success, true, "Register should success");

  info("Workers should be turned back off when we closes the toolbox");
  await toolbox.destroy();

  await reloadBrowser();
  data = await register();
  is(data.success, false, "Register should fail with security error");
});

function sendMessage(name) {
  return SpecialPowers.spawn(gBrowser.selectedBrowser, [name], nameChild => {
    return new Promise(resolve => {
      const channel = new content.MessageChannel();
      content.postMessage(nameChild, "*", [channel.port2]);
      channel.port1.onmessage = function (msg) {
        resolve(msg.data);
        channel.port1.close();
      };
    });
  });
}

function register() {
  return sendMessage("devtools:sw-test:register");
}

function unregister(swr) {
  return sendMessage("devtools:sw-test:unregister");
}

function registerAndUnregisterInFrame() {
  return sendMessage("devtools:sw-test:iframe:register-and-unregister");
}