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

// Tests that disabling JavaScript for a tab works as it should.

const TEST_URI = URL_ROOT_SSL + "browser_toolbox_options_disable_js.html";

add_task(async function () {
  const tab = await addTab(TEST_URI);

  // Start on the options panel from where we will toggle the disabling javascript
  // option.
  const toolbox = await gDevTools.showToolboxForTab(tab, { toolId: "options" });

  await testJSEnabled();
  await testJSEnabledIframe();

  // Disable JS.
  await toggleJS(toolbox);

  await testJSDisabled();
  await testJSDisabledIframe();

  // Navigate and check JS is still disabled
  for (let i = 0; i < 10; i++) {
    await navigateTo(`${TEST_URI}?nocache=${i}`);
    await testJSDisabled();
    await testJSDisabledIframe();
  }

  // Re-enable JS.
  await toggleJS(toolbox);

  await testJSEnabled();
  await testJSEnabledIframe();

  await toolbox.destroy();
  gBrowser.removeCurrentTab();
});

async function testJSEnabled() {
  info("Testing that JS is enabled");

  // We use waitForTick here because switching browsingContext.allowJavascript
  // to true takes a while to become live.
  await waitForTick();

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const doc = content.document;
    const output = doc.getElementById("output");
    doc.querySelector("#logJSEnabled").click();
    is(
      output.textContent,
      "JavaScript Enabled",
      'Output is "JavaScript Enabled"'
    );
  });
}

async function testJSEnabledIframe() {
  info("Testing that JS is enabled in the iframe");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const doc = content.document;
    const iframe = doc.querySelector("iframe");
    const iframeDoc = iframe.contentDocument;
    const output = iframeDoc.getElementById("output");
    iframeDoc.querySelector("#logJSEnabled").click();
    is(
      output.textContent,
      "JavaScript Enabled",
      'Output is "JavaScript Enabled" in iframe'
    );
  });
}

async function toggleJS(toolbox) {
  const panel = toolbox.getCurrentPanel();
  const cbx = panel.panelDoc.getElementById("devtools-disable-javascript");

  if (cbx.checked) {
    info("Clearing checkbox to re-enable JS");
  } else {
    info("Checking checkbox to disable JS");
  }

  let javascriptEnabled =
    await toolbox.commands.targetConfigurationCommand.isJavascriptEnabled();
  is(
    javascriptEnabled,
    !cbx.checked,
    "targetConfigurationCommand.isJavascriptEnabled is correct before the toggle"
  );

  const browserLoaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser
  );
  cbx.click();
  await browserLoaded;

  javascriptEnabled =
    await toolbox.commands.targetConfigurationCommand.isJavascriptEnabled();
  is(
    javascriptEnabled,
    !cbx.checked,
    "targetConfigurationCommand.isJavascriptEnabled is correctly updated"
  );
}

async function testJSDisabled() {
  info("Testing that JS is disabled");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const doc = content.document;
    const output = doc.getElementById("output");
    doc.querySelector("#logJSDisabled").click();

    Assert.notStrictEqual(
      output.textContent,
      "JavaScript Disabled",
      'output is not "JavaScript Disabled"'
    );
  });
}

async function testJSDisabledIframe() {
  info("Testing that JS is disabled in the iframe");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const doc = content.document;
    const iframe = doc.querySelector("iframe");
    const iframeDoc = iframe.contentDocument;
    const output = iframeDoc.getElementById("output");
    iframeDoc.querySelector("#logJSDisabled").click();
    Assert.notStrictEqual(
      output.textContent,
      "JavaScript Disabled",
      'output is not "JavaScript Disabled" in iframe'
    );
  });
}