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
379
380
381
382
383
384
385
386
387
388
389
|
/* 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";
// A fake ProvidersManager.
let fPM;
let sandbox;
let generalListener;
let controller;
/**
* Asserts that the query context has the expected values.
*
* @param {UrlbarQueryContext} context The query context.
* @param {object} expectedValues The expected values for the UrlbarQueryContext.
*/
function assertContextMatches(context, expectedValues) {
Assert.ok(
context instanceof UrlbarQueryContext,
"Should be a UrlbarQueryContext"
);
for (let [key, value] of Object.entries(expectedValues)) {
Assert.equal(
context[key],
value,
`Should have the expected value for ${key} in the UrlbarQueryContext`
);
}
}
add_task(function setup() {
sandbox = sinon.createSandbox();
fPM = {
startQuery: sandbox.stub(),
cancelQuery: sandbox.stub(),
};
generalListener = {
onQueryStarted: sandbox.stub(),
onQueryResults: sandbox.stub(),
onQueryCancelled: sandbox.stub(),
};
controller = UrlbarTestUtils.newMockController({
manager: fPM,
});
controller.addQueryListener(generalListener);
});
add_task(function test_constructor_throws() {
Assert.throws(
() => new UrlbarController(),
/Missing options: input/,
"Should throw if the input was not supplied"
);
Assert.throws(
() => new UrlbarController({ input: {} }),
/input is missing 'window' property/,
"Should throw if the input is not a UrlbarInput"
);
Assert.throws(
() => new UrlbarController({ input: { window: {} } }),
/input.window should be an actual browser window/,
"Should throw if the input.window is not a window"
);
Assert.throws(
() =>
new UrlbarController({
input: {
window: {
location: "about:fake",
},
},
}),
/input.window should be an actual browser window/,
"Should throw if the input.window is not an object"
);
Assert.throws(
() =>
new UrlbarController({
input: {
window: {
location: {
href: "about:fake",
},
},
},
}),
/input.window should be an actual browser window/,
"Should throw if the input.window does not have the correct location"
);
Assert.throws(
() =>
new UrlbarController({
input: {
window: {
location: {
href: AppConstants.BROWSER_CHROME_URL,
},
},
},
}),
/input.isPrivate must be set/,
"Should throw if input.isPrivate is not set"
);
new UrlbarController({
input: {
isPrivate: false,
window: {
location: {
href: AppConstants.BROWSER_CHROME_URL,
},
},
},
});
Assert.ok(true, "Correct call should not throw");
});
add_task(function test_add_and_remove_listeners() {
Assert.throws(
() => controller.addQueryListener(null),
/Expected listener to be an object/,
"Should throw for a null listener"
);
Assert.throws(
() => controller.addQueryListener(123),
/Expected listener to be an object/,
"Should throw for a non-object listener"
);
const listener = {};
controller.addQueryListener(listener);
Assert.ok(
controller._listeners.has(listener),
"Should have added the listener to the list."
);
// Adding a non-existent listener shouldn't throw.
controller.removeQueryListener(123);
controller.removeQueryListener(listener);
Assert.ok(
!controller._listeners.has(listener),
"Should have removed the listener from the list"
);
sandbox.resetHistory();
});
add_task(function test__notify() {
const listener1 = {
onFake: sandbox.stub().callsFake(() => {
throw new Error("fake error");
}),
};
const listener2 = {
onFake: sandbox.stub(),
};
controller.addQueryListener(listener1);
controller.addQueryListener(listener2);
const param = "1234";
controller.notify("onFake", param);
Assert.equal(
listener1.onFake.callCount,
1,
"Should have called the first listener method."
);
Assert.deepEqual(
listener1.onFake.args[0],
[param],
"Should have called the first listener with the correct argument"
);
Assert.equal(
listener2.onFake.callCount,
1,
"Should have called the second listener method."
);
Assert.deepEqual(
listener2.onFake.args[0],
[param],
"Should have called the first listener with the correct argument"
);
controller.removeQueryListener(listener2);
controller.removeQueryListener(listener1);
// This should succeed without errors.
controller.notify("onNewFake");
sandbox.resetHistory();
});
add_task(function test_handle_query_starts_search() {
const context = createContext();
controller.startQuery(context);
Assert.equal(
fPM.startQuery.callCount,
1,
"Should have called startQuery once"
);
Assert.equal(
fPM.startQuery.args[0].length,
2,
"Should have called startQuery with two arguments"
);
assertContextMatches(fPM.startQuery.args[0][0], {});
Assert.equal(
fPM.startQuery.args[0][1],
controller,
"Should have passed the controller as the second argument"
);
Assert.equal(
generalListener.onQueryStarted.callCount,
1,
"Should have called onQueryStarted for the listener"
);
Assert.deepEqual(
generalListener.onQueryStarted.args[0],
[context],
"Should have called onQueryStarted with the context"
);
sandbox.resetHistory();
});
add_task(async function test_handle_query_starts_search_sets_allowAutofill() {
let originalValue = Services.prefs.getBoolPref("browser.urlbar.autoFill");
Services.prefs.setBoolPref("browser.urlbar.autoFill", !originalValue);
await controller.startQuery(createContext());
Assert.equal(
fPM.startQuery.callCount,
1,
"Should have called startQuery once"
);
Assert.equal(
fPM.startQuery.args[0].length,
2,
"Should have called startQuery with two arguments"
);
assertContextMatches(fPM.startQuery.args[0][0], {
allowAutofill: !originalValue,
});
Assert.equal(
fPM.startQuery.args[0][1],
controller,
"Should have passed the controller as the second argument"
);
sandbox.resetHistory();
Services.prefs.clearUserPref("browser.urlbar.autoFill");
});
add_task(function test_cancel_query() {
const context = createContext();
controller.startQuery(context);
controller.cancelQuery();
Assert.equal(
fPM.cancelQuery.callCount,
1,
"Should have called cancelQuery once"
);
Assert.equal(
fPM.cancelQuery.args[0].length,
1,
"Should have called cancelQuery with one argument"
);
Assert.equal(
generalListener.onQueryCancelled.callCount,
1,
"Should have called onQueryCancelled for the listener"
);
Assert.deepEqual(
generalListener.onQueryCancelled.args[0],
[context],
"Should have called onQueryCancelled with the context"
);
sandbox.resetHistory();
});
add_task(function test_receiveResults() {
const context = createContext();
context.results = [];
controller.receiveResults(context);
Assert.equal(
generalListener.onQueryResults.callCount,
1,
"Should have called onQueryResults for the listener"
);
Assert.deepEqual(
generalListener.onQueryResults.args[0],
[context],
"Should have called onQueryResults with the context"
);
sandbox.resetHistory();
});
add_task(async function test_notifications_order() {
// Clear any pending notifications.
const context = createContext();
await controller.startQuery(context);
// Check that when multiple queries are executed, the notifications arrive
// in the proper order.
let collectingListener = new Proxy(
{},
{
_notifications: [],
get(target, name) {
if (name == "notifications") {
return this._notifications;
}
return () => {
this._notifications.push(name);
};
},
}
);
controller.addQueryListener(collectingListener);
controller.startQuery(context);
Assert.deepEqual(
["onQueryStarted"],
collectingListener.notifications,
"Check onQueryStarted is fired synchronously"
);
controller.startQuery(context);
Assert.deepEqual(
["onQueryStarted", "onQueryCancelled", "onQueryFinished", "onQueryStarted"],
collectingListener.notifications,
"Check order of notifications"
);
controller.cancelQuery();
Assert.deepEqual(
[
"onQueryStarted",
"onQueryCancelled",
"onQueryFinished",
"onQueryStarted",
"onQueryCancelled",
"onQueryFinished",
],
collectingListener.notifications,
"Check order of notifications"
);
await controller.startQuery(context);
controller.cancelQuery();
Assert.deepEqual(
[
"onQueryStarted",
"onQueryCancelled",
"onQueryFinished",
"onQueryStarted",
"onQueryCancelled",
"onQueryFinished",
"onQueryStarted",
"onQueryFinished",
],
collectingListener.notifications,
"Check order of notifications"
);
});
|