/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. * Allows registering a mock XPCOM component, that temporarily replaces the
  38. * original one when an object implementing a given ContractID is requested
  39. * using createInstance.
  40. *
  41. * @param aContractID
  42. * The ContractID of the component to replace, for example
  43. * "@mozilla.org/filepicker;1".
  44. *
  45. * @param aReplacementCtor
  46. * The constructor function for the JavaScript object that will be
  47. * created every time createInstance is called. This object must
  48. * implement QueryInterface and provide the XPCOM interfaces required by
  49. * the specified ContractID (for example
  50. * Components.interfaces.nsIFilePicker).
  51. */
  52. function MockObjectRegisterer(aContractID, aReplacementCtor) {
  53. this._contractID = aContractID;
  54. this._replacementCtor = aReplacementCtor;
  55. }
  56. MockObjectRegisterer.prototype = {
  57. /**
  58. * Replaces the current factory with one that returns a new mock object.
  59. *
  60. * After register() has been called, it is mandatory to call unregister() to
  61. * restore the original component. Usually, you should use a try-catch block
  62. * to ensure that unregister() is called.
  63. */
  64. register: function MOR_register() {
  65. if (this._originalFactory)
  66. throw new Exception("Invalid object state when calling register()");
  67. // Define a factory that creates a new object using the given constructor.
  68. var providedConstructor = this._replacementCtor;
  69. this._mockFactory = {
  70. createInstance: function MF_createInstance(aOuter, aIid) {
  71. if (aOuter != null)
  72. throw Components.results.NS_ERROR_NO_AGGREGATION;
  73. return new providedConstructor().QueryInterface(aIid);
  74. }
  75. };
  76. var retVal = SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
  77. if ('error' in retVal) {
  78. throw new Exception("ERROR: " + retVal.error);
  79. } else {
  80. this._cid = retVal.cid;
  81. this._originalFactory = retVal.originalFactory;
  82. }
  83. },
  84. /**
  85. * Restores the original factory.
  86. */
  87. unregister: function MOR_unregister() {
  88. if (!this._originalFactory)
  89. throw new Exception("Invalid object state when calling unregister()");
  90. // Free references to the mock factory.
  91. SpecialPowers.swapFactoryRegistration(this._cid, this._contractID, this._mockFactory, this._originalFactory);
  92. // Allow registering a mock factory again later.
  93. this._cid = null;
  94. this._originalFactory = null;
  95. this._mockFactory = null;
  96. },
  97. // --- Private methods and properties ---
  98. /**
  99. * The factory of the component being replaced.
  100. */
  101. _originalFactory: null,
  102. /**
  103. * The CID under which the mock contractID was registered.
  104. */
  105. _cid: null,
  106. /**
  107. * The nsIFactory that was automatically generated by this object.
  108. */
  109. _mockFactory: null
  110. }