summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg-quick-open.js
blob: 5a1b76a7bd3bc2282b2b6f24c78818162f8aae59 (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
/* 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/>. */

// Testing quick open

"use strict";

add_task(async function () {
  const dbg = await initDebugger("doc-script-switching.html");

  // Inject lots of sources to go beyond the maximum limit of displayed sources (set to 100)
  const injectedSources = await SpecialPowers.spawn(
    gBrowser.selectedBrowser,
    [],
    function () {
      const sources = [];
      for (let i = 1; i <= 200; i++) {
        const value = String(i).padStart(3, "0");
        content.eval(
          `function evalSource() {}; //# sourceURL=eval-source-${value}.js`
        );
        sources.push(`eval-source-${value}.js`);
      }
      return sources;
    }
  );
  await waitForSources(dbg, ...injectedSources);

  info("Test opening and closing");
  await quickOpen(dbg, "");
  // Only the first 100 results are shown in the  quick open menu
  await waitForResults(dbg, injectedSources.slice(0, 100));
  is(resultCount(dbg), 100, "100 file results");
  pressKey(dbg, "Escape");
  assertQuickOpenDisabled(dbg);

  info("Testing the number of results for source search");
  await quickOpen(dbg, "sw");
  await waitForResults(dbg, [undefined, undefined]);
  is(resultCount(dbg), 2, "two file results");
  pressKey(dbg, "Escape");

  // We ensure that sources after maxResult limit are visible
  info("Test that first and last eval source are visible");
  await quickOpen(dbg, "eval-source-001.js");
  await waitForResults(dbg, ["eval-source-001.js"]);
  is(resultCount(dbg), 1, "one file result");
  pressKey(dbg, "Escape");
  await quickOpen(dbg, "eval-source-200.js");
  await waitForResults(dbg, ["eval-source-200.js"]);
  is(resultCount(dbg), 1, "one file result");
  pressKey(dbg, "Escape");

  info("Testing source search and check to see if source is selected");
  await waitForSource(dbg, "script-switching-01.js");
  await quickOpen(dbg, "sw1");
  await waitForResults(dbg, ["script-switching-01.js"]);
  is(resultCount(dbg), 1, "one file results");
  pressKey(dbg, "Enter");
  await waitForSelectedSource(dbg, "script-switching-01.js");

  info("Test that results show tab icons");
  await quickOpen(dbg, "sw1");
  await waitForResults(dbg, ["script-switching-01.js"]);
  await assertResultIsTab(dbg, 1);
  pressKey(dbg, "Tab");

  info(
    "Testing arrow keys in source search and check to see if source is selected"
  );
  await quickOpen(dbg, "sw2");
  await waitForResults(dbg, ["script-switching-02.js"]);
  is(resultCount(dbg), 1, "one file results");
  pressKey(dbg, "Down");
  pressKey(dbg, "Enter");
  await waitForSelectedSource(dbg, "script-switching-02.js");

  info("Testing tab closes the search");
  await quickOpen(dbg, "sw");
  await waitForResults(dbg, [undefined, undefined]);
  pressKey(dbg, "Tab");
  assertQuickOpenDisabled(dbg);

  info("Testing function search (anonymous fuctions should not display)");
  await quickOpen(dbg, "", "quickOpenFunc");
  await waitForResults(dbg, ["secondCall", "foo"]);
  is(resultCount(dbg), 2, "two function results");

  type(dbg, "@x");
  await waitForResults(dbg, []);
  is(resultCount(dbg), 0, "no functions with 'x' in name");

  pressKey(dbg, "Escape");
  assertQuickOpenDisabled(dbg);

  info("Testing goto line:column");
  assertLine(dbg, 0);
  assertColumn(dbg, null);
  await quickOpen(dbg, ":7:12");
  await waitForResults(dbg, [undefined, undefined]);
  pressKey(dbg, "Enter");
  await waitForSelectedSource(dbg, "script-switching-02.js");
  assertLine(dbg, 7);
  assertColumn(dbg, 12);

  info("Testing gotoSource");
  await quickOpen(dbg, "sw1:5");
  await waitForResults(dbg, ["script-switching-01.js"]);
  pressKey(dbg, "Enter");
  await waitForSelectedSource(dbg, "script-switching-01.js");
  assertLine(dbg, 5);
});

function assertQuickOpenEnabled(dbg) {
  is(dbg.selectors.getQuickOpenEnabled(), true, "quickOpen enabled");
}

function assertQuickOpenDisabled(dbg) {
  is(dbg.selectors.getQuickOpenEnabled(), false, "quickOpen disabled");
}

function assertLine(dbg, lineNumber) {
  is(
    dbg.selectors.getSelectedLocation().line,
    lineNumber,
    `goto line is ${lineNumber}`
  );
}

function assertColumn(dbg, columnNumber) {
  let value = dbg.selectors.getSelectedLocation().column;
  if (value === undefined) {
    value = null;
  } else {
    // column is 0-based, while we want to mention 1-based in the test.
    value++;
  }
  is(value, columnNumber, `goto column is ${columnNumber}`);
}

function resultCount(dbg) {
  return findAllElements(dbg, "resultItems").length;
}

async function quickOpen(dbg, query, shortcut = "quickOpen") {
  pressKey(dbg, shortcut);
  assertQuickOpenEnabled(dbg);
  query !== "" && type(dbg, query);
}

async function waitForResults(dbg, results) {
  await waitForAllElements(dbg, "resultItems", results.length, true);

  for (let i = 0; i < results.length; ++i) {
    if (results[i] !== undefined) {
      await waitForElement(dbg, "resultItemName", results[i], i + 1);
    }
  }
}

function findResultEl(dbg, index = 1) {
  return waitForElementWithSelector(dbg, `.result-item:nth-child(${index})`);
}

async function assertResultIsTab(dbg, index) {
  const el = await findResultEl(dbg, index);
  ok(
    el && !!el.querySelector(".tab.result-item-icon"),
    "Result should be a tab"
  );
}