summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/urlbar/browser_urlbarSearchTelemetry.js
blob: 8c28401eae0eeafc505b66f46507a1afa4d4fa90 (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
"use strict";

Cu.import("resource:///modules/BrowserUITelemetry.jsm");

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

// Must run first.
add_task(function* prepare() {
  Services.prefs.setBoolPref(SUGGEST_URLBAR_PREF, true);
  let engine = yield promiseNewSearchEngine(TEST_ENGINE_BASENAME);
  let oldCurrentEngine = Services.search.currentEngine;
  Services.search.currentEngine = engine;

  registerCleanupFunction(function* () {
    Services.prefs.clearUserPref(SUGGEST_URLBAR_PREF);
    Services.search.currentEngine = oldCurrentEngine;

    // Clicking urlbar results causes visits to their associated pages, so clear
    // that history now.
    yield PlacesTestUtils.clearHistory();

    // Make sure the popup is closed for the next test.
    gURLBar.blur();
    Assert.ok(!gURLBar.popup.popupOpen, "popup should be closed");
  });

  // Move the mouse away from the urlbar one-offs so that a one-off engine is
  // not inadvertently selected.
  yield new Promise(resolve => {
    EventUtils.synthesizeNativeMouseMove(window.document.documentElement, 0, 0,
                                         resolve);
  });
});

add_task(function* heuristicResultMouse() {
  yield compareCounts(function* () {
    let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    yield promiseAutocompleteResultPopup("heuristicResult");
    let action = getActionAtIndex(0);
    Assert.ok(!!action, "there should be an action at index 0");
    Assert.equal(action.type, "searchengine", "type should be searchengine");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    gURLBar.popup.richlistbox.getItemAtIndex(0).click();
    yield loadPromise;
    yield BrowserTestUtils.removeTab(tab);
  });
});

add_task(function* heuristicResultKeyboard() {
  yield compareCounts(function* () {
    let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    yield promiseAutocompleteResultPopup("heuristicResult");
    let action = getActionAtIndex(0);
    Assert.ok(!!action, "there should be an action at index 0");
    Assert.equal(action.type, "searchengine", "type should be searchengine");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    EventUtils.sendKey("return");
    yield loadPromise;
    yield BrowserTestUtils.removeTab(tab);
  });
});

add_task(function* searchSuggestionMouse() {
  yield compareCounts(function* () {
    let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    yield promiseAutocompleteResultPopup("searchSuggestion");
    let idx = getFirstSuggestionIndex();
    Assert.ok(idx >= 0, "there should be a first suggestion");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    gURLBar.popup.richlistbox.getItemAtIndex(idx).click();
    yield loadPromise;
    yield BrowserTestUtils.removeTab(tab);
  });
});

add_task(function* searchSuggestionKeyboard() {
  yield compareCounts(function* () {
    let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser);
    gURLBar.focus();
    yield promiseAutocompleteResultPopup("searchSuggestion");
    let idx = getFirstSuggestionIndex();
    Assert.ok(idx >= 0, "there should be a first suggestion");
    let loadPromise = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
    while (idx--) {
      EventUtils.sendKey("down");
    }
    EventUtils.sendKey("return");
    yield loadPromise;
    yield BrowserTestUtils.removeTab(tab);
  });
});

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

  let engine = Services.search.currentEngine;
  let engineID = "org.mozilla.testsearchsuggestions";

  // First, get the current counts.

  // BrowserUITelemetry
  let uiTelemCount = 0;
  let bucket = BrowserUITelemetry.currentBucket;
  let events = BrowserUITelemetry.getToolbarMeasures().countableEvents;
  if (events[bucket] &&
      events[bucket].search &&
      events[bucket].search.urlbar) {
    uiTelemCount = events[bucket].search.urlbar;
  }

  // telemetry histogram SEARCH_COUNTS
  let histogramCount = 0;
  let histogramKey = engineID + ".urlbar";
  let histogram;
  try {
    histogram = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
  } catch (ex) {
    // No searches performed yet, not a problem.
  }
  if (histogram) {
    let snapshot = histogram.snapshot();
    if (histogramKey in snapshot) {
      histogramCount = snapshot[histogramKey].sum;
    }
  }

  // FHR -- first make sure the engine has an identifier so that FHR is happy.
  Object.defineProperty(engine.wrappedJSObject, "identifier",
                        { value: engineID });

  gURLBar.focus();
  yield clickCallback();

  // Now get the new counts and compare them to the old.

  // BrowserUITelemetry
  events = BrowserUITelemetry.getToolbarMeasures().countableEvents;
  Assert.ok(bucket in events, "bucket should be recorded");
  events = events[bucket];
  Assert.ok("search" in events, "search should be recorded");
  events = events.search;
  Assert.ok("urlbar" in events, "urlbar should be recorded");
  Assert.equal(events.urlbar, uiTelemCount + 1,
               "clicked suggestion should be recorded");

  // telemetry histogram SEARCH_COUNTS
  histogram = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
  let snapshot = histogram.snapshot();
  Assert.ok(histogramKey in snapshot, "histogram with key should be recorded");
  Assert.equal(snapshot[histogramKey].sum, histogramCount + 1,
               "histogram sum should be incremented");
}

/**
 * Returns the "action" object at the given index in the urlbar results:
 * { type, params: {}}
 *
 * @param index The index in the urlbar results.
 * @return An action object, or null if index >= number of results.
 */
function getActionAtIndex(index) {
  let controller = gURLBar.popup.input.controller;
  if (controller.matchCount <= index) {
    return null;
  }
  let url = controller.getValueAt(index);
  let mozActionMatch = url.match(/^moz-action:([^,]+),(.*)$/);
  if (!mozActionMatch) {
    let msg = "result at index " + index + " is not a moz-action: " + url;
    Assert.ok(false, msg);
    throw new Error(msg);
  }
  let [, type, paramStr] = mozActionMatch;
  return {
    type: type,
    params: JSON.parse(paramStr),
  };
}

/**
 * Returns the index of the first search suggestion in the urlbar results.
 *
 * @return An index, or -1 if there are no search suggestions.
 */
function getFirstSuggestionIndex() {
  let controller = gURLBar.popup.input.controller;
  let matchCount = controller.matchCount;
  for (let i = 0; i < matchCount; i++) {
    let url = controller.getValueAt(i);
    let mozActionMatch = url.match(/^moz-action:([^,]+),(.*)$/);
    if (mozActionMatch) {
      let [, type, paramStr] = mozActionMatch;
      let params = JSON.parse(paramStr);
      if (type == "searchengine" && "searchSuggestion" in params) {
        return i;
      }
    }
  }
  return -1;
}