summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_searchTelemetry.js
blob: 61ddff4c2da04570cb4e25e94c867e410fb355ff (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
"use strict";

const SUGGEST_URLBAR_PREF = "browser.urlbar.suggest.searches";
const MAX_FORM_HISTORY_PREF = "browser.urlbar.maxHistoricalSearchSuggestions";
const TEST_ENGINE_BASENAME = "searchSuggestionEngine.xml";

// Must run first.
add_task(async function prepare() {
  await SpecialPowers.pushPrefEnv({
    set: [
      [SUGGEST_URLBAR_PREF, true],
      [MAX_FORM_HISTORY_PREF, 2],
    ],
  });

  await SearchTestUtils.promiseNewSearchEngine({
    url: getRootDirectory(gTestPath) + TEST_ENGINE_BASENAME,
    setAsDefault: true,
  });

  registerCleanupFunction(async function () {
    // Clicking urlbar results causes visits to their associated pages, so clear
    // that history now.
    await PlacesUtils.history.clear();
    await UrlbarTestUtils.formHistory.clear();
  });

  // Move the mouse away from the urlbar one-offs so that a one-off engine is
  // not inadvertently selected.
  await EventUtils.promiseNativeMouseEvent({
    type: "mousemove",
    target: window.document.documentElement,
    offsetX: 0,
    offsetY: 0,
  });
});

add_task(async function heuristicResultMouse() {
  await compareCounts(async function () {
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "heuristicResult",
    });
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.SEARCH,
      "Should be of type search"
    );
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    let element = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0);
    EventUtils.synthesizeMouseAtCenter(element, {});
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function heuristicResultKeyboard() {
  await compareCounts(async function () {
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "heuristicResult",
    });
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);
    Assert.equal(
      result.type,
      UrlbarUtils.RESULT_TYPE.SEARCH,
      "Should be of type search"
    );
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    EventUtils.sendKey("return");
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function searchSuggestionMouse() {
  await compareCounts(async function () {
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "searchSuggestion",
    });
    let idx = await getFirstSuggestionIndex();
    Assert.greaterOrEqual(idx, 0, "there should be a first suggestion");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    let element = await UrlbarTestUtils.waitForAutocompleteResultAt(
      window,
      idx
    );
    EventUtils.synthesizeMouseAtCenter(element, {});
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function searchSuggestionKeyboard() {
  await compareCounts(async function () {
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "searchSuggestion",
    });
    let idx = await getFirstSuggestionIndex();
    Assert.greaterOrEqual(idx, 0, "there should be a first suggestion");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    while (idx--) {
      EventUtils.sendKey("down");
    }
    EventUtils.sendKey("return");
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function formHistoryMouse() {
  await compareCounts(async function () {
    await UrlbarTestUtils.formHistory.add(["foofoo", "foobar"]);
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "foo",
    });
    let index = await getFirstSuggestionIndex();
    Assert.greaterOrEqual(index, 0, "there should be a first suggestion");
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
    Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.SEARCH);
    Assert.equal(result.source, UrlbarUtils.RESULT_SOURCE.HISTORY);
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    let element = await UrlbarTestUtils.waitForAutocompleteResultAt(
      window,
      index
    );
    EventUtils.synthesizeMouseAtCenter(element, {});
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

add_task(async function formHistoryKeyboard() {
  await compareCounts(async function () {
    await UrlbarTestUtils.formHistory.add(["foofoo", "foobar"]);
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    await UrlbarTestUtils.promiseAutocompleteResultPopup({
      window,
      value: "foo",
    });
    let index = await getFirstSuggestionIndex();
    Assert.greaterOrEqual(index, 0, "there should be a first suggestion");
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, index);
    Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.SEARCH);
    Assert.equal(result.source, UrlbarUtils.RESULT_SOURCE.HISTORY);
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    while (index--) {
      EventUtils.sendKey("down");
    }
    EventUtils.sendKey("return");
    await loadPromise;
    BrowserTestUtils.removeTab(tab);
    await UrlbarTestUtils.formHistory.clear();
  });
});

/**
 * This does three things: gets current telemetry/FHR counts, calls
 * clickCallback, gets telemetry/FHR counts again to compare them to the old
 * counts.
 *
 * @param {Function} clickCallback Use this to open the urlbar popup and choose
 *   and click a result.
 */
async function compareCounts(clickCallback) {
  // Search events triggered by clicks (not the Return key in the urlbar) are
  // recorded in three places:
  // * Telemetry histogram named "SEARCH_COUNTS"
  // * FHR

  let engine = await Services.search.getDefault();

  let histogramKey = `other-${engine.name}.urlbar`;
  let histogram = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
  histogram.clear();

  gURLBar.focus();
  await clickCallback();

  TelemetryTestUtils.assertKeyedHistogramSum(histogram, histogramKey, 1);
}

/**
 * Returns the index of the first search suggestion in the urlbar results.
 *
 * @returns {number} An index, or -1 if there are no search suggestions.
 */
async function getFirstSuggestionIndex() {
  const matchCount = UrlbarTestUtils.getResultCount(window);
  for (let i = 0; i < matchCount; i++) {
    let result = await UrlbarTestUtils.getDetailsOfResultAt(window, i);
    if (
      result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
      result.searchParams.suggestion
    ) {
      return i;
    }
  }
  return -1;
}