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

"use strict";

// Test that pressing Enter quickly after a letter that makes the input exactly match the
// item in the autocomplete popup does not insert unwanted character. See Bug 1595068.

const TEST_URI = `data:text/html;charset=utf-8,<!DOCTYPE html><script>
  var uvwxyz = "zyxwvu";
</script>Autocomplete race on Enter`;

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

  info(`Enter "scre" and wait for the autocomplete popup to be displayed`);
  let onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "scre");
  await onPopupOpened;
  checkInputCompletionValue(hud, "en", "completeNode has expected value");

  info(`Type "n" and quickly after, "Enter"`);
  let onPopupClosed = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("e");
  await waitForTime(50);
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClosed;

  is(getInputValue(hud), "screen", "the input has the expected value");

  setInputValue(hud, "");

  info(
    "Check that it works when typed word match exactly the item in the popup"
  );
  onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "wind");
  await onPopupOpened;
  checkInputCompletionValue(hud, "ow", "completeNode has expected value");

  info(`Quickly type "o", "w" and "Enter"`);
  onPopupClosed = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("o");
  await waitForTime(5);
  EventUtils.synthesizeKey("w");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClosed;

  is(getInputValue(hud), "window", "the input has the expected value");

  setInputValue(hud, "");

  info("Check that it works when there's no autocomplete popup");
  let onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  await setInputValueForAutocompletion(hud, "uvw");
  await onAutocompleteUpdated;
  checkInputCompletionValue(hud, "xyz", "completeNode has expected value");

  info(`Quickly type "x" and "Enter"`);
  EventUtils.synthesizeKey("x");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  await waitFor(
    () => getInputValue(hud) === "uvwxyz",
    "input has expected 'uvwxyz' value"
  );
  ok(true, "input has the expected value");

  setInputValue(hud, "");

  info(
    "Check that it works when there's no autocomplete popup and the whole word is typed"
  );
  onAutocompleteUpdated = jsterm.once("autocomplete-updated");
  await setInputValueForAutocompletion(hud, "uvw");
  await onAutocompleteUpdated;
  checkInputCompletionValue(hud, "xyz", "completeNode has expected value");

  info(`Quickly type "x", "y", "z" and "Enter"`);
  const onResultMessage = waitForMessageByType(hud, "zyxwvu", ".result");
  EventUtils.synthesizeKey("x");
  await waitForTime(5);
  EventUtils.synthesizeKey("y");
  await waitForTime(5);
  EventUtils.synthesizeKey("z");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  info("wait for result message");
  await onResultMessage;
  is(getInputValue(hud), "", "Expression was evaluated and input was cleared");

  setInputValue(hud, "");

  info("Check that it works when typed letter match another item in the popup");
  onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "[].so");
  await onPopupOpened;
  checkInputCompletionValue(hud, "me", "completeNode has expected value");
  is(
    autocompletePopup.items.map(({ label }) => label).join("|"),
    "some|sort",
    "autocomplete has expected items"
  );

  info(`Quickly type "m" and "Enter"`);
  onPopupClosed = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("m");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClosed;
  is(getInputValue(hud), "[].some", "the input has the expected value");

  setInputValue(hud, "");

  info(
    "Hitting Enter quickly after a letter that should close the popup evaluates the expression"
  );
  onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "doc");
  await onPopupOpened;
  checkInputCompletionValue(hud, "ument", "completeNode has expected value");

  info(`Quickly type "x" and "Enter"`);
  onPopupClosed = autocompletePopup.once("popup-closed");
  const onMessage = waitForMessageByType(hud, "docx is not defined", ".error");
  EventUtils.synthesizeKey("x");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");

  await Promise.all([onPopupClosed, onMessage]);
  is(
    getInputValue(hud),
    "",
    "the input is empty and the expression was evaluated"
  );

  info(
    "Hitting Enter quickly after a letter that will make the expression exactly match another item than the selected one"
  );
  onPopupOpened = autocompletePopup.once("popup-opened");
  await setInputValueForAutocompletion(hud, "cons");
  await onPopupOpened;
  checkInputCompletionValue(hud, "ole", "completeNode has expected value");
  info(`Quickly type "t" and "Enter"`);
  onPopupClosed = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("t");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClosed;
  is(getInputValue(hud), "const", "the input has the expected item");

  info(
    "Hitting Enter quickly after a letter when the expression has text after"
  );
  await setInputValueForAutocompletion(hud, "f(und");
  ok(
    hasExactPopupLabels(autocompletePopup, ["undefined"]),
    `the popup has the "undefined" item`
  );
  info(`Quickly type "e" and "Enter"`);
  onPopupClosed = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("e");
  await waitForTime(5);
  EventUtils.synthesizeKey("KEY_Enter");
  await onPopupClosed;
  is(getInputValue(hud), "f(undefined)", "the input has the expected item");
});