summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-search-file.js
blob: 57238dc36c7a15e88aff3f6cf59c8f02a4dbace3 (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
/* 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 the search bar correctly responds to queries, enter, shift enter

"use strict";

add_task(async function () {
  const dbg = await initDebugger("doc-scripts.html", "simple1.js");
  await selectSource(dbg, "simple1.js");

  pressKey(dbg, "fileSearch");
  is(dbg.selectors.getActiveSearch(), "file", "The search UI was opened");

  info("Test closing and re-opening the search UI");
  pressKey(dbg, "Escape");
  is(
    dbg.selectors.getActiveSearch(),
    null,
    "The search UI was closed when hitting Escape"
  );

  pressKey(dbg, "fileSearch");
  is(dbg.selectors.getActiveSearch(), "file", "The search UI was opened again");

  info("Search for `con` in the script");
  type(dbg, "con");
  await waitForSearchState(dbg);

  // All the lines in the script that include `con`
  const linesWithResults = [
    // const func
    4,
    // const result
    5,
    // constructor (in MyClass)
    42,
    // constructor (in Klass)
    55,
    // console.log
    62,
  ];

  await waitForCursorPosition(dbg, linesWithResults[0]);
  assertCursorPosition(
    dbg,
    linesWithResults[0],
    6,
    "typing in the search input moves the cursor in the source content"
  );

  info("Check that pressing Enter navigates forward through the results");
  await navigateWithKey(
    dbg,
    "Enter",
    linesWithResults[1],
    "Enter moves forward in the search results"
  );

  await navigateWithKey(
    dbg,
    "Enter",
    linesWithResults[2],
    "Enter moves forward in the search results"
  );

  info(
    "Check that pressing Shift+Enter navigates backward through the results"
  );
  await navigateWithKey(
    dbg,
    "ShiftEnter",
    linesWithResults[1],
    "Shift+Enter moves backward in the search results"
  );

  await navigateWithKey(
    dbg,
    "ShiftEnter",
    linesWithResults[0],
    "Shift+Enter moves backward in the search results"
  );

  info(
    "Check that navigating backward goes to the last result when we were at the first one"
  );
  await navigateWithKey(
    dbg,
    "ShiftEnter",
    linesWithResults.at(-1),
    "Shift+Enter cycles back through the results"
  );

  info(
    "Check that navigating forward goes to the first result when we were at the last one"
  );
  await navigateWithKey(
    dbg,
    "Enter",
    linesWithResults[0],
    "Enter cycles forward through the results"
  );

  info("Check that changing the search term works");
  pressKey(dbg, "fileSearch");
  type(dbg, "doEval");

  await waitForCursorPosition(dbg, 9);
  assertCursorPosition(
    dbg,
    9,
    16,
    "The UI navigates to the new search results"
  );

  // selecting another source keeps search open
  await selectSource(dbg, "simple2.js");
  ok(findElement(dbg, "searchField"), "Search field is still visible");

  // search is always focused regardless of when or how it was opened
  pressKey(dbg, "fileSearch");
  await clickElement(dbg, "codeMirror");
  pressKey(dbg, "fileSearch");
  is(dbg.win.document.activeElement.tagName, "INPUT", "Search field focused");
});

async function navigateWithKey(dbg, key, expectedLine) {
  pressKey(dbg, key);
  await waitForCursorPosition(dbg, expectedLine);
}