summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_autocomplete_inside_text.js
blob: 6baf1d4201ec6d6ab3d6341365c4f969376b297e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that editing text inside parens behave as expected, i.e.
// - it does not show the autocompletion text
// - show popup when there's properties to complete
// - insert the selected item from the popup in the input
// - right arrow dismiss popup and don't autocomplete
// - tab key when there is not visible autocomplete suggestion insert a tab
// See Bug 812618, 1479521 and 1334130.

const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html>
<head>
  <script>
    window.testBugAA = "hello world";
    window.testBugBB = "hello world 2";
    window.x = "hello world 3";
  </script>
</head>
<body>bug 812618 - test completion inside text</body>`;

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);
  const { jsterm } = hud;
  info("web console opened");

  const { autocompletePopup: popup } = jsterm;

  await setInitialState(hud);

  ok(popup.isOpen, "popup is open");
  is(popup.itemCount, 2, "popup.itemCount is correct");
  is(popup.selectedIndex, 0, "popup.selectedIndex is correct");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  info("Pressing arrow right");
  let onPopupClose = popup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_ArrowRight");
  await onPopupClose;
  ok(true, "popup was closed");
  checkInputValueAndCursorPosition(
    hud,
    "dump(window.testB)|",
    "input wasn't modified"
  );

  await setInitialState(hud);
  EventUtils.synthesizeKey("KEY_ArrowDown");
  is(popup.selectedIndex, 1, "popup.selectedIndex is correct");
  ok(!getInputCompletionValue(hud), "completeNode.value is empty");

  ok(
    hasExactPopupLabels(popup, ["testBugAA", "testBugBB"]),
    "getItems returns the items we expect"
  );

  info("press Tab and wait for popup to hide");
  onPopupClose = popup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Tab");
  await onPopupClose;

  // At this point the completion suggestion should be accepted.
  ok(!popup.isOpen, "popup is not open");
  checkInputValueAndCursorPosition(
    hud,
    "dump(window.testBugBB|)",
    "completion was successful after VK_TAB"
  );
  ok(!getInputCompletionValue(hud), "there is no completion text");

  info("Test ENTER key when popup is visible with a selected item");
  await setInitialState(hud);
  info("press Enter and wait for popup to hide");
  onPopupClose = popup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClose;

  ok(!popup.isOpen, "popup is not open");
  checkInputValueAndCursorPosition(
    hud,
    "dump(window.testBugAA|)",
    "completion was successful after Enter"
  );
  ok(!getInputCompletionValue(hud), "there is no completion text");

  info("Test autocomplete inside parens");
  await setInputValueForAutocompletion(hud, "dump()", -1);
  let onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("window.testBugA");
  await onAutocompleteUpdated;
  ok(popup.isOpen, "popup is open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  info("Matching the completion proposal should close the popup");
  onPopupClose = popup.once("popup-closed");
  EventUtils.sendString("A");
  await onPopupClose;

  info("Test TAB key when there is no autocomplete suggestion");
  ok(!popup.isOpen, "popup is not open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  EventUtils.synthesizeKey("KEY_Tab");
  checkInputValueAndCursorPosition(
    hud,
    "dump(window.testBugAA\t|)",
    "completion was successful after Enter"
  );

  info("Check that we don't show the popup when editing words");
  await setInputValueForAutocompletion(hud, "estBug", 0);
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("t");
  await onAutocompleteUpdated;
  is(getInputValue(hud), "testBug", "jsterm has expected value");
  is(popup.isOpen, false, "popup is not open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  await setInputValueForAutocompletion(hud, "__foo", 1);
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("t");
  await onAutocompleteUpdated;
  is(getInputValue(hud), "_t_foo", "jsterm has expected value");
  is(popup.isOpen, false, "popup is not open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  await setInputValueForAutocompletion(hud, "$$bar", 1);
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("t");
  await onAutocompleteUpdated;
  is(getInputValue(hud), "$t$bar", "jsterm has expected value");
  is(popup.isOpen, false, "popup is not open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  await setInputValueForAutocompletion(hud, "99luftballons", 1);
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("t");
  await onAutocompleteUpdated;
  is(getInputValue(hud), "9t9luftballons", "jsterm has expected value");
  is(popup.isOpen, false, "popup is not open");
  ok(!getInputCompletionValue(hud), "there is no completion text");

  info("Check that typing the closing paren closes the autocomplete window");
  await setInputValueForAutocompletion(hud, "dump()", -1);
  const onPopupOpen = popup.once("popup-opened");
  EventUtils.sendString("x");
  await onPopupOpen;

  onPopupClose = popup.once("popup-closed");
  // Since the paren is already here, it won't add any new character
  EventUtils.sendString(")");
  checkInputValueAndCursorPosition(
    hud,
    "dump(x)|",
    "the input is the expected one after typing the closing paren"
  );
  await onPopupClose;
  ok(true, "popup was closed when typing the closing paren");
});

async function setInitialState(hud) {
  const { jsterm } = hud;
  await setInputValueForAutocompletion(hud, "dump()", -1);

  const onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  EventUtils.sendString("window.testB");
  checkInputValueAndCursorPosition(hud, "dump(window.testB|)");
  await onAutocompleteUpdated;
}