Moved the test directory.
[jquery.git] / build / js / lib / Test / Harness / Browser.js
1 // # $Id: Kinetic.pm 1493 2005-04-07 19:20:18Z theory $
2
3 if (typeof JSAN != 'undefined') new JSAN().use('Test.Harness');
4
5 Test.Harness.Browser = function () {};
6 Test.Harness.Browser.VERSION = '0.11';
7
8 Test.Harness.Browser.runTests = function () {
9     var harness = new Test.Harness.Browser();
10     harness.runTests.apply(harness, arguments);
11 };
12
13 Test.Harness.Browser.prototype = new Test.Harness();
14 Test.Harness.Browser.prototype.interval = 100;
15
16 Test.Harness.Browser.prototype._setupFrame = function (a) {
17     // Setup the iFrame to run the tests.
18     var node = document.getElementById('buffer');
19     if (node) return node.contentWindow;
20     node = document.createElement("iframe");
21     node.setAttribute("id", "buffer");
22     node.setAttribute("name", "buffer");
23                 node.style.visibility = "hidden";
24                 node.style.height = 0; 
25                 node.style.width = 0;
26     document.body.appendChild(node);
27                 
28                 var self = this;
29                 setTimeout( function(){
30                         self.buffer = node.contentWindow;
31                         self._runTests.apply(self,a);
32                 }, 200 );
33 };
34
35 Test.Harness.Browser.prototype._setupOutput = function () {
36     // Setup the pre element for test output.
37     var node = document.createElement("pre");
38     node.setAttribute("id", "output");
39     document.body.appendChild(node);
40     return function (msg) {
41         node.appendChild(document.createTextNode(msg));
42         window.scrollTo(0, document.body.offsetHeight
43                         || document.body.scrollHeight);
44     };
45 };
46
47 Test.Harness.Browser.prototype._setupSummary = function () {
48     // Setup the div for the summary.
49     var node = document.createElement("div");
50     node.setAttribute("id", "summary");
51     //node.setAttribute("style", "white-space:pre; font-family: Verdana,Arial,serif;");
52     document.body.appendChild(node);
53     return function (msg) {
54         node.appendChild(document.createTextNode(msg));
55         window.scrollTo(0, document.body.offsetHeight
56                         || document.body.scrollHeight);
57     };
58 };
59
60 Test.Harness.Browser.prototype.runTests = function () {
61         this._setupFrame(arguments);
62 }
63
64 Test.Harness.Browser.prototype._runTests = function () {
65     var files = this.args.file
66       ? typeof this.args.file == 'string' ? [this.args.file] : this.args.file
67       : arguments;
68     if (!files.length) return;
69     var outfiles = this.outFileNames(files);
70     var buffer = this.buffer;
71     var harness = this;
72     var ti = 0;
73     var start;
74     var node = document.getElementById('output');
75     var output = this._setupOutput();
76     var summaryOutput = this._setupSummary();
77     // These depend on how we're watching for a test to finish.
78     var finish = function () {}, runNext = function () {};
79
80     // This function handles most of the work of outputting results and
81     // running the next test, if there is one.
82     var runner = function () {
83         harness.outputResults(
84             buffer.Test.Builder.Test,
85             files[ti],
86             output,
87             harness.args
88         );
89
90         if (files[++ti]) {
91             output(outfiles[ti] + (harness.args.verbose ? Test.Harness.LF : ''));
92             buffer.location.href = files[ti] + "?" + start.getTime();
93             runNext();
94         } else {
95             harness.outputSummary(
96                 summaryOutput,
97                 new Date() - start
98              );
99             finish();
100         }
101     };
102
103     if (Object.watch) {
104         // We can use the cool watch method, and avoid setting timeouts!
105         // We just need to unwatch() when all tests are finished.
106         finish = function () { Test.Harness.unwatch('Done') };
107         Test.Harness.watch('Done', function (attr, prev, next) {
108             if (next < buffer.Test.Builder.Instances.length) return next;
109             runner();
110             return 0;
111         });
112     } else {
113         // Damn. We have to set timeouts. :-(
114         var wait = function () {
115             // Check Test.Harness.Done. If it's non-zero, then we know that
116             // the buffer is fully loaded, because it has incremented
117             // Test.Harness.Done.
118             if (Test.Harness.Done > 0
119                 && Test.Harness.Done >= buffer.Test.Builder.Instances.length)
120             {
121                 Test.Harness.Done = 0;
122                 runner();
123             } else {
124                 window.setTimeout(wait, harness.interval);
125             }
126         };
127         // We'll just have to set a timeout for the next test.
128         runNext = function () { window.setTimeout(wait, harness.interval); };
129         window.setTimeout(wait, this.interval);
130     }
131
132     // Now start the first test.
133     output(outfiles[ti] + (this.args.verbose ? Test.Harness.LF : ''));
134     start = new Date();
135     buffer.location.href = files[ti] + "?" + start.getTime(); // replace() doesn't seem to work.
136 };
137
138 // From "JavaScript: The Difinitive Guide 4ed", p 214.
139 Test.Harness.Browser.prototype.args = {};
140 var pairs = location.search.substring(1).split(",");
141 for (var i = 0; i < pairs.length; i++) {
142     var pos = pairs[i].indexOf('=');
143     if (pos == -1) continue;
144     var key = pairs[i].substring(0, pos);
145     var val = pairs[i].substring(pos + 1); 
146     if (Test.Harness.Browser.prototype.args[key]) {
147         if (typeof Test.Harness.Browser.prototype.args[key] == 'string') {
148             Test.Harness.Browser.prototype.args[key] =
149                 [Test.Harness.Browser.prototype.args[key]];
150         }
151         Test.Harness.Browser.prototype.args[key].push(unescape(val));
152     } else {
153         Test.Harness.Browser.prototype.args[key] = unescape(val);
154     }
155 }
156 delete pairs;
157
158 Test.Harness.Browser.prototype.formatFailures = function (fn) {
159     // XXX append new element for table and then populate it.
160     var failedStr = "Failed Test";
161     var middleStr = " Total Fail  Failed  ";
162     var listStr = "List of Failed";
163     var table = '<table style=""><tr><th>Failed Test</th><th>Total</th>'
164       + '<th>Fail</th><th>Failed</th></tr>';
165     for (var i = 0; i < this.failures.length; i++) {
166         var track = this.failures[i];
167         table += '<tr><td>' + track.fn + '</td>'
168           + '<td>' + track.total + '</td>'
169           + '<td>' + track.total - track.ok + '</td>'
170           + '<td>' + this._failList(track.failList) + '</td></tr>'
171     };
172     table += '</table>' + Test.Harness.LF;
173     var node = document.getElementById('summary');
174     node.innerHTML += table;
175     window.scrollTo(0, document.body.offsetHeight || document.body.scrollHeight);
176 };