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

/**
 * These tests check that we record the number of times search terms
 * persist in the Urlbar, and when search terms are cleared due to a
 * PopupNotification.
 *
 * This is different from existing telemetry that tracks whether users
 * interacted with the Urlbar or made another search while the search
 * terms were peristed.
 */

let defaultTestEngine;

// The main search string used in tests
const SEARCH_STRING = "chocolate cake";

// Telemetry keys.
const PERSISTED_VIEWED = "urlbar.persistedsearchterms.view_count";
const PERSISTED_REVERTED = "urlbar.persistedsearchterms.revert_by_popup_count";

add_setup(async function () {
  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 }
  );
  defaultTestEngine = Services.search.getEngineByName("MozSearch");
  Services.telemetry.clearScalars();

  registerCleanupFunction(async function () {
    await PlacesUtils.history.clear();
    Services.telemetry.clearScalars();
  });
});

// Starts a search with a tab and asserts that
// the state of the Urlbar contains the search term.
async function searchWithTab(
  searchString,
  tab = null,
  engine = defaultTestEngine,
  assertSearchString = true
) {
  if (!tab) {
    tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  }

  let [expectedSearchUrl] = UrlbarUtils.getSearchQueryUrl(engine, 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;

  if (assertSearchString) {
    assertSearchStringIsInUrlbar(searchString);
  }

  return { tab, expectedSearchUrl };
}

add_task(async function load_page_with_persisted_search() {
  let { tab } = await searchWithTab(SEARCH_STRING);
  const scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

add_task(async function load_page_without_persisted_search() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.showSearchTerms.featureGate", false]],
  });

  let { tab } = await searchWithTab(
    SEARCH_STRING,
    null,
    defaultTestEngine,
    false
  );

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, undefined);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
  await SpecialPowers.popPrefEnv();
});

// Multiple searches should result in the correct number of recorded views.
add_task(async function load_page_n_times() {
  let N = 5;
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  for (let index = 0; index < N; ++index) {
    await searchWithTab(SEARCH_STRING, tab);
  }

  const scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, N);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

// A persisted search view event should not be recorded when unfocusing the urlbar.
add_task(async function focus_and_unfocus() {
  let { tab } = await searchWithTab(SEARCH_STRING);

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  gURLBar.focus();
  gURLBar.select();
  gURLBar.blur();

  // Focusing and unfocusing the urlbar shouldn't change the persisted view count.
  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

// A persisted search view event should not be recorded by a
// pushState event after a page has been loaded.
add_task(async function history_api() {
  let { tab } = await searchWithTab(SEARCH_STRING);

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    let url = new URL(content.window.location);
    let someState = { value: true };
    url.searchParams.set("pc", "fake_code_2");
    content.history.pushState(someState, "", url);
    someState.value = false;
    content.history.replaceState(someState, "", url);
  });

  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

// A persisted search view event should be recorded when switching back to a tab
// that contains search terms.
add_task(async function switch_tabs() {
  let { tab } = await searchWithTab(SEARCH_STRING);

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await BrowserTestUtils.switchTab(gBrowser, tab);

  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 2);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});

// A telemetry event should be recorded when returning to a persisted SERP via tabhistory.
add_task(async function tabhistory() {
  let { tab } = await searchWithTab(SEARCH_STRING);

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  let browserLoadedPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
  BrowserTestUtils.loadURIString(
    tab.linkedBrowser,
    "https://www.example.com/some_url"
  );
  await browserLoadedPromise;

  let pageShowPromise = BrowserTestUtils.waitForContentEvent(
    tab.linkedBrowser,
    "pageshow"
  );
  tab.linkedBrowser.goBack();
  await pageShowPromise;

  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 2);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

// PopupNotification's that rely on an anchor element in the urlbar should trigger
// an increment of the revert counter.
// This assumes the anchor element is present in the Urlbar during a valid pageproxystate.
add_task(async function popup_in_urlbar() {
  let { tab } = await searchWithTab(SEARCH_STRING);
  let promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "test-notification",
    "This is a sample popup."
  );
  await promisePopupShown;

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

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});

// Non-persistent PopupNotifications won't re-appear if a user switches
// tabs and returns to the tab that had the Popup.
add_task(async function non_persistent_popup_in_urlbar_switch_tab() {
  let { tab } = await searchWithTab(SEARCH_STRING);
  let promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "test-notification",
    "This is a sample popup.",
    "geo-notification-icon"
  );
  await promisePopupShown;

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, 1);

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  await BrowserTestUtils.switchTab(gBrowser, tab);

  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 2);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, 1);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});

// Persistent PopupNotifications will constantly appear to users
// even if they switch to another tab and switch back.
add_task(async function persistent_popup_in_urlbar_switch_tab() {
  let { tab } = await searchWithTab(SEARCH_STRING);
  let promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "test-notification",
    "This is a sample popup.",
    "geo-notification-icon",
    null,
    null,
    { persistent: true }
  );
  await promisePopupShown;

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, 1);

  let tab2 = await BrowserTestUtils.openNewForegroundTab(gBrowser);
  promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  await BrowserTestUtils.switchTab(gBrowser, tab);
  await promisePopupShown;

  scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 2);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, 2);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
  BrowserTestUtils.removeTab(tab2);
});

// If the persist feature is not enabled, a telemetry event should not be recorded
// if a PopupNotification uses an anchor in the Urlbar.
add_task(async function popup_in_urlbar_without_feature() {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.showSearchTerms.featureGate", false]],
  });

  let { tab } = await searchWithTab(
    SEARCH_STRING,
    null,
    defaultTestEngine,
    false
  );
  let promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "test-notification",
    "This is a sample popup."
  );
  await promisePopupShown;

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, undefined);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
  await SpecialPowers.popPrefEnv();
});

// If the anchor element for the PopupNotification is not located in the Urlbar,
// a telemetry event should not be recorded.
add_task(async function popup_not_in_urlbar() {
  let { tab } = await searchWithTab(SEARCH_STRING);

  let promisePopupShown = BrowserTestUtils.waitForEvent(
    PopupNotifications.panel,
    "popupshown"
  );
  PopupNotifications.show(
    gBrowser.selectedBrowser,
    "test-notification",
    "This is a sample popup that uses the unified extensions button.",
    gUnifiedExtensions.getPopupAnchorID(gBrowser.selectedBrowser, window)
  );
  await promisePopupShown;

  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_VIEWED, 1);
  TelemetryTestUtils.assertScalar(scalars, PERSISTED_REVERTED, undefined);

  // Clean up.
  BrowserTestUtils.removeTab(tab);
});