| 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.waitTest'); |
| 5 |
goog.setTestOnly('goog.result.waitTest'); |
| 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, waitCallback, waitOnSuccessCallback, waitOnErrorCallback; |
| 15 |
|
| 16 |
var mockClock, propertyReplacer; |
| 17 |
|
| 18 |
function setUpPage() { |
| 19 |
mockClock = new goog.testing.MockClock(); |
| 20 |
mockClock.install(); |
| 21 |
} |
| 22 |
|
| 23 |
function setUp() { |
| 24 |
mockClock.reset(); |
| 25 |
result = new goog.result.SimpleResult(); |
| 26 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 27 |
waitCallback = new goog.testing.recordFunction(); |
| 28 |
waitOnSuccessCallback = new goog.testing.recordFunction(); |
| 29 |
waitOnErrorCallback = new goog.testing.recordFunction(); |
| 30 |
} |
| 31 |
|
| 32 |
function tearDown() { |
| 33 |
result = waitCallback = waitOnSuccessCallback = waitOnErrorCallback = null; |
| 34 |
propertyReplacer.reset(); |
| 35 |
} |
| 36 |
|
| 37 |
function tearDownPage() { |
| 38 |
mockClock.uninstall(); |
| 39 |
} |
| 40 |
|
| 41 |
function testSynchronousSuccess() { |
| 42 |
assertEquals(goog.result.Result.State.PENDING, result.getState()); |
| 43 |
assertUndefined(result.getValue()); |
| 44 |
|
| 45 |
goog.result.wait(result, waitCallback); |
| 46 |
goog.result.waitOnSuccess(result, waitOnSuccessCallback); |
| 47 |
goog.result.waitOnError(result, waitOnErrorCallback); |
| 48 |
|
| 49 |
result.setValue(1); |
| 50 |
|
| 51 |
assertEquals(goog.result.Result.State.SUCCESS, result.getState()); |
| 52 |
assertEquals(1, result.getValue()); |
| 53 |
|
| 54 |
assertWaitCall(waitCallback, result); |
| 55 |
assertCall(waitOnSuccessCallback, 1, result); |
| 56 |
assertNoCall(waitOnErrorCallback); |
| 57 |
} |
| 58 |
|
| 59 |
function testAsynchronousSuccess() { |
| 60 |
goog.result.wait(result, waitCallback); |
| 61 |
goog.result.waitOnSuccess(result, waitOnSuccessCallback); |
| 62 |
goog.result.waitOnError(result, waitOnErrorCallback); |
| 63 |
|
| 64 |
goog.Timer.callOnce(function() { |
| 65 |
result.setValue(1); |
| 66 |
}); |
| 67 |
|
| 68 |
assertUndefined(result.getValue()); |
| 69 |
assertEquals(goog.result.Result.State.PENDING, result.getState()); |
| 70 |
|
| 71 |
assertNoCall(waitCallback); |
| 72 |
assertNoCall(waitOnSuccessCallback); |
| 73 |
assertNoCall(waitOnErrorCallback); |
| 74 |
|
| 75 |
mockClock.tick(); |
| 76 |
|
| 77 |
assertEquals(goog.result.Result.State.SUCCESS, result.getState()); |
| 78 |
assertEquals(1, result.getValue()); |
| 79 |
|
| 80 |
assertWaitCall(waitCallback, result); |
| 81 |
assertCall(waitOnSuccessCallback, 1, result); |
| 82 |
assertNoCall(waitOnErrorCallback); |
| 83 |
} |
| 84 |
|
| 85 |
function testSynchronousError() { |
| 86 |
assertEquals(goog.result.Result.State.PENDING, result.getState()); |
| 87 |
assertUndefined(result.getValue()); |
| 88 |
|
| 89 |
goog.result.wait(result, waitCallback); |
| 90 |
goog.result.waitOnSuccess(result, waitOnSuccessCallback); |
| 91 |
goog.result.waitOnError(result, waitOnErrorCallback); |
| 92 |
|
| 93 |
result.setError(); |
| 94 |
|
| 95 |
assertEquals(goog.result.Result.State.ERROR, result.getState()); |
| 96 |
assertUndefined(result.getValue()); |
| 97 |
|
| 98 |
assertWaitCall(waitCallback, result); |
| 99 |
assertNoCall(waitOnSuccessCallback); |
| 100 |
assertCall(waitOnErrorCallback, undefined, result); |
| 101 |
} |
| 102 |
|
| 103 |
function testAsynchronousError() { |
| 104 |
goog.result.wait(result, waitCallback); |
| 105 |
goog.result.waitOnSuccess(result, waitOnSuccessCallback); |
| 106 |
goog.result.waitOnError(result, waitOnErrorCallback); |
| 107 |
|
| 108 |
goog.Timer.callOnce(function() { |
| 109 |
result.setError(); |
| 110 |
}); |
| 111 |
|
| 112 |
assertEquals(goog.result.Result.State.PENDING, result.getState()); |
| 113 |
assertUndefined(result.getValue()); |
| 114 |
|
| 115 |
assertNoCall(waitCallback); |
| 116 |
assertNoCall(waitOnSuccessCallback); |
| 117 |
assertNoCall(waitOnErrorCallback); |
| 118 |
|
| 119 |
mockClock.tick(); |
| 120 |
|
| 121 |
assertEquals(goog.result.Result.State.ERROR, result.getState()); |
| 122 |
assertUndefined(result.getValue()); |
| 123 |
|
| 124 |
assertWaitCall(waitCallback, result); |
| 125 |
assertNoCall(waitOnSuccessCallback); |
| 126 |
assertCall(waitOnErrorCallback, undefined, result); |
| 127 |
} |
| 128 |
|
| 129 |
function testCustomScope() { |
| 130 |
var scope = {}; |
| 131 |
goog.result.wait(result, waitCallback, scope); |
| 132 |
result.setValue(1); |
| 133 |
assertEquals(scope, waitCallback.popLastCall().getThis()); |
| 134 |
} |
| 135 |
|
| 136 |
function testDefaultScope() { |
| 137 |
goog.result.wait(result, waitCallback); |
| 138 |
result.setValue(1); |
| 139 |
assertEquals(goog.global, waitCallback.popLastCall().getThis()); |
| 140 |
} |
| 141 |
|
| 142 |
function testOnSuccessScope() { |
| 143 |
var scope = {}; |
| 144 |
goog.result.waitOnSuccess(result, waitOnSuccessCallback, scope); |
| 145 |
result.setValue(1); |
| 146 |
assertCall(waitOnSuccessCallback, 1, result, scope); |
| 147 |
} |
| 148 |
|
| 149 |
function testOnErrorScope() { |
| 150 |
var scope = {}; |
| 151 |
goog.result.waitOnError(result, waitOnErrorCallback, scope); |
| 152 |
result.setError(); |
| 153 |
assertCall(waitOnErrorCallback, undefined, result, scope); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Assert that a callback function stubbed out with goog.recordFunction was |
| 158 |
* called with the expected arguments by goog.result.waitOnSuccess/Error. |
| 159 |
* @param {Function} recordedFunction The callback function. |
| 160 |
* @param {?} value The value stored in the result. |
| 161 |
* @param {!goog.result.Result} result The result that was resolved to SUCCESS |
| 162 |
* or ERROR. |
| 163 |
* @param {Object=} opt_scope Optional scope that the test function should be |
| 164 |
* called in. By default, it is goog.global. |
| 165 |
*/ |
| 166 |
function assertCall(recordedFunction, value, result, opt_scope) { |
| 167 |
var scope = opt_scope || goog.global; |
| 168 |
assertEquals(1, recordedFunction.getCallCount()); |
| 169 |
var call = recordedFunction.popLastCall(); |
| 170 |
assertEquals(2, call.getArguments().length); |
| 171 |
assertEquals(value, call.getArgument(0)); |
| 172 |
assertEquals(result, call.getArgument(1)); |
| 173 |
assertEquals(scope, call.getThis()); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Assert that a callback function stubbed out with goog.recordFunction was |
| 178 |
* called with the expected arguments by goog.result.wait. |
| 179 |
* @param {Function} recordedFunction The callback function. |
| 180 |
* @param {!goog.result.Result} result The result that was resolved to SUCCESS |
| 181 |
* or ERROR. |
| 182 |
* @param {Object=} opt_scope Optional scope that the test function should be |
| 183 |
* called in. By default, it is goog.global. |
| 184 |
*/ |
| 185 |
function assertWaitCall(recordedFunction, result, opt_scope) { |
| 186 |
var scope = opt_scope || goog.global; |
| 187 |
assertEquals(1, recordedFunction.getCallCount()); |
| 188 |
var call = recordedFunction.popLastCall(); |
| 189 |
assertEquals(1, call.getArguments().length); |
| 190 |
assertEquals(result, call.getArgument(0)); |
| 191 |
assertEquals(scope, call.getThis()); |
| 192 |
} |
| 193 |
|
| 194 |
function assertNoCall(recordedFunction) { |
| 195 |
assertEquals(0, recordedFunction.getCallCount()); |
| 196 |
} |