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 | |
21 var TestReporter = function() { | 19 var TestReporter = function() { |
22 this.errorMessage_ = ''; | 20 this.errorMessage_ = ''; |
23 this.failedTestsCount_ = 0; | 21 this.failedTestsCount_ = 0; |
24 this.failedAssertions_ = []; | 22 this.failedAssertions_ = []; |
25 }; | 23 }; |
26 | 24 |
27 TestReporter.prototype.init = function(qunit) { | 25 TestReporter.prototype.init = function(qunit) { |
28 qunit.testStart(this.onTestStart_.bind(this)); | |
29 qunit.testDone(this.onTestDone_.bind(this)); | 26 qunit.testDone(this.onTestDone_.bind(this)); |
30 qunit.log(this.onAssertion_.bind(this)); | 27 qunit.log(this.onAssertion_.bind(this)); |
31 }; | 28 }; |
32 | 29 |
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 */ | |
44 TestReporter.prototype.onTestDone_ = function(details) { | 30 TestReporter.prototype.onTestDone_ = function(details) { |
45 console.log('[---COMPLETED---] ' + details.module + '.' + details.name); | |
46 console.log('[===============]'); | |
47 if (this.failedAssertions_.length > 0) { | 31 if (this.failedAssertions_.length > 0) { |
48 this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n'; | 32 this.errorMessage_ += ' ' + details.module + '.' + details.name + '\n'; |
49 this.errorMessage_ += this.failedAssertions_.map( | 33 this.errorMessage_ += this.failedAssertions_.map( |
50 function(assertion, index){ | 34 function(assertion, index){ |
51 return ' ' + (index + 1) + '. ' + assertion.message + '\n' + | 35 return ' ' + (index + 1) + '. ' + assertion.message + '\n' + |
52 ' ' + assertion.source; | 36 ' ' + assertion.source; |
53 }).join('\n'); | 37 }).join('\n'); |
54 this.failedAssertions_ = []; | 38 this.failedAssertions_ = []; |
55 this.failedTestsCount_++; | 39 this.failedTestsCount_++; |
56 } | 40 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 } | 87 } |
104 | 88 |
105 var testHarness = new BrowserTestHarness( | 89 var testHarness = new BrowserTestHarness( |
106 QUnit, | 90 QUnit, |
107 automationController, | 91 automationController, |
108 new TestReporter()); | 92 new TestReporter()); |
109 testHarness.init(); | 93 testHarness.init(); |
110 exports.browserTestHarness = testHarness; | 94 exports.browserTestHarness = testHarness; |
111 } | 95 } |
112 | 96 |
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 | |
171 })(window.QUnit, window.domAutomationController, window); | 97 })(window.QUnit, window.domAutomationController, window); |
OLD | NEW |