summaryrefslogtreecommitdiffstats
path: root/devtools/client/sourceeditor/test/browser_editor_autocomplete_events.js
blob: e2f976afe5a80a66f5b31ef8fbf78309c8033eb8 (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
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {InspectorFront} = require("devtools/shared/fronts/inspector");
const AUTOCOMPLETION_PREF = "devtools.editor.autocomplete";
const TEST_URI = "data:text/html;charset=UTF-8,<html><body><bar></bar>" +
                 "<div id='baz'></div><body></html>";

add_task(function* () {
  yield addTab(TEST_URI);
  yield runTests();
});

function* runTests() {
  let target = TargetFactory.forTab(gBrowser.selectedTab);
  yield target.makeRemote();
  let inspector = InspectorFront(target.client, target.form);
  let walker = yield inspector.getWalker();
  let {ed, win, edWin} = yield setup(null, {
    autocomplete: true,
    mode: Editor.modes.css,
    autocompleteOpts: {walker: walker, cssProperties: getClientCssProperties()}
  });
  yield testMouse(ed, edWin);
  yield testKeyboard(ed, edWin);
  yield testKeyboardCycle(ed, edWin);
  yield testKeyboardCycleForPrefixedString(ed, edWin);
  yield testKeyboardCSSComma(ed, edWin);
  teardown(ed, win);
}

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

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

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

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

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

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

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

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

  info("Waiting for popup to be opened");
  yield 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");
}

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

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

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

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

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

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

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

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

  yield wait(500);

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

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

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

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

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