testing/specialpowers/content/MockColorPicker.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/specialpowers/content/MockColorPicker.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     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 +this.EXPORTED_SYMBOLS = ["MockColorPicker"];
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cm = Components.manager;
    1.13 +const Cu = Components.utils;
    1.14 +
    1.15 +const CONTRACT_ID = "@mozilla.org/colorpicker;1";
    1.16 +
    1.17 +Cu.import("resource://gre/modules/Services.jsm");
    1.18 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.19 +
    1.20 +var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
    1.21 +var oldClassID = "", oldFactory = null;
    1.22 +var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
    1.23 +var newFactory = function (window) {
    1.24 +  return {
    1.25 +    createInstance: function(aOuter, aIID) {
    1.26 +      if (aOuter)
    1.27 +        throw Components.results.NS_ERROR_NO_AGGREGATION;
    1.28 +      return new MockColorPickerInstance(window).QueryInterface(aIID);
    1.29 +    },
    1.30 +    lockFactory: function(aLock) {
    1.31 +      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    1.32 +    },
    1.33 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
    1.34 +  };
    1.35 +}
    1.36 +
    1.37 +this.MockColorPicker = {
    1.38 +  init: function(window) {
    1.39 +    this.reset();
    1.40 +    this.factory = newFactory(window);
    1.41 +    if (!registrar.isCIDRegistered(newClassID)) {
    1.42 +      try {
    1.43 +        oldClassID = registrar.contractIDToCID(CONTRACT_ID);
    1.44 +        oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
    1.45 +      } catch(ex) {
    1.46 +        oldClassID = "";
    1.47 +        oldFactory = null;
    1.48 +        dump("TEST-INFO | can't get colorpicker registered component, " +
    1.49 +             "assuming there is none");
    1.50 +      }
    1.51 +      if (oldClassID != "" && oldFactory != null) {
    1.52 +        registrar.unregisterFactory(oldClassID, oldFactory);
    1.53 +      }
    1.54 +      registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
    1.55 +    }
    1.56 +  },
    1.57 +
    1.58 +  reset: function() {
    1.59 +    this.returnColor = "";
    1.60 +    this.showCallback = null;
    1.61 +    this.shown = false;
    1.62 +    this.showing = false;
    1.63 +  },
    1.64 +
    1.65 +  cleanup: function() {
    1.66 +    var previousFactory = this.factory;
    1.67 +    this.reset();
    1.68 +    this.factory = null;
    1.69 +
    1.70 +    registrar.unregisterFactory(newClassID, previousFactory);
    1.71 +    if (oldClassID != "" && oldFactory != null) {
    1.72 +      registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory);
    1.73 +    }
    1.74 +  }
    1.75 +};
    1.76 +
    1.77 +function MockColorPickerInstance(window) {
    1.78 +  this.window = window;
    1.79 +};
    1.80 +MockColorPickerInstance.prototype = {
    1.81 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIColorPicker]),
    1.82 +  init: function(aParent, aTitle, aInitialColor) {
    1.83 +    this.parent = aParent;
    1.84 +    this.initialColor = aInitialColor;
    1.85 +  },
    1.86 +  initialColor: "",
    1.87 +  parent: null,
    1.88 +  open: function(aColorPickerShownCallback) {
    1.89 +    MockColorPicker.showing = true;
    1.90 +    MockColorPicker.shown = true;
    1.91 +
    1.92 +    this.window.setTimeout(function() {
    1.93 +      let result = "";
    1.94 +      try {
    1.95 +        if (typeof MockColorPicker.showCallback == "function") {
    1.96 +          var updateCb = function(color) {
    1.97 +            result = color;
    1.98 +            aColorPickerShownCallback.update(color);
    1.99 +          };
   1.100 +          let returnColor = MockColorPicker.showCallback(this, updateCb);
   1.101 +          if (typeof returnColor === "string") {
   1.102 +            result = returnColor;
   1.103 +          }
   1.104 +        } else if (typeof MockColorPicker.returnColor === "string") {
   1.105 +          result = MockColorPicker.returnColor;
   1.106 +        }
   1.107 +      } catch(ex) {
   1.108 +        dump("TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.jsm open() " +
   1.109 +             "method: " + ex + "\n");
   1.110 +      }
   1.111 +      if (aColorPickerShownCallback) {
   1.112 +        aColorPickerShownCallback.done(result);
   1.113 +      }
   1.114 +    }.bind(this), 0);
   1.115 +  }
   1.116 +};
   1.117 +
   1.118 +// Expose everything to content. We call reset() here so that all of the
   1.119 +// relevant lazy expandos get added.
   1.120 +MockColorPicker.reset();
   1.121 +function exposeAll(obj) {
   1.122 +  var props = {};
   1.123 +  for (var prop in obj)
   1.124 +    props[prop] = 'rw';
   1.125 +  obj.__exposedProps__ = props;
   1.126 +}
   1.127 +exposeAll(MockColorPicker);
   1.128 +exposeAll(MockColorPickerInstance.prototype);

mercurial