summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/browser_rich_suggestions.js
blob: 3daefc12d915b2eca0481f7ea4115b940da0943c (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* 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/. */

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

const CONFIG_V2 = [
  {
    recordType: "engine",
    identifier: "basic",
    base: {
      name: "basic",
      urls: {
        search: {
          base: "https://example.com",
          searchTermParamName: "q",
        },
        trending: {
          base: "https://example.com/browser/browser/components/search/test/browser/trendingSuggestionEngine.sjs",
          method: "GET",
          params: [
            {
              name: "richsuggestions",
              value: "true",
            },
          ],
        },
        suggestions: {
          base: "https://example.com/browser/browser/components/search/test/browser/trendingSuggestionEngine.sjs",
          method: "GET",
          params: [
            {
              name: "richsuggestions",
              value: "true",
            },
          ],
          searchTermParamName: "query",
        },
      },
      aliases: ["basic"],
    },
    variants: [
      {
        environment: { allRegionsAndLocales: true },
      },
    ],
  },
  {
    recordType: "defaultEngines",
    globalDefault: "basic",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

SearchTestUtils.init(this);

add_setup(async () => {
  // Use engines in test directory
  let searchExtensions = getChromeDir(getResolvedURI(gTestPath));
  searchExtensions.append("search-engines");
  await SearchTestUtils.useMochitestEngines(searchExtensions);

  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.urlbar.suggest.searches", true],
      ["browser.urlbar.trending.featureGate", true],
      ["browser.urlbar.trending.requireSearchMode", false],
      // Bug 1775917: Disable the persisted-search-terms search tip because if
      // not dismissed, it can cause issues with other search tests.
      ["browser.urlbar.tipShownCount.searchTip_persist", 999],
    ],
  });

  SearchTestUtils.useMockIdleService();
  await SearchTestUtils.updateRemoteSettingsConfig(
    SearchUtils.newSearchConfigEnabled ? CONFIG_V2 : CONFIG_DEFAULT
  );

  registerCleanupFunction(async () => {
    let settingsWritten = SearchTestUtils.promiseSearchNotification(
      "write-settings-to-disk-complete"
    );
    await SearchTestUtils.updateRemoteSettingsConfig();
    await settingsWritten;
  });
});

add_task(async function test_trending_results() {
  await check_results({ featureEnabled: true });
  await check_results({ featureEnabled: false });
});

async function check_results({ featureEnabled = false }) {
  Services.telemetry.clearEvents();
  Services.telemetry.clearScalars();
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.richSuggestions.featureGate", featureEnabled]],
  });

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

  let numResults = UrlbarTestUtils.getResultCount(window);

  for (let i = 0; i < numResults; i++) {
    let { result } = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.SEARCH);
    Assert.equal(result.providerName, "SearchSuggestions");
    Assert.equal(result.payload.engine, "basic");
    Assert.equal(result.isRichSuggestion, featureEnabled);
    if (featureEnabled) {
      Assert.equal(typeof result.payload.description, "string");
      Assert.ok(result.payload.icon.startsWith("data:"));
    }
  }

  info("Select first remote search suggestion & hit Enter.");
  EventUtils.synthesizeKey("KEY_ArrowDown", {}, window);
  EventUtils.synthesizeKey("VK_RETURN", {}, window);

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, "urlbar.engagement", 1);

  await SpecialPowers.popPrefEnv();
}

add_task(async function test_richsuggestion_deduplication() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.richSuggestions.featureGate", true]],
  });

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test0",
    waitForFocus: SimpleTest.waitForFocus,
  });

  let { result: heuristicResult } = await UrlbarTestUtils.getDetailsOfResultAt(
    window,
    0
  );
  let { result: richResult } = await UrlbarTestUtils.getDetailsOfResultAt(
    window,
    1
  );

  // The Rich Suggestion that points to the same query as the Hueristic result
  // should not be deduplicated.
  Assert.equal(heuristicResult.type, UrlbarUtils.RESULT_TYPE.SEARCH);
  Assert.equal(heuristicResult.providerName, "HeuristicFallback");
  Assert.equal(richResult.type, UrlbarUtils.RESULT_TYPE.SEARCH);
  Assert.equal(richResult.providerName, "SearchSuggestions");
  Assert.equal(
    heuristicResult.payload.query,
    richResult.payload.lowerCaseSuggestion
  );

  await UrlbarTestUtils.promisePopupClose(window);
});