Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / unittest / interactive_test_runner.js
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.
4
5 'use strict';
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');
10
11 tvcm.requireStylesheet('tvcm.unittest.common');
12 tvcm.requireTemplate('tvcm.unittest.interactive_test_runner');
13
14 tvcm.exportTo('tvcm.unittest', function() {
15   /**
16    * @constructor
17    */
18   var InteractiveTestRunner = tvcm.ui.define('x-base-interactive-test-runner');
19
20   InteractiveTestRunner.prototype = {
21     __proto__: HTMLUnknownElement.prototype,
22
23     decorate: function() {
24       this.allTests_ = undefined;
25
26       this.suppressStateChange_ = false;
27
28       this.testFilterString_ = '';
29       this.testTypeToRun_ = tvcm.unittest.TestTypes.UNITTEST;
30       this.shortFormat_ = false;
31
32       this.rerunPending_ = false;
33       this.runner_ = undefined;
34       this.results_ = undefined;
35
36       this.onResultsStatsChanged_ = this.onResultsStatsChanged_.bind(this);
37       this.onTestFailed_ = this.onTestFailed_.bind(this);
38       this.onTestPassed_ = this.onTestPassed_.bind(this);
39
40
41       this.appendChild(tvcm.instantiateTemplate(
42           '#x-base-interactive-test-runner-template'));
43
44       this.querySelector(
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]'));
48
49       testTypeToRunEls.forEach(
50           function(inputEl) {
51             inputEl.addEventListener(
52                 'click', this.onTestTypeToRunClick_.bind(this));
53           }, this);
54
55       var shortFormatEl = this.querySelector('#short-format');
56       shortFormatEl.checked = this.shortFormat_;
57       shortFormatEl.addEventListener(
58           'click', this.onShortFormatClick_.bind(this));
59       this.updateShortFormResultsDisplay_();
60     },
61
62     set title(title) {
63       this.querySelector('#title').textContent = title;
64     },
65
66     get allTests() {
67       return this.allTests_;
68     },
69
70     set allTests(allTests) {
71       this.allTests_ = allTests;
72       this.scheduleRerun_();
73     },
74
75     get testLinks() {
76       return this.testLinks_;
77     },
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);
87       }, this);
88     },
89
90     get testFilterString() {
91       return this.testFilterString_;
92     },
93
94     set testFilterString(testFilterString) {
95       this.testFilterString_ = testFilterString;
96       this.scheduleRerun_();
97       if (!this.suppressStateChange_)
98         tvcm.dispatchSimpleEvent(this, 'statechange');
99     },
100
101     get shortFormat() {
102       return this.shortFormat_;
103     },
104
105     set shortFormat(shortFormat) {
106       this.shortFormat_ = shortFormat;
107       this.querySelector('#short-format').checked = shortFormat;
108       if (this.results_)
109         this.results_.shortFormat = shortFormat;
110       if (!this.suppressStateChange_)
111         tvcm.dispatchSimpleEvent(this, 'statechange');
112     },
113
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');
120     },
121
122     updateShortFormResultsDisplay_: function() {
123       var display = this.shortFormat_ ? '' : 'none';
124       this.querySelector('#shortform-results').style.display = display;
125     },
126
127     updateResultsGivenShortFormat_: function() {
128       if (!this.results_)
129         return;
130
131       if (this.testFilterString_.length)
132         this.results_.showHTMLOutput = true;
133       else
134         this.results_.showHTMLOutput = false;
135       this.results_.showPendingAndPassedTests = this.shortFormat_;
136     },
137
138     get testTypeToRun() {
139       return this.testTypeToRun_;
140     },
141
142     set testTypeToRun(testTypeToRun) {
143       this.testTypeToRun_ = testTypeToRun;
144       var sel;
145       if (testTypeToRun == tvcm.unittest.TestTypes.UNITTEST)
146         sel = 'input[name=test-type-to-run][value=unit]';
147       else
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');
153     },
154
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');
163     },
164
165     onTestPassed_: function() {
166       this.querySelector('#shortform-results').textContent += '.';
167     },
168
169     onTestFailed_: function() {
170       this.querySelector('#shortform-results').textContent += 'F';
171     },
172
173     onResultsStatsChanged_: function() {
174       var statsEl = this.querySelector('#stats');
175       var stats = this.results_.getStats();
176       var numTests = this.runner_.testCases.length;
177       statsEl.innerHTML =
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.';
182     },
183
184     scheduleRerun_: function() {
185       if (this.rerunPending_)
186         return;
187       if (this.runner_) {
188         this.rerunPending_ = true;
189         this.runner_.beginToStopRunning();
190         var doRerun = function() {
191           this.rerunPending_ = false;
192           this.scheduleRerun_();
193         }.bind(this);
194         this.runner_.runCompletedPromise.then(
195             doRerun, doRerun);
196         return;
197       }
198       this.beginRunning_();
199     },
200
201     beginRunning_: function() {
202       var resultsContainer = this.querySelector('#results-container');
203       if (this.results_) {
204         this.results_.removeEventListener('testpassed',
205                                           this.onTestPassed_);
206         this.results_.removeEventListener('testfailed',
207                                           this.onTestFailed_);
208         this.results_.removeEventListener('statschange',
209                                           this.onResultsStatsChanged_);
210         delete this.results_.getHRefForTestCase;
211         resultsContainer.removeChild(this.results_);
212       }
213
214       this.results_ = new tvcm.unittest.HTMLTestResults();
215       this.results_.getHRefForTestCase = this.getHRefForTestCase.bind(this);
216       this.updateResultsGivenShortFormat_();
217
218       this.results_.shortFormat = this.shortFormat_;
219       this.results_.addEventListener('testpassed',
220                                      this.onTestPassed_);
221       this.results_.addEventListener('testfailed',
222                                      this.onTestFailed_);
223       this.results_.addEventListener('statschange',
224                                      this.onResultsStatsChanged_);
225       resultsContainer.appendChild(this.results_);
226
227       var tests = this.allTests_.filter(function(test) {
228         var i = test.fullyQualifiedName.indexOf(this.testFilterString_);
229         if (i == -1)
230           return false;
231         if (test.testType != this.testTypeToRun_)
232           return false;
233         return true;
234       }, this);
235
236       this.runner_ = new tvcm.unittest.TestRunner(this.results_, tests);
237       this.runner_.beginRunning();
238
239       this.runner_.runCompletedPromise.then(
240           this.runCompleted_.bind(this),
241           this.runCompleted_.bind(this));
242     },
243
244     setState: function(state) {
245       this.suppressStateChange_ = true;
246       if (state.testFilterString !== undefined)
247         this.testFilterString = state.testFilterString;
248       else
249         this.testFilterString = [];
250
251       if (state.shortFormat === undefined)
252         this.shortFormat = false;
253       else
254         this.shortFormat = state.shortFormat;
255
256       if (state.testTypeToRun === undefined)
257         this.testTypeToRun = tvcm.unittest.TestTypes.UNITTEST;
258       else
259         this.testTypeToRun = state.testTypeToRun;
260
261       this.suppressStateChange_ = false;
262       this.onShortFormatClick_();
263       this.scheduleRerun_();
264     },
265
266     getState: function() {
267       return {
268         testFilterString: this.testFilterString_,
269         shortFormat: this.shortFormat_,
270         testTypeToRun: this.testTypeToRun_
271       };
272     },
273
274     getHRefForTestCase: function(testCases) {
275       return undefined;
276     },
277
278     runCompleted_: function() {
279       this.runner_ = undefined;
280       if (this.results_.getStats().numTestsThatFailed > 0) {
281         this.querySelector('#shortform-results').textContent +=
282             '[THERE WERE FAILURES]';
283       } else {
284         this.querySelector('#shortform-results').textContent += '[DONE]';
285       }
286     }
287   };
288
289   return {
290     InteractiveTestRunner: InteractiveTestRunner
291   };
292 });