Bug 1270301 - Add test for SimpleTest.waitForCondition. r=jmaher

MozReview-Commit-ID: KNLvWJ7VQID

--HG--
extra : transplant_source : t%F9d%E2ih%F3%BF%B3%29%DA%87s%84%B8%28I%11%E2%AB
This commit is contained in:
Xidorn Quan 2016-05-09 15:23:55 +10:00
parent 48b93f48cd
commit 54b9c867d3
2 changed files with 54 additions and 0 deletions

View File

@ -49,3 +49,4 @@ fail-if = true
skip-if = toolkit == 'android' # we use the old manifest style on android
fail-if = true
[test_spawn_task.html]
[test_sanity_waitForCondition.html]

View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>SimpleTest.waitForCondition test</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<script>
var captureFailure = false;
var capturedFailures = [];
window.ok = function (cond, name, diag) {
if (!captureFailure) {
SimpleTest.ok(cond, name, diag);
} else {
if (cond) {
SimpleTest.ok(false, `Expect a failure with "${name}"`);
} else {
capturedFailures.push(name);
}
}
};
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("test behavior SimpleTest.waitForCondition");
addLoadEvent(testNormal);
function testNormal() {
var condition = false;
SimpleTest.waitForCondition(() => condition, () => {
ok(condition, "Should only be called when condition is true");
SimpleTest.executeSoon(testTimeout);
}, "Shouldn't timeout");
setTimeout(() => { condition = true; }, 1000);
}
function testTimeout() {
captureFailure = true;
SimpleTest.waitForCondition(() => false, () => {
captureFailure = false;
is(capturedFailures.length, 1, "Should captured one failure");
is(capturedFailures[0], "Should timeout",
"Should capture the failure passed in");
SimpleTest.executeSoon(() => SimpleTest.finish());
}, "Should timeout");
}
</script>
</body>
</html>