Bug 1270301 - Add test for SimpleTest.waitForCondition. r?jmaher draft
authorXidorn Quan <quanxunzhen@gmail.com>
Mon, 09 May 2016 15:23:55 +1000
changeset 364707 74f964e26968f3bfb329da877384b8284911e2ab
parent 364704 1e9206743b63e55baedffde40467e860e621f83f
child 520377 a7ab93f621870754f8112d796bd5157944173683
push id17553
push userxquan@mozilla.com
push dateMon, 09 May 2016 05:24:11 +0000
reviewersjmaher
bugs1270301
milestone49.0a1
Bug 1270301 - Add test for SimpleTest.waitForCondition. r?jmaher MozReview-Commit-ID: KNLvWJ7VQID
testing/mochitest/tests/Harness_sanity/mochitest.ini
testing/mochitest/tests/Harness_sanity/test_sanity_waitForCondition.html
--- 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>