Bug 1591495 - Update thumbnailer test utility functions. r=Gijs

Significantly, we remove the TestRunner stuff since we have nice things like
add_task now.

Depends on D50850

Differential Revision: https://phabricator.services.mozilla.com/D50851

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mike Conley 2019-11-01 22:54:43 +00:00
parent 3ecb3cbcb2
commit d3d93be943

View File

@ -50,105 +50,6 @@ registerCleanupFunction(function() {
);
});
/**
* Provide the default test function to start our test runner.
*/
function test() {
TestRunner.run();
}
/**
* The test runner that controls the execution flow of our tests.
*/
var TestRunner = {
/**
* Starts the test runner.
*/
run() {
waitForExplicitFinish();
SessionStore.promiseInitialized.then(() => {
this._iter = runTests();
if (this._iter) {
this.next();
} else {
finish();
}
});
},
/**
* Runs the next available test or finishes if there's no test left.
* @param aValue This value will be passed to the yielder via the runner's
* iterator.
*/
next(aValue) {
let obj = TestRunner._iter.next(aValue);
if (obj.done) {
finish();
return;
}
let value = obj.value || obj;
if (value && typeof value.then == "function") {
value.then(
result => {
next(result);
},
error => {
ok(false, error + "\n" + error.stack);
}
);
}
},
};
/**
* Continues the current test execution.
* @param aValue This value will be passed to the yielder via the runner's
* iterator.
*/
function next(aValue) {
TestRunner.next(aValue);
}
/**
* Creates a new tab with the given URI.
* @param aURI The URI that's loaded in the tab.
* @param aCallback The function to call when the tab has loaded.
*/
function addTab(aURI, aCallback) {
let tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, aURI));
let callback = aCallback ? aCallback : next;
BrowserTestUtils.browserLoaded(tab.linkedBrowser).then(callback);
}
/**
* Loads a new URI into the currently selected tab.
* @param aURI The URI to load.
*/
function navigateTo(aURI) {
let browser = gBrowser.selectedBrowser;
BrowserTestUtils.browserLoaded(browser).then(next);
BrowserTestUtils.loadURI(browser, aURI);
}
/**
* Continues the current test execution when a load event for the given element
* has been received.
* @param aElement The DOM element to listen on.
* @param aCallback The function to call when the load event was dispatched.
*/
function whenLoaded(aElement, aCallback = next) {
aElement.addEventListener(
"load",
function() {
executeSoon(aCallback);
},
{ capture: true, once: true }
);
}
/**
* Captures a screenshot for the currently selected tab, stores it in the cache,
* retrieves it from the cache and compares pixel color values.
@ -165,10 +66,8 @@ async function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) {
// Capture the screenshot.
await PageThumbs.captureAndStore(browser);
retrieveImageDataForURL(browser.currentURI.spec, function([r, g, b]) {
let [r, g, b] = await retrieveImageDataForURL(browser.currentURI.spec);
is("" + [r, g, b], "" + [aRed, aGreen, aBlue], aMessage);
next();
});
}
/**
@ -176,9 +75,9 @@ async function captureAndCheckColor(aRed, aGreen, aBlue, aMessage) {
* to a canvas and passes its image data to the callback.
* Note, not compat with e10s!
* @param aURL The url associated with the thumbnail.
* @param aCallback The function to pass the image data to.
* @returns Promise
*/
function retrieveImageDataForURL(aURL, aCallback) {
async function retrieveImageDataForURL(aURL) {
let width = 100,
height = 100;
let thumb = PageThumbs.getThumbnailURL(aURL, width, height);
@ -186,8 +85,8 @@ function retrieveImageDataForURL(aURL, aCallback) {
let htmlns = "http://www.w3.org/1999/xhtml";
let img = document.createElementNS(htmlns, "img");
img.setAttribute("src", thumb);
await BrowserTestUtils.waitForEvent(img, "load", true);
whenLoaded(img, function() {
let canvas = document.createElementNS(htmlns, "canvas");
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
@ -195,9 +94,7 @@ function retrieveImageDataForURL(aURL, aCallback) {
// Draw the image to a canvas and compare the pixel color values.
let ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0, width, height);
let result = ctx.getImageData(0, 0, 100, 100).data;
aCallback(result);
});
return ctx.getImageData(0, 0, 100, 100).data;
}
/**
@ -230,53 +127,47 @@ function removeThumbnail(aURL) {
* Calls addVisits, and then forces the newtab module to repopulate its links.
* See addVisits for parameter descriptions.
*/
function addVisitsAndRepopulateNewTabLinks(aPlaceInfo, aCallback) {
PlacesTestUtils.addVisits(makeURI(aPlaceInfo)).then(() => {
NewTabUtils.links.populateCache(aCallback, true);
async function promiseAddVisitsAndRepopulateNewTabLinks(aPlaceInfo) {
await PlacesTestUtils.addVisits(makeURI(aPlaceInfo));
await new Promise(resolve => {
NewTabUtils.links.populateCache(resolve, true);
});
}
function promiseAddVisitsAndRepopulateNewTabLinks(aPlaceInfo) {
return new Promise(resolve =>
addVisitsAndRepopulateNewTabLinks(aPlaceInfo, resolve)
/**
* Resolves a Promise when the thumbnail for a given URL has been found
* on disk. Keeps trying until the thumbnail has been created.
*
* @param aURL The URL of the thumbnail's page.
* @returns Promise
*/
function whenFileExists(aURL) {
return TestUtils.waitForCondition(
() => {
return thumbnailExists(aURL);
},
`Waiting for ${aURL} to exist.`,
1000,
50
);
}
/**
* Calls a given callback when the thumbnail for a given URL has been found
* on disk. Keeps trying until the thumbnail has been created.
*
* @param aURL The URL of the thumbnail's page.
* @param [optional] aCallback
* Function to be invoked on completion.
*/
function whenFileExists(aURL, aCallback = next) {
let callback = aCallback;
if (!thumbnailExists(aURL)) {
callback = () => whenFileExists(aURL, aCallback);
}
setTimeout(callback, 0);
}
/**
* Calls a given callback when the given file has been removed.
* Resolves a Promise when the given file has been removed.
* Keeps trying until the file is removed.
*
* @param aFile The file that is being removed
* @param [optional] aCallback
* Function to be invoked on completion.
* @returns Promise
*/
function whenFileRemoved(aFile, aCallback) {
let callback = aCallback;
if (aFile.exists()) {
callback = () => whenFileRemoved(aFile, aCallback);
}
executeSoon(callback || next);
}
function wait(aMillis) {
setTimeout(next, aMillis);
function whenFileRemoved(aFile) {
return TestUtils.waitForCondition(
() => {
return !aFile.exists();
},
`Waiting for ${aFile.leafName} to not exist.`,
1000,
50
);
}
/**
@ -294,21 +185,43 @@ function dontExpireThumbnailURLs(aURLs) {
}
function bgCapture(aURL, aOptions) {
bgCaptureWithMethod("capture", aURL, aOptions);
return bgCaptureWithMethod("capture", aURL, aOptions);
}
function bgCaptureIfMissing(aURL, aOptions) {
bgCaptureWithMethod("captureIfMissing", aURL, aOptions);
return bgCaptureWithMethod("captureIfMissing", aURL, aOptions);
}
/**
* Queues a BackgroundPageThumbs capture with the supplied method.
*
* @param {String} aMethodName One of the method names on BackgroundPageThumbs
* for capturing thumbnails. Example: "capture", "captureIfMissing".
* @param {String} aURL The URL of the page to capture.
* @param {Object} aOptions The options object to pass to BackgroundPageThumbs.
*
* @returns {Promise}
* @resolves {Array} Resolves once the capture has completed with an Array of
* results. The first element of the Array is the URL of the captured page,
* and the second element is the completion reason from the BackgroundPageThumbs
* module.
*/
function bgCaptureWithMethod(aMethodName, aURL, aOptions = {}) {
// We'll get oranges if the expiration filter removes the file during the
// test.
dontExpireThumbnailURLs([aURL]);
if (!aOptions.onDone) {
aOptions.onDone = next;
return new Promise(resolve => {
let wrappedDoneFn = aOptions.onDone;
aOptions.onDone = (url, doneReason) => {
if (wrappedDoneFn) {
wrappedDoneFn(url, doneReason);
}
resolve([url, doneReason]);
};
BackgroundPageThumbs[aMethodName](aURL, aOptions);
});
}
function bgTestPageURL(aOpts = {}) {