Bug 461810 - Video content menu should operate on .currentSrc, not .src. r=gavin
[wine-gecko.git] / js / tests / browser.js
blobfdd6bf9e71de78dea8d4ec7563e3221806f94760
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 var gPageCompleted;
39 var GLOBAL = this + '';
41 function htmlesc(str) {
42 if (str == '<')
43 return '&lt;';
44 if (str == '>')
45 return '&gt;';
46 if (str == '&')
47 return '&amp;';
48 return str;
51 function DocumentWrite(s)
53 try
55 var msgDiv = document.createElement('div');
56 msgDiv.innerHTML = s;
57 document.body.appendChild(msgDiv);
58 msgDiv = null;
60 catch(excp)
62 document.write(s + '<br>\n');
66 function print() {
67 var s = '';
68 var a;
69 for (var i = 0; i < arguments.length; i++)
71 a = arguments[i];
72 s += String(a) + ' ';
75 if (typeof dump == 'function')
77 dump( s + '\n');
80 s = s.replace(/[<>&]/g, htmlesc);
82 DocumentWrite(s);
85 function writeHeaderToLog( string ) {
86 string = String(string);
88 if (typeof dump == 'function')
90 dump( string + '\n');
93 string = string.replace(/[<>&]/g, htmlesc);
95 DocumentWrite( "<h2>" + string + "</h2>" );
98 function writeFormattedResult( expect, actual, string, passed ) {
99 string = String(string);
101 if (typeof dump == 'function')
103 dump( string + '\n');
106 string = string.replace(/[<>&]/g, htmlesc);
108 var s = "<tt>"+ string ;
109 s += "<b>" ;
110 s += ( passed ) ? "<font color=#009900> &nbsp;" + PASSED
111 : "<font color=#aa0000>&nbsp;" + FAILED + expect + "</tt>";
113 DocumentWrite( s + "</font></b></tt><br>" );
114 return passed;
117 window.onerror = function (msg, page, line)
119 optionsPush();
121 if (typeof DESCRIPTION == 'undefined')
123 DESCRIPTION = 'Unknown';
125 if (typeof EXPECTED == 'undefined')
127 EXPECTED = 'Unknown';
130 var testcase = new TestCase(gTestfile, DESCRIPTION, EXPECTED, "error");
132 if (document.location.href.indexOf('-n.js') != -1)
134 // negative test
135 testcase.passed = true;
138 testcase.reason = page + ':' + line + ': ' + msg;
140 reportFailure(msg);
142 optionsReset();
145 function gc()
147 // Thanks to igor.bukanov@gmail.com
148 for (var i = 0; i != 4e6; ++i)
150 var tmp = i + 0.1;
154 function jsdgc()
158 // Thanks to dveditz
159 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
160 var jsdIDebuggerService = Components.interfaces.jsdIDebuggerService;
161 var service = Components.classes['@mozilla.org/js/jsd/debugger-service;1'].
162 getService(jsdIDebuggerService);
163 service.GC();
165 catch(ex)
167 print('gc: ' + ex);
171 function quit()
175 function Preferences(aPrefRoot)
179 this.orig = {};
180 this.privs = 'UniversalXPConnect UniversalPreferencesRead ' +
181 'UniversalPreferencesWrite';
183 if (typeof netscape != 'undefined' &&
184 'security' in netscape &&
185 'PrivilegeManager' in netscape.security &&
186 'enablePrivilege' in netscape.security.PrivilegeManager)
188 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
190 var nsIPrefService = Components.interfaces.nsIPrefService;
191 var nsIPrefBranch = Components.interfaces.nsIPrefBranch;
192 var nsPrefService_CONTRACTID = "@mozilla.org/preferences-service;1";
194 this.prefRoot = aPrefRoot;
195 this.prefService = Components.classes[nsPrefService_CONTRACTID].
196 getService(nsIPrefService);
197 this.prefBranch = this.prefService.getBranch(aPrefRoot).
198 QueryInterface(Components.interfaces.nsIPrefBranch2);
201 catch(ex)
207 function Preferences_getPrefRoot()
209 var root;
213 if (typeof netscape != 'undefined' &&
214 'security' in netscape &&
215 'PrivilegeManager' in netscape.security &&
216 'enablePrivilege' in netscape.security.PrivilegeManager)
218 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
221 root = this.prefBranch.root;
223 catch(ex)
226 return root;
229 function Preferences_getPref(aPrefName)
231 var value;
234 if (typeof netscape != 'undefined' &&
235 'security' in netscape &&
236 'PrivilegeManager' in netscape.security &&
237 'enablePrivilege' in netscape.security.PrivilegeManager)
239 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
240 value = this.prefBranch.getBoolPref(aPrefName);
243 catch(ex)
246 return value;
249 function Preferences_getBoolPref(aPrefName)
251 var value;
254 if (typeof netscape != 'undefined' &&
255 'security' in netscape &&
256 'PrivilegeManager' in netscape.security &&
257 'enablePrivilege' in netscape.security.PrivilegeManager)
259 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
260 value = this.prefBranch.getBoolPref(aPrefName);
263 catch(ex)
266 return value;
269 function Preferences_getIntPref(aPrefName)
271 var value;
274 if (typeof netscape != 'undefined' &&
275 'security' in netscape &&
276 'PrivilegeManager' in netscape.security &&
277 'enablePrivilege' in netscape.security.PrivilegeManager)
279 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
280 value = this.prefBranch.getIntPref(aPrefName);
283 catch(ex)
286 return value;
289 function Preferences_getCharPref(aPrefName)
291 var value;
294 if (typeof netscape != 'undefined' &&
295 'security' in netscape &&
296 'PrivilegeManager' in netscape.security &&
297 'enablePrivilege' in netscape.security.PrivilegeManager)
299 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
300 value = this.prefBranch.getCharPref(aPrefName);
303 catch(ex)
306 return value;
309 function Preferences_setPref(aPrefName, aPrefValue)
311 var value;
315 if (typeof netscape != 'undefined' &&
316 'security' in netscape &&
317 'PrivilegeManager' in netscape.security &&
318 'enablePrivilege' in netscape.security.PrivilegeManager)
320 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
322 if (typeof this.orig[aPrefName] == 'undefined')
324 this.orig[aPrefName] = this.getPref(aPrefName);
327 value = this.prefBranch.setBoolPref(aPrefName, Boolean(aPrefValue));
330 catch(ex)
335 function Preferences_setBoolPref(aPrefName, aPrefValue)
337 var value;
341 if (typeof netscape != 'undefined' &&
342 'security' in netscape &&
343 'PrivilegeManager' in netscape.security &&
344 'enablePrivilege' in netscape.security.PrivilegeManager)
346 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
348 if (typeof this.orig[aPrefName] == 'undefined')
350 this.orig[aPrefName] = this.getBoolPref(aPrefName);
353 value = this.prefBranch.setBoolPref(aPrefName, Boolean(aPrefValue));
356 catch(ex)
361 function Preferences_setIntPref(aPrefName, aPrefValue)
363 var value;
367 if (typeof netscape != 'undefined' &&
368 'security' in netscape &&
369 'PrivilegeManager' in netscape.security &&
370 'enablePrivilege' in netscape.security.PrivilegeManager)
372 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
374 if (typeof this.orig[aPrefName] == 'undefined')
376 this.orig[aPrefName] = this.getIntPref(aPrefName);
379 value = this.prefBranch.setIntPref(aPrefName, Number(aPrefValue));
382 catch(ex)
387 function Preferences_setCharPref(aPrefName, aPrefValue)
389 var value;
393 if (typeof netscape != 'undefined' &&
394 'security' in netscape &&
395 'PrivilegeManager' in netscape.security &&
396 'enablePrivilege' in netscape.security.PrivilegeManager)
398 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
400 if (typeof this.orig[aPrefName] == 'undefined')
402 this.orig[aPrefName] = this.getCharPref(aPrefName);
405 value = this.prefBranch.setCharPref(aPrefName, String(aPrefValue));
408 catch(ex)
413 function Preferences_resetPref(aPrefName)
417 if (typeof netscape != 'undefined' &&
418 'security' in netscape &&
419 'PrivilegeManager' in netscape.security &&
420 'enablePrivilege' in netscape.security.PrivilegeManager)
422 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
424 if (aPrefName in this.orig)
426 if (typeof this.orig[aPrefName] == 'undefined')
428 this.clearPref(aPrefName);
430 else
432 this.setPref(aPrefName, this.orig[aPrefName]);
437 catch(ex)
442 function Preferences_resetAllPrefs()
446 var prefName;
447 var prefValue;
449 if (typeof netscape != 'undefined' &&
450 'security' in netscape &&
451 'PrivilegeManager' in netscape.security &&
452 'enablePrivilege' in netscape.security.PrivilegeManager)
454 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
455 for (prefName in this.orig)
457 this.setPref(prefName, this.orig[prefName]);
461 catch(ex)
466 function Preferences_clearPref(aPrefName)
470 if (typeof netscape != 'undefined' &&
471 'security' in netscape &&
472 'PrivilegeManager' in netscape.security &&
473 'enablePrivilege' in netscape.security.PrivilegeManager)
475 netscape.security.PrivilegeManager.enablePrivilege(this.privs);
476 this.prefBranch.clearUserPref(aPrefName);
479 catch(ex)
484 Preferences.prototype.getPrefRoot = Preferences_getPrefRoot;
485 Preferences.prototype.getPref = Preferences_getPref;
486 Preferences.prototype.getBoolPref = Preferences_getBoolPref;
487 Preferences.prototype.getIntPref = Preferences_getIntPref;
488 Preferences.prototype.getCharPref = Preferences_getCharPref;
489 Preferences.prototype.setPref = Preferences_setPref;
490 Preferences.prototype.setBoolPref = Preferences_setBoolPref;
491 Preferences.prototype.setIntPref = Preferences_setIntPref;
492 Preferences.prototype.setCharPref = Preferences_setCharPref;
493 Preferences.prototype.resetAllPrefs = Preferences_resetAllPrefs;
494 Preferences.prototype.resetPref = Preferences_resetPref;
495 Preferences.prototype.clearPref = Preferences_clearPref;
497 function options(aOptionName)
499 // return value of options() is a comma delimited list
500 // of the previously set values
502 var value = '';
503 for (var optionName in options.currvalues)
505 value += optionName + ',';
507 if (value)
509 value = value.substring(0, value.length-1);
512 if (aOptionName)
514 if (options.currvalues[aOptionName])
516 // option is set, toggle it to unset
517 delete options.currvalues[aOptionName];
518 options.preferences.setPref(aOptionName, false);
520 else
522 // option is not set, toggle it to set
523 options.currvalues[aOptionName] = true;
524 options.preferences.setPref(aOptionName, true);
528 return value;
531 function optionsInit() {
533 // hash containing the set options
534 options.currvalues = {strict: '',
535 werror: '',
536 atline: '',
537 xml: '',
538 relimit: '',
539 anonfunfux: ''
542 // record initial values to support resetting
543 // options to their initial values
544 options.initvalues = {};
546 // record values in a stack to support pushing
547 // and popping options
548 options.stackvalues = [];
550 options.preferences = new Preferences('javascript.options.');
552 for (var optionName in options.currvalues)
554 if (!options.preferences.getPref(optionName))
556 delete options.currvalues[optionName];
558 else
560 options.initvalues[optionName] = '';
565 function gczeal(z)
567 var javascriptoptions = new Preferences('javascript.options.');
568 javascriptoptions.setIntPref('gczeal', Number(z));
571 function jit(on)
573 var javascriptoptions = new Preferences('javascript.options.jit.');
574 if (on)
576 javascriptoptions.setBoolPref('content', true);
577 javascriptoptions.setBoolPref('chrome', true);
579 else
581 javascriptoptions.setBoolPref('content', false);
582 javascriptoptions.setBoolPref('chrome', false);
586 var gVersion = 150;
588 function jsTestDriverBrowserInit()
590 if (typeof dump != 'function')
592 dump = print;
595 optionsInit();
596 optionsClear();
598 if (document.location.search.indexOf('?') != 0)
600 // not called with a query string
601 return;
604 var re = /test=([^;]+);language=(language|type);([a-zA-Z0-9.=;\/]+)/;
605 var matches = re.exec(document.location.search);
607 // testpath http://machine/path-to-suite/sub-suite/test.js
608 var testpath = matches[1];
609 var attribute = matches[2];
610 var value = matches[3];
612 if (testpath)
614 testpath = decodeURIComponent(testpath);
615 gTestPath = testpath;
618 var ise4x = /e4x\//.test(testpath);
620 var gczealmatches = /gczeal=([0-9]*)/.exec(document.location.search);
622 if (gczealmatches)
624 var zeal = Number(gczealmatches[1]);
625 gczeal(zeal);
628 var jitmatches = /;jit/.exec(document.location.search);
630 if (jitmatches)
632 jit(true);
635 var versionmatches = /version=([.0-9]*)/.exec(value);
637 if (!versionmatches)
639 value = 'text/javascript;version=';
640 if (testpath.match(/^js1_6/))
642 gVersion = 160;
643 value += '1.6';
645 else if (testpath.match(/^js1_7/))
647 gVersion = 170;
648 value += '1.7';
650 else if (testpath.match(/^js1_8/))
652 gVersion = 180;
653 value += '1.8';
655 else if (testpath.match(/^js1_8_1/))
657 gVersion = 180;
658 value += '1.8';
660 else
662 gVersion = 150;
663 value += '1.5';
666 else
668 gVersion = 10*parseInt(versionmatches[1].replace(/\./g, ''));
671 var testpathparts = testpath.split(/\//);
673 if (testpathparts.length < 3)
675 // must have at least suitepath/subsuite/testcase.js
676 return;
678 var suitepath = testpathparts.slice(0,testpathparts.length-2).join('/');
679 var subsuite = testpathparts[testpathparts.length - 2];
680 var test = testpathparts[testpathparts.length - 1];
682 outputscripttag(suitepath + '/shell.js', attribute, value,
683 ise4x);
684 outputscripttag(suitepath + '/browser.js', attribute, value,
685 ise4x);
686 outputscripttag(suitepath + '/' + subsuite + '/shell.js', attribute, value,
687 ise4x);
688 outputscripttag(suitepath + '/' + subsuite + '/browser.js', attribute, value,
689 ise4x);
690 outputscripttag(suitepath + '/' + subsuite + '/' + test, attribute, value,
691 ise4x);
693 document.write('<title>' + suitepath + '/' + subsuite + '/' + test +
694 '<\/title>');
696 outputscripttag('js-test-driver-end.js', attribute, value,
697 false);
698 return;
701 function outputscripttag(src, attribute, value, ise4x)
703 if (!src)
705 return;
708 var s = '<script src="' + src + '" ';
710 if (ise4x)
712 if (attribute == 'type')
714 value += ';e4x=1 ';
716 else
718 s += ' type="text/javascript';
719 if (gVersion != 150)
721 s += ';version=' + gVersion/100;
723 s += ';e4x=1" ';
727 s += attribute + '="' + value + '"><\/script>';
729 document.write(s);
732 function jsTestDriverEnd()
734 // gDelayTestDriverEnd is used to
735 // delay collection of the test result and
736 // signal to Spider so that tests can continue
737 // to run after page load has fired. They are
738 // responsible for setting gDelayTestDriverEnd = true
739 // then when completed, setting gDelayTestDriverEnd = false
740 // then calling jsTestDriverEnd()
742 if (gDelayTestDriverEnd)
744 return;
747 window.onerror = null;
751 var javascriptoptions = new Preferences('javascript.options.');
752 javascriptoptions.clearPref('gczeal');
753 optionsReset();
755 catch(ex)
757 dump('jsTestDriverEnd ' + ex);
760 if (window.opener && window.opener.runNextTest)
762 if (window.opener.reportCallBack)
764 window.opener.reportCallBack(window.opener.gWindow);
766 setTimeout('window.opener.runNextTest()', 250);
768 else
770 for (var i = 0; i < gTestcases.length; i++)
772 gTestcases[i].dump();
775 // tell Spider page is complete
776 gPageCompleted = true;
780 jsTestDriverBrowserInit();