test_xpcomutils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*-
  2. * vim: sw=4 ts=4 sts=4 et
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /**
  7. * This file tests the methods on XPCOMUtils.jsm.
  8. */
  9. Components.utils.import("resource://gre/modules/Preferences.jsm");
  10. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  11. const Cc = Components.classes;
  12. const Ci = Components.interfaces;
  13. ////////////////////////////////////////////////////////////////////////////////
  14. //// Tests
  15. add_test(function test_generateQI_string_names()
  16. {
  17. var x = {
  18. QueryInterface: XPCOMUtils.generateQI([
  19. Components.interfaces.nsIClassInfo,
  20. "nsIDOMNode"
  21. ])
  22. };
  23. try {
  24. x.QueryInterface(Components.interfaces.nsIClassInfo);
  25. } catch(e) {
  26. do_throw("Should QI to nsIClassInfo");
  27. }
  28. try {
  29. x.QueryInterface(Components.interfaces.nsIDOMNode);
  30. } catch(e) {
  31. do_throw("Should QI to nsIDOMNode");
  32. }
  33. try {
  34. x.QueryInterface(Components.interfaces.nsIDOMDocument);
  35. do_throw("QI should not have succeeded!");
  36. } catch(e) {}
  37. run_next_test();
  38. });
  39. add_test(function test_generateCI()
  40. {
  41. const classID = Components.ID("562dae2e-7cff-432b-995b-3d4c03fa2b89");
  42. const classDescription = "generateCI test component";
  43. const flags = Components.interfaces.nsIClassInfo.DOM_OBJECT;
  44. var x = {
  45. QueryInterface: XPCOMUtils.generateQI([]),
  46. classInfo: XPCOMUtils.generateCI({classID: classID,
  47. interfaces: [],
  48. flags: flags,
  49. classDescription: classDescription})
  50. };
  51. try {
  52. var ci = x.QueryInterface(Components.interfaces.nsIClassInfo);
  53. ci = ci.QueryInterface(Components.interfaces.nsISupports);
  54. ci = ci.QueryInterface(Components.interfaces.nsIClassInfo);
  55. do_check_eq(ci.classID, classID);
  56. do_check_eq(ci.flags, flags);
  57. do_check_eq(ci.classDescription, classDescription);
  58. } catch(e) {
  59. do_throw("Classinfo for x should not be missing or broken");
  60. }
  61. run_next_test();
  62. });
  63. add_test(function test_defineLazyGetter()
  64. {
  65. let accessCount = 0;
  66. let obj = {
  67. inScope: false
  68. };
  69. const TEST_VALUE = "test value";
  70. XPCOMUtils.defineLazyGetter(obj, "foo", function() {
  71. accessCount++;
  72. this.inScope = true;
  73. return TEST_VALUE;
  74. });
  75. do_check_eq(accessCount, 0);
  76. // Get the property, making sure the access count has increased.
  77. do_check_eq(obj.foo, TEST_VALUE);
  78. do_check_eq(accessCount, 1);
  79. do_check_true(obj.inScope);
  80. // Get the property once more, making sure the access count has not
  81. // increased.
  82. do_check_eq(obj.foo, TEST_VALUE);
  83. do_check_eq(accessCount, 1);
  84. run_next_test();
  85. });
  86. add_test(function test_defineLazyServiceGetter()
  87. {
  88. let obj = { };
  89. XPCOMUtils.defineLazyServiceGetter(obj, "service",
  90. "@mozilla.org/consoleservice;1",
  91. "nsIConsoleService");
  92. let service = Cc["@mozilla.org/consoleservice;1"].
  93. getService(Ci.nsIConsoleService);
  94. // Check that the lazy service getter and the actual service have the same
  95. // properties.
  96. for (let prop in obj.service)
  97. do_check_true(prop in service);
  98. for (let prop in service)
  99. do_check_true(prop in obj.service);
  100. run_next_test();
  101. });
  102. add_test(function test_defineLazyPreferenceGetter()
  103. {
  104. const PREF = "xpcomutils.test.pref";
  105. let obj = {};
  106. XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
  107. equal(obj.pref, "defaultValue", "Should return the default value before pref is set");
  108. Preferences.set(PREF, "currentValue");
  109. do_print("Create second getter on new object");
  110. obj = {};
  111. XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
  112. equal(obj.pref, "currentValue", "Should return the current value on initial read when pref is already set");
  113. Preferences.set(PREF, "newValue");
  114. equal(obj.pref, "newValue", "Should return new value after preference change");
  115. Preferences.set(PREF, "currentValue");
  116. equal(obj.pref, "currentValue", "Should return new value after second preference change");
  117. Preferences.reset(PREF);
  118. equal(obj.pref, "defaultValue", "Should return default value after pref is reset");
  119. run_next_test();
  120. });
  121. add_test(function test_categoryRegistration()
  122. {
  123. const CATEGORY_NAME = "test-cat";
  124. const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
  125. const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}");
  126. // Create a fake app entry for our category registration apps filter.
  127. let tmp = {};
  128. Components.utils.import("resource://testing-common/AppInfo.jsm", tmp);
  129. let XULAppInfo = tmp.newAppInfo({
  130. name: "catRegTest",
  131. ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}",
  132. version: "1",
  133. platformVersion: "",
  134. });
  135. let XULAppInfoFactory = {
  136. createInstance: function (outer, iid) {
  137. if (outer != null)
  138. throw Cr.NS_ERROR_NO_AGGREGATION;
  139. return XULAppInfo.QueryInterface(iid);
  140. }
  141. };
  142. let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  143. registrar.registerFactory(
  144. XULAPPINFO_CID,
  145. "XULAppInfo",
  146. XULAPPINFO_CONTRACTID,
  147. XULAppInfoFactory
  148. );
  149. // Load test components.
  150. do_load_manifest("CatRegistrationComponents.manifest");
  151. const EXPECTED_ENTRIES = new Map([
  152. ["CatRegisteredComponent", "@unit.test.com/cat-registered-component;1"],
  153. ["CatAppRegisteredComponent", "@unit.test.com/cat-app-registered-component;1"],
  154. ]);
  155. // Verify the correct entries are registered in the "test-cat" category.
  156. for (let [name, value] of XPCOMUtils.enumerateCategoryEntries(CATEGORY_NAME)) {
  157. print("Verify that the name/value pair exists in the expected entries.");
  158. ok(EXPECTED_ENTRIES.has(name));
  159. do_check_eq(EXPECTED_ENTRIES.get(name), value);
  160. EXPECTED_ENTRIES.delete(name);
  161. }
  162. print("Check that all of the expected entries have been deleted.");
  163. do_check_eq(EXPECTED_ENTRIES.size, 0);
  164. run_next_test();
  165. });
  166. add_test(function test_generateSingletonFactory()
  167. {
  168. const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1";
  169. const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}");
  170. function XPCComponent() {}
  171. XPCComponent.prototype = {
  172. classID: XPCCOMPONENT_CID,
  173. _xpcom_factory: XPCOMUtils.generateSingletonFactory(XPCComponent),
  174. QueryInterface: XPCOMUtils.generateQI([])
  175. };
  176. let NSGetFactory = XPCOMUtils.generateNSGetFactory([XPCComponent]);
  177. let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  178. registrar.registerFactory(
  179. XPCCOMPONENT_CID,
  180. "XPCComponent",
  181. XPCCOMPONENT_CONTRACTID,
  182. NSGetFactory(XPCCOMPONENT_CID)
  183. );
  184. // First, try to instance the component.
  185. let instance = Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports);
  186. // Try again, check that it returns the same instance as before.
  187. do_check_eq(instance,
  188. Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports));
  189. // Now, for sanity, check that getService is also returning the same instance.
  190. do_check_eq(instance,
  191. Cc[XPCCOMPONENT_CONTRACTID].getService(Ci.nsISupports));
  192. run_next_test();
  193. });
  194. ////////////////////////////////////////////////////////////////////////////////
  195. //// Test Runner
  196. function run_test()
  197. {
  198. run_next_test();
  199. }