Debian Codegraph @

closure-library/closure/goog/result/chain_test.js

raw
1 // Copyright 2012 The Closure Library Authors. All Rights Reserved.
2 // Use of this source code is governed by the Apache License, Version 2.0.
3
4 goog.provide('goog.result.chainTest');
5 goog.setTestOnly('goog.result.chainTest');
6
7 goog.require('goog.Timer');
8 goog.require('goog.result');
9 goog.require('goog.testing.MockClock');
10 goog.require('goog.testing.jsunit');
11 goog.require('goog.testing.recordFunction');
12
13 var givenResult, dependentResult, counter, actionCallback;
14 var mockClock;
15
16 function setUpPage() {
17 mockClock = new goog.testing.MockClock();
18 mockClock.install();
19 }
20
21 function setUp() {
22 mockClock.reset();
23 givenResult = new goog.result.SimpleResult();
24 dependentResult = new goog.result.SimpleResult();
25 counter = new goog.testing.recordFunction();
26 actionCallback = goog.testing.recordFunction(function(result) {
27 return dependentResult;
28 });
29 }
30
31 function tearDown() {
32 givenResult = dependentResult = counter = null;
33 }
34
35 function tearDownPage() {
36 mockClock.uninstall();
37 }
38
39 // SYNCHRONOUS TESTS:
40
41 function testChainWhenBothResultsSuccess() {
42 var finalResult = goog.result.chain(givenResult, actionCallback);
43 goog.result.wait(finalResult, counter);
44
45 givenResult.setValue(1);
46 dependentResult.setValue(2);
47
48 assertSuccess(actionCallback, givenResult, 1);
49 assertSuccess(counter, finalResult, 2);
50 }
51
52 function testChainWhenFirstResultError() {
53 var finalResult = goog.result.chain(givenResult, actionCallback);
54 goog.result.wait(finalResult, counter);
55
56 givenResult.setError(4);
57
58 assertNoCall(actionCallback);
59 assertError(counter, finalResult, 4);
60 }
61
62 function testChainWhenSecondResultError() {
63 var finalResult = goog.result.chain(givenResult, actionCallback);
64 goog.result.wait(finalResult, counter);
65
66 givenResult.setValue(1);
67 dependentResult.setError(5);
68
69 assertSuccess(actionCallback, givenResult, 1);
70 assertError(counter, finalResult, 5);
71 }
72
73 function testChainCancelFirstResult() {
74 var finalResult = goog.result.chain(givenResult, actionCallback);
75 goog.result.wait(finalResult, counter);
76
77 goog.result.cancelParentResults(finalResult);
78
79 assertNoCall(actionCallback);
80 assertTrue(givenResult.isCanceled());
81 assertTrue(finalResult.isCanceled());
82 }
83
84 function testChainCancelSecondResult() {
85 var finalResult = goog.result.chain(givenResult, actionCallback);
86 goog.result.wait(finalResult, counter);
87
88 givenResult.setValue(1);
89 goog.result.cancelParentResults(finalResult);
90
91 assertSuccess(actionCallback, givenResult, 1);
92 assertTrue(dependentResult.isCanceled());
93 assertTrue(finalResult.isCanceled());
94 }
95
96 function testDoubleChainCancel() {
97 var intermediateResult = goog.result.chain(givenResult, actionCallback);
98 var finalResult = goog.result.chain(intermediateResult, actionCallback);
99
100 assertTrue(goog.result.cancelParentResults(finalResult));
101 assertTrue(finalResult.isCanceled());
102 assertTrue(intermediateResult.isCanceled());
103 assertFalse(givenResult.isCanceled());
104 assertFalse(goog.result.cancelParentResults(finalResult));
105 }
106
107 function testCustomScope() {
108 var scope = {};
109 var finalResult = goog.result.chain(givenResult, actionCallback, scope);
110 goog.result.wait(finalResult, counter);
111
112 givenResult.setValue(1);
113 dependentResult.setValue(2);
114
115 assertEquals(scope, actionCallback.popLastCall().getThis());
116 }
117
118
119 // ASYNCHRONOUS TESTS:
120
121 function testChainAsyncWhenBothResultsSuccess() {
122 var finalResult = goog.result.chain(givenResult, actionCallback);
123 goog.result.wait(finalResult, counter);
124
125 goog.Timer.callOnce(function() { givenResult.setValue(1); });
126 mockClock.tick();
127
128 assertSuccess(actionCallback, givenResult, 1);
129
130 goog.Timer.callOnce(function() { dependentResult.setValue(2); });
131 mockClock.tick();
132
133 assertSuccess(counter, finalResult, 2);
134 }
135
136 function testChainAsyncWhenFirstResultError() {
137 var finalResult = goog.result.chain(givenResult, actionCallback);
138 goog.result.wait(finalResult, counter);
139
140 goog.Timer.callOnce(function() { givenResult.setError(6); });
141 mockClock.tick();
142
143 assertNoCall(actionCallback);
144 assertError(counter, finalResult, 6);
145 }
146
147 function testChainAsyncWhenSecondResultError() {
148 var finalResult = goog.result.chain(givenResult, actionCallback);
149 goog.result.wait(finalResult, counter);
150
151 goog.Timer.callOnce(function() { givenResult.setValue(1); });
152 mockClock.tick();
153
154 assertSuccess(actionCallback, givenResult, 1);
155
156 goog.Timer.callOnce(function() { dependentResult.setError(7); });
157 mockClock.tick();
158
159 assertError(counter, finalResult, 7);
160 }
161
162 function testChainAsyncCancelFirstResult() {
163 var finalResult = goog.result.chain(givenResult, actionCallback);
164 goog.result.wait(finalResult, counter);
165
166 goog.Timer.callOnce(function() {
167 goog.result.cancelParentResults(finalResult);
168 });
169 mockClock.tick();
170
171 assertNoCall(actionCallback);
172 assertTrue(givenResult.isCanceled());
173 assertTrue(finalResult.isCanceled());
174 }
175
176 function testChainAsyncCancelSecondResult() {
177 var finalResult = goog.result.chain(givenResult, actionCallback);
178 goog.result.wait(finalResult, counter);
179
180 goog.Timer.callOnce(function() { givenResult.setValue(1); });
181 mockClock.tick();
182
183 assertSuccess(actionCallback, givenResult, 1);
184
185 goog.Timer.callOnce(function() {
186 goog.result.cancelParentResults(finalResult);
187 });
188 mockClock.tick();
189
190 assertTrue(dependentResult.isCanceled());
191 assertTrue(finalResult.isCanceled());
192 }
193
194 // HELPER FUNCTIONS:
195
196 // Assert that the recordFunction was called once with an argument of
197 // 'result' (the second argument) which has a state of SUCCESS and
198 // a value of 'value' (the third argument).
199 function assertSuccess(recordFunction, result, value) {
200 assertEquals(1, recordFunction.getCallCount());
201 var res = recordFunction.popLastCall().getArgument(0);
202 assertEquals(result, res);
203 assertEquals(goog.result.Result.State.SUCCESS, res.getState());
204 assertEquals(value, res.getValue());
205 }
206
207 // Assert that the recordFunction was called once with an argument of
208 // 'result' (the second argument) which has a state of ERROR.
209 function assertError(recordFunction, result, value) {
210 assertEquals(1, recordFunction.getCallCount());
211 var res = recordFunction.popLastCall().getArgument(0);
212 assertEquals(result, res);
213 assertEquals(goog.result.Result.State.ERROR, res.getState());
214 assertEquals(value, res.getError());
215 }
216
217 // Assert that the recordFunction wasn't called
218 function assertNoCall(recordFunction) {
219 assertEquals(0, recordFunction.getCallCount());
220 }