2
dojo.provide("doh._browserRunner");
5
// FIXME: need to add prompting for monkey-do testing
6
// FIXME: need to implement progress bar
7
// FIXME: need to implement errors in progress bar
10
if(window.parent == window){
11
// we're the top-dog window.
13
// borrowed from Dojo, etc.
14
var byId = function(id){
15
return document.getElementById(id);
18
var _addOnEvt = function( type, // string
19
refOrName, // function or string
20
scope){ // object, defaults is window
22
if(!scope){ scope = window; }
24
var funcRef = refOrName;
25
if(typeof refOrName == "string"){
26
funcRef = scope[refOrName];
28
var enclosedFunc = function(){ return funcRef.apply(scope, arguments); };
30
if((window["dojo"])&&(type == "load")){
31
dojo.addOnLoad(enclosedFunc);
33
if(window["attachEvent"]){
34
window.attachEvent("on"+type, enclosedFunc);
35
}else if(window["addEventListener"]){
36
window.addEventListener(type, enclosedFunc, false);
37
}else if(document["addEventListener"]){
38
document.addEventListener(type, enclosedFunc, false);
44
// Over-ride or implement base runner.js-provided methods
46
var escapeXml = function(str){
48
// Adds escape sequences for special characters in XML: &<>"'
49
// Optionally skips escapes for single quotes
50
return str.replace(/&/gm, "&").replace(/</gm, "<").replace(/>/gm, ">").replace(/"/gm, """); // string
53
var _logBacklog = [], _loggedMsgLen = 0;
54
var sendToLogPane = function(args, skip){
56
for(var x=0; x<args.length; x++){
62
// workarounds for IE. Wheeee!!!
63
msg = msg.replace("\t", " ")
64
.replace(" ", " ")
65
.replace("\n", "<br> ");
67
_logBacklog.push(msg);
69
}else if(_logBacklog.length && !skip){
71
while((tm=_logBacklog.shift())){
72
sendToLogPane(tm, true);
75
var logBody=byId("logBody");
76
var tn = document.createElement("div");
78
//tn.id="logmsg_"+logBody.childNodes.length;
79
logBody.appendChild(tn);
83
var findTarget = function(n){
84
while(n && !n.getAttribute('_target')){
93
doh._jumpToLog = function(e){
96
var node = findTarget(e?e.target:window.event.srcElement);
100
var _t = Number(node.getAttribute('_target'));
101
var lb = byId("logBody");
102
if(_t>=lb.childNodes.length){
105
var t = lb.childNodes[_t];
108
//t.parentNode.parentNode is <div class="tabBody">, only it has a explicitly set background-color,
109
//all children of it are transparent
110
var bgColor = dojo.style(t.parentNode.parentNode,'backgroundColor');
111
//node.parentNode is the tr which has background-color set explicitly
112
var hicolor = dojo.style(node.parentNode,'backgroundColor');
113
var unhilight = dojo.animateProperty({
118
backgroundColor: { start:hicolor, end: bgColor }
121
t.style.backgroundColor="";
124
var hilight = dojo.animateProperty({
129
backgroundColor: { start:bgColor, end: hicolor }
139
doh._jumpToSuite = function(e){
140
var node = findTarget(e ? e.target : window.event.srcElement);
144
var _g = node.getAttribute('_target');
145
var gn = getGroupNode(_g);
152
doh._init = (function(oi){
154
var lb = byId("logBody");
156
// clear the console before each run
157
while(lb.firstChild){
158
lb.removeChild(lb.firstChild);
163
this._suiteCount = 0;
164
oi.apply(doh, arguments);
168
doh._setupGroupForRun = (function(os){
169
//overload _setupGroupForRun to record which log line to jump to when a suite is clicked
170
return function(groupName){
171
var tg = doh._groups[groupName];
172
doh._curTestCount = tg.length;
173
doh._curGroupCount = 1;
174
var gn = getGroupNode(groupName);
176
//two lines will be added, scroll the second line into view
177
gn.getElementsByTagName("td")[2].setAttribute('_target',_loggedMsgLen+1);
179
os.apply(doh,arguments);
181
})(doh._setupGroupForRun);
183
doh._report = (function(or){
184
//overload _report to insert a tfoot
186
var tb = byId("testList");
188
var tfoots=tb.getElementsByTagName('tfoot');
190
tb.removeChild(tfoots[0]);
192
var foot = tb.createTFoot();
193
var row = foot.insertRow(-1);
194
row.className = 'inProgress';
195
var cell=row.insertCell(-1);
197
cell.innerHTML="Result";
198
cell = row.insertCell(-1);
199
cell.innerHTML=this._testCount+" tests in "+this._groupCount+" groups /<span class='failure'>"+this._errorCount+"</span> errors, <span class='failure'>"+this._failureCount+"</span> failures";
200
cell.setAttribute('_target',_loggedMsgLen+1);
201
row.insertCell(-1).innerHTML=doh._totalTime+"ms";
203
or.apply(doh,arguments);
207
if(this["opera"] && opera.postError){
208
doh.debug = function(){
210
for(var x=0; x<arguments.length; x++){
211
msg += " "+arguments[x];
213
sendToLogPane([msg]);
214
opera.postError("DEBUG:"+msg);
216
}else if(window["console"]){
218
doh.debug = function(){
219
sendToLogPane.call(window, arguments);
220
console.debug.apply(console, arguments);
222
}else if(console.info){
223
doh.debug = function(){
224
sendToLogPane.call(window, arguments);
225
console.info.apply(console, arguments);
228
doh.debug = function(){
230
for(var x=0; x<arguments.length; x++){
231
msg += " "+arguments[x];
233
sendToLogPane([msg]);
234
console.log("DEBUG:"+msg);
238
doh.debug = function(){
239
sendToLogPane.call(window, arguments);
244
var groupTemplate = null;
245
var testTemplate = null;
249
var _groupTogglers = {};
251
var _getGroupToggler = function(group, toggle){
252
if(_groupTogglers[group]){ return _groupTogglers[group]; }
254
return (_groupTogglers[group] = function(evt, forceOpen){
255
var nodes = groupNodes[group].__items;
257
if(rolledUp||forceOpen){
259
for(x=0; x<nodes.length; x++){
260
nodes[x].style.display = "";
262
toggle.innerHTML = "▼";
265
for(x=0; x<nodes.length; x++){
266
nodes[x].style.display = "none";
268
toggle.innerHTML = "►";
273
var addGroupToList = function(group){
274
if(!byId("testList")){ return; }
275
var tb = byId("testList").tBodies[0];
276
var tg = groupTemplate.cloneNode(true);
277
var tds = tg.getElementsByTagName("td");
279
toggle.onclick = _getGroupToggler(group, toggle);
280
var cb = tds[1].getElementsByTagName("input")[0];
282
cb.onclick = function(evt){
283
doh._groups[group].skip = (!this.checked);
285
tds[2].innerHTML = "<div class='testGroupName'>"+group+"</div><div style='width:0;'> </div>";
286
tds[3].innerHTML = "";
292
var addFixtureToList = function(group, fixture){
293
if(!testTemplate){ return; }
294
var cgn = groupNodes[group];
295
if(!cgn["__items"]){ cgn.__items = []; }
296
var tn = testTemplate.cloneNode(true);
297
var tds = tn.getElementsByTagName("td");
299
tds[2].innerHTML = fixture.name;
300
tds[3].innerHTML = "";
302
var nn = (cgn.__lastFixture||cgn.__groupNode).nextSibling;
304
nn.parentNode.insertBefore(tn, nn);
306
cgn.__groupNode.parentNode.appendChild(tn);
308
// FIXME: need to make group display toggleable!!
309
tn.style.display = "none";
310
cgn.__items.push(tn);
311
return (cgn.__lastFixture = tn);
314
var getFixtureNode = function(group, fixture){
315
if(groupNodes[group]){
316
return groupNodes[group][fixture.name];
321
var getGroupNode = function(group){
322
if(groupNodes[group]){
323
return groupNodes[group].__groupNode;
328
var updateBacklog = [];
329
doh._updateTestList = function(group, fixture, unwindingBacklog){
331
if(group && fixture){
332
updateBacklog.push([group, fixture]);
335
}else if(updateBacklog.length && !unwindingBacklog){
337
while((tr=updateBacklog.shift())){
338
doh._updateTestList(tr[0], tr[1], true);
341
if(group && fixture){
342
if(!groupNodes[group]){
343
groupNodes[group] = {
344
"__groupNode": addGroupToList(group)
347
if(!groupNodes[group][fixture.name]){
348
groupNodes[group][fixture.name] = addFixtureToList(group, fixture)
353
doh._testRegistered = doh._updateTestList;
355
doh._groupStarted = function(group){
356
if(this._suiteCount == 0){
357
this._runedSuite = 0;
358
this._currentGlobalProgressBarWidth = 0;
359
this._suiteCount = this._testCount;
361
// console.debug("_groupStarted", group);
362
if(doh._inGroup != group){
363
doh._groupTotalTime = 0;
365
doh._inGroup = group;
368
var gn = getGroupNode(group);
370
gn.className = "inProgress";
374
doh._groupFinished = function(group, success){
375
// console.debug("_groupFinished", group);
376
var gn = getGroupNode(group);
377
if(gn && doh._inGroup == group){
378
doh._totalTime += doh._groupTotalTime;
379
gn.getElementsByTagName("td")[3].innerHTML = doh._groupTotalTime+"ms";
380
gn.getElementsByTagName("td")[2].lastChild.className = "";
383
var failure = doh._updateGlobalProgressBar(this._runedSuite/this._groupCount,success,group);
384
gn.className = failure ? "failure" : "success";
386
doh._currentGlobalProgressBarWidth = parseInt(this._runedSuite/this._groupCount*10000)/100;
387
//byId("progressOuter").style.width = parseInt(this._runedSuite/this._suiteCount*100)+"%";
389
if(doh._inGroup == group){
390
this.debug("Total time for GROUP \"",group,"\" is ",doh._groupTotalTime,"ms");
394
doh._testStarted = function(group, fixture){
395
// console.debug("_testStarted", group, fixture.name);
396
var fn = getFixtureNode(group, fixture);
398
fn.className = "inProgress";
403
var _playSound = function(name){
404
if(byId("hiddenAudio") && byId("audio") && byId("audio").checked){
405
// console.debug("playing:", name);
406
var nt = _nameTimes[name];
407
// only play sounds once every second or so
408
if((!nt)||(((new Date)-nt) > 700)){
409
_nameTimes[name] = new Date();
410
var tc = document.createElement("span");
411
byId("hiddenAudio").appendChild(tc);
412
tc.innerHTML = '<embed src="_sounds/'+name+'.wav" autostart="true" loop="false" hidden="true" width="1" height="1"></embed>';
417
doh._updateGlobalProgressBar = function(p,success,group){
418
var outerContainer=byId("progressOuter");
420
var gdiv=outerContainer.childNodes[doh._runedSuite-1];
422
gdiv=document.createElement('div');
423
outerContainer.appendChild(gdiv);
424
gdiv.className='success';
425
gdiv.setAttribute('_target',group);
427
if(!success && !gdiv._failure){
429
gdiv.className='failure';
431
gdiv.setAttribute('title','failed group '+group);
434
var tp=parseInt(p*10000)/100;
435
gdiv.style.width = (tp-doh._currentGlobalProgressBarWidth)+"%";
436
return gdiv._failure;
438
doh._testFinished = function(group, fixture, success){
439
var fn = getFixtureNode(group, fixture);
440
var elapsed = fixture.endTime-fixture.startTime;
442
fn.getElementsByTagName("td")[3].innerHTML = elapsed+"ms";
443
fn.className = (success) ? "success" : "failure";
444
fn.getElementsByTagName("td")[2].setAttribute('_target', _loggedMsgLen);
447
var gn = getGroupNode(group);
449
gn.className = "failure";
450
_getGroupToggler(group)(null, true);
454
if(doh._inGroup == group){
455
var gn = getGroupNode(group);
457
if(gn && doh._curTestCount){
458
var p = doh._runed/doh._curTestCount;
459
var groupfail = this._updateGlobalProgressBar((doh._runedSuite+p-1)/doh._groupCount,success,group);
461
var pbar = gn.getElementsByTagName("td")[2].lastChild;
462
pbar.className = groupfail?"failure":"success";
463
pbar.style.width = parseInt(p*100)+"%";
464
gn.getElementsByTagName("td")[3].innerHTML = parseInt(p*10000)/100+"%";
467
this._groupTotalTime += elapsed;
468
this.debug((success ? "PASSED" : "FAILED"), "test:", fixture.name, elapsed, 'ms');
471
// FIXME: move implementation to _browserRunner?
472
doh.registerUrl = function( /*String*/ group,
474
/*Integer*/ timeout){
475
var tg = new String(group);
476
this.register(group, {
479
doh.currentGroupName = tg;
480
doh.currentGroup = this;
481
doh.currentUrl = url;
482
this.d = new doh.Deferred();
483
doh.currentTestDeferred = this.d;
485
byId("testBody").src = url;
487
timeout: timeout||10000, // 10s
488
// timeout: timeout||1000, // 10s
490
// FIXME: implement calling into the url's groups here!!
493
tearDown: function(){
494
doh.currentGroupName = null;
495
doh.currentGroup = null;
496
doh.currentTestDeferred = null;
497
doh.currentUrl = null;
498
// this.d.errback(false);
499
// byId("testBody").src = "about:blank";
506
// Utility code for runner.html
508
// var isSafari = navigator.appVersion.indexOf("Safari") >= 0;
510
var _showTab = function(toShow, toHide){
511
// FIXME: I don't like hiding things this way.
512
byId(toHide).style.display = "none";
513
with(byId(toShow).style){
519
doh.showTestPage = function(){
520
_showTab("testBody", "logBody");
523
doh.showLogPage = function(){
524
_showTab("logBody", "testBody");
528
doh.toggleRunAll = function(){
529
// would be easier w/ query...sigh
531
if(!byId("testList")){ return; }
532
var tb = byId("testList").tBodies[0];
533
var inputs = tb.getElementsByTagName("input");
535
while((tn=inputs[x++])){
537
doh._groups[tn.group].skip = (!runAll);
541
var listHeightTimer = null;
542
var setListHeight = function(){
544
clearTimeout(listHeightTimer);
546
var tl = byId("testList");
548
listHeightTimer = setTimeout(function(){
549
tl.style.display = "none";
550
tl.style.display = "";
555
_addOnEvt("resize", setListHeight);
556
_addOnEvt("load", setListHeight);
557
_addOnEvt("load", function(){
558
if(loaded){ return; }
560
groupTemplate = byId("groupTemplate");
562
// make sure we've got an ammenable DOM structure
565
groupTemplate.parentNode.removeChild(groupTemplate);
566
groupTemplate.style.display = "";
567
testTemplate = byId("testTemplate");
568
testTemplate.parentNode.removeChild(testTemplate);
569
testTemplate.style.display = "";
570
doh._updateTestList();
575
// let robot code run if it gets to this first
576
var __onEnd = doh._onEnd;
577
doh._onEnd = function(){
578
__onEnd.apply(doh, arguments);
579
if(doh._failureCount == 0){
580
doh.debug("WOOHOO!!");
581
_playSound("woohoo");
583
console.debug("doh._failureCount:", doh._failureCount);
590
// make sure we've got an amenable DOM structure
593
var isRunning = false;
594
var toggleRunning = function(){
595
// ugg, this would be so much better w/ dojo.query()
597
byId("play").style.display = byId("pausedMsg").style.display = "";
598
byId("playingMsg").style.display = byId("pause").style.display = "none";
601
byId("play").style.display = byId("pausedMsg").style.display = "none";
602
byId("playingMsg").style.display = byId("pause").style.display = "";
606
doh.run = (function(oldRun){
608
if(!doh._currentGroup){
611
return oldRun.apply(doh, arguments);
614
var btns = byId("toggleButtons").getElementsByTagName("span");
616
while((node=btns[idx++])){
617
node.onclick = toggleRunning;
622
// we're in an iframe environment. Time to mix it up a bit.
624
_doh = window.parent.doh;
625
var _thisGroup = _doh.currentGroupName;
626
var _thisUrl = _doh.currentUrl;
628
doh._testRegistered = function(group, tObj){
629
_doh._updateTestList(_thisGroup, tObj);
631
doh._onEnd = function(){
632
_doh._errorCount += doh._errorCount;
633
_doh._failureCount += doh._failureCount;
634
_doh._testCount += doh._testCount;
635
// should we be really adding raw group counts?
636
//_doh._groupCount += doh._groupCount;
637
_doh.currentTestDeferred.callback(true);
639
var otr = doh._getTestObj;
640
doh._getTestObj = function(){
641
var tObj = otr.apply(doh, arguments);
642
tObj.name = _thisUrl+"::"+arguments[0]+"::"+tObj.name;
645
doh.debug = doh.hitch(_doh, "debug");
646
doh.registerUrl = doh.hitch(_doh, "registerUrl");
647
doh._testStarted = function(group, fixture){
648
_doh._testStarted(_thisGroup, fixture);
650
doh._testFinished = function(g, f, s){
651
_doh._testFinished(_thisGroup, f, s);
653
doh._groupStarted = function(g){
654
if(!this._setParent){
655
_doh._curTestCount = this._testCount;
656
_doh._curGroupCount = this._groupCount;
657
this._setParent = true;
660
doh._report = function(){};