summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_gridline-names-autocomplete.js
blob: 7e9bee61bcae87469b540429e42bbbb5788e4670 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that CSS property values are autocompleted and cycled
// correctly when editing an existing property in the rule view.

// format :
//  [
//    what key to press,
//    modifers,
//    expected input box value after keypress,
//    is the popup open,
//    is a suggestion selected in the popup,
//    expect ruleview-changed,
//  ]

const OPEN = true,
  SELECTED = true,
  CHANGE = true;
const changeTestData = [
  ["c", {}, "col1-start", OPEN, SELECTED, CHANGE],
  ["o", {}, "col1-start", OPEN, SELECTED, CHANGE],
  ["l", {}, "col1-start", OPEN, SELECTED, CHANGE],
  ["VK_DOWN", {}, "col2-start", OPEN, SELECTED, CHANGE],
  ["VK_RIGHT", {}, "col2-start", !OPEN, !SELECTED, !CHANGE],
];

// Creates a new CSS property value.
// Checks that grid-area autocompletes column and row names.
const newAreaTestData = [
  ["g", {}, "gap", OPEN, SELECTED, !CHANGE],
  ["VK_DOWN", {}, "grid", OPEN, SELECTED, !CHANGE],
  ["VK_DOWN", {}, "grid-area", OPEN, SELECTED, !CHANGE],
  ["VK_TAB", {}, "", !OPEN, !SELECTED, !CHANGE],
  "grid-line-names-updated",
  ["c", {}, "col1-start", OPEN, SELECTED, CHANGE],
  ["VK_BACK_SPACE", {}, "c", !OPEN, !SELECTED, CHANGE],
  ["VK_BACK_SPACE", {}, "", OPEN, !SELECTED, CHANGE],
  ["r", {}, "revert", OPEN, SELECTED, CHANGE],
  ["VK_DOWN", {}, "revert-layer", OPEN, SELECTED, CHANGE],
  ["VK_DOWN", {}, "row1-start", OPEN, SELECTED, CHANGE],
  ["r", {}, "rr", !OPEN, !SELECTED, CHANGE],
  ["VK_BACK_SPACE", {}, "r", !OPEN, !SELECTED, CHANGE],
  ["o", {}, "row1-start", OPEN, SELECTED, CHANGE],
  ["VK_TAB", {}, "", !OPEN, !SELECTED, CHANGE],
];

// Creates a new CSS property value.
// Checks that grid-row only autocompletes row names.
const newRowTestData = [
  ["g", {}, "gap", OPEN, SELECTED, !CHANGE],
  ["r", {}, "grid", OPEN, SELECTED, !CHANGE],
  ["i", {}, "grid", OPEN, SELECTED, !CHANGE],
  ["d", {}, "grid", OPEN, SELECTED, !CHANGE],
  ["-", {}, "grid-area", OPEN, SELECTED, !CHANGE],
  ["r", {}, "grid-row", OPEN, SELECTED, !CHANGE],
  ["VK_TAB", {}, "", !OPEN, !SELECTED, !CHANGE],
  "grid-line-names-updated",
  ["c", {}, "c", !OPEN, !SELECTED, CHANGE],
  ["VK_BACK_SPACE", {}, "", OPEN, !SELECTED, CHANGE],
  ["r", {}, "revert", OPEN, SELECTED, CHANGE],
  ["VK_DOWN", {}, "revert-layer", OPEN, SELECTED, CHANGE],
  ["VK_DOWN", {}, "row1-start", OPEN, SELECTED, CHANGE],
  ["VK_TAB", {}, "", !OPEN, !SELECTED, CHANGE],
];

const TEST_URL = URL_ROOT + "doc_grid_names.html";

add_task(async function () {
  await addTab(TEST_URL);
  const { toolbox, inspector, view } = await openRuleView();

  info("Test autocompletion changing a preexisting property");
  await runChangePropertyAutocompletionTest(
    toolbox,
    inspector,
    view,
    changeTestData
  );

  info("Test autocompletion creating a new property");
  await runNewPropertyAutocompletionTest(
    toolbox,
    inspector,
    view,
    newAreaTestData
  );

  info("Test autocompletion creating a new property");
  await runNewPropertyAutocompletionTest(
    toolbox,
    inspector,
    view,
    newRowTestData
  );
});

async function runNewPropertyAutocompletionTest(
  toolbox,
  inspector,
  view,
  testData
) {
  info("Selecting the test node");
  await selectNode("#cell2", inspector);

  info("Focusing the css property editable field");
  const ruleEditor = getRuleViewRuleEditor(view, 0);
  const editor = await focusNewRuleViewProperty(ruleEditor);
  const gridLineNamesUpdated = inspector.once("grid-line-names-updated");

  info("Starting to test for css property completion");
  for (const data of testData) {
    if (data == "grid-line-names-updated") {
      await gridLineNamesUpdated;
      continue;
    }
    await testCompletion(data, editor, view);
  }
}

async function runChangePropertyAutocompletionTest(
  toolbox,
  inspector,
  view,
  testData
) {
  info("Selecting the test node");
  await selectNode("#cell3", inspector);

  const ruleEditor = getRuleViewRuleEditor(view, 1).rule;
  const prop = ruleEditor.textProps[0];

  info("Focusing the css property editable value");
  const gridLineNamesUpdated = inspector.once("grid-line-names-updated");
  let editor = await focusEditableField(view, prop.editor.valueSpan);
  await gridLineNamesUpdated;

  info("Starting to test for css property completion");
  for (const data of testData) {
    // Re-define the editor at each iteration, because the focus may have moved
    // from property to value and back
    editor = inplaceEditor(view.styleDocument.activeElement);
    await testCompletion(data, editor, view);
  }
}

async function testCompletion(
  [key, modifiers, completion, open, selected, change],
  editor,
  view
) {
  info("Pressing key " + key);
  info("Expecting " + completion);
  info("Is popup opened: " + open);
  info("Is item selected: " + selected);

  let onDone;
  if (change) {
    // If the key triggers a ruleview-changed, wait for that event, it will
    // always be the last to be triggered and tells us when the preview has
    // been done.
    onDone = view.once("ruleview-changed");
  } else {
    // Otherwise, expect an after-suggest event (except if the popup gets
    // closed).
    onDone =
      key !== "VK_RIGHT" && key !== "VK_BACK_SPACE"
        ? editor.once("after-suggest")
        : null;
  }

  // Also listening for popup opened/closed events if needed.
  const popupEvent = open ? "popup-opened" : "popup-closed";
  const onPopupEvent =
    editor.popup.isOpen !== open ? once(editor.popup, popupEvent) : null;

  info("Synthesizing key " + key + ", modifiers: " + Object.keys(modifiers));

  EventUtils.synthesizeKey(key, modifiers, view.styleWindow);

  // Flush the debounce for the preview text.
  view.debounce.flush();

  await onDone;
  await onPopupEvent;

  // The key might have been a TAB or shift-TAB, in which case the editor will
  // be a new one
  editor = inplaceEditor(view.styleDocument.activeElement);

  info("Checking the state");
  if (completion !== null) {
    is(editor.input.value, completion, "Correct value is autocompleted");
  }

  if (!open) {
    ok(!(editor.popup && editor.popup.isOpen), "Popup is closed");
  } else {
    ok(editor.popup.isOpen, "Popup is open");
    is(editor.popup.selectedIndex !== -1, selected, "An item is selected");
  }
}