summaryrefslogtreecommitdiffstats
path: root/devtools/client/framework/test/browser_toolbox_options_disable_js.js
blob: b0c14a805fd2da16184c71c0850f80b5fee8e8a3 (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
/* -*- 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/ */

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

const TEST_URI = URL_ROOT + "browser_toolbox_options_disable_js.html";

function test() {
  addTab(TEST_URI).then(tab => {
    let target = TargetFactory.forTab(tab);
    gDevTools.showToolbox(target).then(testSelectTool);
  });
}

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

let testToggleJS = Task.async(function* (toolbox) {
  ok(true, "Toolbox selected via selectTool method");

  yield testJSEnabled();
  yield testJSEnabledIframe();

  // Disable JS.
  yield toggleJS(toolbox);

  yield testJSDisabled();
  yield testJSDisabledIframe();

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

  yield testJSEnabled();
  yield testJSEnabledIframe();

  finishUp(toolbox);
});

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

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

  yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function () {
    let doc = content.document;
    let output = doc.getElementById("output");
    doc.querySelector("#logJSEnabled").click();
    is(output.textContent, "JavaScript Enabled", 'Output is "JavaScript Enabled"');
  });
}

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

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

function* toggleJS(toolbox) {
  let panel = toolbox.getCurrentPanel();
  let 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 browserLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  cbx.click();
  yield browserLoaded;
}

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

  yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function () {
    let doc = content.document;
    let output = doc.getElementById("output");
    doc.querySelector("#logJSDisabled").click();

    ok(output.textContent !== "JavaScript Disabled",
       'output is not "JavaScript Disabled"');
  });
}

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

  yield ContentTask.spawn(gBrowser.selectedBrowser, {}, function () {
    let doc = content.document;
    let iframe = doc.querySelector("iframe");
    let iframeDoc = iframe.contentDocument;
    let output = iframeDoc.getElementById("output");
    iframeDoc.querySelector("#logJSDisabled").click();
    ok(output.textContent !== "JavaScript Disabled",
       'output is not "JavaScript Disabled" in iframe');
  });
}

function finishUp(toolbox) {
  toolbox.destroy().then(function () {
    gBrowser.removeCurrentTab();
    finish();
  });
}