summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_pretty-print-06.js
blob: 608df3140463f28a6bea10c6b36f23688f5e308a (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
/* -*- 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/ */

/**
 * Make sure that prettifying JS sources with type errors works as expected.
 */

const TAB_URL = EXAMPLE_URL + "doc_included-script.html";
const JS_URL = EXAMPLE_URL + "code_location-changes.js";

function test() {
  let options = {
    source: JS_URL,
    line: 1
  };
  initDebugger(TAB_URL, options).then(([aTab,, aPanel]) => {
    const gTab = aTab;
    const gPanel = aPanel;
    const gDebugger = gPanel.panelWin;
    const gClient = gDebugger.gClient;
    const gEditor = gDebugger.DebuggerView.editor;
    const gSources = gDebugger.DebuggerView.Sources;
    const queries = gDebugger.require("./content/queries");
    const constants = gDebugger.require("./content/constants");
    const actions = bindActionCreators(gPanel);
    const getState = gDebugger.DebuggerController.getState;
    let gPrettyPrinted = false;

    // We can't feed javascript files with syntax errors to the debugger,
    // because they will never run, thus sometimes getting gc'd before the
    // debugger is opened, or even before the target finishes navigating.
    // Make the client lie about being able to parse perfectly fine code.
    gClient.request = (function (aOriginalRequestMethod) {
      return function (aPacket, aCallback) {
        if (aPacket.type == "prettyPrint") {
          gPrettyPrinted = true;
          return promise.reject({ error: "prettyPrintError" });
        }
        return aOriginalRequestMethod(aPacket, aCallback);
      };
    }(gClient.request));

    Task.spawn(function* () {
      // From this point onward, the source editor's text should never change.
      gEditor.once("change", () => {
        ok(false, "The source editor text shouldn't have changed.");
      });

      is(getSelectedSourceURL(gSources), JS_URL,
        "The correct source is currently selected.");
      ok(gEditor.getText().includes("myFunction"),
        "The source shouldn't be pretty printed yet.");

      const source = queries.getSelectedSource(getState());
      try {
        yield actions.togglePrettyPrint(source);
        ok(false, "The promise for a prettified source should be rejected!");
      } catch (error) {
        ok(error.error, "Error came from a RDP request");
        ok(error.error.includes("prettyPrintError"),
          "The promise was correctly rejected with a meaningful message.");
      }

      const { text } = yield queries.getSourceText(getState(), source.actor);
      is(getSelectedSourceURL(gSources), JS_URL,
        "The correct source is still selected.");
      ok(gEditor.getText().includes("myFunction"),
        "The displayed source hasn't changed.");
      ok(text.includes("myFunction"),
        "The cached source text wasn't altered in any way.");

      is(gPrettyPrinted, true,
        "The hijacked pretty print method was executed.");

      yield closeDebuggerAndFinish(gPanel);
    });
  });
}