summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_search_engine_host.js
blob: 8135566547a7e2a79e4bdbf7e33ab7845a531ac0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

let engine;

add_task(async function test_searchEngine_autoFill() {
  Services.prefs.setBoolPref("browser.urlbar.autoFill.searchEngines", true);
  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
  await SearchTestUtils.installSearchExtension({
    name: "MySearchEngine",
    search_url: "https://my.search.com/",
  });
  engine = Services.search.getEngineByName("MySearchEngine");
  registerCleanupFunction(async () => {
    Services.prefs.clearUserPref("browser.urlbar.autoFill.searchEngines");
    Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
  });

  // Add an uri that matches the search string with high frecency.
  let uri = Services.io.newURI("http://www.example.com/my/");
  let visits = [];
  for (let i = 0; i < 100; ++i) {
    visits.push({ uri, title: "Terms - SearchEngine Search" });
  }
  await PlacesTestUtils.addVisits(visits);
  await PlacesTestUtils.addBookmarkWithDetails({
    uri,
    title: "Example bookmark",
  });
  await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
  ok(
    (await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", {
      url: uri,
    })) > 10000,
    "Added URI should have expected high frecency"
  );

  info(
    "Check search domain is autoFilled even if there's an higher frecency match"
  );
  let context = createContext("my", { isPrivate: false });
  await check_results({
    search: "my",
    autofilled: "my.search.com/",
    matches: [
      makePrioritySearchResult(context, {
        engineName: "MySearchEngine",
        heuristic: true,
      }),
    ],
  });

  await cleanupPlaces();
});

add_task(async function test_searchEngine_noautoFill() {
  Services.prefs.setIntPref(
    "browser.urlbar.tabToSearch.onboard.interactionsLeft",
    0
  );
  await PlacesTestUtils.addVisits(
    Services.io.newURI("http://my.search.com/samplepage/")
  );

  info("Check search domain is not autoFilled if it matches a visited domain");
  let context = createContext("my", { isPrivate: false });
  await check_results({
    context,
    autofilled: "my.search.com/",
    completed: "http://my.search.com/",
    matches: [
      // Note this result is a normal Autofill result and not a priority engine.
      makeVisitResult(context, {
        uri: "http://my.search.com/",
        fallbackTitle: "my.search.com",
        heuristic: true,
      }),
      makeSearchResult(context, {
        engineName: engine.name,
        engineIconUri: UrlbarUtils.ICON.SEARCH_GLASS,
        uri: UrlbarUtils.stripPublicSuffixFromHost(engine.searchUrlDomain),
        providesSearchMode: true,
        query: "",
        providerName: "TabToSearch",
      }),
      makeVisitResult(context, {
        uri: "http://my.search.com/samplepage/",
        title: "test visit for http://my.search.com/samplepage/",
        providerName: "Places",
      }),
    ],
  });

  await cleanupPlaces();
  Services.prefs.clearUserPref(
    "browser.urlbar.tabToSearch.onboard.interactionsLeft"
  );
});