test_notxpcom_scriptable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. var Cc = Components.classes;
  6. var Ci = Components.interfaces;
  7. var Cu = Components.utils;
  8. var Cr = Components.results;
  9. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  10. const kCID = Components.ID("{1f9f7181-e6c5-4f4c-8f71-08005cec8468}");
  11. const kContract = "@testing/notxpcomtest";
  12. function run_test()
  13. {
  14. let manifest = do_get_file("xpcomtest.manifest");
  15. let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
  16. registrar.autoRegister(manifest);
  17. ok(Ci.ScriptableWithNotXPCOM);
  18. let method1Called = false;
  19. let testObject = {
  20. QueryInterface: XPCOMUtils.generateQI([Ci.ScriptableOK,
  21. Ci.ScriptableWithNotXPCOM,
  22. Ci.ScriptableWithNotXPCOMBase]),
  23. method1: function() {
  24. method1Called = true;
  25. },
  26. method2: function() {
  27. ok(false, "method2 should not have been called!");
  28. },
  29. method3: function() {
  30. ok(false, "mehod3 should not have been called!");
  31. },
  32. jsonly: true,
  33. };
  34. let factory = {
  35. QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
  36. createInstance: function(outer, iid) {
  37. if (outer) {
  38. throw Cr.NS_ERROR_NO_AGGREGATION;
  39. }
  40. return testObject.QueryInterface(iid);
  41. },
  42. };
  43. registrar.registerFactory(kCID, null, kContract, factory);
  44. let xpcomObject = Cc[kContract].createInstance();
  45. ok(xpcomObject);
  46. strictEqual(xpcomObject.jsonly, undefined);
  47. xpcomObject.QueryInterface(Ci.ScriptableOK);
  48. xpcomObject.method1();
  49. ok(method1Called);
  50. try {
  51. xpcomObject.QueryInterface(Ci.ScriptableWithNotXPCOM);
  52. ok(false, "Should not have implemented ScriptableWithNotXPCOM");
  53. }
  54. catch(e) {
  55. ok(true, "Should not have implemented ScriptableWithNotXPCOM. Correctly threw error: " + e);
  56. }
  57. strictEqual(xpcomObject.method2, undefined);
  58. try {
  59. xpcomObject.QueryInterface(Ci.ScriptableWithNotXPCOMBase);
  60. ok(false, "Should not have implemented ScriptableWithNotXPCOMBase");
  61. }
  62. catch (e) {
  63. ok(true, "Should not have implemented ScriptableWithNotXPCOMBase. Correctly threw error: " + e);
  64. }
  65. strictEqual(xpcomObject.method3, undefined);
  66. }