OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * Integration module for QUnit tests running in browser tests. | 7 * Integration module for QUnit tests running in browser tests. |
8 * Specifically it: | 8 * Specifically it: |
9 * - Sets QUnit.autostart to false, so that the browser test can hook the test | 9 * - Sets QUnit.autostart to false, so that the browser test can hook the test |
10 * results callback before the test starts. | 10 * results callback before the test starts. |
11 * - Implements a text-based test reporter to report test results back to the | 11 * - Implements a text-based test reporter to report test results back to the |
12 * browser test. | 12 * browser test. |
13 */ | 13 */ |
14 | 14 |
15 (function(QUnit, automationController, exports) { | 15 (function(QUnit, automationController, exports) { |
16 | 16 |
17 'use strict'; | 17 'use strict'; |
18 | 18 |
| 19 var TEST_TIMEOUT_IN_MS = 5000; |
| 20 |
19 var TestReporter = function() { | 21 var TestReporter = function() { |
20 this.errorMessage_ = ''; | 22 this.errorMessage_ = ''; |
21 this.failedTestsCount_ = 0; | 23 this.failedTestsCount_ = 0; |
22 this.failedAssertions_ = []; | 24 this.failedAssertions_ = []; |
23 }; | 25 }; |
24 | 26 |
25 TestReporter.prototype.init = function(qunit) { | 27 TestReporter.prototype.init = function(qunit) { |
| 28 qunit.testStart(this.onTestStart_.bind(this)); |
26 qunit.testDone(this.onTestDone_.bind(this)); | 29 qunit.testDone(this.onTestDone_.bind(this)); |
27 qunit.log(this.onAssertion_.bind(this)); | 30 qunit.log(this.onAssertion_.bind(this)); |
28 }; | 31 }; |
29 | 32 |
| 33 /** |
| 34 * @param {{ module:string, name: string }} details |
| 35 */ |
| 36 TestReporter.prototype.onTestStart_ = function(details) { |
| 37 console.log('[===============]'); |
| 38 console.log('[------RUN------] ' + details.module + '.' + details.name); |
| 39 }; |
| 40 |
| 41 /** |
| 42 * @param {{ module:string, name: string }} details |
| 43 */ |
30 TestReporter.prototype.onTestDone_ = function(details) { | 44 TestReporter.prototype.onTestDone_ = function(details) { |
| 45 console.log('[---COMPLETED---] ' + details.module + '.' + details.name); |
| 46 console.log('[===============]'); |
31 if (this.failedAssertions_.length > 0) { | 47 if (this.failedAssertions_.length > 0) { |
32 this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n'; | 48 this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n'; |
33 this.errorMessage_ += this.failedAssertions_.map( | 49 this.errorMessage_ += this.failedAssertions_.map( |
34 function(assertion, index){ | 50 function(assertion, index){ |
35 return ' ' + (index + 1) + '. ' + assertion.message + '\n' + | 51 return ' ' + (index + 1) + '. ' + assertion.message + '\n' + |
36 ' ' + assertion.source; | 52 ' ' + assertion.source; |
37 }).join('\n'); | 53 }).join('\n'); |
38 this.failedAssertions_ = []; | 54 this.failedAssertions_ = []; |
39 this.failedTestsCount_++; | 55 this.failedTestsCount_++; |
40 } | 56 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 103 } |
88 | 104 |
89 var testHarness = new BrowserTestHarness( | 105 var testHarness = new BrowserTestHarness( |
90 QUnit, | 106 QUnit, |
91 automationController, | 107 automationController, |
92 new TestReporter()); | 108 new TestReporter()); |
93 testHarness.init(); | 109 testHarness.init(); |
94 exports.browserTestHarness = testHarness; | 110 exports.browserTestHarness = testHarness; |
95 } | 111 } |
96 | 112 |
| 113 var qunitTest = QUnit.test; |
| 114 var reasonTimeout = {}; |
| 115 |
| 116 /** |
| 117 * Returns a promise that resolves after |delay| along with a timerId |
| 118 * for cancellation. |
| 119 * |
| 120 * @return {promise: !Promise, timerId: number} |
| 121 */ |
| 122 BrowserTestHarness.timeout = function(delay) { |
| 123 var timerId = 0; |
| 124 var promise = new Promise(function(resolve) { |
| 125 timerId = window.setTimeout(function() { |
| 126 resolve(); |
| 127 }, delay); |
| 128 }); |
| 129 return { |
| 130 timerId: timerId, |
| 131 promise: promise |
| 132 }; |
| 133 }; |
| 134 |
| 135 QUnit.config.urlConfig.push({ |
| 136 id: "disableTestTimeout", |
| 137 label: "disable test timeout", |
| 138 tooltip: "Check this when debugging locally to disable test timeout.", |
| 139 }); |
| 140 |
| 141 /** |
| 142 * Forces the test to fail after |TEST_TIMEOUT_IN_MS|. |
| 143 * |
| 144 * @param {function(QUnit.Assert)} testCallback |
| 145 */ |
| 146 BrowserTestHarness.test = function(testCallback) { |
| 147 return function() { |
| 148 var args = Array.prototype.slice.call(arguments); |
| 149 var timeout = BrowserTestHarness.timeout(TEST_TIMEOUT_IN_MS); |
| 150 |
| 151 var testPromise = Promise.resolve(testCallback.apply(this, args)) |
| 152 .then(function() { |
| 153 window.clearTimeout(timeout.timerId); |
| 154 }); |
| 155 |
| 156 var asserts = args[0]; |
| 157 var timeoutPromise = timeout.promise.then(function(){ |
| 158 asserts.ok(false, 'Test timed out after ' + TEST_TIMEOUT_IN_MS + ' ms') |
| 159 }) |
| 160 |
| 161 return Promise.race([testPromise, timeoutPromise]); |
| 162 }; |
| 163 }; |
| 164 |
| 165 if (!QUnit.urlParams.disableTestTimeout) { |
| 166 QUnit.test = function(name, expected, testCallback, async) { |
| 167 qunitTest(name, expected, BrowserTestHarness.test(testCallback), async); |
| 168 }; |
| 169 } |
| 170 |
97 })(window.QUnit, window.domAutomationController, window); | 171 })(window.QUnit, window.domAutomationController, window); |
OLD | NEW |