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.

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

mercurial