tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_dbg-quick-open.js (5473B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 // Testing quick open
      6 
      7 "use strict";
      8 
      9 add_task(async function () {
     10  const dbg = await initDebugger("doc-script-switching.html");
     11 
     12  // Inject lots of sources to go beyond the maximum limit of displayed sources (set to 100)
     13  const injectedSources = await SpecialPowers.spawn(
     14    gBrowser.selectedBrowser,
     15    [],
     16    function () {
     17      const sources = [];
     18      for (let i = 1; i <= 200; i++) {
     19        const value = String(i).padStart(3, "0");
     20        content.eval(
     21          `function evalSource() {}; //# sourceURL=eval-source-${value}.js`
     22        );
     23        sources.push(`eval-source-${value}.js`);
     24      }
     25      return sources;
     26    }
     27  );
     28  await waitForSources(dbg, ...injectedSources);
     29 
     30  info("Test opening and closing");
     31  await quickOpen(dbg, "");
     32  // Only the first 100 results are shown in the  quick open menu
     33  await waitForResults(dbg, injectedSources.slice(0, 100));
     34  is(resultCount(dbg), 100, "100 file results");
     35  pressKey(dbg, "Escape");
     36  assertQuickOpenDisabled(dbg);
     37 
     38  info("Testing the number of results for source search");
     39  await quickOpen(dbg, "sw");
     40  await waitForResults(dbg, [undefined, undefined]);
     41  is(resultCount(dbg), 2, "two file results");
     42  pressKey(dbg, "Escape");
     43 
     44  // We ensure that sources after maxResult limit are visible
     45  info("Test that first and last eval source are visible");
     46  await quickOpen(dbg, "eval-source-001.js");
     47  await waitForResults(dbg, ["eval-source-001.js"]);
     48  is(resultCount(dbg), 1, "one file result");
     49  pressKey(dbg, "Escape");
     50  await quickOpen(dbg, "eval-source-200.js");
     51  await waitForResults(dbg, ["eval-source-200.js"]);
     52  is(resultCount(dbg), 1, "one file result");
     53  pressKey(dbg, "Escape");
     54 
     55  info("Testing source search and check to see if source is selected");
     56  await waitForSource(dbg, "script-switching-01.js");
     57  await quickOpen(dbg, "sw1");
     58  await waitForResults(dbg, ["script-switching-01.js"]);
     59  is(resultCount(dbg), 1, "one file results");
     60  pressKey(dbg, "Enter");
     61  await waitForSelectedSource(dbg, "script-switching-01.js");
     62 
     63  info("Test that results show tab icons");
     64  await quickOpen(dbg, "sw1");
     65  await waitForResults(dbg, ["script-switching-01.js"]);
     66  await assertResultIsTab(dbg, 1);
     67  pressKey(dbg, "Tab");
     68 
     69  info(
     70    "Testing arrow keys in source search and check to see if source is selected"
     71  );
     72  await quickOpen(dbg, "sw2");
     73  await waitForResults(dbg, ["script-switching-02.js"]);
     74  is(resultCount(dbg), 1, "one file results");
     75  pressKey(dbg, "Down");
     76  pressKey(dbg, "Enter");
     77  await waitForSelectedSource(dbg, "script-switching-02.js");
     78 
     79  info("Testing tab closes the search");
     80  await quickOpen(dbg, "sw");
     81  await waitForResults(dbg, [undefined, undefined]);
     82  pressKey(dbg, "Tab");
     83  assertQuickOpenDisabled(dbg);
     84 
     85  info("Testing function search (anonymous fuctions should not display)");
     86  await quickOpen(dbg, "", "quickOpenFunc");
     87  await waitForResults(dbg, ["secondCall", "foo"]);
     88  is(resultCount(dbg), 2, "two function results");
     89 
     90  type(dbg, "@x");
     91  await waitForResults(dbg, []);
     92  is(resultCount(dbg), 0, "no functions with 'x' in name");
     93 
     94  pressKey(dbg, "Escape");
     95  assertQuickOpenDisabled(dbg);
     96 
     97  info("Testing goto line:column");
     98  assertLine(dbg, 0);
     99  assertColumn(dbg, 1);
    100  await quickOpen(dbg, ":7:12");
    101  await waitForResults(dbg, [undefined, undefined]);
    102  pressKey(dbg, "Enter");
    103  await waitForSelectedSource(dbg, "script-switching-02.js");
    104  await waitForSelectedLocation(dbg, 7, 12);
    105  assertLine(dbg, 7);
    106  assertColumn(dbg, 12);
    107 
    108  info("Testing gotoSource");
    109  await quickOpen(dbg, "sw1:5");
    110  await waitForResults(dbg, ["script-switching-01.js"]);
    111  pressKey(dbg, "Enter");
    112  await waitForSelectedSource(dbg, "script-switching-01.js");
    113  assertLine(dbg, 5);
    114 });
    115 
    116 function assertQuickOpenEnabled(dbg) {
    117  is(dbg.selectors.getQuickOpenEnabled(), true, "quickOpen enabled");
    118 }
    119 
    120 function assertQuickOpenDisabled(dbg) {
    121  is(dbg.selectors.getQuickOpenEnabled(), false, "quickOpen disabled");
    122 }
    123 
    124 function assertLine(dbg, lineNumber) {
    125  is(
    126    dbg.selectors.getSelectedLocation().line,
    127    lineNumber,
    128    `goto line is ${lineNumber}`
    129  );
    130 }
    131 
    132 function assertColumn(dbg, columnNumber) {
    133  let value = dbg.selectors.getSelectedLocation().column;
    134  if (value === undefined) {
    135    value = null;
    136  } else {
    137    // column is 0-based, while we want to mention 1-based in the test.
    138    value++;
    139  }
    140  is(value, columnNumber, `goto column is ${columnNumber}`);
    141 }
    142 
    143 function resultCount(dbg) {
    144  return findAllElements(dbg, "resultItems").length;
    145 }
    146 
    147 async function quickOpen(dbg, query, shortcut = "quickOpen") {
    148  pressKey(dbg, shortcut);
    149  assertQuickOpenEnabled(dbg);
    150  query !== "" && type(dbg, query);
    151 }
    152 
    153 async function waitForResults(dbg, results) {
    154  await waitForAllElements(dbg, "resultItems", results.length, true);
    155 
    156  for (let i = 0; i < results.length; ++i) {
    157    if (results[i] !== undefined) {
    158      await waitForElement(dbg, "resultItemName", results[i], i + 1);
    159    }
    160  }
    161 }
    162 
    163 function findResultEl(dbg, index = 1) {
    164  return waitForElementWithSelector(dbg, `.result-item:nth-child(${index})`);
    165 }
    166 
    167 async function assertResultIsTab(dbg, index) {
    168  const el = await findResultEl(dbg, index);
    169  ok(
    170    el && !!el.querySelector(".dbg-img-tab.dbg-img-result-item-icon"),
    171    "Result should be a tab"
    172  );
    173 }