docshell/test/navigation/NavigationUtils.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 ///////////////////////////////////////////////////////////////////////////
michael@0 6 //
michael@0 7 // Utilities for navigation tests
michael@0 8 //
michael@0 9 ///////////////////////////////////////////////////////////////////////////
michael@0 10
michael@0 11 var body = "This frame was navigated.";
michael@0 12 var target_url = "data:text/html,<html><body>" + body + "</body></html>";
michael@0 13
michael@0 14 var popup_body = "This is a popup";
michael@0 15 var target_popup_url = "data:text/html,<html><body>" + popup_body + "</body></html>";
michael@0 16
michael@0 17 ///////////////////////////////////////////////////////////////////////////
michael@0 18 // Functions that navigate frames
michael@0 19 ///////////////////////////////////////////////////////////////////////////
michael@0 20
michael@0 21 function navigateByLocation(wnd) {
michael@0 22 try {
michael@0 23 wnd.location = target_url;
michael@0 24 } catch(ex) {
michael@0 25 // We need to keep our finished frames count consistent.
michael@0 26 // Oddly, this ends up simulating the behavior of IE7.
michael@0 27 window.open(target_url, "_blank", "width=10,height=10");
michael@0 28 }
michael@0 29 }
michael@0 30
michael@0 31 function navigateByOpen(name) {
michael@0 32 window.open(target_url, name, "width=10,height=10");
michael@0 33 }
michael@0 34
michael@0 35 function navigateByForm(name) {
michael@0 36 var form = document.createElement("form");
michael@0 37 form.action = target_url;
michael@0 38 form.method = "POST";
michael@0 39 form.target = name; document.body.appendChild(form);
michael@0 40 form.submit();
michael@0 41 }
michael@0 42
michael@0 43 var hyperlink_count = 0;
michael@0 44
michael@0 45 function navigateByHyperlink(name) {
michael@0 46 var link = document.createElement("a");
michael@0 47 link.href = target_url;
michael@0 48 link.target = name;
michael@0 49 link.id = "navigation_hyperlink_" + hyperlink_count++;
michael@0 50 document.body.appendChild(link);
michael@0 51 sendMouseEvent({type:"click"}, link.id);
michael@0 52 }
michael@0 53
michael@0 54 ///////////////////////////////////////////////////////////////////////////
michael@0 55 // Functions that call into Mochitest framework
michael@0 56 ///////////////////////////////////////////////////////////////////////////
michael@0 57
michael@0 58 function isNavigated(wnd, message) {
michael@0 59 var result = null;
michael@0 60 try {
michael@0 61 result = SpecialPowers.wrap(wnd).document.body.innerHTML;
michael@0 62 } catch(ex) {
michael@0 63 result = ex;
michael@0 64 }
michael@0 65 is(result, body, message);
michael@0 66 }
michael@0 67
michael@0 68 function isBlank(wnd, message) {
michael@0 69 var result = null;
michael@0 70 try {
michael@0 71 result = wnd.document.body.innerHTML;
michael@0 72 } catch(ex) {
michael@0 73 result = ex;
michael@0 74 }
michael@0 75 is(result, "This is a blank document.", message);
michael@0 76 }
michael@0 77
michael@0 78 function isAccessible(wnd, message) {
michael@0 79 try {
michael@0 80 wnd.document.body.innerHTML;
michael@0 81 ok(true, message);
michael@0 82 } catch(ex) {
michael@0 83 ok(false, message);
michael@0 84 }
michael@0 85 }
michael@0 86
michael@0 87 function isInaccessible(wnd, message) {
michael@0 88 try {
michael@0 89 wnd.document.body.innerHTML;
michael@0 90 ok(false, message);
michael@0 91 } catch(ex) {
michael@0 92 ok(true, message);
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 ///////////////////////////////////////////////////////////////////////////
michael@0 97 // Functions that require UniversalXPConnect privilege
michael@0 98 ///////////////////////////////////////////////////////////////////////////
michael@0 99
michael@0 100 function xpcEnumerateContentWindows(callback) {
michael@0 101
michael@0 102 var Ci = SpecialPowers.Ci;
michael@0 103 var ww = SpecialPowers.Cc["@mozilla.org/embedcomp/window-watcher;1"]
michael@0 104 .getService(Ci.nsIWindowWatcher);
michael@0 105 var enumerator = ww.getWindowEnumerator();
michael@0 106
michael@0 107 var contentWindows = [];
michael@0 108
michael@0 109 while (enumerator.hasMoreElements()) {
michael@0 110 var win = enumerator.getNext();
michael@0 111 if (/ChromeWindow/.exec(win)) {
michael@0 112 var docshellTreeNode = win.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 113 .getInterface(Ci.nsIWebNavigation)
michael@0 114 .QueryInterface(Ci.nsIDocShellTreeItem);
michael@0 115 var childCount = docshellTreeNode.childCount;
michael@0 116 for (var i = 0; i < childCount; ++i) {
michael@0 117 var childTreeNode = docshellTreeNode.getChildAt(i);
michael@0 118
michael@0 119 // we're only interested in content docshells
michael@0 120 if (SpecialPowers.unwrap(childTreeNode.itemType) != Ci.nsIDocShellTreeItem.typeContent)
michael@0 121 continue;
michael@0 122
michael@0 123 var webNav = childTreeNode.QueryInterface(Ci.nsIWebNavigation);
michael@0 124 contentWindows.push(webNav.document.defaultView);
michael@0 125 }
michael@0 126 } else {
michael@0 127 contentWindows.push(win);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131 while (contentWindows.length > 0)
michael@0 132 callback(contentWindows.pop());
michael@0 133 }
michael@0 134
michael@0 135 // Note: This only searches for top-level frames with this name.
michael@0 136 function xpcGetFramesByName(name) {
michael@0 137 var results = [];
michael@0 138
michael@0 139 xpcEnumerateContentWindows(function(win) {
michael@0 140 if (win.name == name)
michael@0 141 results.push(win);
michael@0 142 });
michael@0 143
michael@0 144 return results;
michael@0 145 }
michael@0 146
michael@0 147 function xpcCleanupWindows() {
michael@0 148 xpcEnumerateContentWindows(function(win) {
michael@0 149 if (win.location && win.location.protocol == "data:")
michael@0 150 win.close();
michael@0 151 });
michael@0 152 }
michael@0 153
michael@0 154 function xpcWaitForFinishedFrames(callback, numFrames) {
michael@0 155 var finishedFrameCount = 0;
michael@0 156 function frameFinished() {
michael@0 157 finishedFrameCount++;
michael@0 158
michael@0 159 if (finishedFrameCount == numFrames) {
michael@0 160 clearInterval(frameWaitInterval);
michael@0 161 setTimeout(callback, 0);
michael@0 162 return;
michael@0 163 }
michael@0 164
michael@0 165 if (finishedFrameCount > numFrames)
michael@0 166 throw "Too many frames loaded.";
michael@0 167 }
michael@0 168
michael@0 169 var finishedWindows = [];
michael@0 170
michael@0 171 function contains(obj, arr) {
michael@0 172 for (var i = 0; i < arr.length; i++) {
michael@0 173 if (obj === arr[i])
michael@0 174 return true;
michael@0 175 }
michael@0 176 return false;
michael@0 177 }
michael@0 178
michael@0 179 function searchForFinishedFrames(win) {
michael@0 180 if ((escape(unescape(win.location)) == escape(target_url) ||
michael@0 181 escape(unescape(win.location)) == escape(target_popup_url)) &&
michael@0 182 win.document &&
michael@0 183 win.document.body &&
michael@0 184 (win.document.body.textContent == body ||
michael@0 185 win.document.body.textContent == popup_body) &&
michael@0 186 win.document.readyState == "complete") {
michael@0 187
michael@0 188 var util = win.QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
michael@0 189 .getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
michael@0 190 var windowId = util.outerWindowID;
michael@0 191 if (!contains(windowId, finishedWindows)) {
michael@0 192 finishedWindows.push(windowId);
michael@0 193 frameFinished();
michael@0 194 }
michael@0 195 }
michael@0 196 for (var i = 0; i < win.frames.length; i++)
michael@0 197 searchForFinishedFrames(win.frames[i]);
michael@0 198 }
michael@0 199
michael@0 200 function poll() {
michael@0 201 try {
michael@0 202 // This only gives us UniversalXPConnect for the current stack frame
michael@0 203 // We're using setInterval, so the main page's privileges are still normal
michael@0 204 xpcEnumerateContentWindows(searchForFinishedFrames);
michael@0 205 } catch(ex) {
michael@0 206 // We might be accessing windows before they are fully constructed,
michael@0 207 // which can throw. We'll find those frames on our next poll().
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 var frameWaitInterval = setInterval(poll, 500);
michael@0 212 }
michael@0 213

mercurial