From 07539165c06454bae4971592b3e159e53bea44c5 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Fri, 20 Jan 2017 16:52:01 +1100 Subject: [PATCH] Bug 1326189 part 2 - Rewrite test_visited_reftest mochitest with async function to make it clearer. r=dholbert This patch is a refactor, and should not change any behavior other than providing a potentially better failure message. MozReview-Commit-ID: 9ow1XwBtGDZ --HG-- extra : rebase_source : 5e968e38e924e7c741cf1d3a5c414650d2b093b0 --- layout/style/test/test_visited_reftests.html | 164 +++++++------------ 1 file changed, 62 insertions(+), 102 deletions(-) diff --git a/layout/style/test/test_visited_reftests.html b/layout/style/test/test_visited_reftests.html index eb687df1ad28..a2398899ea08 100644 --- a/layout/style/test/test_visited_reftests.html +++ b/layout/style/test/test_visited_reftests.html @@ -85,14 +85,6 @@ var gTests = [ "== mathml-links.html mathml-links-ref.html", ]; -// Maintain a reference count of how many things we're waiting for until -// we can say the tests are done. -var gDelayCount = 0; -function AddFinishDependency() - { ++gDelayCount; } -function RemoveFinishDependency() - { if (--gDelayCount == 0) SimpleTest.finish(); } - // We record the maximum number of times we had to look at a test before // it switched to the passing state (though we assume it's 10 to start // rather than 0 so that we have a reasonable default). Then we make a @@ -102,107 +94,75 @@ function RemoveFinishDependency() var gMaxPassingTries = 10; var gTimeoutFactor = 10; -function loadVisitedPage() -{ - var element = document.createElement("iframe"); - element.addEventListener("load", visitedPageLoad); - element.src = "css-visited/visited-page.html"; - document.body.appendChild(element); - AddFinishDependency(); -} - -function visitedPageLoad() -{ - for (var i = 0; i < gTests.length; ++i) { - startTest(i); - } - RemoveFinishDependency(); -} - -function takeSnapshot(iframe_element) -{ - return snapshotWindow(iframe_element.contentWindow, false); -} - -function passes(op, shot1, shot2) -{ - var [correct, s1, s2] = compareSnapshots(shot1, shot2, op == "=="); - return correct; -} - -function startTest(i) -{ - var testLine = gTests[i]; - var splitData = testLine.split(" "); - var testData = - { op: splitData[0], test: splitData[1], reference: splitData[2] }; - var tries = 0; - - // Maintain state specific to this test in the closure exposed to all - // the functions nested inside this one. - - function startIframe(url) - { +function startIframe(url) { + return new Promise(resolve => { var element = document.createElement("iframe"); - element.addEventListener("load", handleLoad); + element.addEventListener("load", () => { + resolve(element.contentWindow); + }, {once: true}); // smaller than normal reftests, but enough for these element.setAttribute("style", "width: 30em; height: 10em"); element.src = "css-visited/" + url; document.body.appendChild(element); - function handleLoad(event) - { - iframe.loaded = true; - if (iframe == reference) { - reference.snapshot = takeSnapshot(element); - } - var other = (iframe == test) ? reference : test; - if (other.loaded) { - // Always wait at least 100ms, so that any test that switches - // from passing to failing when the asynchronous link coloring - // happens should fail at least some of the time. - setTimeout(checkTest, 100); - } - } - function checkTest() - { - var test_snapshot = takeSnapshot(test.element); - if (passes(testData.op, test_snapshot, reference.snapshot)) { - if (tries > gMaxPassingTries) { - gMaxPassingTries = tries; - } - report(true); - } else { - ++tries; - if (tries > gMaxPassingTries * gTimeoutFactor) { - info("Giving up after " + tries + " tries, " + - "maxp=" + gMaxPassingTries + - "fact=" + gTimeoutFactor); - report(false); - } else { - // Links might not have been colored yet. Try again in 100ms. - setTimeout(checkTest, 100); - } - } - } - function report(result) - { - ok(result, "(" + i + ") " + - testData.op + " " + testData.test + " " + testData.reference); - RemoveFinishDependency(); - } - var iframe = { element: element, loaded: false }; - - return iframe; - } - - AddFinishDependency(); - var test = startIframe(testData.test); - var reference = startIframe(testData.reference); + }); } -SimpleTest.waitForExplicitFinish(); -SimpleTest.requestFlakyTimeout("untriaged"); -loadVisitedPage(); +async function runTests() { + SimpleTest.waitForExplicitFinish(); + SimpleTest.requestFlakyTimeout("async link coloring"); + await startIframe("visited-page.html"); + await Promise.all(gTests.map(runTest)); + SimpleTest.finish(); +} + +function passes(equal, shot1, shot2) +{ + let [correct] = compareSnapshots(shot1, shot2, equal); + return correct; +} + +function waitFor100ms() { + return new Promise(resolve => setTimeout(resolve, 100)); +} + +async function runTest(testLine) { + var splitData = testLine.split(" "); + let isEqual = splitData[0] == "=="; + let testFile = splitData[1]; + let refFile = splitData[2]; + + let promiseTestWin = startIframe(testFile); + let promiseRefWin = startIframe(refFile); + let refSnapshot = snapshotWindow(await promiseRefWin); + let testWindow = await promiseTestWin; + // Always wait at least 100ms, so that any test that switches + // from passing to failing when the asynchronous link coloring + // happens should fail at least some of the time. + await waitFor100ms(); + + let tries; + let testSnapshot; + for (tries = 0; tries < gMaxPassingTries * gTimeoutFactor; ++tries) { + testSnapshot = snapshotWindow(testWindow); + if (passes(isEqual, testSnapshot, refSnapshot)) { + if (tries > gMaxPassingTries) { + gMaxPassingTries = tries; + } + break; + } + // Links might not have been colored yet. Try again in 100ms. + await waitFor100ms(); + } + + let result = assertSnapshots(testSnapshot, refSnapshot, + isEqual, null, testFile, refFile); + if (!result) { + info(`Gave up after ${tries} tries, ` + + `maxp=${gMaxPassingTries}, fact=${gTimeoutFactor}`); + } +} + +runTests();