Bug 1663941 - Do not call ResourceWatcher onAvailable callback with empty arrays of ressources r=ochameau

Differential Revision: https://phabricator.services.mozilla.com/D89609
This commit is contained in:
Julian Descottes 2020-09-09 15:03:21 +00:00
parent 05c4dcd62f
commit 4512e67b05
2 changed files with 54 additions and 4 deletions

View File

@ -551,11 +551,12 @@ class ResourceWatcher {
}
async _forwardCachedResources(resourceTypes, onAvailable) {
await onAvailable(
this._cache.filter(resource =>
resourceTypes.includes(resource.resourceType)
)
const cachedResources = this._cache.filter(resource =>
resourceTypes.includes(resource.resourceType)
);
if (cachedResources.length > 0) {
await onAvailable(cachedResources);
}
}
/**

View File

@ -275,6 +275,55 @@ async function testIgnoreExistingResources(isFirstListenerIgnoreExisting) {
await client.close();
}
add_task(async function() {
info("Test that onAvailable is not called with an empty resources array");
const tab = await addTab(TEST_URI);
const {
client,
resourceWatcher,
targetList,
} = await initResourceWatcherAndTarget(tab);
info("Register first listener to get all available resources");
const availableResources = [];
let onAvailableCallCount = 0;
const onAvailable = resources => {
ok(
resources.length > 0,
"onAvailable is called with a non empty resources array"
);
availableResources.push(...resources);
onAvailableCallCount++;
};
await resourceWatcher.watchResources(
[ResourceWatcher.TYPES.CONSOLE_MESSAGE],
{ onAvailable }
);
is(availableResources.length, 0, "availableResources array is empty");
is(onAvailableCallCount, 0, "onAvailable was never called");
info("Add messages as console message");
await logMessages(tab.linkedBrowser, ["expected message"]);
await waitUntil(() => availableResources.length === 1);
is(availableResources.length, 1, "availableResources array has one item");
is(onAvailableCallCount, 1, "onAvailable was called only once");
is(
availableResources[0].message.arguments[0],
"expected message",
"onAvailable was called with the expected resource"
);
resourceWatcher.unwatchResources([ResourceWatcher.TYPES.CONSOLE_MESSAGE], {
onAvailable,
});
await targetList.destroy();
await client.close();
});
function assertContents(resources, expectedMessages) {
is(
resources.length,