1 // target for window.open()
2 const KID_URL
= "child-window.html";
4 // formats final results
5 const SERVER_URL
= "http://jrgm.mcom.com/cgi-bin/window-open-2.0/openreport.pl";
7 // let system settle between each window.open
8 const OPENER_DELAY
= 1000;
10 // three phases: single open/close; overlapped open/close; open-all/close-all
15 // keep this many windows concurrently open during overlapped phase
16 var OVERLAP_COUNT
= 3;
18 // repeat three phases CYCLES times
24 // Chrome url for child windows.
25 var KID_CHROME
= null;
26 var SAVED_CHROME
= null;
28 // URL options and correspnding vars.
29 const options
= [ [ "phase1", "PHASE_ONE", false ],
30 [ "phase2", "PHASE_TWO", false ],
31 [ "phase3", "PHASE_THREE", false ],
32 [ "overlap", "OVERLAP_COUNT", false ],
33 [ "cycles", "CYCLES", false ],
34 [ "chrome", "KID_CHROME", true ],
35 [ "close", "AUTOCLOSE", false ] ];
37 // Note: You can attach search options to the url for this file to control
38 // any of the options in the array above. E.g., specifying
39 // mozilla -chrome "file:///D|/mozilla/xpfe/test/winopen.xul?phase1=16&close=0"
40 // will run this script with PHASE_ONE=16 and AUTOCLOSE=0.
42 // On Win32, you must enclose the -chrome option in quotes in order pass funny Win32 shell
43 // characters such as '&' or '|'!
45 var opts
= window
.location
.search
.substring(1).split( '&' );
47 for ( var i
in options
) {
48 if ( opts
[opt
].indexOf( options
[i
][0]+"=" ) == 0 ) {
49 var newVal
= opts
[opt
].split( '=' )[ 1 ];
50 // wrap with quotes, if required.
51 if ( options
[i
][2] ) {
52 newVal
= '"' + newVal
+ '"';
54 eval( options
[i
][1] + "=" + newVal
+ ";" );
62 // Reset browser.chromeURL so it points to KID_CHROME.
63 // This will cause window.open in openWindow to open that chrome.
64 netscape
.security
.PrivilegeManager
.enablePrivilege("UniversalXPConnect");
65 prefs
= Components
.classes
["@mozilla.org/preferences-service;1"]
66 .getService( Components
.interfaces
.nsIPrefBranch
);
67 SAVED_CHROME
= prefs
.getCharPref( "browser.chromeURL" );
68 prefs
.setCharPref( "browser.chromeURL", KID_CHROME
);
71 const CYCLE_SIZE
= PHASE_ONE
+ PHASE_TWO
+ PHASE_THREE
;
72 const MAX_INDEX
= CYCLE_SIZE
* CYCLES
; // total number of windows to open
74 var windowList
= []; // handles to opened windows
75 var startingTimes
= []; // time that window.open is called
76 var openingTimes
= []; // time that child window took to fire onload
77 var closingTimes
= []; // collect stats for case of closing >1 windows
81 function childIsOpen(aTime
) {
82 openingTimes
[currentIndex
] = aTime
- startingTimes
[currentIndex
];
83 updateDisplay(currentIndex
, openingTimes
[currentIndex
]);
84 reapWindows(currentIndex
);
86 if (currentIndex
< MAX_INDEX
)
89 window
.setTimeout(reportResults
, OPENER_DELAY
);
93 function updateDisplay(index
, time
) {
94 var formIndex
= document
.getElementById("formIndex");
96 formIndex
.setAttribute("value", index
+1);
97 var formTime
= document
.getElementById("formTime");
99 formTime
.setAttribute("value", time
);
103 function scheduleNextWindow() {
104 window
.setTimeout(openWindow
, OPENER_DELAY
);
108 function closeOneWindow(aIndex
) {
109 var win
= windowList
[aIndex
];
110 // no-op if window is already closed
111 if (win
&& !win
.closed
) {
113 windowList
[aIndex
] = null;
118 function closeAllWindows(aRecordTimeToClose
) {
119 var timeToClose
= (new Date()).getTime();
121 for (var i
= 0; i
< windowList
.length
; i
++) {
126 if (aRecordTimeToClose
&& count
> 0) {
127 timeToClose
= (new Date()).getTime() - timeToClose
;
128 closingTimes
.push(parseInt(timeToClose
/count
));
133 // close some, none, or all open windows in the list
134 function reapWindows() {
135 var modIndex
= currentIndex
% CYCLE_SIZE
;
136 if (modIndex
< PHASE_ONE
-1) {
137 // first phase in each "cycle", are single open/close sequences
138 closeOneWindow(currentIndex
);
140 else if (PHASE_ONE
-1 <= modIndex
&& modIndex
< PHASE_ONE
+PHASE_TWO
-1) {
141 // next phase in each "cycle", keep N windows concurrently open
142 closeOneWindow(currentIndex
- OVERLAP_COUNT
);
144 else if (modIndex
== PHASE_ONE
+PHASE_TWO
-1) {
145 // end overlapping windows cycle; close all windows
146 closeAllWindows(false);
148 else if (PHASE_ONE
+PHASE_TWO
<= modIndex
&& modIndex
< CYCLE_SIZE
-1) {
149 // do nothing; keep adding windows
151 else if (modIndex
== CYCLE_SIZE
-1) {
152 // end open-all/close-all phase; close windows, recording time to close
153 closeAllWindows(true);
157 function calcMedian( numbers
) {
158 if ( numbers
.length
== 0 ) {
160 } else if ( numbers
.length
== 1 ) {
162 } else if ( numbers
.length
== 2 ) {
163 return ( numbers
[0] + numbers
[1] ) / 2;
165 numbers
.sort( function (a
,b
){ return a
-b
; } );
166 var n
= Math
.floor( numbers
.length
/ 2 );
167 return numbers
.length
% 2 ? numbers
[n
] : ( numbers
[n
-1] + numbers
[n
] ) / 2;
171 function reportResults() {
172 //XXX need to create a client-side method to do this?
173 var opening
= openingTimes
.join(':'); // times for each window open
174 var closing
= closingTimes
.join(':'); // these are for >1 url, as a group
175 //var ua = escape(navigator.userAgent).replace(/\+/g, "%2B"); // + == ' ', on servers
176 //var reportURL = SERVER_URL +
177 // "?opening=" + opening +
178 // "&closing=" + closing +
179 // "&maxIndex=" + MAX_INDEX +
180 // "&cycleSize=" + CYCLE_SIZE +
182 //window.open(reportURL, "test-results");
184 var minOpenTime
= 99999;
186 var medOpenTime
= calcMedian( openingTimes
.slice(1) );
188 for (i
= 1; i
< MAX_INDEX
; i
++) {
189 avgOpenTime
+= openingTimes
[i
];
190 if ( minOpenTime
> openingTimes
[i
] ) {
191 minOpenTime
= openingTimes
[i
];
193 if ( maxOpenTime
< openingTimes
[i
] ) {
194 maxOpenTime
= openingTimes
[i
];
197 avgOpenTime
= Math
.round(avgOpenTime
/ (MAX_INDEX
- 1));
198 dump("openingTimes="+openingTimes
.slice(1)+"\n");
199 dump("avgOpenTime:" + avgOpenTime
+ "\n" );
200 dump("minOpenTime:" + minOpenTime
+ "\n" );
201 dump("maxOpenTime:" + maxOpenTime
+ "\n" );
202 dump("medOpenTime:" + medOpenTime
+ "\n" );
203 dump("__xulWinOpenTime:" + medOpenTime
+ "\n");
204 // Close the root window, if required.
208 document
.getElementById("formTimes").value
= openingTimes
.slice(1);
209 document
.getElementById("formAvg").value
= avgOpenTime
;
210 document
.getElementById("formMin").value
= minOpenTime
;
211 document
.getElementById("formMax").value
= maxOpenTime
;
212 document
.getElementById("formMed").value
= medOpenTime
;
213 document
.getElementById("formAgain").setAttribute( "disabled", "false" );
217 function tryAgain() {
218 document
.getElementById("formAgain").setAttribute( "disabled", "true" );
227 function restoreChromeURL() {
228 // Restore browser.chromeURL pref.
229 if ( KID_CHROME
&& SAVED_CHROME
.length
) {
230 netscape
.security
.PrivilegeManager
.enablePrivilege("UniversalXPConnect");
231 prefs
.setCharPref( "browser.chromeURL", SAVED_CHROME
);
235 function openWindow() {
236 startingTimes
[currentIndex
] = (new Date()).getTime();
237 var path
= window
.location
.pathname
.substring( 0, window
.location
.pathname
.lastIndexOf('/') );
238 var url
= window
.location
.protocol
+ "//" +
239 window
.location
.hostname
+ path
+ "/" +
241 windowList
[currentIndex
] = window
.open(url
, currentIndex
);