Bug 886046 - Add a MockColorPicker module in SpecialPowers. r=ctalbert
authorMounir Lamouri <mounir.lamouri@gmail.com>
Mon, 08 Jul 2013 15:11:36 -0400
changeset 145245 cc0b2e9778477a6c50c7282a7e8a57134156a781
parent 145244 3ca17e6c97aae70bb8af1a3055e863fdbeda71db
child 145246 997713d630322dca5dbe37c896e25bf861db5bff
push idunknown
push userunknown
push dateunknown
reviewersctalbert
bugs886046
milestone25.0a1
Bug 886046 - Add a MockColorPicker module in SpecialPowers. r=ctalbert
testing/marionette/jar.mn
testing/specialpowers/content/MockColorPicker.jsm
testing/specialpowers/content/specialpowersAPI.js
testing/specialpowers/jar.mn
--- a/testing/marionette/jar.mn
+++ b/testing/marionette/jar.mn
@@ -23,9 +23,10 @@ marionette.jar:
   content/SpecialPowersObserver.js (../specialpowers/components/SpecialPowersObserver.js)
   content/specialpowersAPI.js (../specialpowers/content/specialpowersAPI.js)
   content/SpecialPowersObserverAPI.js (../specialpowers/content/SpecialPowersObserverAPI.js)
   content/ChromePowers.js (../mochitest/tests/SimpleTest/ChromePowers.js)
   content/MozillaLogger.js (../specialpowers/content/MozillaLogger.js)
 
 % resource specialpowers %modules/
   modules/MockFilePicker.jsm (../specialpowers/content/MockFilePicker.jsm)
+  modules/MockColorPicker.jsm (../specialpowers/content/MockColorPicker.jsm)
   modules/MockPermissionPrompt.jsm (../specialpowers/content/MockPermissionPrompt.jsm)
new file mode 100644
--- /dev/null
+++ b/testing/specialpowers/content/MockColorPicker.jsm
@@ -0,0 +1,125 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+this.EXPORTED_SYMBOLS = ["MockColorPicker"];
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cm = Components.manager;
+const Cu = Components.utils;
+
+const CONTRACT_ID = "@mozilla.org/colorpicker;1";
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
+var oldClassID = "", oldFactory = null;
+var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
+var newFactory = function (window) {
+  return {
+    createInstance: function(aOuter, aIID) {
+      if (aOuter)
+        throw Components.results.NS_ERROR_NO_AGGREGATION;
+      return new MockColorPickerInstance(window).QueryInterface(aIID);
+    },
+    lockFactory: function(aLock) {
+      throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
+    },
+    QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
+  };
+}
+
+this.MockColorPicker = {
+  init: function(window) {
+    this.reset();
+    this.factory = newFactory(window);
+    if (!registrar.isCIDRegistered(newClassID)) {
+      try {
+        oldClassID = registrar.contractIDToCID(CONTRACT_ID);
+        oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
+      } catch(ex) {
+        oldClassID = "";
+        oldFactory = null;
+        dump("TEST-INFO | can't get colorpicker registered component, " +
+             "assuming there is none");
+      }
+      if (oldClassID != "" && oldFactory != null) {
+        registrar.unregisterFactory(oldClassID, oldFactory);
+      }
+      registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
+    }
+  },
+
+  reset: function() {
+    this.returnColor = "";
+    this.showCallback = null;
+    this.shown = false;
+    this.showing = false;
+  },
+
+  cleanup: function() {
+    var previousFactory = this.factory;
+    this.reset();
+    this.factory = null;
+
+    registrar.unregisterFactory(newClassID, previousFactory);
+    if (oldClassID != "" && oldFactory != null) {
+      registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory);
+    }
+  }
+};
+
+function MockColorPickerInstance(window) {
+  this.window = window;
+};
+MockColorPickerInstance.prototype = {
+  QueryInterface: XPCOMUtils.generateQI([Ci.nsIColorPicker]),
+  init: function(aParent, aTitle, aInitialColor) {
+    this.parent = aParent;
+    this.initialColor = aInitialColor;
+  },
+  initialColor: "",
+  parent: null,
+  open: function(aColorPickerShownCallback) {
+    MockColorPicker.showing = true;
+    MockColorPicker.shown = true;
+
+    this.window.setTimeout(function() {
+      let result = "";
+      try {
+        if (typeof MockColorPicker.showCallback == "function") {
+          var updateCb = function(color) {
+            result = color;
+            aColorPickerShownCallback.update(color);
+          };
+          let returnColor = MockColorPicker.showCallback(this, updateCb);
+          if (typeof returnColor === "string") {
+            result = returnColor;
+          }
+        } else if (typeof MockColorPicker.returnColor === "string") {
+          result = MockColorPicker.returnColor;
+        }
+      } catch(ex) {
+        dump("TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.jsm open() " +
+             "method: " + ex + "\n");
+      }
+      if (aColorPickerShownCallback) {
+        aColorPickerShownCallback.done(result);
+      }
+    }.bind(this), 0);
+  }
+};
+
+// Expose everything to content. We call reset() here so that all of the
+// relevant lazy expandos get added.
+MockColorPicker.reset();
+function exposeAll(obj) {
+  var props = {};
+  for (var prop in obj)
+    props[prop] = 'rw';
+  obj.__exposedProps__ = props;
+}
+exposeAll(MockColorPicker);
+exposeAll(MockColorPickerInstance.prototype);
--- a/testing/specialpowers/content/specialpowersAPI.js
+++ b/testing/specialpowers/content/specialpowersAPI.js
@@ -5,16 +5,17 @@
  * order to be used as a replacement for UniversalXPConnect
  */
 
 var Ci = Components.interfaces;
 var Cc = Components.classes;
 var Cu = Components.utils;
 
 Cu.import("resource://specialpowers/MockFilePicker.jsm");
+Cu.import("resource://specialpowers/MockColorPicker.jsm");
 Cu.import("resource://specialpowers/MockPermissionPrompt.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 function SpecialPowersAPI() {
   this._consoleListeners = [];
   this._encounteredCrashDumpFiles = [];
@@ -480,16 +481,20 @@ SpecialPowersAPI.prototype = {
   compare: function(a, b) {
     return unwrapIfWrapped(a) === unwrapIfWrapped(b);
   },
 
   get MockFilePicker() {
     return MockFilePicker
   },
 
+  get MockColorPicker() {
+    return MockColorPicker
+  },
+
   get MockPermissionPrompt() {
     return MockPermissionPrompt
   },
 
   get Services() {
     return wrapPrivileged(Services);
   },
 
--- a/testing/specialpowers/jar.mn
+++ b/testing/specialpowers/jar.mn
@@ -2,9 +2,10 @@ specialpowers.jar:
 % content specialpowers %content/
   content/specialpowers.js (content/specialpowers.js)
   content/specialpowersAPI.js (content/specialpowersAPI.js)
   content/SpecialPowersObserverAPI.js (content/SpecialPowersObserverAPI.js)
   content/MozillaLogger.js (content/MozillaLogger.js)
 
 % resource specialpowers %modules/
   modules/MockFilePicker.jsm (content/MockFilePicker.jsm)
+  modules/MockColorPicker.jsm (content/MockColorPicker.jsm)
   modules/MockPermissionPrompt.jsm (content/MockPermissionPrompt.jsm)