| 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.AsyncTestCaseAsyncTest'); |
| 16 |
goog.setTestOnly('goog.testing.AsyncTestCaseAsyncTest'); |
| 17 |
|
| 18 |
goog.require('goog.testing.AsyncTestCase'); |
| 19 |
goog.require('goog.testing.TestCase'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
// Has the setUp() function been called. |
| 23 |
var setUpCalled = false; |
| 24 |
// Has the current test function completed. This helps us to ensure that |
| 25 |
// the next test is not started before the previous completed. |
| 26 |
var curTestIsDone = true; |
| 27 |
// Use an asynchronous test runner for our tests. |
| 28 |
var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall(document.title); |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Uses window.setTimeout() to perform asynchronous behaviour and uses |
| 33 |
* asyncTestCase.waitForAsync() and asyncTestCase.continueTesting() to mark |
| 34 |
* the beginning and end of it. |
| 35 |
* @param {number} numAsyncCalls The number of asynchronous calls to make. |
| 36 |
* @param {string} name The name of the current step. |
| 37 |
*/ |
| 38 |
function doAsyncStuff(numAsyncCalls, name) { |
| 39 |
if (numAsyncCalls > 0) { |
| 40 |
curTestIsDone = false; |
| 41 |
asyncTestCase.waitForAsync( |
| 42 |
'doAsyncStuff-' + name + '(' + numAsyncCalls + ')'); |
| 43 |
window.setTimeout(function() { doAsyncStuff(numAsyncCalls - 1, name); }, 0); |
| 44 |
} else { |
| 45 |
curTestIsDone = true; |
| 46 |
asyncTestCase.continueTesting(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
function setUpPage() { |
| 51 |
debug('setUpPage was called.'); |
| 52 |
doAsyncStuff(3, 'setUpPage'); |
| 53 |
} |
| 54 |
function setUp() { |
| 55 |
assertTrue(curTestIsDone); |
| 56 |
doAsyncStuff(3, 'setUp'); |
| 57 |
} |
| 58 |
function tearDown() { |
| 59 |
assertTrue(curTestIsDone); |
| 60 |
} |
| 61 |
function test1() { |
| 62 |
assertTrue(curTestIsDone); |
| 63 |
doAsyncStuff(1, 'test1'); |
| 64 |
} |
| 65 |
function test2_asyncContinueThenWait() { |
| 66 |
var activeTest = asyncTestCase.activeTest_; |
| 67 |
function async1() { |
| 68 |
asyncTestCase.continueTesting(); |
| 69 |
asyncTestCase.waitForAsync('2'); |
| 70 |
window.setTimeout(async2, 0); |
| 71 |
} |
| 72 |
function async2() { |
| 73 |
asyncTestCase.continueTesting(); |
| 74 |
assertEquals( |
| 75 |
'Did not wait for inner waitForAsync', activeTest, |
| 76 |
asyncTestCase.activeTest_); |
| 77 |
} |
| 78 |
asyncTestCase.waitForAsync('1'); |
| 79 |
window.setTimeout(async1, 0); |
| 80 |
} |
| 81 |
function test3() { |
| 82 |
assertTrue(curTestIsDone); |
| 83 |
doAsyncStuff(2, 'test3'); |
| 84 |
} |
| 85 |
|
| 86 |
function tearDownPage() { |
| 87 |
debug('tearDownPage was called.'); |
| 88 |
assertTrue(curTestIsDone); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
var callback = function() { |
| 93 |
curTestIsDone = true; |
| 94 |
asyncTestCase.signal(); |
| 95 |
}; |
| 96 |
var doAsyncSignals = function() { |
| 97 |
curTestIsDone = false; |
| 98 |
window.setTimeout(callback, 0); |
| 99 |
}; |
| 100 |
|
| 101 |
function testSignalsReturn() { |
| 102 |
doAsyncSignals(); |
| 103 |
doAsyncSignals(); |
| 104 |
doAsyncSignals(); |
| 105 |
asyncTestCase.waitForSignals(3); |
| 106 |
} |
| 107 |
|
| 108 |
function testSignalsMixedSyncAndAsync() { |
| 109 |
asyncTestCase.signal(); |
| 110 |
doAsyncSignals(); |
| 111 |
doAsyncSignals(); |
| 112 |
asyncTestCase.waitForSignals(3); |
| 113 |
} |
| 114 |
|
| 115 |
function testSignalsMixedSyncAndAsyncMultipleWaits() { |
| 116 |
asyncTestCase.signal(); |
| 117 |
doAsyncSignals(); |
| 118 |
asyncTestCase.waitForSignals(1); |
| 119 |
doAsyncSignals(); |
| 120 |
asyncTestCase.waitForSignals(2); |
| 121 |
} |
| 122 |
|
| 123 |
function testSignalsCallContinueTestingBeforeFinishing() { |
| 124 |
doAsyncSignals(); |
| 125 |
asyncTestCase.waitForSignals(2); |
| 126 |
|
| 127 |
window.setTimeout(function() { |
| 128 |
var thrown = assertThrows(function() { asyncTestCase.continueTesting(); }); |
| 129 |
assertEquals('Still waiting for 1 signals.', thrown.message); |
| 130 |
}, 0); |
| 131 |
doAsyncSignals(); // To not timeout. |
| 132 |
} |
| 133 |
|
| 134 |
function testCurrentTestName() { |
| 135 |
var currentTestName = goog.testing.TestCase.currentTestName; |
| 136 |
assertEquals('testCurrentTestName', currentTestName); |
| 137 |
} |
| 138 |
|
| 139 |
function testCurrentTestNameAsync() { |
| 140 |
var getAssertSameTest = function() { |
| 141 |
var expectedTestCase = goog.testing.TestCase.getActiveTestCase(); |
| 142 |
var expectedTestName = (expectedTestCase ? expectedTestCase.getName() : |
| 143 |
'<no active TestCase>') + |
| 144 |
'.' + |
| 145 |
(goog.testing.TestCase.currentTestName || '<no active test name>'); |
| 146 |
var assertSameTest = function() { |
| 147 |
var currentTestCase = goog.testing.TestCase.getActiveTestCase(); |
| 148 |
var currentTestName = (currentTestCase ? currentTestCase.getName() : |
| 149 |
'<no active TestCase>') + |
| 150 |
'.' + goog.testing.TestCase.currentTestName || |
| 151 |
'<no active test name>'; |
| 152 |
assertEquals(expectedTestName, currentTestName); |
| 153 |
assertEquals(expectedTestCase, currentTestCase); |
| 154 |
}; |
| 155 |
return assertSameTest; |
| 156 |
}; |
| 157 |
var assertSameTest = getAssertSameTest(); |
| 158 |
// do something asynchronously... |
| 159 |
asyncTestCase.waitForSignals(1, 'Awaiting asynchronous callback'); |
| 160 |
setTimeout(function() { |
| 161 |
// ... and ensure the later half runs during the same test: |
| 162 |
assertSameTest(); |
| 163 |
asyncTestCase.signal(); |
| 164 |
}); |
| 165 |
} |