tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MockColorPicker.sys.mjs (3610B)


      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/. */
      4 
      5 const lazy = {};
      6 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  WrapPrivileged: "resource://testing-common/WrapPrivileged.sys.mjs",
      9 });
     10 
     11 const Cm = Components.manager;
     12 
     13 const CONTRACT_ID = "@mozilla.org/colorpicker;1";
     14 
     15 Cu.crashIfNotInAutomation();
     16 
     17 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
     18 var oldClassID = "";
     19 var newClassID = Services.uuid.generateUUID();
     20 var newFactory = function (window) {
     21  return {
     22    createInstance(aIID) {
     23      return new MockColorPickerInstance(window).QueryInterface(aIID);
     24    },
     25    QueryInterface: ChromeUtils.generateQI(["nsIFactory"]),
     26  };
     27 };
     28 
     29 export var MockColorPicker = {
     30  init(window) {
     31    this.reset();
     32    this.factory = newFactory(window);
     33    if (!registrar.isCIDRegistered(newClassID)) {
     34      try {
     35        oldClassID = registrar.contractIDToCID(CONTRACT_ID);
     36      } catch (ex) {
     37        oldClassID = "";
     38        dump(
     39          "TEST-INFO | can't get colorpicker registered component, " +
     40            "assuming there is none"
     41        );
     42      }
     43      registrar.registerFactory(newClassID, "", CONTRACT_ID, this.factory);
     44    }
     45  },
     46 
     47  reset() {
     48    this.returnColor = "";
     49    this.showCallback = null;
     50    this.shown = false;
     51    this.showing = false;
     52  },
     53 
     54  cleanup() {
     55    var previousFactory = this.factory;
     56    this.reset();
     57    this.factory = null;
     58 
     59    registrar.unregisterFactory(newClassID, previousFactory);
     60    if (oldClassID != "") {
     61      registrar.registerFactory(oldClassID, "", CONTRACT_ID, null);
     62    }
     63  },
     64 };
     65 
     66 function MockColorPickerInstance(window) {
     67  this.window = window;
     68  this.showCallback = null;
     69  this.showCallbackWrapped = null;
     70 }
     71 MockColorPickerInstance.prototype = {
     72  QueryInterface: ChromeUtils.generateQI(["nsIColorPicker"]),
     73  init(aBrowsingContext, aTitle, aInitialColor, aDefaultColors) {
     74    this.browsingContext = aBrowsingContext;
     75    this.initialColor = aInitialColor;
     76    this.defaultColors = aDefaultColors;
     77  },
     78  initialColor: "",
     79  browsingContext: null,
     80  open(aColorPickerShownCallback) {
     81    MockColorPicker.showing = true;
     82    MockColorPicker.shown = true;
     83 
     84    this.window.setTimeout(() => {
     85      let result = "";
     86      try {
     87        if (typeof MockColorPicker.showCallback == "function") {
     88          if (MockColorPicker.showCallback != this.showCallback) {
     89            this.showCallback = MockColorPicker.showCallback;
     90            if (Cu.isXrayWrapper(this.window)) {
     91              this.showCallbackWrapped = lazy.WrapPrivileged.wrapCallback(
     92                MockColorPicker.showCallback,
     93                this.window
     94              );
     95            } else {
     96              this.showCallbackWrapped = this.showCallback;
     97            }
     98          }
     99          var updateCb = function (color) {
    100            result = color;
    101            aColorPickerShownCallback.update(color);
    102          };
    103          let returnColor = this.showCallbackWrapped(this, updateCb);
    104          if (typeof returnColor === "string") {
    105            result = returnColor;
    106          }
    107        } else if (typeof MockColorPicker.returnColor === "string") {
    108          result = MockColorPicker.returnColor;
    109        }
    110      } catch (ex) {
    111        dump(
    112          "TEST-UNEXPECTED-FAIL | Exception in MockColorPicker.sys.mjs open() " +
    113            "method: " +
    114            ex +
    115            "\n"
    116        );
    117      }
    118      if (aColorPickerShownCallback) {
    119        aColorPickerShownCallback.done(result);
    120      }
    121    }, 0);
    122  },
    123 };