summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_auto-pretty-print-02.js
blob: 432bc73d22a0b0823bbe19efc6dcfc4ad5d546d5 (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 auto pretty printing doesn't accidentally toggle
 * pretty printing off when we switch to a minified source
 * that is already pretty printed.
 */

const TAB_URL = EXAMPLE_URL + "doc_auto-pretty-print-02.html";

var gTab, gDebuggee, gPanel, gDebugger;
var gEditor, gSources, gPrefs, gOptions, gView;

var gFirstSource = EXAMPLE_URL + "code_ugly-6.js";
var gSecondSource = EXAMPLE_URL + "code_ugly-7.js";

var gOriginalPref = Services.prefs.getBoolPref("devtools.debugger.auto-pretty-print");
Services.prefs.setBoolPref("devtools.debugger.auto-pretty-print", true);

function test() {
  let options = {
    source: gFirstSource,
    line: 1
  };
  initDebugger(TAB_URL, options).then(([aTab, aDebuggee, aPanel]) => {
    const gTab = aTab;
    const gDebuggee = aDebuggee;
    const gPanel = aPanel;
    const gDebugger = gPanel.panelWin;
    const gEditor = gDebugger.DebuggerView.editor;
    const gSources = gDebugger.DebuggerView.Sources;
    const gPrefs = gDebugger.Prefs;
    const gOptions = gDebugger.DebuggerView.Options;
    const gView = gDebugger.DebuggerView;

    // Should be on by default.
    testAutoPrettyPrintOn();

    Task.spawn(function* () {

      testSourceIsUgly();

      yield waitForSourceShown(gPanel, gFirstSource);
      testSourceIsPretty();
      testPrettyPrintButtonOn();

      // select second source
      yield selectSecondSource();
      testSecondSourceLabel();

      // select first source
      yield selectFirstSource();
      testFirstSourceLabel();
      testPrettyPrintButtonOn();

      // Disable auto pretty printing so it does not affect the following tests.
      yield disableAutoPrettyPrint();

      closeDebuggerAndFinish(gPanel)
        .then(null, aError => {
          ok(false, "Got an error: " + DevToolsUtils.safeErrorString(aError));
        });
    });

    function selectSecondSource() {
      let finished = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.SOURCE_SHOWN, 2);
      gSources.selectedIndex = 1;
      return finished;
    }

    function selectFirstSource() {
      let finished = waitForDebuggerEvents(gPanel, gDebugger.EVENTS.SOURCE_SHOWN);
      gSources.selectedIndex = 0;
      return finished;
    }

    function testSourceIsUgly() {
      ok(!gEditor.getText().includes("\n  "),
        "The source shouldn't be pretty printed yet.");
    }

    function testFirstSourceLabel() {
      let source = gSources.selectedItem.attachment.source;
      ok(source.url === gFirstSource,
        "First source url is correct.");
    }

    function testSecondSourceLabel() {
      let source = gSources.selectedItem.attachment.source;
      ok(source.url === gSecondSource,
        "Second source url is correct.");
    }

    function testAutoPrettyPrintOn() {
      is(gPrefs.autoPrettyPrint, true,
        "The auto-pretty-print pref should be on.");
      is(gOptions._autoPrettyPrint.getAttribute("checked"), "true",
        "The Auto pretty print menu item should be checked.");
    }

    function testPrettyPrintButtonOn() {
      is(gDebugger.document.getElementById("pretty-print").checked, true,
        "The button should be checked when the source is selected.");
    }

    function disableAutoPrettyPrint() {
      gOptions._autoPrettyPrint.setAttribute("checked", "false");
      gOptions._toggleAutoPrettyPrint();
      gOptions._onPopupHidden();
      info("Disabled auto pretty printing.");
    }

    function testSourceIsPretty() {
      ok(gEditor.getText().includes("\n  "),
        "The source should be pretty printed.");
    }

    registerCleanupFunction(function () {
      Services.prefs.setBoolPref("devtools.debugger.auto-pretty-print", gOriginalPref);
    });

  });
}