| 1 |
// Copyright 2015 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.module('goog.testing.MultiTestRunnerTest'); |
| 16 |
goog.setTestOnly('goog.testing.MultiTestRunnerTest'); |
| 17 |
|
| 18 |
// Delay running the tests after page load. This test has some asynchronous |
| 19 |
// behavior that interacts with page load detection. |
| 20 |
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS = 500; |
| 21 |
|
| 22 |
var Promise = goog.require('goog.Promise'); |
| 23 |
var events = goog.require('goog.events'); |
| 24 |
var testingEvents = goog.require('goog.testing.events'); |
| 25 |
var MockControl = goog.require('goog.testing.MockControl'); |
| 26 |
var MultiTestRunner = goog.require('goog.testing.MultiTestRunner'); |
| 27 |
var PropertyReplacer = goog.require('goog.testing.PropertyReplacer'); |
| 28 |
var TestCase = goog.require('goog.testing.TestCase'); |
| 29 |
var jsunit = goog.require('goog.testing.jsunit'); |
| 30 |
var testSuite = goog.require('goog.testing.testSuite'); |
| 31 |
|
| 32 |
var ALL_TESTS = [ |
| 33 |
'testdata/fake_passing_test.html', 'testdata/fake_failing_test.html', |
| 34 |
'testdata/fake_failing_test2.html' |
| 35 |
]; |
| 36 |
var EMPTY_TEST = 'testdata/fake_failing_test3.html'; |
| 37 |
var SKIPPED_TEST = 'testdata/fake_failing_test4.html'; |
| 38 |
|
| 39 |
var testRunner; |
| 40 |
var mocks = new MockControl(); |
| 41 |
var stubs = new PropertyReplacer(); |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Asserts string matches exactly one item in the given array. Useful for |
| 46 |
* matching elements in an array without guaranteed ordering. |
| 47 |
* @param {string} string String to match in the array. |
| 48 |
* @param {!Array<string>} array Array of strings find match. |
| 49 |
*/ |
| 50 |
function assertArrayContainsString(string, array) { |
| 51 |
var matcher = function(item) { return string == item; }; |
| 52 |
assertArrayContainsMatcher(matcher, array); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* Asserts at least one item in array causes matcher to return true. Used by |
| 58 |
* more specific assertion methods and not meant to be used directly. |
| 59 |
* @param {function(string):boolean} matcher Function called for each item in |
| 60 |
* array. Should return true when match is found. |
| 61 |
* @param {!Array<string>} array Array of strings find match. |
| 62 |
*/ |
| 63 |
function assertArrayContainsMatcher(matcher, array) { |
| 64 |
var matching = 0; |
| 65 |
for (var i = 0; i < array.length; i++) { |
| 66 |
if (matcher(array[i])) { |
| 67 |
matching++; |
| 68 |
} |
| 69 |
} |
| 70 |
assertEquals( |
| 71 |
'Matched ' + matching + ' items in array, but should be 1', 1, matching); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Returns promise that resolves when eventType is dispatched from target. |
| 77 |
* @param {!EventTarget|!goog.events.Listenable} target Target to listen for |
| 78 |
* event on. |
| 79 |
* @param {string} eventType Type of event. |
| 80 |
* @return {!Promise} Promise that resolves with triggered event. |
| 81 |
*/ |
| 82 |
function createEventPromise(target, eventType) { |
| 83 |
return new Promise(function(resolve, reject) { |
| 84 |
events.listen(target, eventType, resolve); |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @typedef {{ |
| 91 |
* failureReports: !Array<TestCase.IResult>, |
| 92 |
* testNames: !Array<string> |
| 93 |
* }} |
| 94 |
*/ |
| 95 |
var TestResults; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Processes the test results returned from MultiTestRunner and creates a |
| 100 |
* consolidated test result object. |
| 101 |
* @param {!Array<!Object<string,!Array<TestCase.IResult>>>} |
| 102 |
* testResults The list of individual test results from MultiTestRunner. |
| 103 |
* @return {!TestResults} Consolidated test results for all individual tests. |
| 104 |
*/ |
| 105 |
function processTestResults(testResults) { |
| 106 |
var failureReports = []; |
| 107 |
var testNames = []; |
| 108 |
|
| 109 |
for (var i = 0; i < testResults.length; i++) { |
| 110 |
for (var testName in testResults[i]) { |
| 111 |
testNames.push(testName); |
| 112 |
failureReports = failureReports.concat(testResults[i][testName]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return {failureReports: failureReports, testNames: testNames}; |
| 117 |
} |
| 118 |
|
| 119 |
testSuite({ |
| 120 |
setUpPage: function() { |
| 121 |
TestCase.getActiveTestCase().promiseTimeout = 20000; |
| 122 |
}, |
| 123 |
|
| 124 |
setUp: function() { |
| 125 |
testRunner = new MultiTestRunner().setPoolSize(3).addTests(ALL_TESTS); |
| 126 |
}, |
| 127 |
|
| 128 |
tearDown: function() { |
| 129 |
testRunner.dispose(); |
| 130 |
mocks.$tearDown(); |
| 131 |
stubs.reset(); |
| 132 |
}, |
| 133 |
|
| 134 |
testStartButtonStartsTests: function() { |
| 135 |
testRunner.createDom(); |
| 136 |
testRunner.render(document.getElementById('runner')); |
| 137 |
var el = testRunner.getElement(); |
| 138 |
var startButton = el.querySelectorAll('button')[0]; |
| 139 |
assertEquals('Start', startButton.innerHTML); |
| 140 |
var mockStart = mocks.createMethodMock(MultiTestRunner.prototype, 'start'); |
| 141 |
|
| 142 |
mockStart(); |
| 143 |
|
| 144 |
mocks.$replayAll(); |
| 145 |
testingEvents.fireClickSequence(startButton); |
| 146 |
mocks.$verifyAll(); |
| 147 |
}, |
| 148 |
|
| 149 |
testStopButtonStopsTests: function() { |
| 150 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 151 |
testRunner.createDom(); |
| 152 |
testRunner.render(document.getElementById('runner')); |
| 153 |
var el = testRunner.getElement(); |
| 154 |
var startButton = el.querySelectorAll('button')[0]; |
| 155 |
var stopButton = el.querySelectorAll('button')[1]; |
| 156 |
assertEquals('Stop', stopButton.innerHTML); |
| 157 |
stubs.replace( |
| 158 |
MultiTestRunner.TestFrame.prototype, 'runTest', function() { return; }); |
| 159 |
|
| 160 |
testingEvents.fireClickSequence(startButton); |
| 161 |
testingEvents.fireClickSequence(stopButton); |
| 162 |
return promise.then(function(results) { |
| 163 |
// Tests should be halted and marked as "unfinished". |
| 164 |
assertContains( |
| 165 |
'These tests did not finish:\n' + |
| 166 |
'testdata/fake_passing_test.html\n' + |
| 167 |
'testdata/fake_failing_test.html\n' + |
| 168 |
'testdata/fake_failing_test2.html', |
| 169 |
el.innerHTML); |
| 170 |
}); |
| 171 |
}, |
| 172 |
|
| 173 |
testDisposeInternal: function() { |
| 174 |
testRunner.dispose(); |
| 175 |
|
| 176 |
assertTrue(testRunner.tableSorter_.isDisposed()); |
| 177 |
assertTrue(testRunner.eh_.isDisposed()); |
| 178 |
assertNull(testRunner.startButtonEl_); |
| 179 |
assertNull(testRunner.stopButtonEl_); |
| 180 |
assertNull(testRunner.logEl_); |
| 181 |
assertNull(testRunner.reportEl_); |
| 182 |
assertNull(testRunner.progressEl_); |
| 183 |
assertNull(testRunner.logTabEl_); |
| 184 |
assertNull(testRunner.reportTabEl_); |
| 185 |
assertNull(testRunner.statsTabEl_); |
| 186 |
assertNull(testRunner.statsEl_); |
| 187 |
}, |
| 188 |
|
| 189 |
testRunsTestsAndReportsResults: function() { |
| 190 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 191 |
|
| 192 |
testRunner.render(document.getElementById('runner')); |
| 193 |
testRunner.start(); |
| 194 |
|
| 195 |
return promise.then(function(results) { |
| 196 |
var testResults = processTestResults(results['allTestResults']); |
| 197 |
var testNames = testResults.testNames; |
| 198 |
assertEquals(3, testNames.length); |
| 199 |
assertArrayContainsString( |
| 200 |
'testdata/fake_failing_test2:testFail', testNames); |
| 201 |
assertArrayContainsString( |
| 202 |
'testdata/fake_failing_test:testFail', testNames); |
| 203 |
assertArrayContainsString( |
| 204 |
'testdata/fake_passing_test:testPass', testNames); |
| 205 |
var failureReports = testResults.failureReports; |
| 206 |
var failedTests = testRunner.getTestsThatFailed(); |
| 207 |
assertEquals(2, failureReports.length); |
| 208 |
assertEquals(2, failedTests.length); |
| 209 |
assertArrayContainsString('testdata/fake_failing_test.html', failedTests); |
| 210 |
assertArrayContainsString( |
| 211 |
'testdata/fake_failing_test2.html', failedTests); |
| 212 |
}); |
| 213 |
}, |
| 214 |
|
| 215 |
testMissingTestResultsIsAFailure: function() { |
| 216 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 217 |
|
| 218 |
testRunner.addTests(EMPTY_TEST); |
| 219 |
testRunner.render(document.getElementById('runner')); |
| 220 |
testRunner.start(); |
| 221 |
|
| 222 |
return promise.then(function(results) { |
| 223 |
var testResults = processTestResults(results['allTestResults']); |
| 224 |
var testNames = testResults.testNames; |
| 225 |
assertEquals(4, testNames.length); |
| 226 |
assertArrayContainsString('testdata/fake_failing_test3', testNames); |
| 227 |
var failureReports = testResults.failureReports; |
| 228 |
var failedTests = testRunner.getTestsThatFailed(); |
| 229 |
assertEquals(3, failureReports.length); |
| 230 |
assertEquals(3, failedTests.length); |
| 231 |
assertArrayContainsString( |
| 232 |
'testdata/fake_failing_test3.html', failedTests); |
| 233 |
}); |
| 234 |
}, |
| 235 |
|
| 236 |
testShouldRunTestsFalseIsSuccess: function() { |
| 237 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 238 |
|
| 239 |
testRunner.addTests(SKIPPED_TEST); |
| 240 |
testRunner.render(document.getElementById('runner')); |
| 241 |
testRunner.start(); |
| 242 |
|
| 243 |
return promise.then(function(results) { |
| 244 |
var testResults = processTestResults(results['allTestResults']); |
| 245 |
var testNames = testResults.testNames; |
| 246 |
assertEquals(4, testNames.length); |
| 247 |
assertArrayContainsString('testdata/fake_failing_test4', testNames); |
| 248 |
var failureReports = testResults.failureReports; |
| 249 |
var failedTests = testRunner.getTestsThatFailed(); |
| 250 |
// Test should pass even though its test method is a failure. |
| 251 |
assertNotContains('testdata/fake_failing_test4', failedTests); |
| 252 |
}); |
| 253 |
}, |
| 254 |
|
| 255 |
testRunTestsWithEmptyTestList: function() { |
| 256 |
var testRunner = new MultiTestRunner().setPoolSize(3).addTests([]); |
| 257 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 258 |
|
| 259 |
testRunner.render(document.getElementById('runner')); |
| 260 |
testRunner.start(); |
| 261 |
|
| 262 |
return promise.then(function(results) { |
| 263 |
var allTestResults = results['allTestResults']; |
| 264 |
assertEquals(0, allTestResults.length); |
| 265 |
var failureReports = processTestResults(allTestResults).failureReports; |
| 266 |
assertEquals(0, failureReports.length); |
| 267 |
assertEquals(0, testRunner.getTestsThatFailed().length); |
| 268 |
testRunner.dispose(); |
| 269 |
}); |
| 270 |
}, |
| 271 |
|
| 272 |
testFilterFunctionFiltersTest: function() { |
| 273 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 274 |
|
| 275 |
testRunner.render(document.getElementById('runner')); |
| 276 |
testRunner.setFilterFunction(function(test) { |
| 277 |
return test.indexOf('fake_failing_test2') != -1; |
| 278 |
}); |
| 279 |
testRunner.start(); |
| 280 |
|
| 281 |
return promise.then(function(results) { |
| 282 |
var allTestResults = results['allTestResults']; |
| 283 |
assertEquals(1, allTestResults.length); |
| 284 |
var failureReports = processTestResults(allTestResults).failureReports; |
| 285 |
var failedTests = testRunner.getTestsThatFailed(); |
| 286 |
assertEquals(1, failureReports.length); |
| 287 |
assertEquals(1, failedTests.length); |
| 288 |
assertArrayContainsString( |
| 289 |
'testdata/fake_failing_test2.html', failedTests); |
| 290 |
}); |
| 291 |
}, |
| 292 |
|
| 293 |
testTimeoutFailsAfterTimeout: function() { |
| 294 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 295 |
|
| 296 |
testRunner.render(document.getElementById('runner')); |
| 297 |
testRunner.setTimeout(0); |
| 298 |
// If the fake tests complete before the timeout check, this test will fail. |
| 299 |
testRunner.start(); |
| 300 |
|
| 301 |
return promise.then(function(results) { |
| 302 |
var testResults = processTestResults(results['allTestResults']); |
| 303 |
var testNames = testResults.testNames; |
| 304 |
// Only the filename should be the test name for timeouts. |
| 305 |
assertArrayContainsString('testdata/fake_failing_test2', testNames); |
| 306 |
assertArrayContainsString('testdata/fake_failing_test', testNames); |
| 307 |
assertArrayContainsString('testdata/fake_passing_test', testNames); |
| 308 |
assertEquals(3, testNames.length); |
| 309 |
var failureReports = testResults.failureReports; |
| 310 |
assertContains('timed out', failureReports[0]['message']); |
| 311 |
assertContains('timed out', failureReports[1]['message']); |
| 312 |
assertContains('timed out', failureReports[2]['message']); |
| 313 |
assertEquals(3, failureReports.length); |
| 314 |
var failedTests = testRunner.getTestsThatFailed(); |
| 315 |
assertArrayContainsString('testdata/fake_passing_test.html', failedTests); |
| 316 |
assertArrayContainsString('testdata/fake_failing_test.html', failedTests); |
| 317 |
assertArrayContainsString( |
| 318 |
'testdata/fake_failing_test2.html', failedTests); |
| 319 |
assertEquals(3, failedTests.length); |
| 320 |
}); |
| 321 |
}, |
| 322 |
|
| 323 |
testRunsAllTestsWhenPoolSizeSmallerThanTotalTests: function() { |
| 324 |
var promise = createEventPromise(testRunner, 'testsFinished'); |
| 325 |
|
| 326 |
testRunner.render(document.getElementById('runner')); |
| 327 |
// There are 3 tests, it should load and run the 3 serially without failing. |
| 328 |
testRunner.setPoolSize(1); |
| 329 |
testRunner.start(); |
| 330 |
|
| 331 |
return promise.then(function(results) { |
| 332 |
assertEquals(3, results['allTestResults'].length); |
| 333 |
var testResults = processTestResults(results['allTestResults']); |
| 334 |
var testNames = testResults.testNames; |
| 335 |
assertEquals(3, testNames.length); |
| 336 |
assertArrayContainsString( |
| 337 |
'testdata/fake_failing_test2:testFail', testNames); |
| 338 |
assertArrayContainsString( |
| 339 |
'testdata/fake_failing_test:testFail', testNames); |
| 340 |
assertArrayContainsString( |
| 341 |
'testdata/fake_passing_test:testPass', testNames); |
| 342 |
var failureReports = testResults.failureReports; |
| 343 |
var failedTests = testRunner.getTestsThatFailed(); |
| 344 |
assertEquals(2, failureReports.length); |
| 345 |
assertEquals(2, failedTests.length); |
| 346 |
assertArrayContainsString('testdata/fake_failing_test.html', failedTests); |
| 347 |
assertArrayContainsString( |
| 348 |
'testdata/fake_failing_test2.html', failedTests); |
| 349 |
}); |
| 350 |
}, |
| 351 |
|
| 352 |
testFrameGetStats: function() { |
| 353 |
var frame = new MultiTestRunner.TestFrame('/', 2000, false); |
| 354 |
frame.testFile_ = 'foo'; |
| 355 |
frame.isSuccess_ = true; |
| 356 |
frame.runTime_ = 42; |
| 357 |
frame.totalTime_ = 9000; |
| 358 |
frame.numFilesLoaded_ = 4; |
| 359 |
|
| 360 |
assertObjectEquals( |
| 361 |
{ |
| 362 |
'testFile': 'foo', |
| 363 |
'success': true, |
| 364 |
'runTime': 42, |
| 365 |
'totalTime': 9000, |
| 366 |
'numFilesLoaded': 4 |
| 367 |
}, |
| 368 |
frame.getStats()); |
| 369 |
}, |
| 370 |
|
| 371 |
testFrameDisposeInternal: function() { |
| 372 |
var frame = new MultiTestRunner.TestFrame('', 2000, false); |
| 373 |
frame.createDom(); |
| 374 |
frame.render(); |
| 375 |
stubs.replace(frame, 'checkForCompletion_', function() { return; }); |
| 376 |
frame.runTest(ALL_TESTS[0]); |
| 377 |
assertEquals( |
| 378 |
1, frame.getDomHelper().getElementsByTagNameAndClass('iframe').length); |
| 379 |
frame.dispose(); |
| 380 |
assertTrue(frame.eh_.isDisposed()); |
| 381 |
assertEquals( |
| 382 |
0, frame.getDomHelper().getElementsByTagNameAndClass('iframe').length); |
| 383 |
assertNull(frame.iframeEl_); |
| 384 |
} |
| 385 |
}); |