summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_pprint_error_goto_line.js
blob: c681d3d35426be696634e877b23c2270bd7ea44b (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2; fill-column: 80 -*- */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

function test()
{
  waitForExplicitFinish();

  gBrowser.selectedTab = gBrowser.addTab();
  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
    openScratchpad(runTests);
  }, true);

  content.location = "data:text/html;charset=utf8,"
    + "test Scratchpad pretty print error goto line.";
}

function testJumpToPrettyPrintError(sp, error, remark) {
  info("will test jumpToLine after prettyPrint error" + remark);

  // CodeMirror lines and columns are 0-based, Scratchpad UI and error
  // stack are 1-based.
  is(/Invalid regular expression flag \(3:10\)/.test(error), true,
     "prettyPrint expects error in editor text:\n" + error);

  sp.editor.jumpToLine();

  const editorDoc = sp.editor.container.contentDocument;
  const lineInput = editorDoc.querySelector("input");
  const errorLocation = lineInput.value;
  const [ inputLine, inputColumn ] = errorLocation.split(":");
  const errorLine = 3, errorColumn = 10;

  is(inputLine, errorLine,
     "jumpToLine input field is set from editor selection (line)");
  is(inputColumn, errorColumn,
     "jumpToLine input field is set from editor selection (column)");

  EventUtils.synthesizeKey("VK_RETURN", { }, editorDoc.defaultView);

  // CodeMirror lines and columns are 0-based, Scratchpad UI and error
  // stack are 1-based.
  const cursor = sp.editor.getCursor();
  is(inputLine, cursor.line + 1, "jumpToLine goto error location (line)");
  is(inputColumn, cursor.ch + 1, "jumpToLine goto error location (column)");
}

function runTests(sw, sp)
{
  sp.setText([
    "// line 1",
    "// line 2",
    "var re = /a bad /regexp/; // line 3 is an obvious syntax error!",
    "// line 4",
    "// line 5",
    ""
  ].join("\n"));

  sp.prettyPrint().then(aFulfill => {
    ok(false, "Expecting Invalid regexp flag (3:10)");
    finish();
  }, error => {
    testJumpToPrettyPrintError(sp, error, " (Bug 1005471, first time)");
  });

  sp.prettyPrint().then(aFulfill => {
    ok(false, "Expecting Invalid regexp flag (3:10)");
    finish();
  }, error => {
    // Second time verifies bug in earlier implementation fixed.
    testJumpToPrettyPrintError(sp, error, " (second time)");
    finish();
  });
}