summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_options_enable_serviceworkers_testing.js
blob: 3273f439514e6595d2ed71bbb0c74558abfe0800 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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.

const COMMON_FRAME_SCRIPT_URL =
  "chrome://devtools/content/shared/frame-script-utils.js";
const ROOT_TEST_DIR =
  getRootDirectory(gTestPath);
const FRAME_SCRIPT_URL =
  ROOT_TEST_DIR +
  "browser_toolbox_options_enable_serviceworkers_testing_frame_script.js";
const TEST_URI = URL_ROOT +
                 "browser_toolbox_options_enable_serviceworkers_testing.html";

const ELEMENT_ID = "devtools-enable-serviceWorkersTesting";

var toolbox;

function test() {
  // Note: Pref dom.serviceWorkers.testing.enabled is false since we are testing
  // the same capabilities are enabled with the devtool pref.
  SpecialPowers.pushPrefEnv({"set": [
    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    ["dom.serviceWorkers.enabled", true],
    ["dom.serviceWorkers.testing.enabled", false]
  ]}, init);
}

function init() {
  addTab(TEST_URI).then(tab => {
    let target = TargetFactory.forTab(tab);
    let linkedBrowser = tab.linkedBrowser;

    linkedBrowser.messageManager.loadFrameScript(COMMON_FRAME_SCRIPT_URL, false);
    linkedBrowser.messageManager.loadFrameScript(FRAME_SCRIPT_URL, false);

    gDevTools.showToolbox(target).then(testSelectTool);
  });
}

function testSelectTool(aToolbox) {
  toolbox = aToolbox;
  toolbox.once("options-selected", start);
  toolbox.selectTool("options");
}

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

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

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

function testRegisterFails(data) {
  is(data.success, false, "Register should fail with security error");
  return promise.resolve();
}

function toggleServiceWorkersTestingCheckbox() {
  let panel = toolbox.getCurrentPanel();
  let cbx = panel.panelDoc.getElementById(ELEMENT_ID);

  cbx.scrollIntoView();

  if (cbx.checked) {
    info("Clearing checkbox to disable service workers testing");
  } else {
    info("Checking checkbox to enable service workers testing");
  }

  cbx.click();

  return promise.resolve();
}

function reload() {
  let deferred = defer();

  gBrowser.selectedBrowser.addEventListener("load", function onLoad(evt) {
    gBrowser.selectedBrowser.removeEventListener(evt.type, onLoad, true);
    deferred.resolve();
  }, true);

  executeInContent("devtools:test:reload", {}, {}, false);
  return deferred.promise;
}

function testRegisterSuccesses(data) {
  is(data.success, true, "Register should success");
  return promise.resolve();
}

function start() {
  register()
    .then(testRegisterFails)
    .then(toggleServiceWorkersTestingCheckbox)
    .then(reload)
    .then(register)
    .then(testRegisterSuccesses)
    .then(unregister)
    .then(registerAndUnregisterInFrame)
    .then(testRegisterSuccesses)
    // Workers should be turned back off when we closes the toolbox
    .then(toolbox.destroy.bind(toolbox))
    .then(reload)
    .then(register)
    .then(testRegisterFails)
    .catch(function (e) {
      ok(false, "Some test failed with error " + e);
    }).then(finishUp);
}

function finishUp() {
  gBrowser.removeCurrentTab();
  toolbox = null;
  finish();
}