copy over lots of changes from "shrink" branch.
[dygraphs.git] / auto_tests / misc / local.js
CommitLineData
464b5f50
DV
1'use strict';
2
11c21001
RK
3var DygraphsLocalTester = function() {
4 this.tc = null; // Selected test case
5 this.name = null;
add4749b
RK
6 this.results = [];
7 this.summary = { failed: 0, passed: 0 };
02b1c284 8 this.start;
add4749b
RK
9
10 var self = this;
11 jstestdriver.attachListener({
12 start : function(tc) {
13 self.start_(tc);
14 },
15 finish : function(tc, name, result, e) {
16 self.finish_(tc, name, result, e);
17 }
18 });
11c21001
RK
19};
20
21/**
22 * Call this to replace Dygraphs.warn so it throws an error.
23 *
24 * In some cases we will still allow warnings to be warnings, however.
25 */
26DygraphsLocalTester.prototype.overrideWarn = function() {
243eecfb 27 // save Dygraph.warn so we can catch warnings.
11c21001
RK
28 var originalDygraphWarn = Dygraph.warn;
29 Dygraph.warn = function(msg) {
30 // This warning is still
31 if (msg == "Using default labels. Set labels explicitly via 'labels' in the options parameter") {
32 originalDygraphWarn(msg);
33 return;
243eecfb 34 }
02b1c284 35 throw 'Warnings not permitted: ' + msg;
243eecfb
RK
36 }
37};
38
11c21001 39DygraphsLocalTester.prototype.processVariables = function() {
243eecfb
RK
40 var splitVariables = function() { // http://www.idealog.us/2006/06/javascript_to_p.html
41 var query = window.location.search.substring(1);
42 var args = {};
02b1c284 43 var vars = query.split('&');
243eecfb
RK
44 for (var i = 0; i < vars.length; i++) {
45 if (vars[i].length > 0) {
02b1c284 46 var pair = vars[i].split('=');
243eecfb
RK
47 args[pair[0]] = pair[1];
48 }
49 }
50 return args;
51 }
52
02b1c284
RK
53 var findTestCase = function(stringName, className) {
54 if (stringName) {
243eecfb 55 var testCases = getAllTestCases();
243eecfb
RK
56 for (var idx in testCases) {
57 var entry = testCases[idx];
02b1c284
RK
58 if (entry.name == stringName) {
59 var prototype = entry.testCase;
60 return new entry.testCase();
61 }
243eecfb 62 }
02b1c284
RK
63 } else if (className) {
64 eval('tc__ = new ' + className + '()');
65 return tc__;
243eecfb 66 }
02b1c284 67 return null;
243eecfb 68 }
02b1c284
RK
69
70 var args = splitVariables();
71 this.tc = findTestCase(args.testCaseName, args.testCase);
72 this.test = args.test;
73 this.command = args.command;
74}
75
76DygraphsLocalTester.prototype.createAnchor = function(href, id, text) {
77 var a = document.createElement('a');
78 a.href = href;
79 a.id = id;
464b5f50 80 a.textContent = text;
02b1c284 81 return a;
243eecfb
RK
82}
83
02b1c284 84DygraphsLocalTester.prototype.createResultsDiv = function(summary, durationms) {
464b5f50 85 var div = document.createElement('div');
243eecfb 86 div.id='results';
add4749b 87
02b1c284 88 var body = document.getElementsByTagName('body')[0];
243eecfb
RK
89 body.insertBefore(div, body.firstChild);
90
02b1c284
RK
91 var addText = function(div, text) {
92 div.appendChild(document.createTextNode(text));
93 };
94
95 var passedAnchor = this.createAnchor('#', 'passed', '' + summary.passed + ' passed');
96 var failedAnchor = this.createAnchor('#', 'failed', '' + summary.failed + ' failed');
97 var allAnchor = this.createAnchor('#', 'all', '(view all)');
98
99 addText(div, 'Test results: ');
100 div.appendChild(passedAnchor);
101 addText(div, ', ');
102 div.appendChild(failedAnchor);
103 addText(div, ', ');
104 div.appendChild(allAnchor);
105 addText(div, ', (' + durationms + ' ms)');
106
107 var table = document.createElement('table');
108 div.appendChild(table);
109 div.appendChild(document.createElement('hr'));
110
243eecfb 111 var setByClassName = function(name, displayStyle) {
02b1c284 112 var elements = table.getElementsByClassName(name);
243eecfb
RK
113 for (var i = 0; i < elements.length; i++) {
114 elements[i].style.display = displayStyle;
115 }
116 }
117
243eecfb
RK
118 passedAnchor.onclick = function() {
119 setByClassName('fail', 'none');
120 setByClassName('pass', 'block');
121
02b1c284
RK
122 passedAnchor.setAttribute('class', 'activeAnchor');
123 failedAnchor.setAttribute('class', '');
243eecfb
RK
124 };
125 failedAnchor.onclick = function() {
126 setByClassName('fail', 'block');
127 setByClassName('pass', 'none');
02b1c284
RK
128 passedAnchor.setAttribute('class', '');
129 failedAnchor.setAttribute('class', 'activeAnchor');
243eecfb
RK
130 };
131 allAnchor.onclick = function() {
132 setByClassName('fail', 'block');
133 setByClassName('pass', 'block');
02b1c284
RK
134 passedAnchor.setAttribute('class', '');
135 failedAnchor.setAttribute('class', '');
243eecfb
RK
136 };
137 return div;
138}
139
02b1c284
RK
140DygraphsLocalTester.prototype.postResults = function(summary, durationms) {
141 var resultsDiv = this.createResultsDiv(summary, durationms);
142
143 var table = resultsDiv.getElementsByTagName('table')[0];
add4749b
RK
144 for (var idx = 0; idx < this.results.length; idx++) {
145 var result = this.results[idx];
02b1c284
RK
146 var tr = document.createElement('tr');
147 tr.setAttribute('class', result.result ? 'pass' : 'fail');
7cf85dd5 148
02b1c284
RK
149 var tdResult = document.createElement('td');
150 tdResult.setAttribute('class', 'outcome');
464b5f50 151 tdResult.textContent = result.result ? 'pass' : 'fail';
7cf85dd5
RK
152 tr.appendChild(tdResult);
153
02b1c284
RK
154 var tdName = document.createElement('td');
155 var s = result.name.split('.');
156 var url = window.location.pathname + '?testCaseName=' + s[0] + '&test=' + s[1] + '&command=runTest';
157 var a = this.createAnchor(url, null, result.name);
158
159 tdName.appendChild(a);
7cf85dd5
RK
160 tr.appendChild(tdName);
161
02b1c284 162 var tdDuration = document.createElement('td');
464b5f50 163 tdDuration.textContent = result.duration + ' ms';
7cf85dd5
RK
164 tr.appendChild(tdDuration);
165
b99970f8 166 if (result.e) {
02b1c284
RK
167 var tdDetails = document.createElement('td');
168 var a = this.createAnchor('#', null, '(stack trace)');
169 a.onclick = function(e) {
b99970f8 170 return function() {
02b1c284 171 alert(e + '\n' + e.stack);
b99970f8
RK
172 };
173 }(result.e);
02b1c284 174 tdDetails.appendChild(a);
b99970f8
RK
175 tr.appendChild(tdDetails);
176 }
177
7cf85dd5 178 table.appendChild(tr);
243eecfb
RK
179 }
180}
181
02b1c284
RK
182DygraphsLocalTester.prototype.listTests = function() {
183 var selector = document.getElementById('selector');
243eecfb
RK
184
185 if (selector != null) { // running a test
186 var createAttached = function(name, parent) {
187 var elem = document.createElement(name);
188 parent.appendChild(elem);
189 return elem;
190 }
191
02b1c284
RK
192 var description = createAttached('div', selector);
193 var list = createAttached('ul', selector);
243eecfb 194 var parent = list.parentElement;
02b1c284
RK
195 var createLink = function(parent, title, url) {
196 var li = createAttached('li', parent);
197 var a = createAttached('a', li);
464b5f50 198 a.textContent = title;
243eecfb 199 a.href = url;
02b1c284 200 return li;
243eecfb 201 }
11c21001 202 if (this.tc == null) {
464b5f50 203 description.textContent = 'Test cases:';
243eecfb 204 var testCases = getAllTestCases();
02b1c284 205 createLink(list, '(run all tests)', document.URL + '?command=runAllTests');
243eecfb
RK
206 for (var idx in testCases) {
207 var entryName = testCases[idx].name;
02b1c284 208 createLink(list, entryName, document.URL + '?testCaseName=' + entryName);
243eecfb
RK
209 }
210 } else {
464b5f50 211 description.textContent = 'Tests for ' + name;
add4749b 212 var names = this.tc.getTestNames();
02b1c284 213 createLink(list, 'Run All Tests', document.URL + '&command=runAllTests');
243eecfb
RK
214 for (var idx in names) {
215 var name = names[idx];
02b1c284 216 createLink(list, name, document.URL + '&test=' + name + '&command=runTest');
243eecfb
RK
217 }
218 }
219 }
220}
add4749b 221
02b1c284
RK
222DygraphsLocalTester.prototype.run = function() {
223 var executed = false;
224 var start = new Date(). getTime();
225 if (this.tc != null) {
226 if (this.command == 'runAllTests') {
227 console.log('Running all tests for ' + this.tc.name);
228 this.tc.runAllTests();
229 executed = true;
230 } else if (this.command == 'runTest') {
231 console.log('Running test ' + this.tc.name + '.' + this.test);
232 this.tc.runTest(this.test);
233 executed = true;
234 }
235 } else if (this.command == 'runAllTests') {
236 console.log('Running all tests for all test cases');
237 var testCases = getAllTestCases();
238 for (var idx in testCases) {
239 var entry = testCases[idx];
240 var prototype = entry.testCase;
241 this.tc = new entry.testCase();
242 this.tc.runAllTests();
243 }
244 executed = true;
245 }
246
247 var durationms = new Date().getTime() - start;
248
249 if (executed) {
250 this.postResults(this.summary, durationms);
251 } else {
252 this.listTests();
253 }
254}
255
add4749b
RK
256DygraphsLocalTester.prototype.start_ = function(tc) {
257 this.startms_ = new Date().getTime();
258}
259
260DygraphsLocalTester.prototype.finish_ = function(tc, name, result, e) {
261 var endms_ = new Date().getTime();
262 this.results.push({
02b1c284 263 name : tc.name + '.' + name,
add4749b 264 result : result,
b99970f8
RK
265 duration : endms_ - this.startms_,
266 e : e
add4749b
RK
267 });
268 this.summary.passed += result ? 1 : 0;
269 this.summary.failed += result ? 0 : 1;
270}