browser_webconsole_multiline_input.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vim:set ts=2 sw=2 sts=2 et: */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. // Tests that the console waits for more input instead of evaluating
  6. // when valid, but incomplete, statements are present upon pressing enter
  7. // -or- when the user ends a line with shift + enter.
  8. "use strict";
  9. const TEST_URI = "http://example.com/browser/devtools/client/webconsole/" +
  10. "test/test-console.html";
  11. let SHOULD_ENTER_MULTILINE = [
  12. {input: "function foo() {" },
  13. {input: "var a = 1," },
  14. {input: "var a = 1;", shiftKey: true },
  15. {input: "function foo() { }", shiftKey: true },
  16. {input: "function" },
  17. {input: "(x) =>" },
  18. {input: "let b = {" },
  19. {input: "let a = [" },
  20. {input: "{" },
  21. {input: "{ bob: 3343," },
  22. {input: "function x(y=" },
  23. {input: "Array.from(" },
  24. // shift + enter creates a new line despite parse errors
  25. {input: "{2,}", shiftKey: true },
  26. ];
  27. let SHOULD_EXECUTE = [
  28. {input: "function foo() { }" },
  29. {input: "var a = 1;" },
  30. {input: "function foo() { var a = 1; }" },
  31. {input: '"asdf"' },
  32. {input: "99 + 3" },
  33. {input: "1, 2, 3" },
  34. // errors
  35. {input: "function f(x) { let y = 1, }" },
  36. {input: "function f(x=,) {" },
  37. {input: "{2,}" },
  38. ];
  39. add_task(function* () {
  40. let { tab, browser } = yield loadTab(TEST_URI);
  41. let hud = yield openConsole();
  42. let inputNode = hud.jsterm.inputNode;
  43. for (let test of SHOULD_ENTER_MULTILINE) {
  44. hud.jsterm.setInputValue(test.input);
  45. EventUtils.synthesizeKey("VK_RETURN", { shiftKey: test.shiftKey });
  46. let inputValue = hud.jsterm.getInputValue();
  47. is(inputNode.selectionStart, inputNode.selectionEnd,
  48. "selection is collapsed");
  49. is(inputNode.selectionStart, inputValue.length,
  50. "caret at end of multiline input");
  51. let inputWithNewline = test.input + "\n";
  52. is(inputValue, inputWithNewline, "Input value is correct");
  53. }
  54. for (let test of SHOULD_EXECUTE) {
  55. hud.jsterm.setInputValue(test.input);
  56. EventUtils.synthesizeKey("VK_RETURN", { shiftKey: test.shiftKey });
  57. let inputValue = hud.jsterm.getInputValue();
  58. is(inputNode.selectionStart, 0, "selection starts/ends at 0");
  59. is(inputNode.selectionEnd, 0, "selection starts/ends at 0");
  60. is(inputValue, "", "Input value is cleared");
  61. }
  62. });