tor-browser

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

browser_901207_searchbar_in_panel.js (4542B)


      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 "use strict";
      6 
      7 logActiveElement();
      8 
      9 async function waitForSearchBarFocus() {
     10  let searchbar = document.getElementById(
     11    Services.prefs.getBoolPref("browser.search.widget.new")
     12      ? "searchbar-new"
     13      : "searchbar"
     14  );
     15  await TestUtils.waitForCondition(function () {
     16    logActiveElement();
     17    return document.activeElement === searchbar.inputField;
     18  });
     19 }
     20 
     21 add_setup(async function () {
     22  await SpecialPowers.pushPrefEnv({
     23    set: [["browser.search.widget.new", false]],
     24  });
     25 });
     26 
     27 // Ctrl+K should open the menu panel and focus the search bar if the search bar is in the panel.
     28 add_task(async function check_shortcut_when_in_closed_overflow_panel_closed() {
     29  CustomizableUI.addWidgetToArea(
     30    "search-container",
     31    CustomizableUI.AREA_FIXED_OVERFLOW_PANEL
     32  );
     33 
     34  let shownPanelPromise = promiseOverflowShown(window);
     35  sendWebSearchKeyCommand();
     36  await shownPanelPromise;
     37 
     38  await waitForSearchBarFocus();
     39 
     40  let hiddenPanelPromise = promiseOverflowHidden(window);
     41  EventUtils.synthesizeKey("KEY_Escape");
     42  await hiddenPanelPromise;
     43  CustomizableUI.reset();
     44 });
     45 
     46 // Ctrl+K should give focus to the searchbar when the searchbar is in the menupanel and the panel is already opened.
     47 add_task(async function check_shortcut_when_in_opened_overflow_panel() {
     48  CustomizableUI.addWidgetToArea(
     49    "search-container",
     50    CustomizableUI.AREA_FIXED_OVERFLOW_PANEL
     51  );
     52 
     53  await document.getElementById("nav-bar").overflowable.show();
     54 
     55  sendWebSearchKeyCommand();
     56 
     57  await waitForSearchBarFocus();
     58 
     59  let hiddenPanelPromise = promiseOverflowHidden(window);
     60  EventUtils.synthesizeKey("KEY_Escape");
     61  await hiddenPanelPromise;
     62  CustomizableUI.reset();
     63 });
     64 
     65 // Ctrl+K should open the overflow panel and focus the search bar if the search bar is overflowed.
     66 add_task(async function check_shortcut_when_in_overflow() {
     67  this.originalWindowWidth = window.outerWidth;
     68  let navbar = document.getElementById(CustomizableUI.AREA_NAVBAR);
     69  ok(
     70    !navbar.hasAttribute("overflowing"),
     71    "Should start with a non-overflowing toolbar."
     72  );
     73  ok(CustomizableUI.inDefaultState, "Should start in default state.");
     74 
     75  await gCUITestUtils.addSearchBar();
     76 
     77  window.resizeTo(kForceOverflowWidthPx, window.outerHeight);
     78  await TestUtils.waitForCondition(() => {
     79    return (
     80      navbar.getAttribute("overflowing") == "true" &&
     81      !navbar.querySelector("#search-container")
     82    );
     83  });
     84  ok(
     85    !navbar.querySelector("#search-container"),
     86    "Search container should be overflowing"
     87  );
     88 
     89  let shownPanelPromise = promiseOverflowShown(window);
     90  sendWebSearchKeyCommand();
     91  await shownPanelPromise;
     92 
     93  let chevron = document.getElementById("nav-bar-overflow-button");
     94  await TestUtils.waitForCondition(() => chevron.open);
     95 
     96  await waitForSearchBarFocus();
     97 
     98  let hiddenPanelPromise = promiseOverflowHidden(window);
     99  EventUtils.synthesizeKey("KEY_Escape");
    100  await hiddenPanelPromise;
    101 
    102  gCUITestUtils.removeSearchBar();
    103 
    104  navbar = document.getElementById(CustomizableUI.AREA_NAVBAR);
    105  window.resizeTo(this.originalWindowWidth, window.outerHeight);
    106  await TestUtils.waitForCondition(() => !navbar.hasAttribute("overflowing"));
    107  ok(
    108    !navbar.hasAttribute("overflowing"),
    109    "Should not have an overflowing toolbar."
    110  );
    111 });
    112 
    113 // Ctrl+K should focus the search bar if it is in the navbar and not overflowing.
    114 add_task(async function check_shortcut_when_not_in_overflow() {
    115  await gCUITestUtils.addSearchBar();
    116  let placement = CustomizableUI.getPlacementOfWidget("search-container");
    117  is(placement.area, CustomizableUI.AREA_NAVBAR, "Should be in nav-bar");
    118 
    119  sendWebSearchKeyCommand();
    120 
    121  // This fails if the screen resolution is small and the search bar overflows
    122  // from the nav bar even with the original window width.
    123  await waitForSearchBarFocus();
    124 
    125  gCUITestUtils.removeSearchBar();
    126 });
    127 
    128 function sendWebSearchKeyCommand() {
    129  document.documentElement.focus();
    130  EventUtils.synthesizeKey("k", { accelKey: true });
    131 }
    132 
    133 function logActiveElement() {
    134  let element = document.activeElement;
    135  let str = "";
    136  while (element && element.parentNode) {
    137    str =
    138      " (" +
    139      element.localName +
    140      "#" +
    141      element.id +
    142      "." +
    143      [...element.classList].join(".") +
    144      ") >" +
    145      str;
    146    element = element.parentNode;
    147  }
    148  info("Active element: " + element ? str : "null");
    149 }