aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/deferredtestcase_test.html
blob: d6820bf6423b53b61ebe69d87f5415ba70056038 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
-->
<head>
<title>Closure Unit Tests - goog.testing.DeferredTestCase Asyncronous Tests</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.async.Deferred');
    goog.require('goog.testing.DeferredTestCase');
    goog.require('goog.testing.TestRunner');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.recordFunction');
  </script>
</head>
<body>
  <script type="text/javascript">
    var deferredTestCase =
        goog.testing.DeferredTestCase.createAndInstall(document.title);
    var testTestCase;
    var runner;

    // Optionally, set a longer-than-usual step timeout.
    deferredTestCase.stepTimeout = 15 * 1000; // 15 seconds

    // This is the sample code in deferredtestcase.js
    function testDeferredCallbacks() {
      var callbackTime = goog.now();
      var callbacks = new goog.async.Deferred();
      deferredTestCase.addWaitForAsync('Waiting for 1st callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });
      deferredTestCase.addWaitForAsync('Waiting for 2nd callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });
      deferredTestCase.addWaitForAsync('Waiting for last callback', callbacks);
      callbacks.addCallback(
          function() {
            assertTrue(
                'We\'re going back in time!', goog.now() >= callbackTime);
            callbackTime = goog.now();
          });

      deferredTestCase.waitForDeferred(callbacks);
    }

    function createDeferredTestCase(d) {
      testTestCase = new goog.testing.DeferredTestCase('Foobar TestCase');
      testTestCase.add(new goog.testing.TestCase.Test(
        'Foobar Test',
        function() {
          this.waitForDeferred(d);
        },
        testTestCase));

      var testCompleteCallback = new goog.async.Deferred();
      testTestCase.setCompletedCallback(
          function() {
            testCompleteCallback.callback(true);
          });

      // We're not going to use the runner to run the test, but we attach one
      // here anyway because without a runner TestCase throws an exception in
      // finalize().
      var runner = new goog.testing.TestRunner();
      runner.initialize(testTestCase);

      return testCompleteCallback;
    }

    function testDeferredWait() {
      var d = new goog.async.Deferred();
      deferredTestCase.addWaitForAsync('Foobar', d);
      d.addCallback(function() {
        return goog.async.Deferred.succeed(true);
      });
      deferredTestCase.waitForDeferred(d);
    }

    function testNonAsync() {
      assertTrue(true);
    }

    function testPassWithTestRunner() {
      var d = new goog.async.Deferred();
      d.addCallback(function() {
        return goog.async.Deferred.succeed(true);
      });

      var testCompleteDeferred = createDeferredTestCase(d);
      testTestCase.execute();

      var deferredCallbackOnPass = new goog.async.Deferred();
      deferredCallbackOnPass.addCallback(function() {
        return testCompleteDeferred;
      });
      deferredCallbackOnPass.addCallback(function() {
        assertTrue('Test case should have succeded.', testTestCase.isSuccess());
      });

      deferredTestCase.waitForDeferred(deferredCallbackOnPass);
    }

    function testFailWithTestRunner() {
      var d = new goog.async.Deferred();
      d.addCallback(function() {
        return goog.async.Deferred.fail(true);
      });

      var testCompleteDeferred = createDeferredTestCase(d);

      // Mock doAsyncError to instead let the test completes successfully,
      // but record the failure. The test works as is because the failing
      // deferred is not actually asynchronous.
      var mockDoAsyncError = goog.testing.recordFunction(function() {
        testTestCase.continueTesting();
      });
      testTestCase.doAsyncError = mockDoAsyncError;

      testTestCase.execute();
      assertEquals(1, mockDoAsyncError.getCallCount());
    }

  </script>
</body>
</html>