docshell/test/navigation/NavigationUtils.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/docshell/test/navigation/NavigationUtils.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,213 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +///////////////////////////////////////////////////////////////////////////
     1.9 +//
    1.10 +// Utilities for navigation tests
    1.11 +// 
    1.12 +///////////////////////////////////////////////////////////////////////////
    1.13 +
    1.14 +var body = "This frame was navigated.";
    1.15 +var target_url = "data:text/html,<html><body>" + body + "</body></html>";
    1.16 +
    1.17 +var popup_body = "This is a popup";
    1.18 +var target_popup_url = "data:text/html,<html><body>" + popup_body + "</body></html>";
    1.19 +
    1.20 +///////////////////////////////////////////////////////////////////////////
    1.21 +// Functions that navigate frames
    1.22 +///////////////////////////////////////////////////////////////////////////
    1.23 +
    1.24 +function navigateByLocation(wnd) {
    1.25 +  try {
    1.26 +    wnd.location = target_url;
    1.27 +  } catch(ex) {
    1.28 +    // We need to keep our finished frames count consistent.
    1.29 +    // Oddly, this ends up simulating the behavior of IE7.
    1.30 +    window.open(target_url, "_blank", "width=10,height=10");
    1.31 +  }
    1.32 +}
    1.33 +
    1.34 +function navigateByOpen(name) {
    1.35 +  window.open(target_url, name, "width=10,height=10");
    1.36 +}
    1.37 +
    1.38 +function navigateByForm(name) {
    1.39 +  var form = document.createElement("form");
    1.40 +  form.action = target_url;
    1.41 +  form.method = "POST";
    1.42 +  form.target = name; document.body.appendChild(form);
    1.43 +  form.submit();
    1.44 +}
    1.45 +
    1.46 +var hyperlink_count = 0;
    1.47 +
    1.48 +function navigateByHyperlink(name) {
    1.49 +  var link = document.createElement("a");
    1.50 +  link.href = target_url;
    1.51 +  link.target = name;
    1.52 +  link.id = "navigation_hyperlink_" + hyperlink_count++;
    1.53 +  document.body.appendChild(link);
    1.54 +  sendMouseEvent({type:"click"}, link.id);
    1.55 +}
    1.56 +
    1.57 +///////////////////////////////////////////////////////////////////////////
    1.58 +// Functions that call into Mochitest framework
    1.59 +///////////////////////////////////////////////////////////////////////////
    1.60 +
    1.61 +function isNavigated(wnd, message) {
    1.62 +  var result = null;
    1.63 +  try {
    1.64 +    result = SpecialPowers.wrap(wnd).document.body.innerHTML;
    1.65 +  } catch(ex) {
    1.66 +    result = ex;
    1.67 +  }
    1.68 +  is(result, body, message);
    1.69 +}
    1.70 +
    1.71 +function isBlank(wnd, message) {
    1.72 +  var result = null;
    1.73 +  try {
    1.74 +    result = wnd.document.body.innerHTML;
    1.75 +  } catch(ex) {
    1.76 +    result = ex;
    1.77 +  }
    1.78 +  is(result, "This is a blank document.", message);
    1.79 +}
    1.80 +
    1.81 +function isAccessible(wnd, message) {
    1.82 +  try {
    1.83 +    wnd.document.body.innerHTML;
    1.84 +    ok(true, message);
    1.85 +  } catch(ex) {
    1.86 +    ok(false, message);
    1.87 +  }
    1.88 +}
    1.89 +
    1.90 +function isInaccessible(wnd, message) {
    1.91 +  try {
    1.92 +    wnd.document.body.innerHTML;
    1.93 +    ok(false, message);
    1.94 +  } catch(ex) {
    1.95 +    ok(true, message);
    1.96 +  }
    1.97 +}
    1.98 +
    1.99 +///////////////////////////////////////////////////////////////////////////
   1.100 +// Functions that require UniversalXPConnect privilege
   1.101 +///////////////////////////////////////////////////////////////////////////
   1.102 +
   1.103 +function xpcEnumerateContentWindows(callback) {
   1.104 +
   1.105 +  var Ci = SpecialPowers.Ci;
   1.106 +  var ww = SpecialPowers.Cc["@mozilla.org/embedcomp/window-watcher;1"]
   1.107 +                        .getService(Ci.nsIWindowWatcher);
   1.108 +  var enumerator = ww.getWindowEnumerator();
   1.109 +
   1.110 +  var contentWindows = [];
   1.111 +
   1.112 +  while (enumerator.hasMoreElements()) {
   1.113 +    var win = enumerator.getNext();
   1.114 +    if (/ChromeWindow/.exec(win)) {
   1.115 +      var docshellTreeNode = win.QueryInterface(Ci.nsIInterfaceRequestor)
   1.116 +                                .getInterface(Ci.nsIWebNavigation)
   1.117 +                                .QueryInterface(Ci.nsIDocShellTreeItem);
   1.118 +      var childCount = docshellTreeNode.childCount;
   1.119 +      for (var i = 0; i < childCount; ++i) {
   1.120 +        var childTreeNode = docshellTreeNode.getChildAt(i);
   1.121 +
   1.122 +        // we're only interested in content docshells
   1.123 +        if (SpecialPowers.unwrap(childTreeNode.itemType) != Ci.nsIDocShellTreeItem.typeContent)
   1.124 +          continue;
   1.125 +
   1.126 +        var webNav = childTreeNode.QueryInterface(Ci.nsIWebNavigation);
   1.127 +        contentWindows.push(webNav.document.defaultView);
   1.128 +      }
   1.129 +    } else {
   1.130 +      contentWindows.push(win);
   1.131 +    }
   1.132 +  }
   1.133 +
   1.134 +  while (contentWindows.length > 0)
   1.135 +    callback(contentWindows.pop());
   1.136 +}
   1.137 +
   1.138 +// Note: This only searches for top-level frames with this name.
   1.139 +function xpcGetFramesByName(name) {
   1.140 +  var results = [];
   1.141 +
   1.142 +  xpcEnumerateContentWindows(function(win) {
   1.143 +    if (win.name == name)
   1.144 +      results.push(win);
   1.145 +  });
   1.146 +
   1.147 +  return results;
   1.148 +}
   1.149 +
   1.150 +function xpcCleanupWindows() {
   1.151 +  xpcEnumerateContentWindows(function(win) {
   1.152 +    if (win.location && win.location.protocol == "data:")
   1.153 +      win.close();
   1.154 +  });
   1.155 +}
   1.156 +
   1.157 +function xpcWaitForFinishedFrames(callback, numFrames) {
   1.158 +  var finishedFrameCount = 0;
   1.159 +  function frameFinished() {
   1.160 +    finishedFrameCount++;
   1.161 +
   1.162 +    if (finishedFrameCount == numFrames) {
   1.163 +      clearInterval(frameWaitInterval);
   1.164 +      setTimeout(callback, 0);
   1.165 +      return;
   1.166 +    }
   1.167 +
   1.168 +    if (finishedFrameCount > numFrames)
   1.169 +      throw "Too many frames loaded.";
   1.170 +  }
   1.171 +
   1.172 +  var finishedWindows = [];
   1.173 +
   1.174 +  function contains(obj, arr) {
   1.175 +    for (var i = 0; i < arr.length; i++) {
   1.176 +      if (obj === arr[i])
   1.177 +        return true;
   1.178 +    }
   1.179 +    return false;
   1.180 +  }
   1.181 +
   1.182 +  function searchForFinishedFrames(win) {
   1.183 +    if ((escape(unescape(win.location)) == escape(target_url) ||
   1.184 +         escape(unescape(win.location)) == escape(target_popup_url)) && 
   1.185 +        win.document && 
   1.186 +        win.document.body && 
   1.187 +        (win.document.body.textContent == body ||
   1.188 +         win.document.body.textContent == popup_body) && 
   1.189 +        win.document.readyState == "complete") {
   1.190 +
   1.191 +      var util = win.QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
   1.192 +                    .getInterface(SpecialPowers.Ci.nsIDOMWindowUtils);
   1.193 +      var windowId = util.outerWindowID;
   1.194 +      if (!contains(windowId, finishedWindows)) {
   1.195 +        finishedWindows.push(windowId);
   1.196 +        frameFinished();
   1.197 +      }
   1.198 +    }
   1.199 +    for (var i = 0; i < win.frames.length; i++)
   1.200 +      searchForFinishedFrames(win.frames[i]);
   1.201 +  }
   1.202 +
   1.203 +  function poll() {
   1.204 +    try {
   1.205 +      // This only gives us UniversalXPConnect for the current stack frame
   1.206 +      // We're using setInterval, so the main page's privileges are still normal
   1.207 +      xpcEnumerateContentWindows(searchForFinishedFrames);
   1.208 +    } catch(ex) {
   1.209 +      // We might be accessing windows before they are fully constructed,
   1.210 +      // which can throw.  We'll find those frames on our next poll().
   1.211 +    }
   1.212 +  }
   1.213 +
   1.214 +  var frameWaitInterval = setInterval(poll, 500);
   1.215 +}
   1.216 +

mercurial