tor-browser

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

browser_search_searchTerms.js (7207B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /*
      5  Tests the showSearchTerms option on the about:preferences#search page.
      6 */
      7 
      8 "use strict";
      9 
     10 ChromeUtils.defineLazyGetter(this, "QuickSuggestTestUtils", () => {
     11  const { QuickSuggestTestUtils: module } = ChromeUtils.importESModule(
     12    "resource://testing-common/QuickSuggestTestUtils.sys.mjs"
     13  );
     14  module.init(this);
     15  return module;
     16 });
     17 
     18 const { CustomizableUITestUtils } = ChromeUtils.importESModule(
     19  "resource://testing-common/CustomizableUITestUtils.sys.mjs"
     20 );
     21 let gCUITestUtils = new CustomizableUITestUtils(window);
     22 
     23 const CHECKBOX_ID = "searchShowSearchTermCheckbox";
     24 const PREF_SEARCHTERMS = "browser.urlbar.showSearchTerms.enabled";
     25 const PREF_FEATUREGATE = "browser.urlbar.showSearchTerms.featureGate";
     26 const PREF_SCOTCH_BONNET = "browser.urlbar.scotchBonnet.enableOverride";
     27 
     28 add_task(async function showSearchTermsVisibility_scotchBonnet() {
     29  await SpecialPowers.pushPrefEnv({
     30    set: [[PREF_SCOTCH_BONNET, false]],
     31  });
     32 
     33  await BrowserTestUtils.withNewTab(
     34    "about:preferences#search",
     35    async browser => {
     36      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
     37      Assert.ok(
     38        !BrowserTestUtils.isVisible(container),
     39        "The option box is not visible"
     40      );
     41    }
     42  );
     43 
     44  await SpecialPowers.pushPrefEnv({
     45    set: [[PREF_SCOTCH_BONNET, true]],
     46  });
     47 
     48  await BrowserTestUtils.withNewTab(
     49    "about:preferences#search",
     50    async browser => {
     51      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
     52      Assert.ok(
     53        BrowserTestUtils.isVisible(container),
     54        "The option box is visible"
     55      );
     56    }
     57  );
     58 
     59  await SpecialPowers.popPrefEnv();
     60 });
     61 
     62 // To avoid impacting users who could be using Persisted Search but not Scotch
     63 // Bonnet, deprecate the feature gate preference only after Scotch Bonnet is
     64 // enabled by default.
     65 add_task(async function showSearchTermsVisibility_featureGate() {
     66  await SpecialPowers.pushPrefEnv({
     67    set: [
     68      [PREF_SCOTCH_BONNET, false],
     69      [PREF_FEATUREGATE, false],
     70    ],
     71  });
     72  await BrowserTestUtils.withNewTab(
     73    "about:preferences#search",
     74    async browser => {
     75      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
     76      Assert.ok(
     77        !BrowserTestUtils.isVisible(container),
     78        "The option box is not visible"
     79      );
     80    }
     81  );
     82  await SpecialPowers.popPrefEnv();
     83 
     84  await SpecialPowers.pushPrefEnv({
     85    set: [
     86      [PREF_SCOTCH_BONNET, false],
     87      [PREF_FEATUREGATE, true],
     88    ],
     89  });
     90  await BrowserTestUtils.withNewTab(
     91    "about:preferences#search",
     92    async browser => {
     93      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
     94      Assert.ok(
     95        BrowserTestUtils.isVisible(container),
     96        "The option box is visible"
     97      );
     98    }
     99  );
    100  await SpecialPowers.popPrefEnv();
    101 
    102  await SpecialPowers.pushPrefEnv({
    103    set: [
    104      [PREF_SCOTCH_BONNET, true],
    105      [PREF_FEATUREGATE, false],
    106    ],
    107  });
    108  await BrowserTestUtils.withNewTab(
    109    "about:preferences#search",
    110    async browser => {
    111      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
    112      Assert.ok(
    113        BrowserTestUtils.isVisible(container),
    114        "The option box is visible"
    115      );
    116    }
    117  );
    118  await SpecialPowers.popPrefEnv();
    119 
    120  await SpecialPowers.pushPrefEnv({
    121    set: [
    122      [PREF_SCOTCH_BONNET, true],
    123      [PREF_FEATUREGATE, true],
    124    ],
    125  });
    126  await BrowserTestUtils.withNewTab(
    127    "about:preferences#search",
    128    async browser => {
    129      let container = browser.contentDocument.getElementById(CHECKBOX_ID);
    130      Assert.ok(
    131        BrowserTestUtils.isVisible(container),
    132        "The option box is visible"
    133      );
    134    }
    135  );
    136  await SpecialPowers.popPrefEnv();
    137 });
    138 
    139 /*
    140  Check using the checkbox modifies the preference.
    141 */
    142 add_task(async function showSearchTerms_checkbox() {
    143  // Enable the feature.
    144  await SpecialPowers.pushPrefEnv({
    145    set: [[PREF_FEATUREGATE, true]],
    146  });
    147  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    148  let doc = gBrowser.selectedBrowser.contentDocument;
    149 
    150  let option = doc.getElementById(CHECKBOX_ID);
    151 
    152  // Evaluate checkbox pref is true.
    153  Assert.ok(option.checked, "Option box should be checked.");
    154 
    155  // Evaluate checkbox when pref is false.
    156  await SpecialPowers.pushPrefEnv({
    157    set: [[PREF_SEARCHTERMS, false]],
    158  });
    159  Assert.ok(!option.checked, "Option box should not be checked.");
    160  await SpecialPowers.popPrefEnv();
    161 
    162  // Evaluate pref when checkbox is un-checked.
    163  await BrowserTestUtils.synthesizeMouseAtCenter(
    164    "#" + CHECKBOX_ID,
    165    {},
    166    gBrowser.selectedBrowser
    167  );
    168  Assert.equal(
    169    Services.prefs.getBoolPref(PREF_SEARCHTERMS),
    170    false,
    171    "Preference should be false if un-checked."
    172  );
    173 
    174  // Evaluate pref when checkbox is checked.
    175  await BrowserTestUtils.synthesizeMouseAtCenter(
    176    "#" + CHECKBOX_ID,
    177    {},
    178    gBrowser.selectedBrowser
    179  );
    180  Assert.equal(
    181    Services.prefs.getBoolPref(PREF_SEARCHTERMS),
    182    true,
    183    "Preference should be true if checked."
    184  );
    185 
    186  // Clean-up.
    187  Services.prefs.clearUserPref(PREF_SEARCHTERMS);
    188  gBrowser.removeCurrentTab();
    189  await SpecialPowers.popPrefEnv();
    190 });
    191 
    192 /*
    193  When loading the search preferences panel, the showSearchTerms checkbox
    194  should be hidden if the search bar is enabled.
    195 */
    196 add_task(async function showSearchTerms_and_searchBar_preference_load() {
    197  // Enable the feature.
    198  await SpecialPowers.pushPrefEnv({
    199    set: [
    200      [PREF_SCOTCH_BONNET, true],
    201      [PREF_FEATUREGATE, true],
    202    ],
    203  });
    204  await gCUITestUtils.addSearchBar();
    205 
    206  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    207  let doc = gBrowser.selectedBrowser.contentDocument;
    208 
    209  let checkbox = doc.getElementById(CHECKBOX_ID).parentElement;
    210  Assert.ok(
    211    checkbox.hidden,
    212    "showSearchTerms checkbox should be hidden when search bar is enabled."
    213  );
    214 
    215  // Clean-up.
    216  gBrowser.removeCurrentTab();
    217  await SpecialPowers.popPrefEnv();
    218  gCUITestUtils.removeSearchBar();
    219 });
    220 
    221 /*
    222  If the search bar is enabled while the search preferences panel is open, the
    223  showSearchTerms checkbox should not be clickable.
    224 */
    225 add_task(async function showSearchTerms_and_searchBar_preference_change() {
    226  // Enable the feature.
    227  await SpecialPowers.pushPrefEnv({
    228    set: [
    229      [PREF_SCOTCH_BONNET, true],
    230      [PREF_FEATUREGATE, true],
    231    ],
    232  });
    233 
    234  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
    235  let doc = gBrowser.selectedBrowser.contentDocument;
    236 
    237  let checkbox = doc.getElementById(CHECKBOX_ID).parentElement;
    238  Assert.ok(!checkbox.hidden, "showSearchTerms checkbox should be shown.");
    239 
    240  await gCUITestUtils.addSearchBar();
    241  Assert.ok(
    242    checkbox.hidden,
    243    "showSearchTerms checkbox should be hidden when search bar is enabled."
    244  );
    245 
    246  // Clean-up.
    247  gCUITestUtils.removeSearchBar();
    248  await TestUtils.waitForCondition(
    249    () => !checkbox.hidden,
    250    "Wait for preferences to know search bar has been removed."
    251  );
    252  Assert.ok(!checkbox.hidden, "showSearchTerms checkbox should be shown.");
    253 
    254  gBrowser.removeCurrentTab();
    255  await SpecialPowers.popPrefEnv();
    256 });