xpfe/test/winopen.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpfe/test/winopen.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,244 @@
     1.4 +// target for window.open()
     1.5 +const KID_URL       = "child-window.html";
     1.6 +
     1.7 +// formats final results
     1.8 +const SERVER_URL    = "http://jrgm.mcom.com/cgi-bin/window-open-2.0/openreport.pl";
     1.9 +
    1.10 +// let system settle between each window.open
    1.11 +const OPENER_DELAY  = 1000;   
    1.12 +
    1.13 +// three phases: single open/close; overlapped open/close; open-all/close-all
    1.14 +var PHASE_ONE     = 10; 
    1.15 +var PHASE_TWO     = 0; 
    1.16 +var PHASE_THREE   = 0; 
    1.17 +
    1.18 +// keep this many windows concurrently open during overlapped phase
    1.19 +var OVERLAP_COUNT = 3;
    1.20 +
    1.21 +// repeat three phases CYCLES times    
    1.22 +var CYCLES        = 1;  
    1.23 +
    1.24 +// autoclose flag
    1.25 +var AUTOCLOSE = 1;
    1.26 +
    1.27 +// Chrome url for child windows.
    1.28 +var KID_CHROME   = null;
    1.29 +var SAVED_CHROME = null;
    1.30 +
    1.31 +// URL options and correspnding vars.
    1.32 +const options = [ [ "phase1", "PHASE_ONE", false ],
    1.33 +                  [ "phase2", "PHASE_TWO", false ],
    1.34 +                  [ "phase3", "PHASE_THREE", false ],
    1.35 +                  [ "overlap", "OVERLAP_COUNT", false ],
    1.36 +                  [ "cycles", "CYCLES", false ],
    1.37 +                  [ "chrome", "KID_CHROME", true ],
    1.38 +                  [ "close", "AUTOCLOSE", false ] ];
    1.39 +
    1.40 +// Note: You can attach search options to the url for this file to control
    1.41 +// any of the options in the array above.  E.g., specifying
    1.42 +// mozilla -chrome "file:///D|/mozilla/xpfe/test/winopen.xul?phase1=16&close=0"
    1.43 +// will run this script with PHASE_ONE=16 and AUTOCLOSE=0.
    1.44 +//
    1.45 +// On Win32, you must enclose the -chrome option in quotes in order pass funny Win32 shell
    1.46 +// characters such as '&' or '|'!
    1.47 +
    1.48 +var opts = window.location.search.substring(1).split( '&' );
    1.49 +for ( opt in opts ) {
    1.50 +    for ( var i in options ) {
    1.51 +        if ( opts[opt].indexOf( options[i][0]+"=" ) == 0 ) {
    1.52 +            var newVal = opts[opt].split( '=' )[ 1 ];
    1.53 +            // wrap with quotes, if required.
    1.54 +            if ( options[i][2] ) {
    1.55 +                newVal = '"' + newVal + '"';
    1.56 +            }
    1.57 +            eval( options[i][1] + "=" + newVal + ";" );
    1.58 +        }
    1.59 +    }
    1.60 +}
    1.61 +
    1.62 +var prefs = null;
    1.63 +
    1.64 +if ( KID_CHROME ) {
    1.65 +    // Reset browser.chromeURL so it points to KID_CHROME.
    1.66 +    // This will cause window.open in openWindow to open that chrome.
    1.67 +    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    1.68 +    prefs = Components.classes["@mozilla.org/preferences-service;1"]
    1.69 +              .getService( Components.interfaces.nsIPrefBranch );
    1.70 +    SAVED_CHROME = prefs.getCharPref( "browser.chromeURL" );
    1.71 +    prefs.setCharPref( "browser.chromeURL", KID_CHROME );
    1.72 +}
    1.73 +
    1.74 +const CYCLE_SIZE    = PHASE_ONE + PHASE_TWO + PHASE_THREE;
    1.75 +const MAX_INDEX     = CYCLE_SIZE * CYCLES;  // total number of windows to open
    1.76 +
    1.77 +var windowList      = [];   // handles to opened windows
    1.78 +var startingTimes   = [];   // time that window.open is called
    1.79 +var openingTimes    = [];   // time that child window took to fire onload
    1.80 +var closingTimes    = [];   // collect stats for case of closing >1 windows
    1.81 +var currentIndex    = 0;       
    1.82 +
    1.83 +
    1.84 +function childIsOpen(aTime) {
    1.85 +    openingTimes[currentIndex] = aTime - startingTimes[currentIndex];
    1.86 +    updateDisplay(currentIndex, openingTimes[currentIndex]);
    1.87 +    reapWindows(currentIndex);
    1.88 +    currentIndex++;
    1.89 +    if (currentIndex < MAX_INDEX)
    1.90 +        scheduleNextWindow();
    1.91 +    else
    1.92 +        window.setTimeout(reportResults, OPENER_DELAY);
    1.93 +}
    1.94 +
    1.95 +
    1.96 +function updateDisplay(index, time) {
    1.97 +    var formIndex = document.getElementById("formIndex");
    1.98 +    if (formIndex) 
    1.99 +        formIndex.setAttribute("value", index+1);
   1.100 +    var formTime  = document.getElementById("formTime");
   1.101 +    if (formTime) 
   1.102 +        formTime.setAttribute("value", time);
   1.103 +}
   1.104 +
   1.105 +
   1.106 +function scheduleNextWindow() {
   1.107 +    window.setTimeout(openWindow, OPENER_DELAY);
   1.108 +}
   1.109 +
   1.110 +
   1.111 +function closeOneWindow(aIndex) {
   1.112 +    var win = windowList[aIndex];
   1.113 +    // no-op if window is already closed
   1.114 +    if (win && !win.closed) {
   1.115 +	win.close();
   1.116 +	windowList[aIndex] = null;
   1.117 +    }
   1.118 +}    
   1.119 +
   1.120 +
   1.121 +function closeAllWindows(aRecordTimeToClose) {
   1.122 +    var timeToClose = (new Date()).getTime();
   1.123 +    var count = 0;
   1.124 +    for (var i = 0; i < windowList.length; i++) {
   1.125 +	if (windowList[i])
   1.126 +	    count++;
   1.127 +	closeOneWindow(i);
   1.128 +    }
   1.129 +    if (aRecordTimeToClose && count > 0) {
   1.130 +        timeToClose = (new Date()).getTime() - timeToClose;
   1.131 +        closingTimes.push(parseInt(timeToClose/count));
   1.132 +    }
   1.133 +}    
   1.134 +
   1.135 +
   1.136 +// close some, none, or all open windows in the list
   1.137 +function reapWindows() {
   1.138 +    var modIndex = currentIndex % CYCLE_SIZE;
   1.139 +    if (modIndex < PHASE_ONE-1) {
   1.140 +	// first phase in each "cycle", are single open/close sequences
   1.141 +	closeOneWindow(currentIndex);
   1.142 +    } 
   1.143 +    else if (PHASE_ONE-1 <= modIndex && modIndex < PHASE_ONE+PHASE_TWO-1) {
   1.144 +	// next phase in each "cycle", keep N windows concurrently open
   1.145 +	closeOneWindow(currentIndex - OVERLAP_COUNT);
   1.146 +    }
   1.147 +    else if (modIndex == PHASE_ONE+PHASE_TWO-1) {
   1.148 +	// end overlapping windows cycle; close all windows
   1.149 +	closeAllWindows(false);
   1.150 +    }
   1.151 +    else if (PHASE_ONE+PHASE_TWO <= modIndex && modIndex < CYCLE_SIZE-1) {
   1.152 +	// do nothing; keep adding windows
   1.153 +    }
   1.154 +    else if (modIndex == CYCLE_SIZE-1) {
   1.155 +	// end open-all/close-all phase; close windows, recording time to close
   1.156 +	closeAllWindows(true);
   1.157 +    }
   1.158 +}
   1.159 +
   1.160 +function calcMedian( numbers ) {
   1.161 +    if ( numbers.length == 0 ) {
   1.162 +        return 0;
   1.163 +    } else if ( numbers.length == 1 ) {
   1.164 +        return numbers[0];
   1.165 +    } else if ( numbers.length == 2 ) {
   1.166 +        return ( numbers[0] + numbers[1] ) / 2;
   1.167 +    } else {
   1.168 +        numbers.sort( function (a,b){ return a-b; } );
   1.169 +        var n = Math.floor( numbers.length / 2 );
   1.170 +        return numbers.length % 2 ? numbers[n] : ( numbers[n-1] + numbers[n] ) / 2;
   1.171 +    }
   1.172 +}
   1.173 +
   1.174 +function reportResults() {
   1.175 +    //XXX need to create a client-side method to do this?
   1.176 +    var opening = openingTimes.join(':'); // times for each window open
   1.177 +    var closing = closingTimes.join(':'); // these are for >1 url, as a group
   1.178 +    //var ua = escape(navigator.userAgent).replace(/\+/g, "%2B"); // + == ' ', on servers
   1.179 +    //var reportURL = SERVER_URL + 
   1.180 +    //    "?opening="    + opening + 
   1.181 +    //    "&closing="    + closing + 
   1.182 +    //    "&maxIndex="   + MAX_INDEX + 
   1.183 +    //    "&cycleSize="  + CYCLE_SIZE + 
   1.184 +	//"&ua="         + ua;
   1.185 +    //window.open(reportURL, "test-results");
   1.186 +    var avgOpenTime = 0;
   1.187 +    var minOpenTime = 99999;
   1.188 +    var maxOpenTime = 0;
   1.189 +    var medOpenTime = calcMedian( openingTimes.slice(1) );
   1.190 +    // ignore first open
   1.191 +    for (i = 1; i < MAX_INDEX; i++) {
   1.192 +        avgOpenTime += openingTimes[i];
   1.193 +        if ( minOpenTime > openingTimes[i] ) {
   1.194 +            minOpenTime = openingTimes[i];
   1.195 +        }
   1.196 +        if ( maxOpenTime < openingTimes[i] ) {
   1.197 +            maxOpenTime = openingTimes[i];
   1.198 +        }
   1.199 +    }
   1.200 +    avgOpenTime = Math.round(avgOpenTime / (MAX_INDEX - 1));
   1.201 +    dump("openingTimes="+openingTimes.slice(1)+"\n");
   1.202 +    dump("avgOpenTime:" + avgOpenTime + "\n" );
   1.203 +    dump("minOpenTime:" + minOpenTime + "\n" );
   1.204 +    dump("maxOpenTime:" + maxOpenTime + "\n" );
   1.205 +    dump("medOpenTime:" + medOpenTime + "\n" );
   1.206 +    dump("__xulWinOpenTime:" + medOpenTime + "\n");
   1.207 +    // Close the root window, if required.
   1.208 +    if ( AUTOCLOSE ) {
   1.209 +        window.close();
   1.210 +    } else {
   1.211 +        document.getElementById("formTimes").value = openingTimes.slice(1);
   1.212 +        document.getElementById("formAvg").value   = avgOpenTime;
   1.213 +        document.getElementById("formMin").value   = minOpenTime;
   1.214 +        document.getElementById("formMax").value   = maxOpenTime;
   1.215 +        document.getElementById("formMed").value   = medOpenTime;
   1.216 +        document.getElementById("formAgain").setAttribute( "disabled", "false" );
   1.217 +    }
   1.218 +}
   1.219 +
   1.220 +function tryAgain() {
   1.221 +    document.getElementById("formAgain").setAttribute( "disabled", "true" );
   1.222 +    windowList      = [];
   1.223 +    startingTimes   = [];
   1.224 +    openingTimes    = [];
   1.225 +    closingTimes    = [];
   1.226 +    currentIndex    = 0;       
   1.227 +    openWindow();
   1.228 +}
   1.229 +
   1.230 +function restoreChromeURL() {
   1.231 +    // Restore browser.chromeURL pref.
   1.232 +    if ( KID_CHROME && SAVED_CHROME.length ) {
   1.233 +        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
   1.234 +        prefs.setCharPref( "browser.chromeURL", SAVED_CHROME );
   1.235 +    }
   1.236 +}
   1.237 +
   1.238 +function openWindow() {
   1.239 +    startingTimes[currentIndex] = (new Date()).getTime();
   1.240 +    var path   = window.location.pathname.substring( 0, window.location.pathname.lastIndexOf('/') );
   1.241 +    var url    = window.location.protocol + "//" + 
   1.242 +                 window.location.hostname + path + "/" +
   1.243 +                 KID_URL;
   1.244 +    windowList[currentIndex] = window.open(url, currentIndex);
   1.245 +}
   1.246 +
   1.247 +

mercurial