1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 tvcm.require('tvcm.unittest');
7 tvcm.require('tvcm.unittest.suite_loader');
8 tvcm.require('tvcm.unittest.test_runner');
9 tvcm.require('tvcm.unittest.html_test_results');
11 tvcm.requireStylesheet('tvcm.unittest.common');
12 tvcm.requireTemplate('tvcm.unittest.interactive_test_runner');
14 tvcm.exportTo('tvcm.unittest', function() {
18 var InteractiveTestRunner = tvcm.ui.define('x-base-interactive-test-runner');
20 InteractiveTestRunner.prototype = {
21 __proto__: HTMLUnknownElement.prototype,
23 decorate: function() {
24 this.allTests_ = undefined;
26 this.suppressStateChange_ = false;
28 this.testFilterString_ = '';
29 this.testTypeToRun_ = tvcm.unittest.TestTypes.UNITTEST;
30 this.shortFormat_ = false;
32 this.rerunPending_ = false;
33 this.runner_ = undefined;
34 this.results_ = undefined;
36 this.onResultsStatsChanged_ = this.onResultsStatsChanged_.bind(this);
37 this.onTestFailed_ = this.onTestFailed_.bind(this);
38 this.onTestPassed_ = this.onTestPassed_.bind(this);
41 this.appendChild(tvcm.instantiateTemplate(
42 '#x-base-interactive-test-runner-template'));
45 'input[name=test-type-to-run][value=unit]').checked = true;
46 var testTypeToRunEls = tvcm.asArray(this.querySelectorAll(
47 'input[name=test-type-to-run]'));
49 testTypeToRunEls.forEach(
51 inputEl.addEventListener(
52 'click', this.onTestTypeToRunClick_.bind(this));
55 var shortFormatEl = this.querySelector('#short-format');
56 shortFormatEl.checked = this.shortFormat_;
57 shortFormatEl.addEventListener(
58 'click', this.onShortFormatClick_.bind(this));
59 this.updateShortFormResultsDisplay_();
63 this.querySelector('#title').textContent = title;
67 return this.allTests_;
70 set allTests(allTests) {
71 this.allTests_ = allTests;
72 this.scheduleRerun_();
76 return this.testLinks_;
78 set testLinks(testLinks) {
79 this.testLinks_ = testLinks;
80 var linksEl = this.querySelector('#links');
81 linksEl.textContent = '';
82 this.testLinks_.forEach(function(l) {
83 var link = document.createElement('a');
84 link.href = l.linkPath;
85 link.textContent = l.title;
86 linksEl.appendChild(link);
90 get testFilterString() {
91 return this.testFilterString_;
94 set testFilterString(testFilterString) {
95 this.testFilterString_ = testFilterString;
96 this.scheduleRerun_();
97 if (!this.suppressStateChange_)
98 tvcm.dispatchSimpleEvent(this, 'statechange');
102 return this.shortFormat_;
105 set shortFormat(shortFormat) {
106 this.shortFormat_ = shortFormat;
107 this.querySelector('#short-format').checked = shortFormat;
109 this.results_.shortFormat = shortFormat;
110 if (!this.suppressStateChange_)
111 tvcm.dispatchSimpleEvent(this, 'statechange');
114 onShortFormatClick_: function(e) {
115 this.shortFormat_ = this.querySelector('#short-format').checked;
116 this.updateShortFormResultsDisplay_();
117 this.updateResultsGivenShortFormat_();
118 if (!this.suppressStateChange_)
119 tvcm.dispatchSimpleEvent(this, 'statechange');
122 updateShortFormResultsDisplay_: function() {
123 var display = this.shortFormat_ ? '' : 'none';
124 this.querySelector('#shortform-results').style.display = display;
127 updateResultsGivenShortFormat_: function() {
131 if (this.testFilterString_.length)
132 this.results_.showHTMLOutput = true;
134 this.results_.showHTMLOutput = false;
135 this.results_.showPendingAndPassedTests = this.shortFormat_;
138 get testTypeToRun() {
139 return this.testTypeToRun_;
142 set testTypeToRun(testTypeToRun) {
143 this.testTypeToRun_ = testTypeToRun;
145 if (testTypeToRun == tvcm.unittest.TestTypes.UNITTEST)
146 sel = 'input[name=test-type-to-run][value=unit]';
148 sel = 'input[name=test-type-to-run][value=perf]';
149 this.querySelector(sel).checked = true;
150 this.scheduleRerun_();
151 if (!this.suppressStateChange_)
152 tvcm.dispatchSimpleEvent(this, 'statechange');
155 onTestTypeToRunClick_: function(e) {
156 if (e.target.value == 'unit')
157 this.testTypeToRun_ = tvcm.unittest.TestTypes.UNITTEST;
158 else // e.value == 'perf'
159 this.testTypeToRun_ = tvcm.unittest.TestTypes.PERFTEST;
160 this.scheduleRerun_();
161 if (!this.suppressStateChange_)
162 tvcm.dispatchSimpleEvent(this, 'statechange');
165 onTestPassed_: function() {
166 this.querySelector('#shortform-results').textContent += '.';
169 onTestFailed_: function() {
170 this.querySelector('#shortform-results').textContent += 'F';
173 onResultsStatsChanged_: function() {
174 var statsEl = this.querySelector('#stats');
175 var stats = this.results_.getStats();
176 var numTests = this.runner_.testCases.length;
178 '<span class="unittest-passed">' + numTests + '</span> tests, ' +
179 '<span class="unittest-failed">' + stats.numTestsThatFailed +
180 '</span> failures, ' +
181 ' in ' + stats.totalRunTime.toFixed(2) + 'ms.';
184 scheduleRerun_: function() {
185 if (this.rerunPending_)
188 this.rerunPending_ = true;
189 this.runner_.beginToStopRunning();
190 var doRerun = function() {
191 this.rerunPending_ = false;
192 this.scheduleRerun_();
194 this.runner_.runCompletedPromise.then(
198 this.beginRunning_();
201 beginRunning_: function() {
202 var resultsContainer = this.querySelector('#results-container');
204 this.results_.removeEventListener('testpassed',
206 this.results_.removeEventListener('testfailed',
208 this.results_.removeEventListener('statschange',
209 this.onResultsStatsChanged_);
210 delete this.results_.getHRefForTestCase;
211 resultsContainer.removeChild(this.results_);
214 this.results_ = new tvcm.unittest.HTMLTestResults();
215 this.results_.getHRefForTestCase = this.getHRefForTestCase.bind(this);
216 this.updateResultsGivenShortFormat_();
218 this.results_.shortFormat = this.shortFormat_;
219 this.results_.addEventListener('testpassed',
221 this.results_.addEventListener('testfailed',
223 this.results_.addEventListener('statschange',
224 this.onResultsStatsChanged_);
225 resultsContainer.appendChild(this.results_);
227 var tests = this.allTests_.filter(function(test) {
228 var i = test.fullyQualifiedName.indexOf(this.testFilterString_);
231 if (test.testType != this.testTypeToRun_)
236 this.runner_ = new tvcm.unittest.TestRunner(this.results_, tests);
237 this.runner_.beginRunning();
239 this.runner_.runCompletedPromise.then(
240 this.runCompleted_.bind(this),
241 this.runCompleted_.bind(this));
244 setState: function(state) {
245 this.suppressStateChange_ = true;
246 if (state.testFilterString !== undefined)
247 this.testFilterString = state.testFilterString;
249 this.testFilterString = [];
251 if (state.shortFormat === undefined)
252 this.shortFormat = false;
254 this.shortFormat = state.shortFormat;
256 if (state.testTypeToRun === undefined)
257 this.testTypeToRun = tvcm.unittest.TestTypes.UNITTEST;
259 this.testTypeToRun = state.testTypeToRun;
261 this.suppressStateChange_ = false;
262 this.onShortFormatClick_();
263 this.scheduleRerun_();
266 getState: function() {
268 testFilterString: this.testFilterString_,
269 shortFormat: this.shortFormat_,
270 testTypeToRun: this.testTypeToRun_
274 getHRefForTestCase: function(testCases) {
278 runCompleted_: function() {
279 this.runner_ = undefined;
280 if (this.results_.getStats().numTestsThatFailed > 0) {
281 this.querySelector('#shortform-results').textContent +=
282 '[THERE WERE FAILURES]';
284 this.querySelector('#shortform-results').textContent += '[DONE]';
290 InteractiveTestRunner: InteractiveTestRunner