summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_search-suggests-ids-and-classes.js
blob: 5360d632985c91a4734786dae67ba01d2ca46c4d (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test that the selector-search input proposes ids and classes even when . and
// # is missing, but that this only occurs when the query is one word (no
// selector combination)

// The various states of the inspector: [key, suggestions array]
// [
//  what key to press,
//  suggestions array with count [
//    [suggestion1, count1], [suggestion2] ...
//  ] count can be left to represent 1
// ]
const KEY_STATES = [
  [
    "s",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "p",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "a",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "n",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [" ", [["span div", 1]]],
  // mixed tag/class/id suggestions only work for the first word
  ["d", [["span div", 1]]],
  ["VK_BACK_SPACE", [["span div", 1]]],
  [
    "VK_BACK_SPACE",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "VK_BACK_SPACE",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "VK_BACK_SPACE",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  [
    "VK_BACK_SPACE",
    [
      ["span", 1],
      [".span", 1],
      ["#span", 1],
    ],
  ],
  ["VK_BACK_SPACE", []],
  // Test that mixed tags, classes and ids are grouped by types, sorted by
  // count and alphabetical order
  [
    "b",
    [
      ["button", 3],
      ["body", 1],
      [".bc", 3],
      [".ba", 1],
      [".bb", 1],
      ["#ba", 1],
      ["#bb", 1],
      ["#bc", 1],
    ],
  ],
];

const TEST_URL = `<span class="span" id="span">
                    <div class="div" id="div"></div>
                  </span>
                  <button class="ba bc" id="bc"></button>
                  <button class="bb bc" id="bb"></button>
                  <button class="bc" id="ba"></button>`;

add_task(async function () {
  const { inspector } = await openInspectorForURL(
    "data:text/html;charset=utf-8," + encodeURI(TEST_URL)
  );

  const searchBox = inspector.panelWin.document.getElementById(
    "inspector-searchbox"
  );
  const popup = inspector.searchSuggestions.searchPopup;

  await focusSearchBoxUsingShortcut(inspector.panelWin);

  for (const [key, expectedSuggestions] of KEY_STATES) {
    info(
      "pressing key " +
        key +
        " to get suggestions " +
        JSON.stringify(expectedSuggestions)
    );

    const onCommand = once(searchBox, "input", true);
    const onSearchProcessingDone =
      inspector.searchSuggestions.once("processing-done");
    EventUtils.synthesizeKey(key, {}, inspector.panelWin);
    await onCommand;

    info("Waiting for the suggestions to be retrieved");
    await onSearchProcessingDone;

    const actualSuggestions = popup.getItems();
    is(
      popup.isOpen ? actualSuggestions.length : 0,
      expectedSuggestions.length,
      "There are expected number of suggestions"
    );

    for (let i = 0; i < expectedSuggestions.length; i++) {
      is(
        expectedSuggestions[i][0],
        actualSuggestions[i].label,
        "The suggestion at " + i + "th index is correct."
      );
    }
  }
});