Bug 1270301 - Add test for SimpleTest.waitForCondition. r?jmaher
MozReview-Commit-ID: KNLvWJ7VQID
--- a/testing/mochitest/tests/Harness_sanity/mochitest.ini
+++ b/testing/mochitest/tests/Harness_sanity/mochitest.ini
@@ -44,8 +44,9 @@ skip-if = buildapp == 'mulet' || toolkit
skip-if = toolkit == 'android' #bug 688052
[test_sanity_manifest.html]
skip-if = toolkit == 'android' # we use the old manifest style on android
fail-if = true
[test_sanity_manifest_pf.html]
skip-if = toolkit == 'android' # we use the old manifest style on android
fail-if = true
[test_spawn_task.html]
+[test_sanity_waitForCondition.html]
new file mode 100644
--- /dev/null
+++ b/testing/mochitest/tests/Harness_sanity/test_sanity_waitForCondition.html
@@ -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>