tor-browser

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

test_providerTabToSearch_partialHost.js (8028B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Search engine origins are autofilled normally when they get over the
      5 // threshold, though certain origins redirect to localized subdomains, that
      6 // the user is unlikely to type, for example wikipedia.org => en.wikipedia.org.
      7 // We should get a tab to search result also for these cases, where a normal
      8 // autofill wouldn't happen.
      9 
     10 "use strict";
     11 
     12 ChromeUtils.defineESModuleGetters(this, {
     13  UrlbarProviderAutofill:
     14    "moz-src:///browser/components/urlbar/UrlbarProviderAutofill.sys.mjs",
     15 });
     16 
     17 add_setup(async function () {
     18  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
     19  Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);
     20  // Disable tab-to-search onboarding results.
     21  Services.prefs.setIntPref(
     22    "browser.urlbar.tabToSearch.onboard.interactionsLeft",
     23    0
     24  );
     25  Services.prefs.setBoolPref(
     26    "browser.search.separatePrivateDefault.ui.enabled",
     27    false
     28  );
     29 
     30  registerCleanupFunction(() => {
     31    Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
     32    Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
     33    Services.prefs.clearUserPref(
     34      "browser.search.separatePrivateDefault.ui.enabled"
     35    );
     36    Services.prefs.clearUserPref(
     37      "browser.urlbar.tabToSearch.onboard.interactionsLeft"
     38    );
     39  });
     40 });
     41 
     42 add_task(async function test() {
     43  let url = "https://en.example.com/";
     44  await SearchTestUtils.installSearchExtension(
     45    {
     46      name: "TestEngine",
     47      search_url: url,
     48    },
     49    { setAsDefault: true }
     50  );
     51 
     52  // Make sure the engine domain would be autofilled.
     53  await PlacesUtils.bookmarks.insert({
     54    url,
     55    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
     56    title: "bookmark",
     57  });
     58 
     59  info("Test matching cases");
     60 
     61  for (let searchStr of ["ex", "example.c"]) {
     62    info("Searching for " + searchStr);
     63    let context = createContext(searchStr, { isPrivate: false });
     64    await check_results({
     65      context,
     66      matches: [
     67        makeSearchResult(context, {
     68          engineName: Services.search.defaultEngine.name,
     69          providerName: "UrlbarProviderHeuristicFallback",
     70          heuristic: true,
     71        }),
     72        makeSearchResult(context, {
     73          engineName: "TestEngine",
     74          engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS,
     75          searchUrlDomainWithoutSuffix: "en.example.",
     76          providesSearchMode: true,
     77          query: "",
     78          providerName: "UrlbarProviderTabToSearch",
     79          satisfiesAutofillThreshold: true,
     80        }),
     81        makeBookmarkResult(context, {
     82          uri: url,
     83          title: "bookmark",
     84        }),
     85      ],
     86    });
     87  }
     88 
     89  info("Test a www engine");
     90  let url2 = "https://www.it.mochi.com/";
     91  await SearchTestUtils.installSearchExtension({
     92    name: "TestEngine2",
     93    search_url: url2,
     94  });
     95 
     96  let engine2 = Services.search.getEngineByName("TestEngine2");
     97  // Make sure the engine domain would be autofilled.
     98  await PlacesUtils.bookmarks.insert({
     99    url: url2,
    100    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    101    title: "bookmark",
    102  });
    103 
    104  for (let searchStr of ["mo", "mochi.c"]) {
    105    info("Searching for " + searchStr);
    106    let context = createContext(searchStr, { isPrivate: false });
    107    await check_results({
    108      context,
    109      matches: [
    110        makeSearchResult(context, {
    111          engineName: Services.search.defaultEngine.name,
    112          providerName: "UrlbarProviderHeuristicFallback",
    113          heuristic: true,
    114        }),
    115        makeSearchResult(context, {
    116          engineName: engine2.name,
    117          engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS,
    118          searchUrlDomainWithoutSuffix: "www.it.mochi.",
    119          providesSearchMode: true,
    120          query: "",
    121          providerName: "UrlbarProviderTabToSearch",
    122          satisfiesAutofillThreshold: true,
    123        }),
    124        makeBookmarkResult(context, {
    125          uri: url2,
    126          title: "bookmark",
    127        }),
    128      ],
    129    });
    130  }
    131 
    132  info("Test only base domain gets autofilled");
    133  let url3 = "https://search.foo.com/";
    134  await SearchTestUtils.installSearchExtension(
    135    {
    136      name: "TestEngine3",
    137      search_url: url3,
    138    },
    139    { setAsDefault: true }
    140  );
    141 
    142  // Make sure the base domain will be autofilled.
    143  await PlacesUtils.bookmarks.insert({
    144    url: "https://foo.com/",
    145    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    146    title: "bookmark",
    147  });
    148 
    149  for (let searchStr of ["fo", "foo.c"]) {
    150    info("Searching for " + searchStr);
    151    let context = createContext(searchStr, { isPrivate: false });
    152    await check_results({
    153      context,
    154      autofilled: "foo.com/",
    155      completed: "https://foo.com/",
    156      matches: [
    157        makeVisitResult(context, {
    158          uri: "https://foo.com/",
    159          title: "bookmark",
    160          heuristic: true,
    161          providerName: "UrlbarProviderAutofill",
    162        }),
    163        makeSearchResult(context, {
    164          engineName: "TestEngine3",
    165          engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS,
    166          searchUrlDomainWithoutSuffix: "search.foo.",
    167          providesSearchMode: true,
    168          query: "",
    169          providerName: "UrlbarProviderTabToSearch",
    170          satisfiesAutofillThreshold: true,
    171        }),
    172      ],
    173    });
    174  }
    175 
    176  info("Test non-matching cases");
    177 
    178  for (let searchStr of ["www.en", "www.ex", "https://ex"]) {
    179    info("Searching for " + searchStr);
    180    let context = createContext(searchStr, { isPrivate: false });
    181    // We don't want to generate all the possible results here, just check
    182    // the heuristic result is not autofill.
    183    let controller = UrlbarTestUtils.newMockController();
    184    await UrlbarProvidersManager.startQuery(context, controller);
    185    Assert.ok(context.results[0].heuristic, "Check heuristic result");
    186    Assert.notEqual(context.results[0].providerName, "UrlbarProviderAutofill");
    187  }
    188 
    189  info("Tab-to-search is not shown when an unrelated site is autofilled.");
    190  let wikiUrl = "https://wikipedia.org/";
    191  await SearchTestUtils.installSearchExtension({
    192    name: "FakeWikipedia",
    193    search_url: url,
    194  });
    195  let wikiEngine = Services.search.getEngineByName("TestEngine");
    196 
    197  // Make sure that wikiUrl will pass getTopHostOverThreshold.
    198  await PlacesUtils.bookmarks.insert({
    199    url: wikiUrl,
    200    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    201    title: "Wikipedia",
    202  });
    203 
    204  // Make sure an unrelated www site is autofilled.
    205  let wwwUrl = "https://www.example.com";
    206  await PlacesUtils.bookmarks.insert({
    207    url: wwwUrl,
    208    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    209    title: "Example",
    210  });
    211 
    212  let searchStr = "w";
    213  let context = createContext(searchStr, {
    214    isPrivate: false,
    215    sources: [UrlbarUtils.RESULT_SOURCE.BOOKMARKS],
    216  });
    217  let host = await UrlbarProviderAutofill.getTopHostOverThreshold(context, [
    218    wikiEngine.searchUrlDomain,
    219  ]);
    220  Assert.equal(
    221    host,
    222    wikiEngine.searchUrlDomain,
    223    "The search satisfies the autofill threshold requirement."
    224  );
    225  await check_results({
    226    context,
    227    autofilled: "www.example.com/",
    228    completed: "https://www.example.com/",
    229    matches: [
    230      makeVisitResult(context, {
    231        uri: `${wwwUrl}/`,
    232        title: "Example",
    233        heuristic: true,
    234        providerName: "UrlbarProviderAutofill",
    235      }),
    236      // Note that tab-to-search is not shown.
    237      makeBookmarkResult(context, {
    238        uri: wikiUrl,
    239        title: "Wikipedia",
    240      }),
    241      makeBookmarkResult(context, {
    242        uri: url2,
    243        title: "bookmark",
    244      }),
    245    ],
    246  });
    247 
    248  info("Restricting to history should not autofill our bookmark");
    249  context = createContext("ex", {
    250    isPrivate: false,
    251    sources: [UrlbarUtils.RESULT_SOURCE.HISTORY],
    252  });
    253  let controller = UrlbarTestUtils.newMockController();
    254  await UrlbarProvidersManager.startQuery(context, controller);
    255  Assert.ok(context.results[0].heuristic, "Check heuristic result");
    256  Assert.notEqual(context.results[0].providerName, "UrlbarProviderAutofill");
    257 
    258  await cleanupPlaces();
    259 });