summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_reverse_search_keyboard_navigation.js
blob: ec8aab19249dd12f3834001acaa9fb96b363296f (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Tests reverse search results keyboard navigation.

"use strict";

const TEST_URI = `data:text/html,<!DOCTYPE html><meta charset=utf8>Test reverse search`;
const isMacOS = AppConstants.platform === "macosx";

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  const jstermHistory = [
    `document`,
    `document
       .querySelectorAll("*")
       .forEach(console.log)`,
    `Dog = "Snoopy"`,
  ];

  const onLastMessage = waitForMessageByType(hud, `"Snoopy"`, ".result");
  for (const input of jstermHistory) {
    execute(hud, input);
  }
  await onLastMessage;

  await openReverseSearch(hud);
  EventUtils.sendString("d");
  const infoElement = await waitFor(() => getReverseSearchInfoElement(hud));
  is(
    infoElement.textContent,
    "3 of 3 results",
    "The reverse info has the expected text"
  );

  is(getInputValue(hud), jstermHistory[2], "JsTerm has the expected input");
  is(
    hud.jsterm.autocompletePopup.isOpen,
    false,
    "Setting the input value did not trigger the autocompletion"
  );

  await navigateResultsAndCheckState(hud, {
    direction: "previous",
    expectedInfoText: "2 of 3 results",
    expectedJsTermInputValue: jstermHistory[1],
  });

  await navigateResultsAndCheckState(hud, {
    direction: "previous",
    expectedInfoText: "1 of 3 results",
    expectedJsTermInputValue: jstermHistory[0],
  });

  info(
    "Check that we go back to the last matching item if we were at the first"
  );
  await navigateResultsAndCheckState(hud, {
    direction: "previous",
    expectedInfoText: "3 of 3 results",
    expectedJsTermInputValue: jstermHistory[2],
  });

  await navigateResultsAndCheckState(hud, {
    direction: "next",
    expectedInfoText: "1 of 3 results",
    expectedJsTermInputValue: jstermHistory[0],
  });

  await navigateResultsAndCheckState(hud, {
    direction: "next",
    expectedInfoText: "2 of 3 results",
    expectedJsTermInputValue: jstermHistory[1],
  });

  await navigateResultsAndCheckState(hud, {
    direction: "next",
    expectedInfoText: "3 of 3 results",
    expectedJsTermInputValue: jstermHistory[2],
  });

  info(
    "Check that trying to navigate when there's only 1 result does not throw"
  );
  EventUtils.sendString("og");
  await waitFor(
    () => getReverseSearchInfoElement(hud).textContent === "1 result"
  );
  triggerPreviousResultShortcut();
  triggerNextResultShortcut();

  info("Check that trying to navigate when there's no result does not throw");
  EventUtils.sendString("g");
  await waitFor(
    () => getReverseSearchInfoElement(hud).textContent === "No results"
  );
  triggerPreviousResultShortcut();
  triggerNextResultShortcut();
});

async function navigateResultsAndCheckState(
  hud,
  { direction, expectedInfoText, expectedJsTermInputValue }
) {
  const onJsTermValueChanged = hud.jsterm.once("set-input-value");
  if (direction === "previous") {
    triggerPreviousResultShortcut();
  } else {
    triggerNextResultShortcut();
  }
  await onJsTermValueChanged;

  is(getInputValue(hud), expectedJsTermInputValue, "JsTerm has expected value");

  const infoElement = getReverseSearchInfoElement(hud);
  is(
    infoElement.textContent,
    expectedInfoText,
    "The reverse info has the expected text"
  );
  is(
    isReverseSearchInputFocused(hud),
    true,
    "reverse search input is still focused"
  );
}

function triggerPreviousResultShortcut() {
  if (isMacOS) {
    EventUtils.synthesizeKey("r", { ctrlKey: true });
  } else {
    EventUtils.synthesizeKey("VK_F9");
  }
}

function triggerNextResultShortcut() {
  if (isMacOS) {
    EventUtils.synthesizeKey("s", { ctrlKey: true });
  } else {
    EventUtils.synthesizeKey("VK_F9", { shiftKey: true });
  }
}