summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_completion.js
blob: 4327daa9cebd4bcaba7d93a5a8f3c97e8ddd1910 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that code completion works properly.

"use strict";

const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><p>test code completion
  <script>
    foobar = true;
  </script>`;

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

  // Test typing 'docu'.
  await setInputValueForAutocompletion(hud, "foob");
  is(getInputValue(hud), "foob", "'foob' completion (input.value)");
  checkInputCompletionValue(hud, "ar", "'foob' completion (completeNode)");
  is(autocompletePopup.items.length, 1, "autocomplete popup has 1 item");
  is(autocompletePopup.isOpen, false, "autocomplete popup is not open");

  // Test typing 'docu' and press tab.
  EventUtils.synthesizeKey("KEY_Tab");
  is(getInputValue(hud), "foobar", "'foob' tab completion");

  checkInputCursorPosition(
    hud,
    "foobar".length,
    "cursor is at the end of 'foobar'"
  );
  is(getInputCompletionValue(hud).replace(/ /g, ""), "", "'foob' completed");

  // Test typing 'window.Ob' and press tab.  Just 'window.O' is
  // ambiguous: could be window.Object, window.Option, etc.
  await setInputValueForAutocompletion(hud, "window.Ob");
  EventUtils.synthesizeKey("KEY_Tab");
  is(getInputValue(hud), "window.Object", "'window.Ob' tab completion");

  // Test typing 'document.getElem'.
  const onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "document.getElem");
  is(getInputValue(hud), "document.getElem", "'document.getElem' completion");
  checkInputCompletionValue(hud, "entById", "'document.getElem' completion");

  // Test pressing key down.
  await onPopupOpened;
  EventUtils.synthesizeKey("KEY_ArrowDown");
  is(getInputValue(hud), "document.getElem", "'document.getElem' completion");
  checkInputCompletionValue(
    hud,
    "entsByClassName",
    "'document.getElem' another tab completion"
  );

  // Test pressing key up.
  EventUtils.synthesizeKey("KEY_ArrowUp");
  await waitFor(() => (getInputCompletionValue(hud) || "").includes("entById"));
  is(
    getInputValue(hud),
    "document.getElem",
    "'document.getElem' untab completion"
  );
  checkInputCompletionValue(hud, "entById", "'document.getElem' completion");

  await clearOutput(hud);

  await setInputValueForAutocompletion(hud, "docu");
  checkInputCompletionValue(hud, "ment", "'docu' completion");

  let onAutocompletUpdated = jsterm.once("autocomplete-updated");
  EventUtils.synthesizeKey("KEY_Enter");
  await onAutocompletUpdated;
  checkInputCompletionValue(hud, "", "clear completion on execute()");

  // Test multi-line completion works. We can't use setInputValueForAutocompletion because
  // it would trigger an evaluation (because of the new line, an Enter keypress is
  // simulated).
  onAutocompletUpdated = jsterm.once("autocomplete-updated");
  setInputValue(hud, "console.log('one');\n");
  EventUtils.sendString("consol");
  await onAutocompletUpdated;
  checkInputCompletionValue(hud, "e", "multi-line completion");

  // Test multi-line completion works even if there is text after the cursor
  onAutocompletUpdated = jsterm.once("autocomplete-updated");
  setInputValue(hud, "{\n\n}");
  EventUtils.synthesizeKey("KEY_ArrowUp");
  EventUtils.sendString("console.g");
  await onAutocompletUpdated;
  checkInputValueAndCursorPosition(hud, "{\nconsole.g|\n}");
  checkInputCompletionValue(hud, "roup", "multi-line completion");
  is(autocompletePopup.isOpen, true, "popup is opened");

  // Test non-object autocompletion.
  await setInputValueForAutocompletion(hud, "Object.name.sl");
  checkInputCompletionValue(hud, "ice", "non-object completion");

  // Test string literal autocompletion.
  await setInputValueForAutocompletion(hud, "'Asimov'.sl");
  checkInputCompletionValue(hud, "ice", "string literal completion");
});