summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_urlbar_telemetry_tabtosearch.js
blob: 35607d2f949ad20af01f516141abbf998f2adf61 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * This file tests telemetry for tabtosearch results.
 * NB: This file does not test the search mode `entry` field for tab-to-search
 * results. That is tested in browser_UsageTelemetry_urlbar_searchmode.js.
 */

"use strict";

const ENGINE_NAME = "MozSearch";
const ENGINE_DOMAIN = "example.com";

ChromeUtils.defineESModuleGetters(this, {
  UrlbarProviderTabToSearch:
    "resource:///modules/UrlbarProviderTabToSearch.sys.mjs",
});

function snapshotHistograms() {
  Services.telemetry.clearScalars();
  Services.telemetry.clearEvents();
  return {
    resultMethodHist: TelemetryTestUtils.getAndClearHistogram(
      "FX_URLBAR_SELECTED_RESULT_METHOD"
    ),
    search_hist: TelemetryTestUtils.getAndClearKeyedHistogram("SEARCH_COUNTS"),
  };
}

function assertTelemetryResults(histograms, type, index, method) {
  TelemetryTestUtils.assertHistogram(histograms.resultMethodHist, method, 1);

  TelemetryTestUtils.assertKeyedScalar(
    TelemetryTestUtils.getProcessScalars("parent", true, true),
    `urlbar.picked.${type}`,
    index,
    1
  );
}

/**
 * Checks to see if the second result in the Urlbar is a tab-to-search result
 * with the correct engine.
 *
 * @param {string} engineName
 *   The expected engine name.
 * @param {boolean} [isOnboarding]
 *   If true, expects the tab-to-search result to be an onbarding result.
 */
async function checkForTabToSearchResult(engineName, isOnboarding) {
  Assert.ok(UrlbarTestUtils.isPopupOpen(window), "Popup should be open.");
  let tabToSearchResult = (
    await UrlbarTestUtils.waitForAutocompleteResultAt(window, 1)
  ).result;
  Assert.equal(
    tabToSearchResult.providerName,
    "TabToSearch",
    "The second result is a tab-to-search result."
  );
  Assert.equal(
    tabToSearchResult.payload.engine,
    engineName,
    "The tab-to-search result is for the first engine."
  );
  if (isOnboarding) {
    Assert.equal(
      tabToSearchResult.payload.dynamicType,
      "onboardTabToSearch",
      "The tab-to-search result is an onboarding result."
    );
  } else {
    Assert.ok(
      !tabToSearchResult.payload.dynamicType,
      "The tab-to-search result should not be an onboarding result."
    );
  }
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.tabToSearch.onboard.interactionsLeft", 0]],
  });

  await SearchTestUtils.installSearchExtension({
    name: ENGINE_NAME,
    search_url: `https://${ENGINE_DOMAIN}/`,
  });

  // Reset the enginesShown sets in case a previous test showed a tab-to-search
  // result but did not end its engagement.
  UrlbarProviderTabToSearch.enginesShown.regular.clear();
  UrlbarProviderTabToSearch.enginesShown.onboarding.clear();

  // Enable local telemetry recording for the duration of the tests.
  let oldCanRecord = Services.telemetry.canRecordExtended;
  Services.telemetry.canRecordExtended = true;

  registerCleanupFunction(async () => {
    Services.telemetry.canRecordExtended = oldCanRecord;
  });
});

add_task(async function test() {
  await BrowserTestUtils.withNewTab("about:blank", async () => {
    const histograms = snapshotHistograms();

    for (let i = 0; i < 3; i++) {
      await PlacesTestUtils.addVisits([`https://${ENGINE_DOMAIN}/`]);
    }

    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: ENGINE_DOMAIN.slice(0, 4),
      fireInputEvent: true,
    });

    let tabToSearchResult = (
      await UrlbarTestUtils.waitForAutocompleteResultAt(window, 1)
    ).result;
    Assert.equal(
      tabToSearchResult.providerName,
      "TabToSearch",
      "The second result is a tab-to-search result."
    );
    Assert.equal(
      tabToSearchResult.payload.engine,
      ENGINE_NAME,
      "The tab-to-search result is for the correct engine."
    );
    EventUtils.synthesizeKey("KEY_ArrowDown");
    Assert.equal(
      UrlbarTestUtils.getSelectedRowIndex(window),
      1,
      "Sanity check: The second result is selected."
    );

    // Select the tab-to-search result.
    let searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey("KEY_Enter");
    await searchPromise;

    await UrlbarTestUtils.assertSearchMode(window, {
      engineName: ENGINE_NAME,
      entry: "tabtosearch",
    });

    assertTelemetryResults(
      histograms,
      "tabtosearch",
      1,
      UrlbarTestUtils.SELECTED_RESULT_METHODS.arrowEnterSelection
    );

    await UrlbarTestUtils.exitSearchMode(window);
    await UrlbarTestUtils.promisePopupClose(window, () => {
      gURLBar.blur();
    });
    await PlacesUtils.history.clear();
  });

  Services.telemetry.clearScalars();
  Services.telemetry.clearEvents();
});

add_task(async function impressions() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.tabToSearch.onboard.interactionsLeft", 0]],
  });
  await impressions_test(false);
  await SpecialPowers.popPrefEnv();
});

add_task(async function onboarding_impressions() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.tabToSearch.onboard.interactionsLeft", 3]],
  });
  await impressions_test(true);
  await SpecialPowers.popPrefEnv();
  delete UrlbarProviderTabToSearch.onboardingInteractionAtTime;
});

async function impressions_test(isOnboarding) {
  await BrowserTestUtils.withNewTab("about:blank", async browser => {
    const firstEngineHost = "example";
    let extension = await SearchTestUtils.installSearchExtension(
      {
        name: `${ENGINE_NAME}2`,
        search_url: `https://${firstEngineHost}-2.com/`,
      },
      { skipUnload: true }
    );

    for (let i = 0; i < 3; i++) {
      await PlacesTestUtils.addVisits([`https://${firstEngineHost}-2.com`]);
      await PlacesTestUtils.addVisits([`https://${ENGINE_DOMAIN}/`]);
    }

    // First do multiple searches for substrings of firstEngineHost. The view
    // should show the same tab-to-search onboarding result the entire time, so
    // we should not continue to increment urlbar.tips.
    for (let i = 1; i < firstEngineHost.length; i++) {
      info(
        `Search for "${firstEngineHost.slice(
          0,
          i
        )}". Only record one impression for this sequence.`
      );
      await UrlbarTestUtils.promiseAutocompleteResultPopup({
        window,
        value: firstEngineHost.slice(0, i),
        fireInputEvent: true,
      });
      await checkForTabToSearchResult(ENGINE_NAME, isOnboarding);
    }

    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    let scalars = TelemetryTestUtils.getProcessScalars("parent", true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      1
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      // "other" is recorded as the engine name because we're not using a built-in engine.
      "other",
      1
    );

    info("Type through autofill to second engine hostname. Record impression.");
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: firstEngineHost,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(ENGINE_NAME, isOnboarding);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: `${firstEngineHost}-`,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    // Since the user typed past the autofill for the first engine, we showed a
    // different onboarding result and now we increment
    // tabtosearch_onboard-shown.
    scalars = TelemetryTestUtils.getProcessScalars("parent", true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      3
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      "other",
      3
    );

    info("Make a typo and return to autofill. Do not record impression.");
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: `${firstEngineHost}-`,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: `${firstEngineHost}-3`,
      fireInputEvent: true,
    });
    Assert.equal(
      UrlbarTestUtils.getResultCount(window),
      1,
      "We are not showing a tab-to-search result."
    );
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: `${firstEngineHost}-2`,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    scalars = TelemetryTestUtils.getProcessScalars("parent", true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      4
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      "other",
      4
    );

    info(
      "Cancel then restart autofill. Continue to show the tab-to-search result."
    );
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: `${firstEngineHost}-2`,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    let searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey("KEY_Backspace");
    await searchPromise;
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
    // Type the "." from `example-2.com`.
    EventUtils.synthesizeKey(".");
    await searchPromise;
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    scalars = TelemetryTestUtils.getProcessScalars("parent", true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      5
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      // "other" is recorded as the engine name because we're not using a built-in engine.
      "other",
      5
    );

    // See javadoc for UrlbarProviderTabToSearch.onEngagement for discussion
    // about retained results.
    info("Reopen the result set with retained results. Record impression.");
    await UrlbarTestUtils.promisePopupOpen(window, () => {
      EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
    });
    await checkForTabToSearchResult(`${ENGINE_NAME}2`, isOnboarding);
    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    scalars = TelemetryTestUtils.getProcessScalars("parent", true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      6
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      "other",
      6
    );

    info(
      "Open a result page and then autofill engine host. Record impression."
    );
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: firstEngineHost,
      fireInputEvent: true,
    });
    await checkForTabToSearchResult(ENGINE_NAME, isOnboarding);
    // Press enter on the heuristic result so we visit example.com without
    // doing an additional search.
    let loadPromise = BrowserTestUtils.browserLoaded(browser);
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;
    // Click the Urlbar and type to simulate what a user would actually do. If
    // we use promiseAutocompleteResultPopup, no query would be made between
    // this one and the previous tab-to-search query. Thus
    // `onboardingEnginesShown` would not be cleared. This would not happen
    // in real-world usage.
    await UrlbarTestUtils.promisePopupOpen(window, () => {
      EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {});
    });
    searchPromise = UrlbarTestUtils.promiseSearchComplete(window);
    EventUtils.synthesizeKey(firstEngineHost.slice(0, 4));
    await searchPromise;
    await checkForTabToSearchResult(ENGINE_NAME, isOnboarding);
    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    // We clear the scalar this time.
    scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      "urlbar.tips",
      isOnboarding ? "tabtosearch_onboard-shown" : "tabtosearch-shown",
      8
    );
    TelemetryTestUtils.assertKeyedScalar(
      scalars,
      isOnboarding
        ? "urlbar.tabtosearch.impressions_onboarding"
        : "urlbar.tabtosearch.impressions",
      "other",
      8
    );

    await PlacesUtils.history.clear();
    await extension.unload();
  });
}