Debian Codegraph @

closure-library/closure/goog/testing/deferredtestcase_test.js

raw
1 // Copyright 2010 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.DeferredTestCaseTest');
16 goog.setTestOnly('goog.testing.DeferredTestCaseTest');
17
18 goog.require('goog.async.Deferred');
19 goog.require('goog.testing.DeferredTestCase');
20 goog.require('goog.testing.TestCase');
21 goog.require('goog.testing.TestRunner');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.testing.recordFunction');
24
25 var deferredTestCase =
26 goog.testing.DeferredTestCase.createAndInstall(document.title);
27 var testTestCase;
28 var runner;
29
30 // Optionally, set a longer-than-usual step timeout.
31 deferredTestCase.stepTimeout = 15 * 1000; // 15 seconds
32
33 // This is the sample code in deferredtestcase.js
34 function testDeferredCallbacks() {
35 var callbackTime = goog.now();
36 var callbacks = new goog.async.Deferred();
37 deferredTestCase.addWaitForAsync('Waiting for 1st callback', callbacks);
38 callbacks.addCallback(function() {
39 assertTrue('We\'re going back in time!', goog.now() >= callbackTime);
40 callbackTime = goog.now();
41 });
42 deferredTestCase.addWaitForAsync('Waiting for 2nd callback', callbacks);
43 callbacks.addCallback(function() {
44 assertTrue('We\'re going back in time!', goog.now() >= callbackTime);
45 callbackTime = goog.now();
46 });
47 deferredTestCase.addWaitForAsync('Waiting for last callback', callbacks);
48 callbacks.addCallback(function() {
49 assertTrue('We\'re going back in time!', goog.now() >= callbackTime);
50 callbackTime = goog.now();
51 });
52
53 deferredTestCase.waitForDeferred(callbacks);
54 }
55
56 function createDeferredTestCase(d) {
57 testTestCase = new goog.testing.DeferredTestCase('Foobar TestCase');
58 testTestCase.add(new goog.testing.TestCase.Test('Foobar Test', function() {
59 this.waitForDeferred(d);
60 }, testTestCase));
61
62 var testCompleteCallback = new goog.async.Deferred();
63 testTestCase.setCompletedCallback(function() {
64 testCompleteCallback.callback(true);
65 });
66
67 // We're not going to use the runner to run the test, but we attach one
68 // here anyway because without a runner TestCase throws an exception in
69 // finalize().
70 var runner = new goog.testing.TestRunner();
71 runner.initialize(testTestCase);
72
73 return testCompleteCallback;
74 }
75
76 function testDeferredWait() {
77 var d = new goog.async.Deferred();
78 deferredTestCase.addWaitForAsync('Foobar', d);
79 d.addCallback(function() { return goog.async.Deferred.succeed(true); });
80 deferredTestCase.waitForDeferred(d);
81 }
82
83 function testNonAsync() {
84 assertTrue(true);
85 }
86
87 function testPassWithTestRunner() {
88 var d = new goog.async.Deferred();
89 d.addCallback(function() { return goog.async.Deferred.succeed(true); });
90
91 var testCompleteDeferred = createDeferredTestCase(d);
92 testTestCase.execute();
93
94 var deferredCallbackOnPass = new goog.async.Deferred();
95 deferredCallbackOnPass.addCallback(function() {
96 return testCompleteDeferred;
97 });
98 deferredCallbackOnPass.addCallback(function() {
99 assertTrue('Test case should have succeeded.', testTestCase.isSuccess());
100 });
101
102 deferredTestCase.waitForDeferred(deferredCallbackOnPass);
103 }
104
105 function testFailWithTestRunner() {
106 var d = new goog.async.Deferred();
107 d.addCallback(function() { return goog.async.Deferred.fail(true); });
108
109 var testCompleteDeferred = createDeferredTestCase(d);
110
111 // Mock doAsyncError to instead let the test completes successfully,
112 // but record the failure. The test works as is because the failing
113 // deferred is not actually asynchronous.
114 var mockDoAsyncError = goog.testing.recordFunction(function() {
115 testTestCase.continueTesting();
116 });
117 testTestCase.doAsyncError = mockDoAsyncError;
118
119 testTestCase.execute();
120 assertEquals(1, mockDoAsyncError.getCallCount());
121 }