michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["MockColorPicker"]; michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cm = Components.manager; michael@0: const Cu = Components.utils; michael@0: michael@0: const CONTRACT_ID = "@mozilla.org/colorpicker;1"; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: var oldClassID = "", oldFactory = null; michael@0: var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID(); michael@0: var newFactory = function (window) { michael@0: return { michael@0: createInstance: function(aOuter, aIID) { michael@0: if (aOuter) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return new MockColorPickerInstance(window).QueryInterface(aIID); michael@0: }, michael@0: lockFactory: function(aLock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) michael@0: }; michael@0: } michael@0: michael@0: this.MockColorPicker = { michael@0: init: function(window) { michael@0: this.reset(); michael@0: this.factory = newFactory(window); michael@0: if (!registrar.isCIDRegistered(newClassID)) { michael@0: try { michael@0: oldClassID = registrar.contractIDToCID(CONTRACT_ID); michael@0: oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: } catch(ex) { michael@0: oldClassID = ""; michael@0: oldFactory = null; michael@0: dump("TEST-INFO | can't get colorpicker registered component, " + michael@0: "assuming there is none"); michael@0: } michael@0: if (oldClassID != "" && oldFactory != null) { michael@0: registrar.unregisterFactory(oldClassID, oldFactory); michael@0: } michael@0: registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory); michael@0: } michael@0: }, michael@0: michael@0: reset: function() { michael@0: this.returnColor = ""; michael@0: this.showCallback = null; michael@0: this.shown = false; michael@0: this.showing = false; michael@0: }, michael@0: michael@0: cleanup: function() { michael@0: var previousFactory = this.factory; michael@0: this.reset(); michael@0: this.factory = null; michael@0: michael@0: registrar.unregisterFactory(newClassID, previousFactory); michael@0: if (oldClassID != "" && oldFactory != null) { michael@0: registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: function MockColorPickerInstance(window) { michael@0: this.window = window; michael@0: }; michael@0: MockColorPickerInstance.prototype = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIColorPicker]), michael@0: init: function(aParent, aTitle, aInitialColor) { michael@0: this.parent = aParent; michael@0: this.initialColor = aInitialColor; michael@0: }, michael@0: initialColor: "", michael@0: parent: null, michael@0: open: function(aColorPickerShownCallback) { michael@0: MockColorPicker.showing = true; michael@0: MockColorPicker.shown = true; michael@0: michael@0: this.window.setTimeout(function() { michael@0: let result = ""; michael@0: try { michael@0: if (typeof MockColorPicker.showCallback == "function") { michael@0: var updateCb = function(color) { michael@0: result = color; michael@0: aColorPickerShownCallback.update(color); michael@0: }; michael@0: let returnColor = MockColorPicker.showCallback(this, updateCb); michael@0: if (typeof returnColor === "string") { michael@0: result = returnColor; michael@0: } michael@0: } else if (typeof MockColorPicker.returnColor === "string") { michael@0: result = MockColorPicker.returnColor; michael@0: } michael@0: } catch(ex) { michael@0: dump("TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.jsm open() " + michael@0: "method: " + ex + "\n"); michael@0: } michael@0: if (aColorPickerShownCallback) { michael@0: aColorPickerShownCallback.done(result); michael@0: } michael@0: }.bind(this), 0); michael@0: } michael@0: }; michael@0: michael@0: // Expose everything to content. We call reset() here so that all of the michael@0: // relevant lazy expandos get added. michael@0: MockColorPicker.reset(); michael@0: function exposeAll(obj) { michael@0: var props = {}; michael@0: for (var prop in obj) michael@0: props[prop] = 'rw'; michael@0: obj.__exposedProps__ = props; michael@0: } michael@0: exposeAll(MockColorPicker); michael@0: exposeAll(MockColorPickerInstance.prototype);