mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-11-08 20:28:42 +02:00
The problem is that the test creates and registers many providers but never unregisters them. Usually that's not a problem since test providers have random names and test query contexts are created with the `providers` option. But sometimes two different providers end up with the same name because `Math.random() * 100000` ends up being the same. Therefore a context can end up with two (duplicate) providers when there should be only one, which means `context.results` ends up with two copies of each result. This patch makes several changes: * test_resultBuckets.js now unregisters providers. * Stop using `Math.random()` as the basis for making random test provider names. Instead, use UUIDs. * Add a `registerCleanupFunction` in head.js for unregistering test providers (even though that wouldn't have fixed this bug). * Make `registerBasicTestProvider` return the provider instead of its name so that the provider can be passed to `UrlbarProvidersManager.unregisterProvider()`. Differential Revision: https://phabricator.services.mozilla.com/D104929
104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
/* Any copyright is dedicated to the Public Domain.
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
/**
|
|
* These tests test the UrlbarController in association with the model.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
const { PromiseUtils } = ChromeUtils.import(
|
|
"resource://gre/modules/PromiseUtils.jsm"
|
|
);
|
|
|
|
const TEST_URL = "http://example.com";
|
|
const match = new UrlbarResult(
|
|
UrlbarUtils.RESULT_TYPE.TAB_SWITCH,
|
|
UrlbarUtils.RESULT_SOURCE.TABS,
|
|
{ url: TEST_URL }
|
|
);
|
|
let controller;
|
|
|
|
/**
|
|
* Asserts that the query context has the expected values.
|
|
*
|
|
* @param {UrlbarQueryContext} 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(async function setup() {
|
|
controller = UrlbarTestUtils.newMockController();
|
|
});
|
|
|
|
add_task(async function test_basic_search() {
|
|
let provider = registerBasicTestProvider([match]);
|
|
const context = createContext(TEST_URL, { providers: [provider.name] });
|
|
|
|
let startedPromise = promiseControllerNotification(
|
|
controller,
|
|
"onQueryStarted"
|
|
);
|
|
let resultsPromise = promiseControllerNotification(
|
|
controller,
|
|
"onQueryResults"
|
|
);
|
|
|
|
controller.startQuery(context);
|
|
|
|
let params = await startedPromise;
|
|
|
|
Assert.equal(params[0], context);
|
|
|
|
params = await resultsPromise;
|
|
|
|
Assert.deepEqual(
|
|
params[0].results,
|
|
[match],
|
|
"Should have the expected match"
|
|
);
|
|
});
|
|
|
|
add_task(async function test_cancel_search() {
|
|
let providerCanceledDeferred = PromiseUtils.defer();
|
|
let provider = registerBasicTestProvider(
|
|
[match],
|
|
providerCanceledDeferred.resolve
|
|
);
|
|
const context = createContext(TEST_URL, { providers: [provider.name] });
|
|
|
|
let startedPromise = promiseControllerNotification(
|
|
controller,
|
|
"onQueryStarted"
|
|
);
|
|
let cancelPromise = promiseControllerNotification(
|
|
controller,
|
|
"onQueryCancelled"
|
|
);
|
|
|
|
controller.startQuery(context);
|
|
|
|
let params = await startedPromise;
|
|
|
|
controller.cancelQuery(context);
|
|
|
|
Assert.equal(params[0], context);
|
|
|
|
info("Should tell the provider the query is canceled");
|
|
await providerCanceledDeferred.promise;
|
|
|
|
params = await cancelPromise;
|
|
});
|