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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* These tests unit test the functionality of UrlbarController by stubbing out the
* model and providing stubs to be called.
*/
"use strict";
const TEST_URL = "http://example.com";
const MATCH = new UrlbarResult(
UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
UrlbarUtils.RESULT_SOURCE.TABS,
{ url: TEST_URL }
);
const TELEMETRY_1ST_RESULT = "PLACES_AUTOCOMPLETE_1ST_RESULT_TIME_MS";
const TELEMETRY_6_FIRST_RESULTS = "PLACES_AUTOCOMPLETE_6_FIRST_RESULTS_TIME_MS";
let controller;
let firstHistogram;
let sixthHistogram;
/**
* A delayed test provider, allowing the query to be delayed for an amount of time.
*/
class DelayedProvider extends TestProvider {
async startQuery(context, add) {
Assert.ok(context, "context is passed-in");
Assert.equal(typeof add, "function", "add is a callback");
this._add = add;
await new Promise(resolve => {
this._resultsAdded = resolve;
});
}
async addResults(matches, finish = true) {
// startQuery may have not been invoked yet, so wait for it
await TestUtils.waitForCondition(
() => !!this._add,
"Waiting for the _add callback"
);
for (const match of matches) {
this._add(this, match);
}
if (finish) {
this._add = null;
this._resultsAdded();
}
}
}
/**
* Returns the number of reports sent recorded within the histogram results.
*
* @param {object} results a snapshot of histogram results to check.
* @returns {number} The count of reports recorded in the histogram.
*/
function getHistogramReportsCount(results) {
let sum = 0;
for (let [, value] of Object.entries(results.values)) {
sum += value;
}
return sum;
}
add_task(function setup() {
controller = UrlbarTestUtils.newMockController();
firstHistogram = Services.telemetry.getHistogramById(TELEMETRY_1ST_RESULT);
sixthHistogram = Services.telemetry.getHistogramById(
TELEMETRY_6_FIRST_RESULTS
);
});
add_task(async function test_n_autocomplete_cancel() {
firstHistogram.clear();
sixthHistogram.clear();
let providerCanceledDeferred = PromiseUtils.defer();
let provider = new TestProvider({
results: [],
onCancel: providerCanceledDeferred.resolve,
});
UrlbarProvidersManager.registerProvider(provider);
const context = createContext(TEST_URL, { providers: [provider.name] });
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should not have started first result stopwatch"
);
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should not have started first 6 results stopwatch"
);
controller.startQuery(context);
Assert.ok(
TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should have started first result stopwatch"
);
Assert.ok(
TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should have started first 6 results stopwatch"
);
controller.cancelQuery(context);
await providerCanceledDeferred.promise;
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should have canceled first result stopwatch"
);
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should have canceled first 6 results stopwatch"
);
let results = firstHistogram.snapshot();
Assert.equal(
results.sum,
0,
"Should not have recorded any times (first result)"
);
results = sixthHistogram.snapshot();
Assert.equal(
results.sum,
0,
"Should not have recorded any times (first 6 results)"
);
});
add_task(async function test_n_autocomplete_results() {
firstHistogram.clear();
sixthHistogram.clear();
let provider = new DelayedProvider();
UrlbarProvidersManager.registerProvider(provider);
const context = createContext(TEST_URL, { providers: [provider.name] });
let resultsPromise = promiseControllerNotification(
controller,
"onQueryResults"
);
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should not have started first result stopwatch"
);
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should not have started first 6 results stopwatch"
);
controller.startQuery(context);
Assert.ok(
TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should have started first result stopwatch"
);
Assert.ok(
TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should have started first 6 results stopwatch"
);
await provider.addResults([MATCH], false);
await resultsPromise;
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should have stopped the first stopwatch"
);
Assert.ok(
TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should have kept the first 6 results stopwatch running"
);
let firstResults = firstHistogram.snapshot();
let first6Results = sixthHistogram.snapshot();
Assert.equal(
getHistogramReportsCount(firstResults),
1,
"Should have recorded one time for the first result"
);
Assert.equal(
getHistogramReportsCount(first6Results),
0,
"Should not have recorded any times (first 6 results)"
);
// Now add 5 more results, so that the first 6 results is triggered.
for (let i = 0; i < 5; i++) {
resultsPromise = promiseControllerNotification(
controller,
"onQueryResults"
);
await provider.addResults(
[
new UrlbarResult(
UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
UrlbarUtils.RESULT_SOURCE.TABS,
{ url: TEST_URL + "/" + i }
),
],
false
);
await resultsPromise;
}
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_1ST_RESULT, context),
"Should have stopped the first stopwatch"
);
Assert.ok(
!TelemetryStopwatch.running(TELEMETRY_6_FIRST_RESULTS, context),
"Should have stopped the first 6 results stopwatch"
);
let updatedResults = firstHistogram.snapshot();
let updated6Results = sixthHistogram.snapshot();
Assert.deepEqual(
updatedResults,
firstResults,
"Should not have changed the histogram for the first result"
);
Assert.equal(
getHistogramReportsCount(updated6Results),
1,
"Should have recorded one time for the first 6 results"
);
// Add one more, to check neither are updated.
resultsPromise = promiseControllerNotification(controller, "onQueryResults");
await provider.addResults([
new UrlbarResult(
UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
UrlbarUtils.RESULT_SOURCE.TABS,
{ url: TEST_URL + "/6" }
),
]);
await resultsPromise;
let secondUpdateResults = firstHistogram.snapshot();
let secondUpdate6Results = sixthHistogram.snapshot();
Assert.deepEqual(
secondUpdateResults,
firstResults,
"Should not have changed the histogram for the first result"
);
Assert.equal(
getHistogramReportsCount(secondUpdate6Results),
1,
"Should not have changed the histogram for the first 6 results"
);
});
|