summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_urlbar_telemetry_persisted.js
blob: 904e774a2c397540c2b6e8f72f8cf014af39fbed (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
/* 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/. */

/**
 * This file tests browser.engagement.navigation.urlbar_persisted and the
 * event navigation.search.urlbar_persisted
 */

"use strict";

const { SearchSERPTelemetry } = ChromeUtils.importESModule(
  "resource:///modules/SearchSERPTelemetry.sys.mjs"
);

const SCALAR_URLBAR_PERSISTED =
  "browser.engagement.navigation.urlbar_persisted";

const SEARCH_STRING = "chocolate";

let testEngine;
add_setup(async () => {
  Services.telemetry.clearScalars();
  Services.telemetry.clearEvents();
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.showSearchTerms.featureGate", true]],
  });

  await SearchTestUtils.installSearchExtension(
    {
      name: "MozSearch",
      search_url: "https://www.example.com/",
      search_url_get_params: "q={searchTerms}&pc=fake_code",
    },
    { setAsDefault: true }
  );

  testEngine = Services.search.getEngineByName("MozSearch");

  // Enable event recording for the events.
  Services.telemetry.setEventRecordingEnabled("navigation", true);

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    Services.telemetry.clearScalars();
    Services.telemetry.clearEvents();
    Services.telemetry.setEventRecordingEnabled("navigation", false);
  });
});

async function searchForString(searchString, tab) {
  info(`Search for string: ${searchString}.`);
  let [expectedSearchUrl] = UrlbarUtils.getSearchQueryUrl(
    testEngine,
    searchString
  );
  let browserLoadedPromise = BrowserTestUtils.browserLoaded(
    tab.linkedBrowser,
    false,
    expectedSearchUrl
  );
  gURLBar.focus();
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    waitForFocus,
    value: searchString,
    fireInputEvent: true,
  });
  EventUtils.synthesizeKey("KEY_Enter");
  await browserLoadedPromise;
  info("Finished loading search.");
  return expectedSearchUrl;
}

async function gotoUrl(url, tab) {
  let browserLoadedPromise = BrowserTestUtils.browserLoaded(
    tab.linkedBrowser,
    false,
    url
  );
  BrowserTestUtils.loadURIString(tab.linkedBrowser, url);
  await browserLoadedPromise;
  info(`Loaded page: ${url}`);
}

async function goBack(browser) {
  let pageShowPromise = BrowserTestUtils.waitForContentEvent(
    browser,
    "pageshow"
  );
  browser.goBack();
  await pageShowPromise;
  info("Go back a page.");
}

async function goForward(browser) {
  let pageShowPromise = BrowserTestUtils.waitForContentEvent(
    browser,
    "pageshow"
  );
  browser.goForward();
  await pageShowPromise;
  info("Go forward a page.");
}

function assertScalarSearchEnter(number) {
  let scalars = TelemetryTestUtils.getProcessScalars("parent", true, true);
  TelemetryTestUtils.assertKeyedScalar(
    scalars,
    SCALAR_URLBAR_PERSISTED,
    "search_enter",
    number
  );
}

function assertScalarDoesNotExist(scalar) {
  let scalars = TelemetryTestUtils.getProcessScalars("parent", true, false);
  Assert.ok(!(scalar in scalars), scalar + " must not be recorded.");
}

function assertTelemetryEvents() {
  TelemetryTestUtils.assertEvents(
    [
      [
        "navigation",
        "search",
        "urlbar",
        "enter",
        { engine: "other-MozSearch" },
      ],
      [
        "navigation",
        "search",
        "urlbar_persisted",
        "enter",
        { engine: "other-MozSearch" },
      ],
    ],
    {
      category: "navigation",
      method: "search",
    }
  );
}

// A user making a search after making a search should result
// in the telemetry being recorded.
add_task(async function search_after_search() {
  let search_hist =
    TelemetryTestUtils.getAndClearKeyedHistogram("SEARCH_COUNTS");

  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await searchForString(SEARCH_STRING, tab);

  // Scalar should not exist from a blank page, only when a search
  // is conducted from a default SERP.
  await assertScalarDoesNotExist(SCALAR_URLBAR_PERSISTED);

  // After the first search, we should expect the SAP to change
  // because the search term should show up on the SERP.
  await searchForString(SEARCH_STRING, tab);
  assertScalarSearchEnter(1);

  // Check search counts.
  TelemetryTestUtils.assertKeyedHistogramSum(
    search_hist,
    "other-MozSearch.urlbar-persisted",
    1
  );

  // Check events.
  assertTelemetryEvents();

  BrowserTestUtils.removeTab(tab);
});

// A user going to a tab that contains a SERP should
// trigger the telemetry when conducting a search.
add_task(async function switch_to_tab_and_search() {
  let search_hist =
    TelemetryTestUtils.getAndClearKeyedHistogram("SEARCH_COUNTS");

  const tab1 = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await searchForString(SEARCH_STRING, tab1);

  const tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await gotoUrl("https://www.example.com/some-place", tab2);

  await BrowserTestUtils.switchTab(gBrowser, tab1);
  await searchForString(SEARCH_STRING, tab1);
  assertScalarSearchEnter(1);

  // Check search count.
  TelemetryTestUtils.assertKeyedHistogramSum(
    search_hist,
    "other-MozSearch.urlbar-persisted",
    1
  );

  // Check events.
  assertTelemetryEvents();

  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);
});

// When a user reverts the Urlbar after the search terms persist,
// conducting another search should still be registered as a
// urlbar-persisted SAP.
add_task(async function handle_revert() {
  let search_hist =
    TelemetryTestUtils.getAndClearKeyedHistogram("SEARCH_COUNTS");

  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await searchForString(SEARCH_STRING, tab);

  gURLBar.handleRevert();
  await searchForString(SEARCH_STRING, tab);

  assertScalarSearchEnter(1);

  // Check search count.
  TelemetryTestUtils.assertKeyedHistogramSum(
    search_hist,
    "other-MozSearch.urlbar-persisted",
    1
  );

  // Check events.
  assertTelemetryEvents();

  BrowserTestUtils.removeTab(tab);
});

// A user going back and forth in history should trigger
// urlbar-persisted telemetry when returning to a SERP
// and conducting a search.
add_task(async function back_and_forth() {
  let search_hist =
    TelemetryTestUtils.getAndClearKeyedHistogram("SEARCH_COUNTS");

  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);

  // Create three pages in history: a page, a SERP, and a page.
  await gotoUrl("https://www.example.com/some-place", tab);
  await searchForString(SEARCH_STRING, tab);
  await gotoUrl("https://www.example.com/another-page", tab);

  // Go back to the SERP by using both back and forward.
  await goBack(tab.linkedBrowser);
  await goBack(tab.linkedBrowser);
  await goForward(tab.linkedBrowser);
  await assertScalarDoesNotExist(SCALAR_URLBAR_PERSISTED);

  // Then do a search.
  await searchForString(SEARCH_STRING, tab);
  assertScalarSearchEnter(1);

  // Check search count.
  TelemetryTestUtils.assertKeyedHistogramSum(
    search_hist,
    "other-MozSearch.urlbar-persisted",
    1
  );

  // Check events.
  assertTelemetryEvents();

  BrowserTestUtils.removeTab(tab);
});