summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/Harness_sanity/test_sanity_waitForCondition.html
blob: 5ac1daf2e0fa06a42c8548bdb3a9770ab2f5eef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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>