test-xpcom.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. const xpcom = require("sdk/platform/xpcom");
  5. const { Cc, Ci, Cm, Cr } = require("chrome");
  6. const { isCIDRegistered } = Cm.QueryInterface(Ci.nsIComponentRegistrar);
  7. const { Class } = require("sdk/core/heritage");
  8. const { Loader } = require("sdk/test/loader");
  9. exports['test Unknown implements nsISupports'] = function(assert) {
  10. let actual = xpcom.Unknown();
  11. assert.equal(actual.QueryInterface(Ci.nsISupports),
  12. actual,
  13. 'component implements nsISupports');
  14. };
  15. exports['test implement xpcom interfaces'] = function(assert) {
  16. let WeakReference = Class({
  17. extends: xpcom.Unknown,
  18. interfaces: [ 'nsIWeakReference' ],
  19. QueryReferent: function() {}
  20. });
  21. let weakReference = WeakReference()
  22. assert.equal(weakReference.QueryInterface(Ci.nsISupports),
  23. weakReference,
  24. 'component implements nsISupports');
  25. assert.equal(weakReference.QueryInterface(Ci.nsIWeakReference),
  26. weakReference,
  27. 'component implements specified interface');
  28. assert.throws(function() {
  29. component.QueryInterface(Ci.nsIObserver);
  30. }, "component does not implements interface");
  31. let Observer = Class({
  32. extends: WeakReference,
  33. interfaces: [ 'nsIObserver', 'nsIRequestObserver' ],
  34. observe: function() {},
  35. onStartRequest: function() {},
  36. onStopRequest: function() {}
  37. });
  38. let observer = Observer()
  39. assert.equal(observer.QueryInterface(Ci.nsISupports),
  40. observer,
  41. 'derived component implements nsISupports');
  42. assert.equal(observer.QueryInterface(Ci.nsIWeakReference),
  43. observer,
  44. 'derived component implements supers interface');
  45. assert.equal(observer.QueryInterface(Ci.nsIObserver),
  46. observer.QueryInterface(Ci.nsIRequestObserver),
  47. 'derived component implements specified interfaces');
  48. };
  49. exports['test implement factory without contract'] = function(assert) {
  50. let actual = xpcom.Factory({
  51. get wrappedJSObject() this,
  52. });
  53. assert.ok(isCIDRegistered(actual.id), 'factory is regiseterd');
  54. xpcom.unregister(actual);
  55. assert.ok(!isCIDRegistered(actual.id), 'factory is unregistered');
  56. };
  57. exports['test implement xpcom factory'] = function(assert) {
  58. let Component = Class({
  59. extends: xpcom.Unknown,
  60. interfaces: [ 'nsIObserver' ],
  61. get wrappedJSObject() this,
  62. observe: function() {}
  63. });
  64. let factory = xpcom.Factory({
  65. register: false,
  66. contract: '@jetpack/test/factory;1',
  67. Component: Component
  68. });
  69. assert.ok(!isCIDRegistered(factory.id), 'factory is not registered');
  70. xpcom.register(factory);
  71. assert.ok(isCIDRegistered(factory.id), 'factory is registered');
  72. let actual = Cc[factory.contract].createInstance(Ci.nsIObserver);
  73. assert.ok(actual.wrappedJSObject instanceof Component,
  74. "createInstance returnes wrapped factory instances");
  75. assert.notEqual(Cc[factory.contract].createInstance(Ci.nsIObserver),
  76. Cc[factory.contract].createInstance(Ci.nsIObserver),
  77. "createInstance returns new instance each time");
  78. };
  79. exports['test implement xpcom service'] = function(assert) {
  80. let actual = xpcom.Service({
  81. contract: '@jetpack/test/service;1',
  82. register: false,
  83. Component: Class({
  84. extends: xpcom.Unknown,
  85. interfaces: [ 'nsIObserver'],
  86. get wrappedJSObject() this,
  87. observe: function() {},
  88. name: 'my-service'
  89. })
  90. });
  91. assert.ok(!isCIDRegistered(actual.id), 'component is not registered');
  92. xpcom.register(actual);
  93. assert.ok(isCIDRegistered(actual.id), 'service is regiseterd');
  94. assert.ok(Cc[actual.contract].getService(Ci.nsIObserver).observe,
  95. 'service can be accessed via get service');
  96. assert.equal(Cc[actual.contract].getService(Ci.nsIObserver).wrappedJSObject,
  97. actual.component,
  98. 'wrappedJSObject is an actual component');
  99. xpcom.unregister(actual);
  100. assert.ok(!isCIDRegistered(actual.id), 'service is unregistered');
  101. };
  102. function testRegister(assert, text) {
  103. const service = xpcom.Service({
  104. description: 'test about:boop page',
  105. contract: '@mozilla.org/network/protocol/about;1?what=boop',
  106. register: false,
  107. Component: Class({
  108. extends: xpcom.Unknown,
  109. get wrappedJSObject() this,
  110. interfaces: [ 'nsIAboutModule' ],
  111. newChannel : function(aURI) {
  112. var ios = Cc["@mozilla.org/network/io-service;1"].
  113. getService(Ci.nsIIOService);
  114. var channel = ios.newChannel(
  115. "data:text/plain;charset=utf-8," + text,
  116. null,
  117. null
  118. );
  119. channel.originalURI = aURI;
  120. return channel;
  121. },
  122. getURIFlags: function(aURI) {
  123. return Ci.nsIAboutModule.ALLOW_SCRIPT;
  124. }
  125. })
  126. });
  127. xpcom.register(service);
  128. assert.equal(isCIDRegistered(service.id), true);
  129. var aboutFactory = xpcom.factoryByContract(service.contract);
  130. var about = aboutFactory.createInstance(Ci.nsIAboutModule);
  131. var ios = Cc["@mozilla.org/network/io-service;1"].
  132. getService(Ci.nsIIOService);
  133. assert.equal(
  134. about.getURIFlags(ios.newURI("http://foo.com", null, null)),
  135. Ci.nsIAboutModule.ALLOW_SCRIPT
  136. );
  137. var aboutURI = ios.newURI("about:boop", null, null);
  138. var channel = ios.newChannelFromURI(aboutURI);
  139. var iStream = channel.open();
  140. var siStream = Cc['@mozilla.org/scriptableinputstream;1']
  141. .createInstance(Ci.nsIScriptableInputStream);
  142. siStream.init(iStream);
  143. var data = new String();
  144. data += siStream.read(-1);
  145. siStream.close();
  146. iStream.close();
  147. assert.equal(data, text);
  148. xpcom.unregister(service);
  149. assert.equal(isCIDRegistered(service.id), false);
  150. }
  151. exports["test register"] = function(assert) {
  152. testRegister(assert, "hai2u");
  153. };
  154. exports["test re-register"] = function(assert) {
  155. testRegister(assert, "hai2u again");
  156. };
  157. exports["test unload"] = function(assert) {
  158. let loader = Loader(module);
  159. let sbxpcom = loader.require("sdk/platform/xpcom");
  160. let auto = sbxpcom.Factory({
  161. contract: "@mozilla.org/test/auto-unload;1",
  162. description: "test auto",
  163. name: "auto"
  164. });
  165. let manual = sbxpcom.Factory({
  166. contract: "@mozilla.org/test/manual-unload;1",
  167. description: "test manual",
  168. register: false,
  169. unregister: false,
  170. name: "manual"
  171. });
  172. assert.equal(isCIDRegistered(auto.id), true, 'component registered');
  173. assert.equal(isCIDRegistered(manual.id), false, 'component not registered');
  174. sbxpcom.register(manual)
  175. assert.equal(isCIDRegistered(manual.id), true,
  176. 'component was automatically registered on first instance');
  177. loader.unload();
  178. assert.equal(isCIDRegistered(auto.id), false,
  179. 'component was atumatically unregistered on unload');
  180. assert.equal(isCIDRegistered(manual.id), true,
  181. 'component was not automatically unregistered on unload');
  182. sbxpcom.unregister(manual);
  183. assert.equal(isCIDRegistered(manual.id), false,
  184. 'component was manually unregistered on unload');
  185. };
  186. require("test").run(exports);