summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_search_continuation.js
blob: 8a24d578561ae3a41fe1e9cf59f2786edb3fc090 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests how trending and recent searches work together.
 */

const CONFIG_DEFAULT = [
  {
    webExtension: { id: "basic@search.mozilla.org" },
    urls: {
      trending: {
        fullPath:
          "https://example.com/browser/browser/components/search/test/browser/trendingSuggestionEngine.sjs",
        query: "",
      },
    },
    appliesTo: [{ included: { everywhere: true } }],
    default: "yes",
  },
];

add_setup(async () => {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.searches", true],
      ["browser.urlbar.suggest.trending", true],
      ["browser.urlbar.maxRichResults", 3],
      ["browser.urlbar.trending.featureGate", true],
      ["browser.urlbar.trending.requireSearchMode", false],
      ["browser.urlbar.suggest.recentsearches", true],
      ["browser.urlbar.recentsearches.featureGate", true],
      [
        "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features",
        false,
      ],
    ],
  });

  await UrlbarTestUtils.formHistory.clear();
  await SearchTestUtils.setupTestEngines("search-engines", CONFIG_DEFAULT);

  registerCleanupFunction(async () => {
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function test_trending_results() {
  await check_results([
    "SearchSuggestions",
    "SearchSuggestions",
    "SearchSuggestions",
  ]);
  await doSearch("Testing 1");
  await check_results([
    "RecentSearches",
    "SearchSuggestions",
    "SearchSuggestions",
  ]);
  await doSearch("Testing 2");
  await check_results([
    "RecentSearches",
    "RecentSearches",
    "SearchSuggestions",
  ]);
  await doSearch("Testing 3");
  await check_results(["RecentSearches", "RecentSearches", "RecentSearches"]);
});

async function check_results(results) {
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "",
    waitForFocus: SimpleTest.waitForFocus,
  });

  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    results.length,
    "We matched the expected number of results"
  );

  for (let i = 0; i < results.length; i++) {
    let { result } = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    Assert.equal(result.providerName, results[i]);
  }

  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_Escape");
  });
}

async function doSearch(search) {
  info("Perform a search that will be added to search history.");
  let tab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    "data:text/html,"
  );
  let browserLoaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: search,
    waitForFocus: SimpleTest.waitForFocus,
  });

  await UrlbarTestUtils.promisePopupClose(window, () => {
    EventUtils.synthesizeKey("KEY_Enter", {}, window);
  });
  await browserLoaded;

  await BrowserTestUtils.removeTab(tab);
}