tor-browser

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

test_providerRecentSearches.js (5038B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 ChromeUtils.defineESModuleGetters(this, {
      7  setTimeout: "resource://gre/modules/Timer.sys.mjs",
      8  UrlbarPrefs: "moz-src:///browser/components/urlbar/UrlbarPrefs.sys.mjs",
      9 });
     10 
     11 let ENABLED_PREF = "recentsearches.featureGate";
     12 let EXPIRE_PREF = "recentsearches.expirationMs";
     13 let SUGGESTS_PREF = "suggest.recentsearches";
     14 
     15 let TEST_SEARCHES = ["Bob Vylan", "Glasgow Weather", "Joy Formidable"];
     16 let defaultEngine;
     17 
     18 function makeRecentSearchResult(context, engine, suggestion) {
     19  let result = makeFormHistoryResult(context, {
     20    suggestion,
     21    engineName: engine.name,
     22  });
     23  delete result.payload.lowerCaseSuggestion;
     24  return result;
     25 }
     26 
     27 async function addSearches(searches = TEST_SEARCHES) {
     28  // Add the searches sequentially so they get a new timestamp
     29  // and we can order by the time added.
     30  for (let search of searches) {
     31    // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
     32    await new Promise(r => setTimeout(r, 10));
     33    await UrlbarTestUtils.formHistory.add([
     34      { value: search, source: defaultEngine.name },
     35    ]);
     36  }
     37 }
     38 
     39 add_setup(async () => {
     40  defaultEngine = await addTestSuggestionsEngine();
     41  await Services.search.setDefault(
     42    defaultEngine,
     43    Ci.nsISearchService.CHANGE_REASON_ADDON_INSTALL
     44  );
     45 
     46  let oldCurrentEngine = Services.search.defaultEngine;
     47 
     48  registerCleanupFunction(async () => {
     49    await Services.search.setDefault(
     50      oldCurrentEngine,
     51      Ci.nsISearchService.CHANGE_REASON_ADDON_INSTALL
     52    );
     53    UrlbarPrefs.clear(ENABLED_PREF);
     54    UrlbarPrefs.clear(SUGGESTS_PREF);
     55  });
     56 });
     57 
     58 add_task(async function test_enabled() {
     59  UrlbarPrefs.set(ENABLED_PREF, true);
     60  UrlbarPrefs.set(SUGGESTS_PREF, true);
     61  await addSearches();
     62  let context = createContext("", { isPrivate: false });
     63  await check_results({
     64    context,
     65    matches: [
     66      makeRecentSearchResult(context, defaultEngine, "Joy Formidable"),
     67      makeRecentSearchResult(context, defaultEngine, "Glasgow Weather"),
     68      makeRecentSearchResult(context, defaultEngine, "Bob Vylan"),
     69    ],
     70  });
     71 });
     72 
     73 add_task(async function test_disabled() {
     74  UrlbarPrefs.set(ENABLED_PREF, false);
     75  UrlbarPrefs.set(SUGGESTS_PREF, false);
     76  await addSearches();
     77  await check_results({
     78    context: createContext("", { isPrivate: false }),
     79    matches: [],
     80  });
     81 });
     82 
     83 add_task(async function test_most_recent_shown() {
     84  UrlbarPrefs.set(ENABLED_PREF, true);
     85  UrlbarPrefs.set(SUGGESTS_PREF, true);
     86 
     87  await addSearches(Array.from(Array(10).keys()).map(i => `Search ${i}`));
     88  let context = createContext("", { isPrivate: false });
     89  await check_results({
     90    context,
     91    matches: [
     92      makeRecentSearchResult(context, defaultEngine, "Search 9"),
     93      makeRecentSearchResult(context, defaultEngine, "Search 8"),
     94      makeRecentSearchResult(context, defaultEngine, "Search 7"),
     95      makeRecentSearchResult(context, defaultEngine, "Search 6"),
     96      makeRecentSearchResult(context, defaultEngine, "Search 5"),
     97    ],
     98  });
     99  await UrlbarTestUtils.formHistory.clear();
    100 });
    101 
    102 add_task(async function test_per_engine() {
    103  UrlbarPrefs.set(ENABLED_PREF, true);
    104  UrlbarPrefs.set(SUGGESTS_PREF, true);
    105 
    106  let oldEngine = defaultEngine;
    107  await addSearches();
    108 
    109  defaultEngine = await addTestSuggestionsEngine(null, {
    110    name: "NewTestEngine",
    111  });
    112  await Services.search.setDefault(
    113    defaultEngine,
    114    Ci.nsISearchService.CHANGE_REASON_ADDON_INSTALL
    115  );
    116 
    117  await addSearches();
    118 
    119  let context = createContext("", {
    120    isPrivate: false,
    121  });
    122  await check_results({
    123    context,
    124    matches: [
    125      makeRecentSearchResult(context, defaultEngine, "Joy Formidable"),
    126      makeRecentSearchResult(context, defaultEngine, "Glasgow Weather"),
    127      makeRecentSearchResult(context, defaultEngine, "Bob Vylan"),
    128    ],
    129  });
    130 
    131  defaultEngine = oldEngine;
    132  await Services.search.setDefault(
    133    defaultEngine,
    134    Ci.nsISearchService.CHANGE_REASON_ADDON_INSTALL
    135  );
    136 
    137  info("We only show searches made since last default engine change");
    138  context = createContext("", { isPrivate: false });
    139  await check_results({
    140    context,
    141    matches: [],
    142  });
    143  await UrlbarTestUtils.formHistory.clear();
    144 });
    145 
    146 add_task(async function test_expiry() {
    147  UrlbarPrefs.set(ENABLED_PREF, true);
    148  UrlbarPrefs.set(SUGGESTS_PREF, true);
    149  await addSearches();
    150  let context = createContext("", { isPrivate: false });
    151  await check_results({
    152    context,
    153    matches: [
    154      makeRecentSearchResult(context, defaultEngine, "Joy Formidable"),
    155      makeRecentSearchResult(context, defaultEngine, "Glasgow Weather"),
    156      makeRecentSearchResult(context, defaultEngine, "Bob Vylan"),
    157    ],
    158  });
    159 
    160  let shortExpiration = 100;
    161  UrlbarPrefs.set(EXPIRE_PREF, shortExpiration.toString());
    162  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    163  await new Promise(r => setTimeout(r, shortExpiration * 2));
    164 
    165  await check_results({
    166    context: createContext("", { isPrivate: false }),
    167    matches: [],
    168  });
    169 });