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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Tests that old results from UrlbarProviderAutofill do not overwrite results
* from UrlbarProviderHeuristicFallback after the autofillable query is
* cancelled. See bug 1653436.
*/
const { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
/**
* A test provider that waits before returning results to simulate a slow DB
* lookup.
*/
class SlowHeuristicProvider extends TestProvider {
get type() {
return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
}
async startQuery(context, add) {
this._context = context;
// eslint-disable-next-line mozilla/no-arbitrary-setTimeout
await new Promise(resolve => setTimeout(resolve, 300));
for (let result of this._results) {
add(this, result);
}
}
}
/**
* A fast provider that alterts the test when it has added its results.
*/
class FastHeuristicProvider extends TestProvider {
get type() {
return UrlbarUtils.PROVIDER_TYPE.HEURISTIC;
}
async startQuery(context, add) {
this._context = context;
for (let result of this._results) {
add(this, result);
}
Services.obs.notifyObservers(null, "results-added");
}
}
add_task(async function setup() {
registerCleanupFunction(async () => {
Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
});
Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
});
add_task(async function() {
let context = createContext("m", { isPrivate: false });
await PlacesTestUtils.promiseAsyncUpdates();
info("Manually set up query and then overwrite it.");
// slowProvider is a stand-in for a slow UnifiedComplete returning a
// non-heuristic result.
let slowProvider = new SlowHeuristicProvider({
results: [
makeVisitResult(context, {
uri: `http://mozilla.org/`,
title: `mozilla.org/`,
}),
],
});
UrlbarProvidersManager.registerProvider(slowProvider);
// fastProvider is a stand-in for a fast Autofill returning a heuristic
// result.
let fastProvider = new FastHeuristicProvider({
results: [
makeVisitResult(context, {
uri: `http://mozilla.com/`,
title: `mozilla.com/`,
heuristic: true,
}),
],
});
UrlbarProvidersManager.registerProvider(fastProvider);
let firstContext = createContext("m", {
providers: [slowProvider.name, fastProvider.name],
});
let secondContext = createContext("ma", {
providers: [slowProvider.name, fastProvider.name],
});
let controller = UrlbarTestUtils.newMockController();
let queryRecieved, queryCancelled;
const controllerListener = {
onQueryResults(queryContext) {
console.trace(`finished query. context: ${JSON.stringify(queryContext)}`);
Assert.equal(
queryContext,
secondContext,
"Only the second query should finish."
);
queryRecieved = true;
},
onQueryCancelled(queryContext) {
Assert.equal(
queryContext,
firstContext,
"The first query should be cancelled."
);
Assert.ok(!queryCancelled, "No more than one query should be cancelled.");
queryCancelled = true;
},
};
controller.addQueryListener(controllerListener);
// Wait until FastProvider sends its results to the providers manager.
// Then they will be queued up in a _heuristicProvidersTimer, waiting for
// the results from SlowProvider.
let resultsAddedPromise = new Promise(resolve => {
let observe = async (subject, topic, data) => {
Services.obs.removeObserver(observe, "results-added");
// Fire the second query to cancel the first.
await controller.startQuery(secondContext);
resolve();
};
Services.obs.addObserver(observe, "results-added");
});
controller.startQuery(firstContext);
await resultsAddedPromise;
Assert.ok(queryCancelled, "At least one query was cancelled.");
Assert.ok(queryRecieved, "At least one query finished.");
controller.removeQueryListener(controllerListener);
});
|