summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/sourceeditor/test/browser_editor_autocomplete_events.js
blob: 88fbcdbf1d40f98235579c54272cf97fc7e52df9 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_URI =
  "data:text/html;charset=UTF-8,<html><body><bar></bar>" +
  "<div id='baz'></div><body></html>";

add_task(async function () {
  await addTab(TEST_URI);
  await runTests();
});

async function runTests() {
  const target = await createAndAttachTargetForTab(gBrowser.selectedTab);
  const inspector = await target.getFront("inspector");
  const walker = inspector.walker;
  const cssPropertiesFront = await target.getFront("cssProperties");
  const { ed, win, edWin } = await setup({
    autocomplete: true,
    mode: Editor.modes.css,
    autocompleteOpts: {
      walker,
      cssProperties: cssPropertiesFront.cssProperties,
    },
  });
  await testMouse(ed, edWin);
  await testKeyboard(ed, edWin);
  await testKeyboardCycle(ed, edWin);
  await testKeyboardCycleForPrefixedString(ed, edWin);
  await testKeyboardCSSComma(ed, edWin);
  await testCloseOnEscape(ed, edWin);
  teardown(ed, win);
}

async function testKeyboard(ed, win) {
  ed.focus();
  ed.setText("b");
  ed.setCursor({ line: 1, ch: 1 });

  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");

  const autocompleteKey = Editor.keyFor("autocompletion", {
    noaccel: true,
  }).toUpperCase();
  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);

  info("Waiting for popup to be opened");
  await popupOpened;

  EventUtils.synthesizeKey("VK_RETURN", {}, win);
  is(ed.getText(), "bar", "Editor text has been updated");
}

async function testKeyboardCycle(ed, win) {
  ed.focus();
  ed.setText("b");
  ed.setCursor({ line: 1, ch: 1 });

  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");

  const autocompleteKey = Editor.keyFor("autocompletion", {
    noaccel: true,
  }).toUpperCase();
  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);

  info("Waiting for popup to be opened");
  await popupOpened;

  EventUtils.synthesizeKey("VK_DOWN", {}, win);
  is(ed.getText(), "bar", "Editor text has been updated");

  EventUtils.synthesizeKey("VK_DOWN", {}, win);
  is(ed.getText(), "body", "Editor text has been updated");

  EventUtils.synthesizeKey("VK_DOWN", {}, win);
  is(ed.getText(), "#baz", "Editor text has been updated");
}

async function testKeyboardCycleForPrefixedString(ed, win) {
  ed.focus();
  ed.setText("#b");
  ed.setCursor({ line: 1, ch: 2 });

  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");

  const autocompleteKey = Editor.keyFor("autocompletion", {
    noaccel: true,
  }).toUpperCase();
  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);

  info("Waiting for popup to be opened");
  await popupOpened;

  EventUtils.synthesizeKey("VK_DOWN", {}, win);
  is(ed.getText(), "#baz", "Editor text has been updated");
}

async function testKeyboardCSSComma(ed, win) {
  ed.focus();
  ed.setText("b");
  ed.setCursor({ line: 1, ch: 1 });

  let isPopupOpened = false;
  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");
  popupOpened.then(() => {
    isPopupOpened = true;
  });

  EventUtils.synthesizeKey(",", {}, win);

  await wait(500);

  ok(!isPopupOpened, "Autocompletion shouldn't be opened");
}

async function testMouse(ed, win) {
  ed.focus();
  ed.setText("b");
  ed.setCursor({ line: 1, ch: 1 });

  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");

  const autocompleteKey = Editor.keyFor("autocompletion", {
    noaccel: true,
  }).toUpperCase();
  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);

  info("Waiting for popup to be opened");
  await popupOpened;
  ed.getAutocompletionPopup()._list.children[2].click();
  is(ed.getText(), "#baz", "Editor text has been updated");
}

async function testCloseOnEscape(ed, win) {
  ed.focus();
  ed.setText("b");
  ed.setCursor({ line: 1, ch: 1 });

  const popupOpened = ed.getAutocompletionPopup().once("popup-opened");

  const autocompleteKey = Editor.keyFor("autocompletion", {
    noaccel: true,
  }).toUpperCase();
  EventUtils.synthesizeKey("VK_" + autocompleteKey, { ctrlKey: true }, win);

  info("Waiting for popup to be opened");
  await popupOpened;

  is(ed.getAutocompletionPopup().isOpen, true, "The popup is open");

  const popupClosed = ed.getAutocompletionPopup().once("popup-closed");
  EventUtils.synthesizeKey("VK_ESCAPE", {}, win);

  await popupClosed;
  is(ed.getAutocompletionPopup().isOpen, false, "Escape key closed popup");
}