Bug 1388082 - Use async/await in reftest module. r=automatedtester

MozReview-Commit-ID: 9sUL77lPCK1

--HG--
extra : rebase_source : 4388640825b40e8cb93216d1f1323ebabd474c16
This commit is contained in:
Andreas Tolfsen 2017-08-07 18:57:37 +01:00
Родитель 0b25415ec8
Коммит e13cd58584
1 изменённых файлов: 37 добавлений и 31 удалений

Просмотреть файл

@ -8,7 +8,6 @@ const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Log.jsm");
Cu.import("resource://gre/modules/Preferences.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/capture.js");
@ -69,7 +68,7 @@ reftest.Runner = class {
* @param {string} screenshotMode
* String enum representing when screenshots should be taken
*/
*setup(urlCount, screenshotMode) {
async setup(urlCount, screenshotMode) {
this.parentWindow = assert.window(this.driver.getCurrentWindow());
this.screenshotMode = SCREENSHOT_MODE[screenshotMode] ||
@ -78,18 +77,18 @@ reftest.Runner = class {
this.urlCount = Object.keys(urlCount || {})
.reduce((map, key) => map.set(key, urlCount[key]), new Map());
yield this.ensureWindow();
await this.ensureWindow();
}
*ensureWindow() {
async ensureWindow() {
if (this.reftestWin && !this.reftestWin.closed) {
return this.reftestWin;
}
let reftestWin = yield this.openWindow();
let reftestWin = await this.openWindow();
let found = this.driver.findWindow([reftestWin], () => true);
yield this.driver.setWindowHandle(found, true);
await this.driver.setWindowHandle(found, true);
this.windowUtils = reftestWin.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
@ -97,14 +96,14 @@ reftest.Runner = class {
return reftestWin;
}
*openWindow() {
async openWindow() {
let reftestWin;
yield new Promise(resolve => {
await new Promise(resolve => {
reftestWin = this.parentWindow.openDialog(
"chrome://marionette/content/reftest.xul",
"reftest",
"chrome,dialog,height=600,width=600,all",
() => resolve());
resolve);
});
let browser = reftestWin.document.createElementNS(XUL_NS, "xul:browser");
@ -152,14 +151,19 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
* @param {string} testUrl
* URL of the test itself.
* @param {Array.<Array>} references
* Array representing a tree of references to try. Each item in
* the array represents a single reference node and has the form
* [referenceUrl, references, relation], where referenceUrl is a
* string to the url, relation is either "==" or "!=" depending on
* the type of reftest, and references is another array containing
* Array representing a tree of references to try.
*
* Each item in the array represents a single reference node and
* has the form <code>[referenceUrl, references, relation]</code>,
* where <var>referenceUrl</var> is a string to the URL, relation
* is either <code>==</code> or <code>!=</code> depending on the
* type of reftest, and references is another array containing
* items of the same form, representing further comparisons treated
* as AND with the current item. Sibling entries are treated as OR.
*
* For example with testUrl of T:
*
* <pre><code>
* references = [[A, [[B, [], ==]], ==]]
* Must have T == A AND A == B to pass
*
@ -168,15 +172,17 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
*
* references = [[A, [[B, [], ==], [C, [], ==]], ==], [D, [], ]]
* Must have (T == A AND A == B) OR (T == A AND A == C) OR (T == D)
* </code></pre>
*
* @param {string} expected
* Expected test outcome (e.g. PASS, FAIL).
* Expected test outcome (e.g. <tt>PASS</tt>, <tt>FAIL</tt>).
* @param {number} timeout
* Test timeout in ms
* Test timeout in milliseconds.
*
* @return {Object}
* Result object with fields status, message and extra.
*/
*run(testUrl, references, expected, timeout) {
async run(testUrl, references, expected, timeout) {
let timeoutHandle;
@ -186,17 +192,17 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
}, timeout);
});
let testRunner = Task.spawn(function*() {
let testRunner = (async () => {
let result;
try {
result = yield this.runTest(testUrl, references, expected, timeout);
result = await this.runTest(testUrl, references, expected, timeout);
} catch (e) {
result = {status: STATUS.ERROR, message: e.stack, extra: {}};
}
return result;
}.bind(this));
})();
let result = yield Promise.race([testRunner, timeoutPromise]);
let result = await Promise.race([testRunner, timeoutPromise]);
this.parentWindow.clearTimeout(timeoutHandle);
if (result.status === STATUS.TIMEOUT) {
this.abort();
@ -205,8 +211,8 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
return result;
}
*runTest(testUrl, references, expected, timeout) {
let win = yield this.ensureWindow();
async runTest(testUrl, references, expected, timeout) {
let win = await this.ensureWindow();
function toBase64(screenshot) {
let dataURL = screenshot.canvas.toDataURL();
@ -232,7 +238,7 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
let [lhsUrl, rhsUrl, references, relation] = stack.pop();
message += `Testing ${lhsUrl} ${relation} ${rhsUrl}\n`;
let comparison = yield this.compareUrls(
let comparison = await this.compareUrls(
win, lhsUrl, rhsUrl, relation, timeout);
function recordScreenshot() {
@ -293,13 +299,13 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
return result;
}
*compareUrls(win, lhsUrl, rhsUrl, relation, timeout) {
async compareUrls(win, lhsUrl, rhsUrl, relation, timeout) {
logger.info(`Testing ${lhsUrl} ${relation} ${rhsUrl}`);
// Take the reference screenshot first so that if we pause
// we see the test rendering
let rhs = yield this.screenshot(win, rhsUrl, timeout);
let lhs = yield this.screenshot(win, lhsUrl, timeout);
let rhs = await this.screenshot(win, rhsUrl, timeout);
let lhs = await this.screenshot(win, lhsUrl, timeout);
let maxDifferences = {};
@ -327,7 +333,7 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
return {lhs, rhs, passed};
}
*screenshot(win, url, timeout) {
async screenshot(win, url, timeout) {
let canvas = null;
let remainingCount = this.urlCount.get(url) || 1;
let cache = remainingCount > 1;
@ -361,16 +367,16 @@ min-width: 600px; min-height: 600px; max-width: 600px; max-height: 600px`;
};
if (this.lastURL === url) {
logger.debug(`Refreshing page`);
yield this.driver.listener.refresh(navigateOpts);
await this.driver.listener.refresh(navigateOpts);
} else {
navigateOpts.url = url;
navigateOpts.loadEventExpected = false;
yield this.driver.listener.get(navigateOpts);
await this.driver.listener.get(navigateOpts);
this.lastURL = url;
}
this.driver.curBrowser.contentBrowser.focus();
yield this.driver.listener.reftestWait(url, this.remote);
await this.driver.listener.reftestWait(url, this.remote);
canvas = capture.canvas(
win,