tor-browser

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

browser_trendingsuggestions.js (3190B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { SearchTestUtils } = ChromeUtils.importESModule(
      5  "resource://testing-common/SearchTestUtils.sys.mjs"
      6 );
      7 
      8 const MAIN_PREF = "browser.search.suggest.enabled";
      9 const URLBAR_PREF = "browser.urlbar.suggest.searches";
     10 const TRENDING_PREF = "browser.urlbar.trending.featureGate";
     11 
     12 const TRENDING_CHECKBOX_ID = "showTrendingSuggestionsCheckbox";
     13 const SUGGESTIONED_CHECKBOX_ID = "suggestionsInSearchFieldsCheckbox";
     14 
     15 SearchTestUtils.init(this);
     16 
     17 add_setup(async function () {
     18  await SpecialPowers.pushPrefEnv({
     19    set: [
     20      [MAIN_PREF, true],
     21      [URLBAR_PREF, true],
     22      [TRENDING_PREF, true],
     23    ],
     24  });
     25 
     26  await SearchTestUtils.installSearchExtension({
     27    name: "engine1",
     28    search_url: "https://example.com/engine1",
     29    search_url_get_params: "search={searchTerms}",
     30  });
     31  const defaultEngine = await Services.search.getDefault();
     32 
     33  registerCleanupFunction(async () => {
     34    await Services.search.setDefault(
     35      defaultEngine,
     36      Ci.nsISearchService.CHANGE_REASON_UNKNOWN
     37    );
     38  });
     39 });
     40 
     41 add_task(async function testSuggestionsDisabled() {
     42  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     43  let doc = gBrowser.selectedBrowser.contentDocument;
     44  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);
     45  let suggestionsCheckbox = doc.getElementById(SUGGESTIONED_CHECKBOX_ID);
     46 
     47  Assert.ok(trendingCheckbox.checked, "Checkbox should be visible and checked");
     48  Assert.ok(!trendingCheckbox.disabled, "Checkbox should not be disabled");
     49 
     50  // Disable search suggestions.
     51  suggestionsCheckbox.click();
     52 
     53  await BrowserTestUtils.waitForCondition(
     54    () => trendingCheckbox.disabled,
     55    "Trending checkbox should be disabled when search suggestions are disabled"
     56  );
     57 
     58  // Clean up.
     59  Services.prefs.clearUserPref(MAIN_PREF);
     60  gBrowser.removeCurrentTab();
     61 });
     62 
     63 add_task(async function testNonTrendingEngine() {
     64  // Set engine that does not support trending suggestions as default.
     65  const engine1 = Services.search.getEngineByName("engine1");
     66  Services.search.setDefault(
     67    engine1,
     68    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
     69  );
     70  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     71  let doc = gBrowser.selectedBrowser.contentDocument;
     72  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);
     73 
     74  Assert.ok(
     75    trendingCheckbox.disabled,
     76    "Checkbox should be disabled when an engine that doesnt support trending suggestions is default"
     77  );
     78  gBrowser.removeCurrentTab();
     79 });
     80 
     81 add_task(async function testEnabledTrendingEngine() {
     82  const engine1 = Services.search.getEngineByName("Google");
     83  Services.search.setDefault(
     84    engine1,
     85    Ci.nsISearchService.CHANGE_REASON_UNKNOWN
     86  );
     87  await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true });
     88  let doc = gBrowser.selectedBrowser.contentDocument;
     89  let trendingCheckbox = doc.getElementById(TRENDING_CHECKBOX_ID);
     90 
     91  Assert.ok(
     92    !trendingCheckbox.disabled,
     93    "Checkbox should not be disabled when an engine that supports trending suggestions is default"
     94  );
     95  gBrowser.removeCurrentTab();
     96 });