michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: sw=4 ts=4 sts=4 et michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * This file tests the methods on XPCOMUtils.jsm. michael@0: */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Tests michael@0: michael@0: add_test(function test_generateQI_string_names() michael@0: { michael@0: var x = { michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Components.interfaces.nsIClassInfo, michael@0: "nsIDOMNode" michael@0: ]) michael@0: }; michael@0: michael@0: try { michael@0: x.QueryInterface(Components.interfaces.nsIClassInfo); michael@0: } catch(e) { michael@0: do_throw("Should QI to nsIClassInfo"); michael@0: } michael@0: try { michael@0: x.QueryInterface(Components.interfaces.nsIDOMNode); michael@0: } catch(e) { michael@0: do_throw("Should QI to nsIDOMNode"); michael@0: } michael@0: try { michael@0: x.QueryInterface(Components.interfaces.nsIDOMDocument); michael@0: do_throw("QI should not have succeeded!"); michael@0: } catch(e) {} michael@0: run_next_test(); michael@0: }); michael@0: michael@0: michael@0: add_test(function test_generateCI() michael@0: { michael@0: const classID = Components.ID("562dae2e-7cff-432b-995b-3d4c03fa2b89"); michael@0: const classDescription = "generateCI test component"; michael@0: const flags = Components.interfaces.nsIClassInfo.DOM_OBJECT; michael@0: var x = { michael@0: QueryInterface: XPCOMUtils.generateQI([]), michael@0: classInfo: XPCOMUtils.generateCI({classID: classID, michael@0: interfaces: [], michael@0: flags: flags, michael@0: classDescription: classDescription}) michael@0: }; michael@0: michael@0: try { michael@0: var ci = x.QueryInterface(Components.interfaces.nsIClassInfo); michael@0: ci = ci.QueryInterface(Components.interfaces.nsISupports); michael@0: ci = ci.QueryInterface(Components.interfaces.nsIClassInfo); michael@0: do_check_eq(ci.classID, classID); michael@0: do_check_eq(ci.flags, flags); michael@0: do_check_eq(ci.classDescription, classDescription); michael@0: } catch(e) { michael@0: do_throw("Classinfo for x should not be missing or broken"); michael@0: } michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_defineLazyGetter() michael@0: { michael@0: let accessCount = 0; michael@0: let obj = { michael@0: inScope: false michael@0: }; michael@0: const TEST_VALUE = "test value"; michael@0: XPCOMUtils.defineLazyGetter(obj, "foo", function() { michael@0: accessCount++; michael@0: this.inScope = true; michael@0: return TEST_VALUE; michael@0: }); michael@0: do_check_eq(accessCount, 0); michael@0: michael@0: // Get the property, making sure the access count has increased. michael@0: do_check_eq(obj.foo, TEST_VALUE); michael@0: do_check_eq(accessCount, 1); michael@0: do_check_true(obj.inScope); michael@0: michael@0: // Get the property once more, making sure the access count has not michael@0: // increased. michael@0: do_check_eq(obj.foo, TEST_VALUE); michael@0: do_check_eq(accessCount, 1); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: michael@0: add_test(function test_defineLazyServiceGetter() michael@0: { michael@0: let obj = { }; michael@0: XPCOMUtils.defineLazyServiceGetter(obj, "service", michael@0: "@mozilla.org/consoleservice;1", michael@0: "nsIConsoleService"); michael@0: let service = Cc["@mozilla.org/consoleservice;1"]. michael@0: getService(Ci.nsIConsoleService); michael@0: michael@0: // Check that the lazy service getter and the actual service have the same michael@0: // properties. michael@0: for (let prop in obj.service) michael@0: do_check_true(prop in service); michael@0: for (let prop in service) michael@0: do_check_true(prop in obj.service); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: michael@0: add_test(function test_categoryRegistration() michael@0: { michael@0: const CATEGORY_NAME = "test-cat"; michael@0: const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; michael@0: const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}"); michael@0: michael@0: // Create a fake app entry for our category registration apps filter. michael@0: let XULAppInfo = { michael@0: vendor: "Mozilla", michael@0: name: "catRegTest", michael@0: ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}", michael@0: version: "1", michael@0: appBuildID: "2007010101", michael@0: platformVersion: "", michael@0: platformBuildID: "2007010101", michael@0: inSafeMode: false, michael@0: logConsoleErrors: true, michael@0: OS: "XPCShell", michael@0: XPCOMABI: "noarch-spidermonkey", michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIXULAppInfo, michael@0: Ci.nsIXULRuntime, michael@0: ]) michael@0: }; michael@0: let XULAppInfoFactory = { michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return XULAppInfo.QueryInterface(iid); michael@0: } michael@0: }; michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory( michael@0: XULAPPINFO_CID, michael@0: "XULAppInfo", michael@0: XULAPPINFO_CONTRACTID, michael@0: XULAppInfoFactory michael@0: ); michael@0: michael@0: // Load test components. michael@0: do_load_manifest("CatRegistrationComponents.manifest"); michael@0: michael@0: const EXPECTED_ENTRIES = ["CatAppRegisteredComponent", michael@0: "CatRegisteredComponent"]; michael@0: michael@0: // Check who is registered in "test-cat" category. michael@0: let foundEntriesCount = 0; michael@0: let catMan = Cc["@mozilla.org/categorymanager;1"]. michael@0: getService(Ci.nsICategoryManager); michael@0: let entries = catMan.enumerateCategory(CATEGORY_NAME); michael@0: while (entries.hasMoreElements()) { michael@0: foundEntriesCount++; michael@0: let entry = entries.getNext().QueryInterface(Ci.nsISupportsCString).data; michael@0: print("Check the found category entry (" + entry + ") is expected."); michael@0: do_check_true(EXPECTED_ENTRIES.indexOf(entry) != -1); michael@0: } michael@0: print("Check there are no more or less than expected entries."); michael@0: do_check_eq(foundEntriesCount, EXPECTED_ENTRIES.length); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_generateSingletonFactory() michael@0: { michael@0: const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1"; michael@0: const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}"); michael@0: michael@0: function XPCComponent() {} michael@0: XPCComponent.prototype = { michael@0: classID: XPCCOMPONENT_CID, michael@0: _xpcom_factory: XPCOMUtils.generateSingletonFactory(XPCComponent), michael@0: QueryInterface: XPCOMUtils.generateQI([]) michael@0: }; michael@0: let NSGetFactory = XPCOMUtils.generateNSGetFactory([XPCComponent]); michael@0: let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory( michael@0: XPCCOMPONENT_CID, michael@0: "XPCComponent", michael@0: XPCCOMPONENT_CONTRACTID, michael@0: NSGetFactory(XPCCOMPONENT_CID) michael@0: ); michael@0: michael@0: // First, try to instance the component. michael@0: let instance = Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports); michael@0: // Try again, check that it returns the same instance as before. michael@0: do_check_eq(instance, michael@0: Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports)); michael@0: // Now, for sanity, check that getService is also returning the same instance. michael@0: do_check_eq(instance, michael@0: Cc[XPCCOMPONENT_CONTRACTID].getService(Ci.nsISupports)); michael@0: michael@0: run_next_test(); michael@0: }); michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Runner michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: }