| 1 |
// Copyright 2014 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.labs.testing.environmentTest'); |
| 16 |
goog.setTestOnly('goog.labs.testing.environmentTest'); |
| 17 |
|
| 18 |
goog.require('goog.Promise'); |
| 19 |
goog.require('goog.labs.testing.Environment'); |
| 20 |
goog.require('goog.testing.MockClock'); |
| 21 |
goog.require('goog.testing.MockControl'); |
| 22 |
goog.require('goog.testing.PropertyReplacer'); |
| 23 |
goog.require('goog.testing.TestCase'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.testing.testSuite'); |
| 26 |
|
| 27 |
var testCase = null; |
| 28 |
var mockControl = null; |
| 29 |
var replacer = null; |
| 30 |
|
| 31 |
// Use this flag to control whether the global JsUnit lifecycle events are being |
| 32 |
// called as part of the test lifecycle or as part of the "mocked" environment. |
| 33 |
var testing = false; |
| 34 |
|
| 35 |
// Bootstrap it because... why not. |
| 36 |
var env = new goog.labs.testing.Environment(); |
| 37 |
// The outer test case. |
| 38 |
var realTestCase = goog.labs.testing.EnvironmentTestCase_.getInstance(); |
| 39 |
|
| 40 |
function setUp() { |
| 41 |
// These methods end up being called by the test framework for these tests |
| 42 |
// as well as the part of the environment that is being tested as part |
| 43 |
// of the test. Bail if the test is already running. |
| 44 |
if (testing) { |
| 45 |
// This value is used by the testSetupReturnsValue test below |
| 46 |
return 'hello'; |
| 47 |
} |
| 48 |
|
| 49 |
// Temporarily override the initializeTestRunner method to avoid installing |
| 50 |
// our "test" TestCase. |
| 51 |
var initFn = goog.testing.TestCase.initializeTestRunner; |
| 52 |
goog.testing.TestCase.initializeTestRunner = function() {}; |
| 53 |
testCase = new goog.labs.testing.EnvironmentTestCase_(); |
| 54 |
goog.labs.testing.EnvironmentTestCase_.getInstance = function() { |
| 55 |
return testCase; |
| 56 |
}; |
| 57 |
goog.testing.TestCase.initializeTestRunner = initFn; |
| 58 |
|
| 59 |
mockControl = new goog.testing.MockControl(); |
| 60 |
|
| 61 |
replacer = new goog.testing.PropertyReplacer(); |
| 62 |
} |
| 63 |
|
| 64 |
function tearDown() { |
| 65 |
if (testing) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
replacer.reset(); |
| 70 |
|
| 71 |
mockControl.$resetAll(); |
| 72 |
mockControl.$tearDown(); |
| 73 |
} |
| 74 |
|
| 75 |
function testLifecycle() { |
| 76 |
testing = true; |
| 77 |
|
| 78 |
var envOne = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 79 |
var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 80 |
var envThree = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 81 |
var testMethod = mockControl.createFunctionMock('testMethod'); |
| 82 |
|
| 83 |
testCase.addNewTest('testFake', testMethod); |
| 84 |
|
| 85 |
testCase.registerEnvironment_(envOne); |
| 86 |
testCase.registerEnvironment_(envTwo); |
| 87 |
testCase.registerEnvironment_(envThree); |
| 88 |
|
| 89 |
envOne.setUpPage(); |
| 90 |
envTwo.setUpPage(); |
| 91 |
envThree.setUpPage(); |
| 92 |
|
| 93 |
envOne.setUp(); |
| 94 |
envTwo.setUp(); |
| 95 |
envThree.setUp(); |
| 96 |
|
| 97 |
testMethod(); |
| 98 |
|
| 99 |
envThree.tearDown(); |
| 100 |
envTwo.tearDown(); |
| 101 |
envOne.tearDown(); |
| 102 |
|
| 103 |
envThree.tearDownPage(); |
| 104 |
envTwo.tearDownPage(); |
| 105 |
envOne.tearDownPage(); |
| 106 |
|
| 107 |
mockControl.$replayAll(); |
| 108 |
testCase.runTests(); |
| 109 |
mockControl.$verifyAll(); |
| 110 |
|
| 111 |
testing = false; |
| 112 |
} |
| 113 |
|
| 114 |
function testLifecycle_withObject() { |
| 115 |
testing = true; |
| 116 |
|
| 117 |
var envOne = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 118 |
var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 119 |
var envThree = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 120 |
var testObject = { |
| 121 |
configureEnvironment: |
| 122 |
mockControl.createFunctionMock('configureEnvironment1'), |
| 123 |
setUp: mockControl.createFunctionMock('setUp1'), |
| 124 |
testSuite: { |
| 125 |
configureEnvironment: |
| 126 |
mockControl.createFunctionMock('configureEnvironment2'), |
| 127 |
setUp: mockControl.createFunctionMock('setUp2'), |
| 128 |
test: mockControl.createFunctionMock('test'), |
| 129 |
tearDown: mockControl.createFunctionMock('tearDown2') |
| 130 |
}, |
| 131 |
tearDown: mockControl.createFunctionMock('tearDown1') |
| 132 |
}; |
| 133 |
|
| 134 |
testCase.setTestObj(testObject); |
| 135 |
|
| 136 |
testCase.registerEnvironment_(envOne); |
| 137 |
testCase.registerEnvironment_(envTwo); |
| 138 |
testCase.registerEnvironment_(envThree); |
| 139 |
|
| 140 |
envOne.setUpPage(); |
| 141 |
envTwo.setUpPage(); |
| 142 |
envThree.setUpPage(); |
| 143 |
|
| 144 |
testObject.configureEnvironment(); |
| 145 |
testObject.testSuite.configureEnvironment(); |
| 146 |
envOne.setUp(); |
| 147 |
envTwo.setUp(); |
| 148 |
envThree.setUp(); |
| 149 |
|
| 150 |
testObject.setUp(); |
| 151 |
testObject.testSuite.setUp(); |
| 152 |
testObject.testSuite.test(); |
| 153 |
testObject.testSuite.tearDown(); |
| 154 |
testObject.tearDown(); |
| 155 |
|
| 156 |
envThree.tearDown(); |
| 157 |
envTwo.tearDown(); |
| 158 |
envOne.tearDown(); |
| 159 |
|
| 160 |
envThree.tearDownPage(); |
| 161 |
envTwo.tearDownPage(); |
| 162 |
envOne.tearDownPage(); |
| 163 |
|
| 164 |
mockControl.$replayAll(); |
| 165 |
testCase.runTests(); |
| 166 |
mockControl.$verifyAll(); |
| 167 |
|
| 168 |
testing = false; |
| 169 |
} |
| 170 |
|
| 171 |
function testLifecycle_withPromises() { |
| 172 |
var mockClock = new goog.testing.MockClock(true /* autoinstall */); |
| 173 |
testing = true; |
| 174 |
|
| 175 |
var envOne = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 176 |
testCase.registerEnvironment_(envOne); |
| 177 |
var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment); |
| 178 |
testCase.registerEnvironment_(envTwo); |
| 179 |
var testObj = { |
| 180 |
'configureEnvironment': |
| 181 |
mockControl.createFunctionMock('configureEnvironment'), |
| 182 |
'setUpPage': mockControl.createFunctionMock('setUpPage'), |
| 183 |
'setUp': mockControl.createFunctionMock('setUp'), |
| 184 |
'tearDown': mockControl.createFunctionMock('tearDown'), |
| 185 |
'tearDownPage': mockControl.createFunctionMock('tearDownPage'), |
| 186 |
'testFoo': mockControl.createFunctionMock('testFoo'), |
| 187 |
'testBar': { |
| 188 |
'configureEnvironment': |
| 189 |
mockControl.createFunctionMock('configureEnvironmentTest'), |
| 190 |
'setUp': mockControl.createFunctionMock('setUpTest'), |
| 191 |
'test': mockControl.createFunctionMock('testBar'), |
| 192 |
'tearDown': mockControl.createFunctionMock('tearDownTest') |
| 193 |
} |
| 194 |
}; |
| 195 |
testCase.setTestObj(testObj); |
| 196 |
|
| 197 |
// Make the testCase.runTestsReturningPromise() a pending operation so we can |
| 198 |
// use assertNextCall also for checking the first call. |
| 199 |
var resultPromise; |
| 200 |
var pendingOp = goog.Promise.withResolver(); |
| 201 |
pendingOp.promise.then(function() { |
| 202 |
resultPromise = testCase.runTestsReturningPromise(); |
| 203 |
}); |
| 204 |
var nextOp = null; |
| 205 |
var finishPendingOp = function() { |
| 206 |
pendingOp.resolve(); |
| 207 |
pendingOp = nextOp; |
| 208 |
nextOp = null; |
| 209 |
mockClock.tick(); |
| 210 |
}; |
| 211 |
var expectCallTo = function(expectedCall) { |
| 212 |
assertNull(nextOp); |
| 213 |
nextOp = goog.Promise.withResolver(); |
| 214 |
expectedCall().$returns(nextOp.promise); |
| 215 |
mockControl.$replayAll(); |
| 216 |
finishPendingOp(); |
| 217 |
mockControl.$verifyAll(); |
| 218 |
mockControl.$resetAll(); |
| 219 |
}; |
| 220 |
// Make sure there are no hanging expecations dropped by the first $resetAll. |
| 221 |
mockControl.$replayAll(); |
| 222 |
mockControl.$verifyAll(); |
| 223 |
mockControl.$resetAll(); |
| 224 |
|
| 225 |
expectCallTo(envOne.setUpPage); |
| 226 |
expectCallTo(envTwo.setUpPage); |
| 227 |
expectCallTo(testObj.setUpPage); |
| 228 |
expectCallTo(testObj.configureEnvironment); |
| 229 |
expectCallTo(envOne.setUp); |
| 230 |
expectCallTo(envTwo.setUp); |
| 231 |
expectCallTo(testObj.setUp); |
| 232 |
expectCallTo(testObj.testFoo); |
| 233 |
|
| 234 |
mockControl.$resetAll(); |
| 235 |
testObj.tearDown(); |
| 236 |
envTwo.tearDown(); |
| 237 |
envOne.tearDown(); |
| 238 |
|
| 239 |
expectCallTo(testObj.configureEnvironment); |
| 240 |
expectCallTo(testObj.testBar.configureEnvironment); |
| 241 |
expectCallTo(envOne.setUp); |
| 242 |
expectCallTo(envTwo.setUp); |
| 243 |
expectCallTo(testObj.setUp); |
| 244 |
expectCallTo(testObj.testBar.setUp); |
| 245 |
expectCallTo(testObj.testBar.test); |
| 246 |
|
| 247 |
mockControl.$resetAll(); |
| 248 |
testObj.testBar.tearDown(); |
| 249 |
testObj.tearDown(); |
| 250 |
envTwo.tearDown(); |
| 251 |
envOne.tearDown(); |
| 252 |
testObj.tearDownPage(); |
| 253 |
envTwo.tearDownPage(); |
| 254 |
envOne.tearDownPage(); |
| 255 |
mockControl.$replayAll(); |
| 256 |
finishPendingOp(); |
| 257 |
var result = mockClock.tickPromise(resultPromise); |
| 258 |
mockControl.$verifyAll(); |
| 259 |
assertTrue(result.complete); |
| 260 |
assertEquals(2, result.totalCount); |
| 261 |
assertEquals(2, result.runCount); |
| 262 |
assertEquals(2, result.successCount); |
| 263 |
assertEquals(0, result.errors.length); |
| 264 |
|
| 265 |
testing = false; |
| 266 |
mockClock.uninstall(); |
| 267 |
} |
| 268 |
|
| 269 |
function testTearDownWithMockControl() { |
| 270 |
testing = true; |
| 271 |
|
| 272 |
var envWith = new goog.labs.testing.Environment(); |
| 273 |
var envWithout = new goog.labs.testing.Environment(); |
| 274 |
|
| 275 |
var mockControlMock = mockControl.createStrictMock(goog.testing.MockControl); |
| 276 |
var mockControlCtorMock = |
| 277 |
mockControl.createMethodMock(goog.testing, 'MockControl'); |
| 278 |
mockControlCtorMock().$times(1).$returns(mockControlMock); |
| 279 |
// Expecting verify / reset calls twice since two environments use the same |
| 280 |
// mockControl, but only one created it and is allowed to tear it down. |
| 281 |
mockControlMock.$verifyAll(); |
| 282 |
mockControlMock.$replayAll(); |
| 283 |
mockControlMock.$verifyAll(); |
| 284 |
mockControlMock.$resetAll(); |
| 285 |
mockControlMock.$tearDown().$times(1); |
| 286 |
mockControlMock.$verifyAll(); |
| 287 |
mockControlMock.$replayAll(); |
| 288 |
mockControlMock.$verifyAll(); |
| 289 |
mockControlMock.$resetAll(); |
| 290 |
|
| 291 |
mockControl.$replayAll(); |
| 292 |
envWith.withMockControl(); |
| 293 |
envWithout.mockControl = mockControlMock; |
| 294 |
envWith.tearDown(); |
| 295 |
envWithout.tearDown(); |
| 296 |
mockControl.$verifyAll(); |
| 297 |
mockControl.$resetAll(); |
| 298 |
|
| 299 |
testing = false; |
| 300 |
} |
| 301 |
|
| 302 |
function testAutoDiscoverTests() { |
| 303 |
testing = true; |
| 304 |
|
| 305 |
var setUpPageFn = testCase.setUpPage; |
| 306 |
var setUpFn = testCase.setUp; |
| 307 |
var tearDownFn = testCase.tearDownFn; |
| 308 |
var tearDownPageFn = testCase.tearDownPageFn; |
| 309 |
|
| 310 |
testCase.autoDiscoverTests(); |
| 311 |
|
| 312 |
assertEquals(setUpPageFn, testCase.setUpPage); |
| 313 |
assertEquals(setUpFn, testCase.setUp); |
| 314 |
assertEquals(tearDownFn, testCase.tearDownFn); |
| 315 |
assertEquals(tearDownPageFn, testCase.tearDownPageFn); |
| 316 |
|
| 317 |
// Note that this number changes when more tests are added to this file as |
| 318 |
// the environment reflects on the window global scope for JsUnit. |
| 319 |
assertEquals(10, testCase.tests_.length); |
| 320 |
|
| 321 |
testing = false; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// Verify "goog.testing.testSuite" integration |
| 326 |
function testTestSuiteTests() { |
| 327 |
testing = true; |
| 328 |
|
| 329 |
// don't try to reinitialize the test runner, while a test is running. |
| 330 |
replacer.set(goog.testing.TestCase, 'initializeTestRunner', function() {}); |
| 331 |
|
| 332 |
// with an active environment. |
| 333 |
var envOne = new goog.labs.testing.Environment(); |
| 334 |
|
| 335 |
var setUpPageFn = testCase.setUpPage; |
| 336 |
var setUpFn = testCase.setUp; |
| 337 |
var tearDownFn = testCase.tearDownFn; |
| 338 |
var tearDownPageFn = testCase.tearDownPageFn; |
| 339 |
|
| 340 |
goog.testing.testSuite({ |
| 341 |
// These lifecycle methods should not override the environment testcase |
| 342 |
// methods but they should be called, when the test runs. |
| 343 |
setUp: function() {}, |
| 344 |
tearDown: function() {}, |
| 345 |
setUpPage: function() {}, |
| 346 |
tearDownPage: function() {}, |
| 347 |
// This test method should be added. |
| 348 |
testMe: function() {}, |
| 349 |
testMeToo: { |
| 350 |
setUp: function() {}, |
| 351 |
testA: function() {}, |
| 352 |
testB: function() {}, |
| 353 |
tearDown: function() {} |
| 354 |
} |
| 355 |
}); |
| 356 |
|
| 357 |
assertEquals(setUpPageFn, testCase.setUpPage); |
| 358 |
assertEquals(setUpFn, testCase.setUp); |
| 359 |
assertEquals(tearDownFn, testCase.tearDownFn); |
| 360 |
assertEquals(tearDownPageFn, testCase.tearDownPageFn); |
| 361 |
|
| 362 |
assertEquals(3, testCase.tests_.length); |
| 363 |
|
| 364 |
testing = false; |
| 365 |
} |
| 366 |
|
| 367 |
function testSetupReturnsValue() { |
| 368 |
testing = true; |
| 369 |
|
| 370 |
var env = new goog.labs.testing.Environment(); |
| 371 |
|
| 372 |
// Expect the environment to pass on any value returned by the user defined |
| 373 |
// setUp method. |
| 374 |
assertEquals('hello', testCase.setUp()); |
| 375 |
|
| 376 |
testCase.tearDown(); |
| 377 |
testing = false; |
| 378 |
} |
| 379 |
|
| 380 |
function testMockClock() { |
| 381 |
testing = true; |
| 382 |
|
| 383 |
var env = new goog.labs.testing.Environment().withMockClock(); |
| 384 |
|
| 385 |
testCase.addNewTest('testThatThrowsEventually', function() { |
| 386 |
setTimeout(function() { throw new Error('LateErrorMessage'); }, 200); |
| 387 |
}); |
| 388 |
|
| 389 |
testCase.runTests(); |
| 390 |
assertTestFailure(testCase, 'testThatThrowsEventually', 'LateErrorMessage'); |
| 391 |
|
| 392 |
testing = false; |
| 393 |
} |
| 394 |
|
| 395 |
function testMockControl() { |
| 396 |
testing = true; |
| 397 |
|
| 398 |
var env = new goog.labs.testing.Environment().withMockControl(); |
| 399 |
var test = env.mockControl.createFunctionMock('test'); |
| 400 |
|
| 401 |
testCase.addNewTest('testWithoutVerify', function() { |
| 402 |
test(); |
| 403 |
env.mockControl.$replayAll(); |
| 404 |
test(); |
| 405 |
}); |
| 406 |
|
| 407 |
testCase.runTests(); |
| 408 |
assertNull(env.mockClock); |
| 409 |
|
| 410 |
testing = false; |
| 411 |
} |
| 412 |
|
| 413 |
function testMock() { |
| 414 |
testing = true; |
| 415 |
|
| 416 |
var env = new goog.labs.testing.Environment().withMockControl(); |
| 417 |
var mock = env.mock({test: function() {}}); |
| 418 |
|
| 419 |
testCase.addNewTest('testMockCalled', function() { |
| 420 |
mock.test().$times(2); |
| 421 |
|
| 422 |
env.mockControl.$replayAll(); |
| 423 |
mock.test(); |
| 424 |
mock.test(); |
| 425 |
env.mockControl.verifyAll(); |
| 426 |
}); |
| 427 |
|
| 428 |
testCase.runTests(); |
| 429 |
|
| 430 |
testing = false; |
| 431 |
} |
| 432 |
|
| 433 |
function assertTestFailure(testCase, name, message) { |
| 434 |
assertContains(message, testCase.result_.resultsByName[name][0].toString()); |
| 435 |
} |