summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_jsterm_completion_bracket.js
blob: 7704230d6ec448fb3f94dd3dd64c5c0b95ba1527 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that code completion works properly with `[`

"use strict";

const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html><p>test [ completion.
  <script>
    window.testObject = Object.create(null, Object.getOwnPropertyDescriptors({
      bar: 0,
      dataTest: 1,
      "data-test": 2,
      'da"ta"test': 3,
      "da\`ta\`test": 4,
      "da'ta'test": 5,
      "DATA-TEST": 6,
      "DAT_\\\\a\\"'\`\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d\\ud83d\\ude80_TEST": 7,
    }));
  </script>`;

add_task(async function () {
  await pushPref("devtools.editor.autoclosebrackets", false);
  const hud = await openNewTabAndConsole(TEST_URI);
  await testInputs(hud, false);
  await testCompletionTextUpdateOnPopupNavigate(hud, false);
  await testAcceptCompletionExistingClosingBracket(hud);

  info("Test again with autoclosebracket set to true");
  await pushPref("devtools.editor.autoclosebrackets", true);
  const hudAutoclose = await openNewTabAndConsole(TEST_URI);
  await testInputs(hudAutoclose, true);
  await testCompletionTextUpdateOnPopupNavigate(hudAutoclose, true);
  await testAcceptCompletionExistingClosingBracket(hudAutoclose);
});

async function testInputs(hud, autocloseEnabled) {
  const tests = [
    {
      description: "Check that the popup is opened when typing `[`",
      input: "window.testObject[",
      expectedItems: [
        `"bar"`,
        `"da'ta'test"`,
        `"da\\"ta\\"test"`,
        `"da\`ta\`test"`,
        `"data-test"`,
        `"dataTest"`,
        `"DAT_\\\\a\\"'\`\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d🚀_TEST"`,
        `"DATA-TEST"`,
      ],
      expectedCompletionText: autocloseEnabled ? "" : `"bar"]`,
      expectedInputAfterCompletion: `window.testObject["bar"]`,
    },
    {
      description: "Test that the list can be filtered even without quote",
      input: "window.testObject[d",
      expectedItems: [
        `"da'ta'test"`,
        `"da\\"ta\\"test"`,
        `"da\`ta\`test"`,
        `"data-test"`,
        `"dataTest"`,
        `"DAT_\\\\a\\"'\`\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d🚀_TEST"`,
        `"DATA-TEST"`,
      ],
      expectedCompletionText: autocloseEnabled ? "" : `a'ta'test"]`,
      expectedInputAfterCompletion: `window.testObject["da'ta'test"]`,
    },
    {
      description: "Test filtering with quote and string",
      input: `window.testObject["d`,
      expectedItems: [
        `"da'ta'test"`,
        `"da\\"ta\\"test"`,
        `"da\`ta\`test"`,
        `"data-test"`,
        `"dataTest"`,
        `"DAT_\\\\a\\"'\`\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d🚀_TEST"`,
        `"DATA-TEST"`,
      ],
      expectedCompletionText: autocloseEnabled ? "" : `a'ta'test"]`,
      expectedInputAfterCompletion: `window.testObject["da'ta'test"]`,
    },
    {
      description: "Test filtering with simple quote and string",
      input: `window.testObject['d`,
      expectedItems: [
        `'da"ta"test'`,
        `'da\\'ta\\'test'`,
        `'da\`ta\`test'`,
        `'data-test'`,
        `'dataTest'`,
        `'DAT_\\\\a"\\'\`\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d🚀_TEST'`,
        `'DATA-TEST'`,
      ],
      expectedCompletionText: autocloseEnabled ? "" : `a"ta"test']`,
      expectedInputAfterCompletion: `window.testObject['da"ta"test']`,
    },
    {
      description: "Test filtering with template literal and string",
      input: "window.testObject[`d",
      expectedItems: [
        "`da'ta'test`",
        '`da"ta"test`',
        "`da\\`ta\\`test`",
        "`data-test`",
        "`dataTest`",
        "`DAT_\\\\a\"'\\`\\${0}\\u0000\\b\\t\\n\\f\\r\\ude80\\ud83d🚀_TEST`",
        "`DATA-TEST`",
      ],
      expectedCompletionText: autocloseEnabled ? "" : "a'ta'test`]",
      expectedInputAfterCompletion: "window.testObject[`da'ta'test`]",
    },
    {
      description: "Test that filtering is case insensitive",
      input: "window.testObject[data-t",
      expectedItems: [`"data-test"`, `"DATA-TEST"`],
      expectedCompletionText: autocloseEnabled ? "" : `est"]`,
      expectedInputAfterCompletion: `window.testObject["data-test"]`,
    },
    {
      description:
        "Test that filtering without quote displays the popup when there's only 1 match",
      input: "window.testObject[DATA-",
      expectedItems: [`"DATA-TEST"`],
      expectedCompletionText: autocloseEnabled ? "" : `TEST"]`,
      expectedInputAfterCompletion: `window.testObject["DATA-TEST"]`,
    },
  ];

  for (const test of tests) {
    await testInput(hud, test);
  }
}

async function testInput(
  hud,
  {
    description,
    input,
    expectedItems,
    expectedCompletionText,
    expectedInputAfterCompletion,
  }
) {
  const { jsterm } = hud;
  const { autocompletePopup } = jsterm;

  info(`${description} - test popup opening`);
  const onPopUpOpen = autocompletePopup.once("popup-opened");
  EventUtils.sendString(input);
  await onPopUpOpen;

  ok(
    hasExactPopupLabels(autocompletePopup, expectedItems),
    `${description} - popup has expected item, in expected order`
  );
  checkInputCompletionValue(
    hud,
    expectedCompletionText,
    `${description} - completeNode has expected value`
  );

  info(`${description} - test accepting completion`);
  const onPopupClose = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Tab");
  await onPopupClose;
  checkInputValueAndCursorPosition(
    hud,
    expectedInputAfterCompletion + "|",
    `${description} - input was completed as expected`
  );
  checkInputCompletionValue(hud, "", `${description} - completeNode is empty`);

  setInputValue(hud, "");
}

async function testCompletionTextUpdateOnPopupNavigate(hud, autocloseEnabled) {
  const { jsterm } = hud;
  const { autocompletePopup } = jsterm;

  info(
    "Test that navigating the popup list update the completionText as expected"
  );
  const onPopUpOpen = autocompletePopup.once("popup-opened");
  const input = `window.testObject[data`;
  EventUtils.sendString(input);
  await onPopUpOpen;

  ok(
    hasExactPopupLabels(autocompletePopup, [
      `"data-test"`,
      `"dataTest"`,
      `"DATA-TEST"`,
    ]),
    `popup has expected items, in expected order`
  );
  checkInputCompletionValue(
    hud,
    autocloseEnabled ? "" : `-test"]`,
    `completeNode has expected value`
  );

  EventUtils.synthesizeKey("KEY_ArrowDown");
  checkInputCompletionValue(
    hud,
    autocloseEnabled ? "" : `Test"]`,
    `completeNode has expected value`
  );

  EventUtils.synthesizeKey("KEY_ArrowDown");
  checkInputCompletionValue(
    hud,
    autocloseEnabled ? "" : `-TEST"]`,
    `completeNode has expected value`
  );

  const onPopupClose = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Tab");
  await onPopupClose;
  checkInputValueAndCursorPosition(
    hud,
    `window.testObject["DATA-TEST"]|`,
    `input was completed as expected after navigating the popup`
  );
}

async function testAcceptCompletionExistingClosingBracket(hud) {
  const { jsterm } = hud;
  const { autocompletePopup } = jsterm;

  info(
    "Check that accepting completion when there's a closing bracket does not append " +
      "another closing bracket"
  );
  await setInputValueForAutocompletion(hud, "window.testObject[]", -1);
  const onPopUpOpen = autocompletePopup.once("popup-opened");
  EventUtils.sendString(`"b`);
  await onPopUpOpen;
  ok(
    hasExactPopupLabels(autocompletePopup, [`"bar"`]),
    `popup has expected item`
  );

  const onPopupClose = autocompletePopup.once("popup-closed");
  EventUtils.synthesizeKey("KEY_Tab");
  await onPopupClose;
  checkInputValueAndCursorPosition(
    hud,
    `window.testObject["bar"]|`,
    `input was completed as expected, without adding a closing bracket`
  );
}