Debian Codegraph @

closure-library/closure/goog/result/transform_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.transformTest');
5 goog.setTestOnly('goog.result.transformTest');
6
7 goog.require('goog.Timer');
8 goog.require('goog.result.SimpleResult');
9 goog.require('goog.result');
10 goog.require('goog.testing.MockClock');
11 goog.require('goog.testing.jsunit');
12 goog.require('goog.testing.recordFunction');
13
14 var result, resultCallback, multiplyResult, mockClock;
15
16 function setUpPage() {
17 mockClock = new goog.testing.MockClock();
18 mockClock.install();
19 }
20
21 function setUp() {
22 mockClock.reset();
23 result = new goog.result.SimpleResult();
24 resultCallback = new goog.testing.recordFunction();
25 multiplyResult = goog.testing.recordFunction(function(value) {
26 return value * 2;
27 });
28 }
29
30 function tearDown() {
31 result = multiplyResult = null;
32 }
33
34 function tearDownPage() {
35 mockClock.uninstall();
36 goog.dispose(mockClock);
37 }
38
39 function testTransformWhenResultSuccess() {
40 var transformedResult = goog.result.transform(result, multiplyResult);
41 goog.result.wait(transformedResult, resultCallback);
42
43 assertEquals(goog.result.Result.State.PENDING, result.getState());
44 result.setValue(1);
45 assertTransformerCall(multiplyResult, 1);
46 assertSuccessCall(resultCallback, transformedResult, 2);
47 }
48
49 function testTransformWhenResultSuccessAsync() {
50 var transformedResult = goog.result.transform(result, multiplyResult);
51 goog.result.wait(transformedResult, resultCallback);
52
53 goog.Timer.callOnce(function() {
54 result.setValue(1);
55 });
56
57 assertEquals(goog.result.Result.State.PENDING, result.getState());
58 mockClock.tick();
59 assertTransformerCall(multiplyResult, 1);
60 assertSuccessCall(resultCallback, transformedResult, 2);
61 }
62
63 function testTransformWhenResultError() {
64 var transformedResult = goog.result.transform(result, multiplyResult);
65 goog.result.wait(transformedResult, resultCallback);
66
67 assertEquals(goog.result.Result.State.PENDING, result.getState());
68 result.setError(4);
69 assertNoCall(multiplyResult);
70 assertErrorCall(resultCallback, transformedResult, 4);
71 }
72
73 function testTransformWhenResultErrorAsync() {
74 var transformedResult = goog.result.transform(result, multiplyResult);
75
76 goog.result.wait(transformedResult, resultCallback);
77
78 goog.Timer.callOnce(function() {
79 result.setError(5);
80 });
81
82 assertEquals(goog.result.Result.State.PENDING, result.getState());
83 mockClock.tick();
84 assertNoCall(multiplyResult);
85 assertErrorCall(resultCallback, transformedResult, 5);
86 }
87
88 function testCancelParentResults() {
89 var transformedResult = goog.result.transform(result, multiplyResult);
90 goog.result.wait(transformedResult, resultCallback);
91
92 goog.result.cancelParentResults(transformedResult);
93
94 assertTrue(result.isCanceled());
95 result.setValue(1);
96 assertNoCall(multiplyResult);
97 }
98
99 function testDoubleTransformCancel() {
100 var step1Result = goog.result.transform(result, multiplyResult);
101 var step2Result = goog.result.transform(step1Result, multiplyResult);
102
103 goog.result.cancelParentResults(step2Result);
104
105 assertFalse(result.isCanceled());
106 assertTrue(step1Result.isCanceled());
107 assertTrue(step2Result.isCanceled());
108 }
109
110 function assertSuccessCall(recordFunction, result, value) {
111 assertEquals(1, recordFunction.getCallCount());
112
113 var res = recordFunction.popLastCall().getArgument(0);
114 assertEquals(result, res);
115 assertEquals(goog.result.Result.State.SUCCESS, res.getState());
116 assertEquals(value, res.getValue());
117 }
118
119 function assertErrorCall(recordFunction, result, value) {
120 assertEquals(1, recordFunction.getCallCount());
121
122 var res = recordFunction.popLastCall().getArgument(0);
123 assertEquals(result, res);
124 assertEquals(goog.result.Result.State.ERROR, res.getState());
125 assertEquals(value, res.getError());
126 }
127
128 function assertNoCall(recordFunction) {
129 assertEquals(0, recordFunction.getCallCount());
130 }
131
132 function assertTransformerCall(recordFunction, value) {
133 assertEquals(1, recordFunction.getCallCount());
134
135 var argValue = recordFunction.popLastCall().getArgument(0);
136 assertEquals(value, argValue);
137 }