summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_urlbar_telemetry_zeroPrefix.js
blob: b331921553640c5d87fa4b65866826d0a8a13b9c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * This file tests urlbar telemetry related to the zero-prefix view, i.e., when
 * the search string is empty.
 */

"use strict";

const HISTOGRAM_DWELL_TIME = "FX_URLBAR_ZERO_PREFIX_DWELL_TIME_MS";
const SCALARS = {
  ABANDONMENT: "urlbar.zeroprefix.abandonment",
  ENGAGEMENT: "urlbar.zeroprefix.engagement",
  EXPOSURE: "urlbar.zeroprefix.exposure",
};

add_setup(async function () {
  await PlacesUtils.history.clear();
  await PlacesUtils.bookmarks.eraseEverything();
  Services.telemetry.clearScalars();

  await SearchTestUtils.installSearchExtension({}, { setAsDefault: true });
  await updateTopSitesAndAwaitChanged();
});

// zero prefix engagement
add_task(async function engagement() {
  let dwellHistogram =
    TelemetryTestUtils.getAndClearHistogram(HISTOGRAM_DWELL_TIME);

  await BrowserTestUtils.withNewTab("about:blank", async () => {
    await showZeroPrefix();
    checkScalars({
      [SCALARS.EXPOSURE]: 1,
    });
    checkAndClearHistogram(dwellHistogram, false);

    info("Finding row with result type URL");
    let foundURLRow = false;
    let count = UrlbarTestUtils.getResultCount(window);
    for (let i = 0; i < count && !foundURLRow; i++) {
      EventUtils.synthesizeKey("KEY_ArrowDown");
      let index = UrlbarTestUtils.getSelectedRowIndex(window);
      Assert.equal(index, i, "The expected row index should be selected");
      let details = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
      info(`Checked row at index ${i}, result type is: ${details.type}`);
      if (details.type == UrlbarUtils.RESULT_TYPE.URL) {
        foundURLRow = true;
      }
    }
    Assert.ok(foundURLRow, "Should have found a row with result type URL");

    let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;
  });

  checkScalars({
    [SCALARS.ENGAGEMENT]: 1,
  });
  checkAndClearHistogram(dwellHistogram, true);
});

// zero prefix abandonment
add_task(async function abandonment() {
  let dwellHistogram =
    TelemetryTestUtils.getAndClearHistogram(HISTOGRAM_DWELL_TIME);

  // Open and close the view twice. The second time the view will used a cached
  // query context and that shouldn't interfere with telemetry.
  for (let i = 0; i < 2; i++) {
    await showZeroPrefix();
    checkScalars({
      [SCALARS.EXPOSURE]: 1,
    });
    checkAndClearHistogram(dwellHistogram, false);

    await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
    checkScalars({
      [SCALARS.ABANDONMENT]: 1,
    });
    dwellHistogram = checkAndClearHistogram(dwellHistogram, true);
  }
});

// Shows the zero-prefix view, does some searches, then shows it again by doing
// a search for an empty string.
add_task(async function searches() {
  let dwellHistogram =
    TelemetryTestUtils.getAndClearHistogram(HISTOGRAM_DWELL_TIME);

  info("Show zero prefix");
  await showZeroPrefix();
  checkScalars({
    [SCALARS.EXPOSURE]: 1,
  });
  checkAndClearHistogram(dwellHistogram, false);

  info("Search for 't'");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "t",
  });
  checkScalars({});
  dwellHistogram = checkAndClearHistogram(dwellHistogram, true);

  info("Search for 'te'");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "te",
  });
  checkScalars({});
  checkAndClearHistogram(dwellHistogram, false);

  info("Search for 't'");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "t",
  });
  checkScalars({});
  checkAndClearHistogram(dwellHistogram, false);

  info("Search for ''");
  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "",
  });
  checkScalars({
    [SCALARS.EXPOSURE]: 1,
  });
  checkAndClearHistogram(dwellHistogram, false);

  info("Blur urlbar and close view");
  await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());
  checkScalars({
    [SCALARS.ABANDONMENT]: 1,
  });
  checkAndClearHistogram(dwellHistogram, true);
});

// A zero prefix engagement should not be recorded when the view isn't showing
// zero prefix.
add_task(async function notZeroPrefix_engagement() {
  let dwellHistogram =
    TelemetryTestUtils.getAndClearHistogram(HISTOGRAM_DWELL_TIME);

  await BrowserTestUtils.withNewTab("about:blank", async () => {
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "test",
    });
    let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    EventUtils.synthesizeKey("KEY_Enter");
    await loadPromise;
  });

  checkScalars({});
  checkAndClearHistogram(dwellHistogram, false);
});

// A zero prefix abandonment should not be recorded when the view isn't showing
// zero prefix.
add_task(async function notZeroPrefix_abandonment() {
  let dwellHistogram =
    TelemetryTestUtils.getAndClearHistogram(HISTOGRAM_DWELL_TIME);

  await UrlbarTestUtils.promiseAutocompleteResultPopup({
    window,
    value: "test",
  });
  await UrlbarTestUtils.promisePopupClose(window, () => gURLBar.blur());

  checkScalars({});
  checkAndClearHistogram(dwellHistogram, false);
});

function checkScalars(expected) {
  let scalars = TelemetryTestUtils.getProcessScalars("parent", false, true);
  for (let scalar of Object.values(SCALARS)) {
    if (expected.hasOwnProperty(scalar)) {
      TelemetryTestUtils.assertScalar(scalars, scalar, expected[scalar]);
    } else {
      Assert.ok(
        !scalars.hasOwnProperty(scalar),
        "Scalar should not be recorded: " + scalar
      );
    }
  }
}

function checkAndClearHistogram(histogram, expected) {
  if (expected) {
    Assert.deepEqual(
      Object.values(histogram.snapshot().values).filter(v => v > 0),
      [1],
      "Dwell histogram should be updated"
    );
  } else {
    Assert.strictEqual(
      histogram.snapshot().sum,
      0,
      "Dwell histogram should not be updated"
    );
  }

  return TelemetryTestUtils.getAndClearHistogram(histogram.name());
}

async function showZeroPrefix() {
  let { promise, cleanup } = waitForQueryFinished();
  await SimpleTest.promiseFocus(window);
  await UrlbarTestUtils.promisePopupOpen(window, () =>
    document.getElementById("Browser:OpenLocation").doCommand()
  );
  await promise;
  cleanup();

  Assert.greater(
    UrlbarTestUtils.getResultCount(window),
    0,
    "There should be at least one row in the zero prefix view"
  );
}

/**
 * Returns a promise that's resolved on the next `onQueryFinished()`. It's
 * important to wait for `onQueryFinished()` because that's when the view checks
 * whether it's showing zero prefix.
 *
 * @returns {object}
 *   An object with the following properties:
 *     {Promise} promise
 *       Resolved when `onQueryFinished()` is called.
 *     {Function} cleanup
 *       This should be called to remove the listener.
 */
function waitForQueryFinished() {
  let deferred = PromiseUtils.defer();
  let listener = {
    onQueryFinished: () => deferred.resolve(),
  };
  gURLBar.controller.addQueryListener(listener);

  return {
    promise: deferred.promise,
    cleanup() {
      gURLBar.controller.removeQueryListener(listener);
    },
  };
}

async function updateTopSitesAndAwaitChanged() {
  let url = "http://mochi.test:8888/topsite";
  for (let i = 0; i < 5; i++) {
    await PlacesTestUtils.addVisits(url);
  }

  info("Updating top sites and awaiting newtab-top-sites-changed");
  let changedPromise = TestUtils.topicObserved("newtab-top-sites-changed").then(
    () => info("Observed newtab-top-sites-changed")
  );
  await updateTopSites(sites => sites?.length);
  await changedPromise;
}