testing/mochitest/tests/SimpleTest/ChromePowers.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/tests/SimpleTest/ChromePowers.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     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 +function ChromePowers(window) {
     1.9 +  this.window = Components.utils.getWeakReference(window);
    1.10 +
    1.11 +  // In the case of browser-chrome tests, we are running as a [ChromeWindow]
    1.12 +  // and we have no window.QueryInterface available, content.window is what we need
    1.13 +  if (typeof(window) == "ChromeWindow" && typeof(content.window) == "Window") {
    1.14 +    this.DOMWindowUtils = bindDOMWindowUtils(content.window);
    1.15 +    this.window = Components.utils.getWeakReference(content.window);
    1.16 +  } else {
    1.17 +    this.DOMWindowUtils = bindDOMWindowUtils(window);
    1.18 +  }
    1.19 +
    1.20 +  this.spObserver = new SpecialPowersObserverAPI();
    1.21 +}
    1.22 +
    1.23 +ChromePowers.prototype = new SpecialPowersAPI();
    1.24 +
    1.25 +ChromePowers.prototype.toString = function() { return "[ChromePowers]"; };
    1.26 +ChromePowers.prototype.sanityCheck = function() { return "foo"; };
    1.27 +
    1.28 +// This gets filled in in the constructor.
    1.29 +ChromePowers.prototype.DOMWindowUtils = undefined;
    1.30 +
    1.31 +ChromePowers.prototype._sendSyncMessage = function(type, msg) {
    1.32 +  var aMessage = {'name':type, 'json': msg};
    1.33 +  return [this._receiveMessage(aMessage)];
    1.34 +};
    1.35 +
    1.36 +ChromePowers.prototype._sendAsyncMessage = function(type, msg) {
    1.37 +  var aMessage = {'name':type, 'json': msg};
    1.38 +  this._receiveMessage(aMessage);
    1.39 +};
    1.40 +
    1.41 +ChromePowers.prototype.registerProcessCrashObservers = function() {
    1.42 +  this._sendSyncMessage("SPProcessCrashService", { op: "register-observer" });
    1.43 +};
    1.44 +
    1.45 +ChromePowers.prototype.unregisterProcessCrashObservers = function() {
    1.46 +  this._sendSyncMessage("SPProcessCrashService", { op: "unregister-observer" });
    1.47 +};
    1.48 +
    1.49 +ChromePowers.prototype._receiveMessage = function(aMessage) {
    1.50 +  switch (aMessage.name) {
    1.51 +    case "SpecialPowers.Quit":
    1.52 +      let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
    1.53 +      appStartup.quit(Ci.nsIAppStartup.eForceQuit);
    1.54 +      break;
    1.55 +    case "SPProcessCrashService":
    1.56 +      if (aMessage.json.op == "register-observer" || aMessage.json.op == "unregister-observer") {
    1.57 +        // Hack out register/unregister specifically for browser-chrome leaks
    1.58 +        break;
    1.59 +      } else if (aMessage.type == "crash-observed") {
    1.60 +        for (let e of msg.dumpIDs) {
    1.61 +          this._encounteredCrashDumpFiles.push(e.id + "." + e.extension);
    1.62 +        }
    1.63 +      }
    1.64 +    default:
    1.65 +      // All calls go here, because we need to handle SPProcessCrashService calls as well
    1.66 +      return this.spObserver._receiveMessageAPI(aMessage);
    1.67 +  }
    1.68 +  return undefined;		// Avoid warning.
    1.69 +};
    1.70 +
    1.71 +ChromePowers.prototype.quit = function() {
    1.72 +  // We come in here as SpecialPowers.quit, but SpecialPowers is really ChromePowers.
    1.73 +  // For some reason this.<func> resolves to TestRunner, so using SpecialPowers
    1.74 +  // allows us to use the ChromePowers object which we defined below.
    1.75 +  SpecialPowers._sendSyncMessage("SpecialPowers.Quit", {});
    1.76 +};
    1.77 +
    1.78 +ChromePowers.prototype.focus = function(aWindow) {
    1.79 +  // We come in here as SpecialPowers.focus, but SpecialPowers is really ChromePowers.
    1.80 +  // For some reason this.<func> resolves to TestRunner, so using SpecialPowers
    1.81 +  // allows us to use the ChromePowers object which we defined below.
    1.82 +  if (aWindow)
    1.83 +    aWindow.focus();
    1.84 +};
    1.85 +
    1.86 +ChromePowers.prototype.executeAfterFlushingMessageQueue = function(aCallback) {
    1.87 +  aCallback();
    1.88 +};
    1.89 +
    1.90 +// Expose everything but internal APIs (starting with underscores) to
    1.91 +// web content.  We cannot use Object.keys to view SpecialPowers.prototype since
    1.92 +// we are using the functions from SpecialPowersAPI.prototype
    1.93 +ChromePowers.prototype.__exposedProps__ = {};
    1.94 +for (var i in ChromePowers.prototype) {
    1.95 +  if (i.charAt(0) != "_")
    1.96 +    ChromePowers.prototype.__exposedProps__[i] = "r";
    1.97 +}
    1.98 +
    1.99 +if ((window.parent !== null) &&
   1.100 +    (window.parent !== undefined) &&
   1.101 +    (window.parent.wrappedJSObject.SpecialPowers) &&
   1.102 +    !(window.wrappedJSObject.SpecialPowers)) {
   1.103 +  window.wrappedJSObject.SpecialPowers = window.parent.SpecialPowers;
   1.104 +} else {
   1.105 +  window.wrappedJSObject.SpecialPowers = new ChromePowers(window);
   1.106 +}
   1.107 +

mercurial