| <!DOCTYPE html> | |
| <!-- | |
| Copyright 2011 Software Freedom Conservancy. All Rights Reserved. | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License. | |
| --> | |
| <html> | |
| <head> | |
| <title>testcase_test.html</title> | |
| <script src="../test_bootstrap.js"></script> | |
| <script> | |
| goog.require('goog.testing.MockControl'); | |
| goog.require('goog.testing.PropertyReplacer'); | |
| goog.require('goog.testing.mockmatchers'); | |
| goog.require('goog.testing.jsunit'); | |
| goog.require('goog.testing.recordFunction'); | |
| goog.require('webdriver.test.AppTester'); | |
| goog.require('webdriver.test.testutil'); | |
| goog.require('webdriver.testing.TestCase'); | |
| </script> | |
| </head> | |
| <body> | |
| <script> | |
| // Aliases for readability. | |
| var IGNORE_ARGUMENT = goog.testing.mockmatchers.ignoreArgument, | |
| IS_ARRAY_ARGUMENT = goog.testing.mockmatchers.isArray, | |
| STUB_ERROR = webdriver.test.testutil.STUB_ERROR, | |
| throwStubError = webdriver.test.testutil.throwStubError, | |
| assertIsStubError = webdriver.test.testutil.assertIsStubError; | |
| var control = new goog.testing.MockControl(); | |
| var appTester, clock, mockTestCase, testStub, mockOnComplete, mockOnError; | |
| function setUp() { | |
| clock = webdriver.test.testutil.createMockClock(); | |
| appTester = new webdriver.test.AppTester(clock); | |
| // Use one master mock so we can assert execution order. | |
| mockTestCase = control.createStrictMock({ | |
| setUp: goog.nullFunction, | |
| testFn: goog.nullFunction, | |
| tearDown: goog.nullFunction, | |
| onComplete: goog.nullFunction, | |
| onError: goog.nullFunction | |
| }, true); | |
| mockOnComplete = goog.bind(mockTestCase.onComplete, mockTestCase); | |
| mockOnError = goog.bind(mockTestCase.onError, mockTestCase); | |
| testStub = { | |
| name: 'testStub', | |
| scope: mockTestCase, | |
| ref: mockTestCase.testFn | |
| }; | |
| webdriver.test.testutil.messages = []; | |
| } | |
| function tearDown() { | |
| appTester.$assertAppNotRunning(); | |
| appTester.$tearDown(); | |
| control.$tearDown(); | |
| clock.dispose(); | |
| } | |
| /** | |
| * @param {string} msg Task message. | |
| * @param {*=} opt_return The optional return value. | |
| */ | |
| function schedule(msg, opt_return) { | |
| return appTester.schedule(msg, function() { | |
| return opt_return; | |
| }); | |
| } | |
| function runTest() { | |
| webdriver.testing.TestCase.prototype.runSingleTest_. | |
| call(mockTestCase, testStub, mockOnError). | |
| then(mockOnComplete); | |
| appTester.$runApplication(); | |
| control.$verifyAll(); | |
| } | |
| function testExecutesTheBasicTestFlow() { | |
| mockTestCase.setUp(); | |
| mockTestCase.testFn(); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testExecutingAHappyTestWithScheduledActions() { | |
| mockTestCase.setUp().$does(function() { schedule('a'); }); | |
| mockTestCase.testFn().$does(function() { schedule('b'); }); | |
| mockTestCase.tearDown().$does(function() { schedule('c'); }); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testShouldSkipTestFnIfSetupThrows() { | |
| mockTestCase.setUp().$does(throwStubError); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testShouldSkipTestFnIfSetupActionFails_1() { | |
| mockTestCase.setUp().$does(function() { | |
| appTester.schedule('an explosion', throwStubError); | |
| }); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testShouldSkipTestFnIfSetupActionFails_2() { | |
| mockTestCase.setUp().$does(function() { | |
| appTester.schedule('an explosion', throwStubError); | |
| }); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testShouldSkipTestFnIfNestedSetupActionFails() { | |
| mockTestCase.setUp().$does(function() { | |
| appTester.schedule('a', goog.nullFunction).then(function() { | |
| appTester.schedule('b', throwStubError); | |
| }); | |
| }); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testRunsAllTasksForEachPhaseBeforeTheNextPhase() { | |
| webdriver.test.testutil.messages = []; | |
| mockTestCase.setUp().$does(function() { schedule('a'); }); | |
| mockTestCase.testFn().$does(function() { schedule('b'); }); | |
| mockTestCase.tearDown().$does(function() { schedule('c'); }); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testRecordsErrorsFromTestFnBeforeTearDown() { | |
| mockTestCase.setUp(); | |
| mockTestCase.testFn().$does(throwStubError); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown(); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testRecordsErrorsFromTearDown() { | |
| mockTestCase.setUp(); | |
| mockTestCase.testFn(); | |
| mockTestCase.tearDown().$does(throwStubError); | |
| mockOnError(STUB_ERROR); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testErrorFromSetUpAndTearDown() { | |
| mockTestCase.setUp().$does(throwStubError); | |
| mockOnError(STUB_ERROR); | |
| mockTestCase.tearDown().$does(throwStubError); | |
| mockOnError(STUB_ERROR); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| function testErrorFromTestFnAndTearDown() { | |
| mockTestCase.setUp(); | |
| mockTestCase.testFn().$does(function() { throw 'error1' }); | |
| mockOnError('error1'); | |
| mockTestCase.tearDown().$does(function() { throw 'error2' }); | |
| mockOnError('error2'); | |
| mockOnComplete(IGNORE_ARGUMENT); | |
| control.$replayAll(); | |
| runTest(); | |
| } | |
| </script> | |
| </body> | |
| </html> |