CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / docshell / test / navigation / NavigationUtils.js
blobd0baf963e6ba32af6e9a62701f851c18aaf7695f
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
12 * License.
14 * The Original Code is NavigationUtils.js
16 * The Initial Developer of the Original Code is
17 * Stanford University
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
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
42 //
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) {
53 try {
54 wnd.location = target_url;
55 } catch(ex) {
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;
69 form.method = "POST";
70 form.target = name; document.body.appendChild(form);
71 form.submit();
74 var hyperlink_count = 0;
76 function navigateByHyperlink(name) {
77 var link = document.createElement("a");
78 link.href = target_url;
79 link.target = name;
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) {
90 var result = null;
91 try {
92 result = wnd.document.body.innerHTML;
93 } catch(ex) {
94 result = ex;
96 is(result, body, message);
99 function isBlank(wnd, message) {
100 var result = null;
101 try {
102 result = wnd.document.body.innerHTML;
103 } catch(ex) {
104 result = ex;
106 is(result, "This is a blank document.", message);
109 function isAccessible(wnd, message) {
110 try {
111 wnd.document.body.innerHTML;
112 ok(true, message);
113 } catch(ex) {
114 ok(false, message);
118 function isInaccessible(wnd, message) {
119 try {
120 wnd.document.body.innerHTML;
121 ok(false, message);
122 } catch(ex) {
123 ok(true, message);
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)
152 continue;
154 var webNav = childTreeNode.QueryInterface(Ci.nsIWebNavigation);
155 contentWindows.push(webNav.document.defaultView);
157 } else {
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) {
168 var results = [];
170 xpcEnumerateContentWindows(function(win) {
171 if (win.name == name)
172 results.push(win);
175 return results;
178 function xpcCleanupWindows() {
179 xpcEnumerateContentWindows(function(win) {
180 if (win.location && win.location.protocol == "data:")
181 win.close();
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);
193 return;
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++) {
204 if (obj === arr[i])
205 return true;
207 return false;
210 function searchForFinishedFrames(win) {
211 if (escape(unescape(win.location)) == escape(target_url) &&
212 win.document &&
213 win.document.body &&
214 win.document.body.textContent == body &&
215 win.document.readyState == "complete") {
216 if (!contains(win, finishedWindows)) {
217 finishedWindows.push(win);
218 frameFinished();
221 for (var i = 0; i < win.frames.length; i++)
222 searchForFinishedFrames(win.frames[i]);
225 function poll() {
226 try {
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);
230 } catch(ex) {
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);