summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/test/browser_inplace-editor_autocomplete_css_variable.js
blob: 8a27778670c4a008cdea431a7fcaeb02fa5be20b (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
/* import-globals-from helper_inplace_editor.js */

"use strict";

const AutocompletePopup = require("resource://devtools/client/shared/autocomplete-popup.js");
const {
  InplaceEditor,
} = require("resource://devtools/client/shared/inplace-editor.js");
loadHelperScript("helper_inplace_editor.js");

// Test the inplace-editor autocomplete popup for variable suggestions.
// Using a mocked list of CSS variables to avoid test failures linked to
// engine changes (new property, removed property, ...).
// Also using a mocked list of CSS properties to avoid autocompletion when
// typing in "var"

// Used for representing the expectation of a visible color swatch
const COLORSWATCH = true;
// format :
//  [
//    what key to press,
//    expected input box value after keypress,
//    selected suggestion index (-1 if popup is hidden),
//    number of suggestions in the popup (0 if popup is hidden),
//    expected post label corresponding with the input box value,
//    boolean representing if there should be a colour swatch visible,
//  ]
const testData = [
  ["v", "v", -1, 0, null, !COLORSWATCH],
  ["a", "va", -1, 0, null, !COLORSWATCH],
  ["r", "var", -1, 0, null, !COLORSWATCH],
  ["(", "var()", -1, 0, null, !COLORSWATCH],
  ["-", "var(--abc)", 0, 9, "inherit", !COLORSWATCH],
  ["VK_BACK_SPACE", "var(-)", -1, 0, null, !COLORSWATCH],
  ["-", "var(--abc)", 0, 9, "inherit", !COLORSWATCH],
  ["VK_DOWN", "var(--def)", 1, 9, "transparent", !COLORSWATCH],
  ["VK_DOWN", "var(--ghi)", 2, 9, "#00FF00", COLORSWATCH],
  ["VK_DOWN", "var(--jkl)", 3, 9, "rgb(255, 0, 0)", COLORSWATCH],
  ["VK_DOWN", "var(--mno)", 4, 9, "hsl(120, 60%, 70%)", COLORSWATCH],
  ["VK_DOWN", "var(--pqr)", 5, 9, "BlueViolet", COLORSWATCH],
  ["VK_DOWN", "var(--stu)", 6, 9, "15px", !COLORSWATCH],
  ["VK_DOWN", "var(--vwx)", 7, 9, "rgba(255, 0, 0, 0.4)", COLORSWATCH],
  ["VK_DOWN", "var(--yz)", 8, 9, "hsla(120, 60%, 70%, 0.3)", COLORSWATCH],
  ["VK_DOWN", "var(--abc)", 0, 9, "inherit", !COLORSWATCH],
  ["VK_DOWN", "var(--def)", 1, 9, "transparent", !COLORSWATCH],
  ["VK_DOWN", "var(--ghi)", 2, 9, "#00FF00", COLORSWATCH],
  ["VK_LEFT", "var(--ghi)", -1, 0, null, !COLORSWATCH],
];

const CSS_VARIABLES = [
  ["--abc", "inherit"],
  ["--def", "transparent"],
  ["--ghi", "#00FF00"],
  ["--jkl", "rgb(255, 0, 0)"],
  ["--mno", "hsl(120, 60%, 70%)"],
  ["--pqr", "BlueViolet"],
  ["--stu", "15px"],
  ["--vwx", "rgba(255, 0, 0, 0.4)"],
  ["--yz", "hsla(120, 60%, 70%, 0.3)"],
];

add_task(async function () {
  await addTab(
    "data:text/html;charset=utf-8,inplace editor CSS variable autocomplete"
  );
  const { host, doc } = await createHost();

  const popup = new AutocompletePopup(doc, { autoSelect: true });

  await new Promise(resolve => {
    createInplaceEditorAndClick(
      {
        start: runAutocompletionTest,
        contentType: InplaceEditor.CONTENT_TYPES.CSS_VALUE,
        property: {
          name: "color",
        },
        cssProperties: {
          getNames: () => [],
          getValues: () => [],
        },
        cssVariables: new Map(CSS_VARIABLES),
        done: resolve,
        popup,
      },
      doc
    );
  });

  popup.destroy();
  host.destroy();
  gBrowser.removeCurrentTab();
});

const runAutocompletionTest = async function (editor) {
  info("Starting to test for css variable completion");
  for (const data of testData) {
    await testCompletion(data, editor);
  }

  EventUtils.synthesizeKey("VK_RETURN", {}, editor.input.defaultView);
};