| 1 |
// Copyright 2009 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.ContinuationTestCaseTest'); |
| 16 |
goog.setTestOnly('goog.testing.ContinuationTestCaseTest'); |
| 17 |
|
| 18 |
goog.require('goog.events'); |
| 19 |
goog.require('goog.events.EventTarget'); |
| 20 |
goog.require('goog.testing.ContinuationTestCase'); |
| 21 |
goog.require('goog.testing.MockClock'); |
| 22 |
goog.require('goog.testing.PropertyReplacer'); |
| 23 |
goog.require('goog.testing.TestCase'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
|
| 26 |
/** |
| 27 |
* @fileoverview This test file uses the ContinuationTestCase to test itself, |
| 28 |
* which is a little confusing. It's also difficult to write a truly effective |
| 29 |
* test, since testing a failure causes an actual failure in the test runner. |
| 30 |
* All tests have been manually verified using a sophisticated combination of |
| 31 |
* alerts and false assertions. |
| 32 |
*/ |
| 33 |
|
| 34 |
var testCase = new goog.testing.ContinuationTestCase('Continuation Test Case'); |
| 35 |
testCase.autoDiscoverTests(); |
| 36 |
|
| 37 |
// Standalone Closure Test Runner. |
| 38 |
if (typeof G_testRunner != 'undefined') { |
| 39 |
G_testRunner.initialize(testCase); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
var clock = new goog.testing.MockClock(); |
| 44 |
var count = 0; |
| 45 |
var stubs = new goog.testing.PropertyReplacer(); |
| 46 |
|
| 47 |
|
| 48 |
function setUpPage() { |
| 49 |
count = testCase.getCount(); |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Resets the mock clock. Includes a wait step to verify that setUp routines |
| 55 |
* can contain continuations. |
| 56 |
*/ |
| 57 |
function setUp() { |
| 58 |
waitForTimeout(function() { |
| 59 |
// Pointless assertion to verify that setUp methods can contain waits. |
| 60 |
assertEquals(count, testCase.getCount()); |
| 61 |
}, 0); |
| 62 |
|
| 63 |
clock.reset(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/** |
| 68 |
* Uninstalls the mock clock if it was installed, and restores the Step timeout |
| 69 |
* functions to the default window implementations. |
| 70 |
*/ |
| 71 |
function tearDown() { |
| 72 |
clock.uninstall(); |
| 73 |
stubs.reset(); |
| 74 |
|
| 75 |
waitForTimeout(function() { |
| 76 |
// Pointless assertion to verify that tearDown methods can contain waits. |
| 77 |
assertTrue(testCase.now() >= testCase.startTime_); |
| 78 |
}, 0); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* Installs the Mock Clock and replaces the Step timeouts with the mock |
| 84 |
* implementations. |
| 85 |
*/ |
| 86 |
function installMockClock() { |
| 87 |
clock.install(); |
| 88 |
|
| 89 |
// Overwrite the "protected" setTimeout and clearTimeout with the versions |
| 90 |
// replaced by MockClock. Normal tests should never do this, but we need to |
| 91 |
// test the ContinuationTest itself. |
| 92 |
stubs.set( |
| 93 |
goog.testing.ContinuationTestCase.Step, 'protectedClearTimeout_', |
| 94 |
window.clearTimeout); |
| 95 |
stubs.set( |
| 96 |
goog.testing.ContinuationTestCase.Step, 'protectedSetTimeout_', |
| 97 |
window.setTimeout); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* @return {goog.testing.ContinuationTestCase.Step} A generic step in a |
| 103 |
* continuation test. |
| 104 |
*/ |
| 105 |
function getSampleStep() { |
| 106 |
return new goog.testing.ContinuationTestCase.Step('test', function() {}); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* @return {goog.testing.ContinuationTestCase.ContinuationTest} A simple |
| 112 |
* continuation test with generic setUp, test, and tearDown functions. |
| 113 |
*/ |
| 114 |
function getSampleTest() { |
| 115 |
var setupStep = new goog.testing.TestCase.Test('setup', function() {}); |
| 116 |
var testStep = new goog.testing.TestCase.Test('test', function() {}); |
| 117 |
var teardownStep = new goog.testing.TestCase.Test('teardown', function() {}); |
| 118 |
|
| 119 |
return new goog.testing.ContinuationTestCase.ContinuationTest( |
| 120 |
setupStep, testStep, teardownStep); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
function testStepWaiting() { |
| 125 |
var step = getSampleStep(); |
| 126 |
assertTrue(step.waiting); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
function testStepSetTimeout() { |
| 131 |
installMockClock(); |
| 132 |
var step = getSampleStep(); |
| 133 |
|
| 134 |
var timeoutReached = false; |
| 135 |
step.setTimeout(function() { timeoutReached = true }, 100); |
| 136 |
|
| 137 |
clock.tick(50); |
| 138 |
assertFalse(timeoutReached); |
| 139 |
clock.tick(50); |
| 140 |
assertTrue(timeoutReached); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
function testStepClearTimeout() { |
| 145 |
var step = new goog.testing.ContinuationTestCase.Step('test', function() {}); |
| 146 |
|
| 147 |
var timeoutReached = false; |
| 148 |
step.setTimeout(function() { timeoutReached = true }, 100); |
| 149 |
|
| 150 |
clock.tick(50); |
| 151 |
assertFalse(timeoutReached); |
| 152 |
step.clearTimeout(); |
| 153 |
clock.tick(50); |
| 154 |
assertFalse(timeoutReached); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function testTestPhases() { |
| 159 |
var test = getSampleTest(); |
| 160 |
|
| 161 |
assertEquals('setup', test.getCurrentPhase()[0].name); |
| 162 |
test.cancelCurrentPhase(); |
| 163 |
|
| 164 |
assertEquals('test', test.getCurrentPhase()[0].name); |
| 165 |
test.cancelCurrentPhase(); |
| 166 |
|
| 167 |
assertEquals('teardown', test.getCurrentPhase()[0].name); |
| 168 |
test.cancelCurrentPhase(); |
| 169 |
|
| 170 |
assertNull(test.getCurrentPhase()); |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
function testTestSetError() { |
| 175 |
var test = getSampleTest(); |
| 176 |
|
| 177 |
var error1 = new Error('Oh noes!'); |
| 178 |
var error2 = new Error('B0rken.'); |
| 179 |
|
| 180 |
assertNull(test.getError()); |
| 181 |
test.setError(error1); |
| 182 |
assertEquals(error1, test.getError()); |
| 183 |
test.setError(error2); |
| 184 |
assertEquals( |
| 185 |
'Once an error has been set, it should not be overwritten.', error1, |
| 186 |
test.getError()); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
function testAddStep() { |
| 191 |
var test = getSampleTest(); |
| 192 |
var step = getSampleStep(); |
| 193 |
|
| 194 |
// Try adding a step to each phase and then cancelling the phase. |
| 195 |
for (var i = 0; i < 3; i++) { |
| 196 |
assertEquals(1, test.getCurrentPhase().length); |
| 197 |
test.addStep(step); |
| 198 |
|
| 199 |
assertEquals(2, test.getCurrentPhase().length); |
| 200 |
assertEquals(step, test.getCurrentPhase()[1]); |
| 201 |
test.cancelCurrentPhase(); |
| 202 |
} |
| 203 |
|
| 204 |
assertNull(test.getCurrentPhase()); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
function testCancelTestPhase() { |
| 209 |
var test = getSampleTest(); |
| 210 |
|
| 211 |
test.cancelTestPhase(); |
| 212 |
assertEquals('teardown', test.getCurrentPhase()[0].name); |
| 213 |
|
| 214 |
test = getSampleTest(); |
| 215 |
test.cancelCurrentPhase(); |
| 216 |
test.cancelTestPhase(); |
| 217 |
assertEquals('teardown', test.getCurrentPhase()[0].name); |
| 218 |
|
| 219 |
test = getSampleTest(); |
| 220 |
test.cancelTestPhase(); |
| 221 |
test.cancelTestPhase(); |
| 222 |
assertEquals('teardown', test.getCurrentPhase()[0].name); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
function testWaitForTimeout() { |
| 227 |
var reachedA = false; |
| 228 |
var reachedB = false; |
| 229 |
var reachedC = false; |
| 230 |
|
| 231 |
waitForTimeout(function a() { |
| 232 |
reachedA = true; |
| 233 |
|
| 234 |
assertTrue('A must be true at callback a.', reachedA); |
| 235 |
assertFalse('B must be false at callback a.', reachedB); |
| 236 |
assertFalse('C must be false at callback a.', reachedC); |
| 237 |
}, 10); |
| 238 |
|
| 239 |
waitForTimeout(function b() { |
| 240 |
reachedB = true; |
| 241 |
|
| 242 |
assertTrue('A must be true at callback b.', reachedA); |
| 243 |
assertTrue('B must be true at callback b.', reachedB); |
| 244 |
assertFalse('C must be false at callback b.', reachedC); |
| 245 |
}, 20); |
| 246 |
|
| 247 |
waitForTimeout(function c() { |
| 248 |
reachedC = true; |
| 249 |
|
| 250 |
assertTrue('A must be true at callback c.', reachedA); |
| 251 |
assertTrue('B must be true at callback c.', reachedB); |
| 252 |
assertTrue('C must be true at callback c.', reachedC); |
| 253 |
}, 20); |
| 254 |
|
| 255 |
assertFalse('a', reachedA); |
| 256 |
assertFalse('b', reachedB); |
| 257 |
assertFalse('c', reachedC); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
function testWaitForEvent() { |
| 262 |
var et = new goog.events.EventTarget(); |
| 263 |
|
| 264 |
var eventFired = false; |
| 265 |
goog.events.listen(et, 'testPrefire', function() { |
| 266 |
eventFired = true; |
| 267 |
et.dispatchEvent('test'); |
| 268 |
}); |
| 269 |
|
| 270 |
waitForEvent(et, 'test', function() { assertTrue(eventFired); }); |
| 271 |
|
| 272 |
et.dispatchEvent('testPrefire'); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
function testWaitForCondition() { |
| 277 |
var counter = 0; |
| 278 |
|
| 279 |
waitForCondition( |
| 280 |
function() { return ++counter >= 2; }, |
| 281 |
function() { assertEquals(2, counter); }, 10, 200); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
function testOutOfOrderWaits() { |
| 286 |
var counter = 0; |
| 287 |
|
| 288 |
// Note that if the delta between the timeout is too small, two |
| 289 |
// continuation may be invoked at the same timer tick, using the |
| 290 |
// registration order. |
| 291 |
waitForTimeout(function() { assertEquals(3, ++counter); }, 200); |
| 292 |
waitForTimeout(function() { assertEquals(1, ++counter); }, 0); |
| 293 |
waitForTimeout(function() { assertEquals(2, ++counter); }, 100); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/* |
| 298 |
* Any of the test functions below (except the condition check passed into |
| 299 |
* waitForCondition) can raise an assertion successfully. Any level of nested |
| 300 |
* test steps should be possible, in any configuration. |
| 301 |
*/ |
| 302 |
|
| 303 |
var testObj; |
| 304 |
|
| 305 |
|
| 306 |
function testCrazyNestedWaitFunction() { |
| 307 |
testObj = {lock: true, et: new goog.events.EventTarget(), steps: 0}; |
| 308 |
|
| 309 |
waitForTimeout(handleTimeout, 10); |
| 310 |
waitForEvent(testObj.et, 'test', handleEvent); |
| 311 |
waitForCondition(condition, handleCondition, 1); |
| 312 |
} |
| 313 |
|
| 314 |
function handleTimeout() { |
| 315 |
testObj.steps++; |
| 316 |
assertEquals('handleTimeout should be called first.', 1, testObj.steps); |
| 317 |
waitForTimeout(fireEvent, 10); |
| 318 |
} |
| 319 |
|
| 320 |
function fireEvent() { |
| 321 |
testObj.steps++; |
| 322 |
assertEquals('fireEvent should be called second.', 2, testObj.steps); |
| 323 |
testObj.et.dispatchEvent('test'); |
| 324 |
} |
| 325 |
|
| 326 |
function handleEvent() { |
| 327 |
testObj.steps++; |
| 328 |
assertEquals('handleEvent should be called third.', 3, testObj.steps); |
| 329 |
testObj.lock = false; |
| 330 |
} |
| 331 |
|
| 332 |
function condition() { |
| 333 |
return !testObj.lock; |
| 334 |
} |
| 335 |
|
| 336 |
function handleCondition() { |
| 337 |
assertFalse(testObj.lock); |
| 338 |
testObj.steps++; |
| 339 |
assertEquals('handleCondition should be called last.', 4, testObj.steps); |
| 340 |
} |