testing/specialpowers/content/MockColorPicker.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = ["MockColorPicker"];
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cm = Components.manager;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 const CONTRACT_ID = "@mozilla.org/colorpicker;1";
michael@0 13
michael@0 14 Cu.import("resource://gre/modules/Services.jsm");
michael@0 15 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 16
michael@0 17 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 18 var oldClassID = "", oldFactory = null;
michael@0 19 var newClassID = Cc["@mozilla.org/uuid-generator;1"].getService(Ci.nsIUUIDGenerator).generateUUID();
michael@0 20 var newFactory = function (window) {
michael@0 21 return {
michael@0 22 createInstance: function(aOuter, aIID) {
michael@0 23 if (aOuter)
michael@0 24 throw Components.results.NS_ERROR_NO_AGGREGATION;
michael@0 25 return new MockColorPickerInstance(window).QueryInterface(aIID);
michael@0 26 },
michael@0 27 lockFactory: function(aLock) {
michael@0 28 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
michael@0 29 },
michael@0 30 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
michael@0 31 };
michael@0 32 }
michael@0 33
michael@0 34 this.MockColorPicker = {
michael@0 35 init: function(window) {
michael@0 36 this.reset();
michael@0 37 this.factory = newFactory(window);
michael@0 38 if (!registrar.isCIDRegistered(newClassID)) {
michael@0 39 try {
michael@0 40 oldClassID = registrar.contractIDToCID(CONTRACT_ID);
michael@0 41 oldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory);
michael@0 42 } catch(ex) {
michael@0 43 oldClassID = "";
michael@0 44 oldFactory = null;
michael@0 45 dump("TEST-INFO | can't get colorpicker registered component, " +
michael@0 46 "assuming there is none");
michael@0 47 }
michael@0 48 if (oldClassID != "" && oldFactory != null) {
michael@0 49 registrar.unregisterFactory(oldClassID, oldFactory);
michael@0 50 }
michael@0 51 registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
michael@0 52 }
michael@0 53 },
michael@0 54
michael@0 55 reset: function() {
michael@0 56 this.returnColor = "";
michael@0 57 this.showCallback = null;
michael@0 58 this.shown = false;
michael@0 59 this.showing = false;
michael@0 60 },
michael@0 61
michael@0 62 cleanup: function() {
michael@0 63 var previousFactory = this.factory;
michael@0 64 this.reset();
michael@0 65 this.factory = null;
michael@0 66
michael@0 67 registrar.unregisterFactory(newClassID, previousFactory);
michael@0 68 if (oldClassID != "" && oldFactory != null) {
michael@0 69 registrar.registerFactory(oldClassID, "", CONTRACT_ID, oldFactory);
michael@0 70 }
michael@0 71 }
michael@0 72 };
michael@0 73
michael@0 74 function MockColorPickerInstance(window) {
michael@0 75 this.window = window;
michael@0 76 };
michael@0 77 MockColorPickerInstance.prototype = {
michael@0 78 QueryInterface: XPCOMUtils.generateQI([Ci.nsIColorPicker]),
michael@0 79 init: function(aParent, aTitle, aInitialColor) {
michael@0 80 this.parent = aParent;
michael@0 81 this.initialColor = aInitialColor;
michael@0 82 },
michael@0 83 initialColor: "",
michael@0 84 parent: null,
michael@0 85 open: function(aColorPickerShownCallback) {
michael@0 86 MockColorPicker.showing = true;
michael@0 87 MockColorPicker.shown = true;
michael@0 88
michael@0 89 this.window.setTimeout(function() {
michael@0 90 let result = "";
michael@0 91 try {
michael@0 92 if (typeof MockColorPicker.showCallback == "function") {
michael@0 93 var updateCb = function(color) {
michael@0 94 result = color;
michael@0 95 aColorPickerShownCallback.update(color);
michael@0 96 };
michael@0 97 let returnColor = MockColorPicker.showCallback(this, updateCb);
michael@0 98 if (typeof returnColor === "string") {
michael@0 99 result = returnColor;
michael@0 100 }
michael@0 101 } else if (typeof MockColorPicker.returnColor === "string") {
michael@0 102 result = MockColorPicker.returnColor;
michael@0 103 }
michael@0 104 } catch(ex) {
michael@0 105 dump("TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.jsm open() " +
michael@0 106 "method: " + ex + "\n");
michael@0 107 }
michael@0 108 if (aColorPickerShownCallback) {
michael@0 109 aColorPickerShownCallback.done(result);
michael@0 110 }
michael@0 111 }.bind(this), 0);
michael@0 112 }
michael@0 113 };
michael@0 114
michael@0 115 // Expose everything to content. We call reset() here so that all of the
michael@0 116 // relevant lazy expandos get added.
michael@0 117 MockColorPicker.reset();
michael@0 118 function exposeAll(obj) {
michael@0 119 var props = {};
michael@0 120 for (var prop in obj)
michael@0 121 props[prop] = 'rw';
michael@0 122 obj.__exposedProps__ = props;
michael@0 123 }
michael@0 124 exposeAll(MockColorPicker);
michael@0 125 exposeAll(MockColorPickerInstance.prototype);

mercurial