Move code into src/
[dygraphs.git] / phantom-driver.js
CommitLineData
08fcae0c
DV
1// Invoke via: ./test.sh
2//
3// or phantomjs phantom-driver.js [testCase.test]
4//
5// For more on phantomjs, visit www.phantomjs.org.
6
300c5ff0
DV
7var RunAllAutoTests = function(done_callback) {
8
86a3e64f
DV
9var page = require('webpage').create();
10
11// NOTE: Cannot include '#' or '?' in this URL.
12var url = 'auto_tests/misc/local.html';
13
14// NOTE: changing the line below to this:
15// page.open(url, function(status)) {
16// makes phantomjs hang.
17page.open(url, function(status) {
18 if (status !== 'success') {
19 console.warn('Page status: ' + status);
20 console.log(page);
21 phantom.exit();
22 }
23
24 var testCase, test;
2323945c
RK
25 var verbose = false;
26 var optIdx = 0;
27 if (phantom.args.length > 0 && phantom.args[0] === "--verbose") {
0c59a428
RK
28 verbose = true;
29 optIdx = 1;
2323945c
RK
30 }
31 if (phantom.args.length == optIdx + 1) {
32 var parts = phantom.args[optIdx].split('.');
86a3e64f 33 if (2 != parts.length) {
2323945c 34 console.warn('Usage: phantomjs phantom-driver.js [--verbose] [testCase.test]');
86a3e64f
DV
35 phantom.exit();
36 }
37 testCase = parts[0];
38 test = parts[1];
39 }
40
41 var loggingOn = false;
0c59a428 42
86a3e64f
DV
43 page.onConsoleMessage = function (msg) {
44 if (msg == 'Running ' + test) {
45 loggingOn = true;
46 } else if (msg.substr(0, 'Running'.length) == 'Running') {
47 loggingOn = false;
48 }
2323945c 49 if (verbose || loggingOn) console.log(msg);
86a3e64f
DV
50 };
51
52 page.onError = function (msg, trace) {
53 console.log(msg);
54 trace.forEach(function(item) {
55 console.log(' ', item.file, ':', item.line);
56 })
57 };
58
59 var results;
60
61 // Run all tests.
08fcae0c 62 var start = new Date().getTime();
86a3e64f 63 results = page.evaluate(function() {
0c59a428
RK
64 var num_passing = 0, num_failing = 0;
65 var failures = [];
86a3e64f 66
0c59a428
RK
67 jstestdriver.attachListener({
68 finish : function(tc, name, result, e) {
69 console.log("Result:", tc.name, name, result, e);
70 if (result) {
71 // console.log(testCase + '.' + test + ' passed');
72 num_passing++;
73 } else {
74 num_failing++;
75 failures.push(tc.name + '.' + name);
76 }
77 }
78 });
86a3e64f 79 var testCases = getAllTestCases();
86a3e64f
DV
80 for (var idx in testCases) {
81 var entry = testCases[idx];
82
83 var prototype = entry.testCase;
84 var tc = new entry.testCase();
85 var result = tc.runAllTests();
86a3e64f 86 }
0c59a428
RK
87 return {
88 num_passing : num_passing,
89 num_failing : num_failing,
90 failures : failures
91 };
86a3e64f 92 });
08fcae0c
DV
93 var end = new Date().getTime();
94 var elapsed = (end - start) / 1000;
86a3e64f 95
0c59a428
RK
96 console.log('Ran ' + (results.num_passing + results.num_failing) + ' tests in ' + elapsed + 's.');
97 console.log(results.num_passing + ' test(s) passed');
98 console.log(results.num_failing + ' test(s) failed:');
99 for (var i = 0; i < results.failures.length; i++) {
08fcae0c 100 // TODO(danvk): print an auto_test/misc/local URL that runs this test.
0c59a428 101 console.log(' ' + results.failures[i] + ' failed.');
08fcae0c 102 }
86a3e64f 103
0c59a428 104 done_callback(results.num_failing, results.num_passing);
300c5ff0
DV
105});
106
107};
108
109// Load all "tests/" pages.
110var LoadAllManualTests = function(totally_done_callback) {
111
112var fs = require('fs');
113var tests = fs.list('tests');
114var pages = [];
115
116function make_barrier_closure(n, fn) {
117 var calls = 0;
118 return function() {
119 calls++;
120 if (calls == n) {
121 fn();
122 } else {
123 // console.log('' + calls + ' done, ' + (n - calls) + ' remain');
124 }
125 };
126}
127
128var tasks = [];
129for (var i = 0; i < tests.length; i++) {
130 if (tests[i].substr(-5) != '.html') continue;
131 tasks.push(tests[i]);
132}
133tasks = [ 'independent-series.html' ];
134
135var loaded_page = make_barrier_closure(tasks.length, function() {
136 // Wait 2 secs to allow JS errors to happen after page load.
137 setTimeout(function() {
138 var success = 0, failures = 0;
139 for (var i = 0; i < pages.length; i++) {
140 if (pages[i].success && !pages[i].hasErrors) {
141 success++;
142 } else {
143 failures++;
144 }
145 }
146 console.log('Successfully loaded ' + success + ' / ' +
147 (success + failures) + ' test pages.');
148 totally_done_callback(failures, success);
149 }, 2000);
150});
151
152
153for (var i = 0; i < tasks.length; i++) {
154 var url = 'file://' + fs.absolute('tests/' + tasks[i]);
155 pages.push(function(path, url) {
156 var page = require('webpage').create();
157 page.success = false;
158 page.hasErrors = false;
159 page.onError = function (msg, trace) {
160 console.log(path + ': ' + msg);
161 page.hasErrors = true;
162 trace.forEach(function(item) {
163 console.log(' ', item.file, ':', item.line);
164 });
165 };
166 page.onLoadFinished = function(status) {
167 if (status == 'success') {
168 page.success = true;
169 }
170 if (!page.done) loaded_page();
171 page.done = true;
172 };
173 page.open(url);
174 return page;
175 }(tasks[i], url));
176}
177
178};
179
180
181// First run all auto_tests.
182// If they all pass, load the manual tests.
183RunAllAutoTests(function(num_failing, num_passing) {
184 if (num_failing !== 0) {
86a3e64f 185 console.log('FAIL');
300c5ff0 186 phantom.exit();
5776905d
RK
187 } else {
188 console.log('PASS');
86a3e64f 189 }
86a3e64f 190 phantom.exit();
300c5ff0
DV
191
192 // This is not yet reliable enough to be useful:
193 /*
194 LoadAllManualTests(function(failing, passing) {
195 if (failing !== 0) {
196 console.log('FAIL');
197 } else {
198 console.log('PASS');
199 }
200 phantom.exit();
201 });
202 */
86a3e64f 203});