1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is NavigationUtils.js
16 * The Initial Developer of the Original Code is
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
22 * Adam Barth <hk9565@gmail.com>
23 * Collin Jackson <mozilla@collinjackson.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 ///////////////////////////////////////////////////////////////////////////
41 // Utilities for navigation tests
43 ///////////////////////////////////////////////////////////////////////////
45 var body
= "This frame was navigated.";
46 var target_url
= "data:text/html,<html><body>" + body
+ "</body></html>";
48 ///////////////////////////////////////////////////////////////////////////
49 // Functions that navigate frames
50 ///////////////////////////////////////////////////////////////////////////
52 function navigateByLocation(wnd
) {
54 wnd
.location
= target_url
;
56 // We need to keep our finished frames count consistent.
57 // Oddly, this ends up simulating the behavior of IE7.
58 window
.open(target_url
, "_blank", "width=10,height=10");
62 function navigateByOpen(name
) {
63 window
.open(target_url
, name
, "width=10,height=10");
66 function navigateByForm(name
) {
67 var form
= document
.createElement("form");
68 form
.action
= target_url
;
70 form
.target
= name
; document
.body
.appendChild(form
);
74 var hyperlink_count
= 0;
76 function navigateByHyperlink(name
) {
77 var link
= document
.createElement("a");
78 link
.href
= target_url
;
80 link
.id
= "navigation_hyperlink_" + hyperlink_count
++;
81 document
.body
.appendChild(link
);
82 sendMouseEvent({type
:"click"}, link
.id
);
85 ///////////////////////////////////////////////////////////////////////////
86 // Functions that call into Mochitest framework
87 ///////////////////////////////////////////////////////////////////////////
89 function isNavigated(wnd
, message
) {
92 result
= wnd
.document
.body
.innerHTML
;
96 is(result
, body
, message
);
99 function isBlank(wnd
, message
) {
102 result
= wnd
.document
.body
.innerHTML
;
106 is(result
, "This is a blank document.", message
);
109 function isAccessible(wnd
, message
) {
111 wnd
.document
.body
.innerHTML
;
118 function isInaccessible(wnd
, message
) {
120 wnd
.document
.body
.innerHTML
;
127 ///////////////////////////////////////////////////////////////////////////
128 // Functions that require UniversalXPConnect privilege
129 ///////////////////////////////////////////////////////////////////////////
131 function xpcEnumerateContentWindows(callback
) {
132 netscape
.security
.PrivilegeManager
.enablePrivilege("UniversalXPConnect");
133 var Ci
= Components
.interfaces
;
134 var ww
= Components
.classes
["@mozilla.org/embedcomp/window-watcher;1"]
135 .getService(Ci
.nsIWindowWatcher
);
136 var enumerator
= ww
.getWindowEnumerator();
138 var contentWindows
= [];
140 while (enumerator
.hasMoreElements()) {
141 var win
= enumerator
.getNext();
142 if (typeof ChromeWindow
!= "undefined" && win
instanceof ChromeWindow
) {
143 var docshellTreeNode
= win
.QueryInterface(Ci
.nsIInterfaceRequestor
)
144 .getInterface(Ci
.nsIWebNavigation
)
145 .QueryInterface(Ci
.nsIDocShellTreeNode
);
146 var childCount
= docshellTreeNode
.childCount
;
147 for (var i
= 0; i
< childCount
; ++i
) {
148 var childTreeNode
= docshellTreeNode
.getChildAt(i
);
150 // we're only interested in content docshells
151 if (childTreeNode
.itemType
!= Ci
.nsIDocShellTreeItem
.typeContent
)
154 var webNav
= childTreeNode
.QueryInterface(Ci
.nsIWebNavigation
);
155 contentWindows
.push(webNav
.document
.defaultView
);
158 contentWindows
.push(win
);
162 while (contentWindows
.length
> 0)
163 callback(contentWindows
.pop());
166 // Note: This only searches for top-level frames with this name.
167 function xpcGetFramesByName(name
) {
170 xpcEnumerateContentWindows(function(win
) {
171 if (win
.name
== name
)
178 function xpcCleanupWindows() {
179 xpcEnumerateContentWindows(function(win
) {
180 if (win
.location
&& win
.location
.protocol
== "data:")
185 function xpcWaitForFinishedFrames(callback
, numFrames
) {
186 var finishedFrameCount
= 0;
187 function frameFinished() {
188 finishedFrameCount
++;
190 if (finishedFrameCount
== numFrames
) {
191 clearInterval(frameWaitInterval
);
192 setTimeout(callback
, 1);
196 if (finishedFrameCount
> numFrames
)
197 throw "Too many frames loaded.";
200 var finishedWindows
= [];
202 function contains(obj
, arr
) {
203 for (var i
= 0; i
< arr
.length
; i
++) {
210 function searchForFinishedFrames(win
) {
211 if (escape(unescape(win
.location
)) == escape(target_url
) &&
214 win
.document
.body
.textContent
== body
&&
215 win
.document
.readyState
== "complete") {
216 if (!contains(win
, finishedWindows
)) {
217 finishedWindows
.push(win
);
221 for (var i
= 0; i
< win
.frames
.length
; i
++)
222 searchForFinishedFrames(win
.frames
[i
]);
227 // This only gives us UniversalXPConnect for the current stack frame
228 // We're using setInterval, so the main page's privileges are still normal
229 xpcEnumerateContentWindows(searchForFinishedFrames
);
231 // We might be accessing windows before they are fully constructed,
232 // which can throw. We'll find those frames on our next poll().
236 var frameWaitInterval
= setInterval(poll
, 500);