summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_multiline.js
blob: cfd103f5e5d94644a16bb0970d277b8fa2187f1b (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Tests that the console waits for more input instead of evaluating
// when valid, but incomplete, statements are present upon pressing enter
// -or- when the user ends a line with shift + enter.

"use strict";

const TEST_URI =
  "http://example.com/browser/devtools/client/webconsole/test/browser/test-console.html";

const SHOULD_ENTER_MULTILINE = [
  { input: "function foo() {" },
  { input: "var a = 1," },
  { input: "var a = 1;", shiftKey: true },
  { input: "function foo() { }", shiftKey: true },
  { input: "function" },
  { input: "(x) =>" },
  { input: "let b = {" },
  { input: "let a = [" },
  { input: "{" },
  { input: "{ bob: 3343," },
  { input: "function x(y=" },
  { input: "Array.from(" },
  // shift + enter creates a new line despite parse errors
  { input: "{2,}", shiftKey: true },
];
const SHOULD_EXECUTE = [
  { input: "function foo() { }" },
  { input: "var a = 1;" },
  { input: "function foo() { var a = 1; }" },
  { input: '"asdf"' },
  { input: "99 + 3" },
  { input: "1, 2, 3" },
  // errors
  { input: "function f(x) { let y = 1, }" },
  { input: "function f(x=,) {" },
  { input: "{2,}" },
];

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  for (const { input, shiftKey } of SHOULD_ENTER_MULTILINE) {
    setInputValue(hud, input);
    EventUtils.synthesizeKey("VK_RETURN", { shiftKey });

    // We need to remove the spaces at the end of the input since code mirror do some
    // automatic indent in some case.
    const newValue = getInputValue(hud).replace(/ +$/g, "");
    is(newValue, input + "\n", "A new line was added");
  }

  for (const { input, shiftKey } of SHOULD_EXECUTE) {
    setInputValue(hud, input);
    const onMessage = waitForMessageByType(hud, "", ".result");
    EventUtils.synthesizeKey("VK_RETURN", { shiftKey });
    await onMessage;

    await waitFor(() => !getInputValue(hud));
    is(getInputValue(hud), "", "Input is cleared");
  }
});