PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/content/tests/browser/common/mockObjects.js

http://github.com/zpao/v8monkey
JavaScript | 122 lines | 36 code | 12 blank | 74 comment | 6 complexity | b992dc26eddca77de235e072654b5d00 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  4 * The contents of this file are subject to the Mozilla Public License Version
  5 * 1.1 (the "License"); you may not use this file except in compliance with
  6 * the License. You may obtain a copy of the License at
  7 * http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 11 * for the specific language governing rights and limitations under the
 12 * License.
 13 *
 14 * The Original Code is Mozilla XUL Toolkit Testing Code.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Paolo Amadini <http://www.amadzone.org/>.
 18 * Portions created by the Initial Developer are Copyright (C) 2009
 19 * the Initial Developer. All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *
 23 * Alternatively, the contents of this file may be used under the terms of
 24 * either the GNU General Public License Version 2 or later (the "GPL"), or
 25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 26 * in which case the provisions of the GPL or the LGPL are applicable instead
 27 * of those above. If you wish to allow use of your version of this file only
 28 * under the terms of either the GPL or the LGPL, and not to allow others to
 29 * use your version of this file under the terms of the MPL, indicate your
 30 * decision by deleting the provisions above and replace them with the notice
 31 * and other provisions required by the GPL or the LGPL. If you do not delete
 32 * the provisions above, a recipient may use your version of this file under
 33 * the terms of any one of the MPL, the GPL or the LGPL.
 34 *
 35 * ***** END LICENSE BLOCK ***** */
 36
 37/**
 38 * Allows registering a mock XPCOM component, that temporarily replaces the
 39 *  original one when an object implementing a given ContractID is requested
 40 *  using createInstance.
 41 *
 42 * @param aContractID
 43 *        The ContractID of the component to replace, for example
 44 *        "@mozilla.org/filepicker;1".
 45 *
 46 * @param aReplacementCtor
 47 *        The constructor function for the JavaScript object that will be
 48 *        created every time createInstance is called. This object must
 49 *        implement QueryInterface and provide the XPCOM interfaces required by
 50 *        the specified ContractID (for example
 51 *        Components.interfaces.nsIFilePicker).
 52 */
 53
 54function MockObjectRegisterer(aContractID, aReplacementCtor) {
 55  this._contractID = aContractID;
 56  this._replacementCtor = aReplacementCtor;
 57}
 58
 59MockObjectRegisterer.prototype = {
 60  /**
 61   * Replaces the current factory with one that returns a new mock object.
 62   *
 63   * After register() has been called, it is mandatory to call unregister() to
 64   * restore the original component. Usually, you should use a try-catch block
 65   * to ensure that unregister() is called.
 66   */
 67  register: function MOR_register() {
 68    if (this._originalFactory)
 69      throw new Exception("Invalid object state when calling register()");
 70
 71    // Define a factory that creates a new object using the given constructor.
 72    var providedConstructor = this._replacementCtor;
 73    this._mockFactory = {
 74      createInstance: function MF_createInstance(aOuter, aIid) {
 75        if (aOuter != null)
 76          throw Components.results.NS_ERROR_NO_AGGREGATION;
 77        return new providedConstructor().QueryInterface(aIid);
 78      }
 79    };
 80
 81    var retVal = SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
 82    if ('error' in retVal) {
 83      throw new Exception("ERROR: " + retVal.error);
 84    } else {
 85      this._cid = retVal.cid;
 86      this._originalFactory = retVal.originalFactory;
 87    }
 88  },
 89
 90  /**
 91   * Restores the original factory.
 92   */
 93  unregister: function MOR_unregister() {
 94    if (!this._originalFactory)
 95      throw new Exception("Invalid object state when calling unregister()");
 96
 97    // Free references to the mock factory.
 98    SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
 99
100    // Allow registering a mock factory again later.
101    this._cid = null;
102    this._originalFactory = null;
103    this._mockFactory = null;
104  },
105
106  // --- Private methods and properties ---
107
108  /**
109   * The factory of the component being replaced.
110   */
111  _originalFactory: null,
112
113  /**
114   * The CID under which the mock contractID was registered.
115   */
116  _cid: null,
117
118  /**
119   * The nsIFactory that was automatically generated by this object.
120   */
121  _mockFactory: null
122}