File: MockObjects.js

package info (click to toggle)
thunderbird 1%3A115.16.0esr-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,476,252 kB
  • sloc: cpp: 6,972,150; javascript: 5,209,211; ansic: 3,507,222; python: 1,137,609; asm: 432,531; xml: 205,149; java: 175,761; sh: 116,485; makefile: 22,152; perl: 13,971; objc: 12,561; yacc: 4,583; pascal: 2,840; lex: 1,720; ruby: 1,075; exp: 762; sql: 666; awk: 580; php: 436; lisp: 430; sed: 70; csh: 10
file content (95 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* 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/. */

/**
 * Allows registering a mock XPCOM component, that temporarily replaces the
 *  original one when an object implementing a given ContractID is requested
 *  using createInstance.
 *
 * @param aContractID
 *        The ContractID of the component to replace, for example
 *        "@mozilla.org/filepicker;1".
 *
 * @param aReplacementCtor
 *        The constructor function for the JavaScript object that will be
 *        created every time createInstance is called. This object must
 *        implement QueryInterface and provide the XPCOM interfaces required by
 *        the specified ContractID (for example
 *        Components.interfaces.nsIFilePicker).
 */

function MockObjectRegisterer(aContractID, aReplacementCtor) {
  this._contractID = aContractID;
  this._replacementCtor = aReplacementCtor;
}

MockObjectRegisterer.prototype = {
  /**
   * Replaces the current factory with one that returns a new mock object.
   *
   * After register() has been called, it is mandatory to call unregister() to
   * restore the original component. Usually, you should use a try-catch block
   * to ensure that unregister() is called.
   */
  register: function MOR_register() {
    if (this._originalCID) {
      throw new Error("Invalid object state when calling register()");
    }

    // Define a factory that creates a new object using the given constructor.
    var isChrome = location.protocol == "chrome:";
    var providedConstructor = this._replacementCtor;
    this._mockFactory = {
      createInstance: function MF_createInstance(aIid) {
        var inst = new providedConstructor().QueryInterface(aIid);
        if (!isChrome) {
          inst = SpecialPowers.wrapCallbackObject(inst);
        }
        return inst;
      },
    };
    if (!isChrome) {
      this._mockFactory = SpecialPowers.wrapCallbackObject(this._mockFactory);
    }

    var retVal = SpecialPowers.swapFactoryRegistration(
      null,
      this._contractID,
      this._mockFactory
    );
    if ("error" in retVal) {
      throw new Error("ERROR: " + retVal.error);
    } else {
      this._originalCID = retVal.originalCID;
    }
  },

  /**
   * Restores the original factory.
   */
  unregister: function MOR_unregister() {
    if (!this._originalCID) {
      throw new Error("Invalid object state when calling unregister()");
    }

    // Free references to the mock factory.
    SpecialPowers.swapFactoryRegistration(this._originalCID, this._contractID);

    // Allow registering a mock factory again later.
    this._originalCID = null;
    this._mockFactory = null;
  },

  // --- Private methods and properties ---

  /**
   * The factory of the component being replaced.
   */
  _originalCID: null,

  /**
   * The nsIFactory that was automatically generated by this object.
   */
  _mockFactory: null,
};