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/. */
8 * This file tests the methods on XPCOMUtils.sys.mjs.
9 * Also on ComponentUtils.jsm. Which is deprecated.
12 const {AppConstants} = ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs");
13 const {ComponentUtils} = ChromeUtils.importESModule("resource://gre/modules/ComponentUtils.sys.mjs");
14 const {Preferences} = ChromeUtils.importESModule("resource://gre/modules/Preferences.sys.mjs");
15 const {TestUtils} = ChromeUtils.importESModule("resource://testing-common/TestUtils.sys.mjs");
16 const {XPCOMUtils} = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");
18 ////////////////////////////////////////////////////////////////////////////////
21 add_test(function test_generateQI_string_names()
24 QueryInterface: ChromeUtils.generateQI([
31 x.QueryInterface(Ci.nsIClassInfo);
33 do_throw("Should QI to nsIClassInfo");
36 x.QueryInterface(Ci.nsIObserver);
38 do_throw("Should QI to nsIObserver");
41 x.QueryInterface(Ci.nsIObserverService);
42 do_throw("QI should not have succeeded!");
47 add_test(function test_defineLazyServiceGetter()
50 XPCOMUtils.defineLazyServiceGetter(obj, "service",
51 "@mozilla.org/consoleservice;1",
53 let service = Cc["@mozilla.org/consoleservice;1"].
54 getService(Ci.nsIConsoleService);
56 // Check that the lazy service getter and the actual service have the same
58 for (let prop in obj.service)
59 Assert.ok(prop in service);
60 for (let prop in service)
61 Assert.ok(prop in obj.service);
66 add_test(function test_defineLazyPreferenceGetter()
68 const PREF = "xpcomutils.test.pref";
71 XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
74 equal(obj.pref, "defaultValue", "Should return the default value before pref is set");
76 Preferences.set(PREF, "currentValue");
79 info("Create second getter on new object");
82 XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "defaultValue");
85 equal(obj.pref, "currentValue", "Should return the current value on initial read when pref is already set");
87 Preferences.set(PREF, "newValue");
89 equal(obj.pref, "newValue", "Should return new value after preference change");
91 Preferences.set(PREF, "currentValue");
93 equal(obj.pref, "currentValue", "Should return new value after second preference change");
96 Preferences.reset(PREF);
98 equal(obj.pref, "defaultValue", "Should return default value after pref is reset");
101 XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", PREF, "a,b",
102 null, value => value.split(","));
104 deepEqual(obj.pref, ["a", "b"], "transform is applied to default value");
106 Preferences.set(PREF, "x,y,z");
107 deepEqual(obj.pref, ["x", "y", "z"], "transform is applied to updated value");
109 Preferences.reset(PREF);
110 deepEqual(obj.pref, ["a", "b"], "transform is applied to reset default");
112 if (AppConstants.DEBUG) {
113 // Need to use a 'real' pref so it will have a valid prefType
116 () => XPCOMUtils.defineLazyPreferenceGetter(obj, "pref", "javascript.enabled", 1),
117 /Default value does not match preference type/,
118 "Providing a default value of a different type than the preference throws an exception"
126 add_test(function test_categoryRegistration()
128 const CATEGORY_NAME = "test-cat";
129 const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1";
130 const XULAPPINFO_CID = Components.ID("{fc937916-656b-4fb3-a395-8c63569e27a8}");
132 // Create a fake app entry for our category registration apps filter.
133 let { newAppInfo } = ChromeUtils.importESModule("resource://testing-common/AppInfo.sys.mjs");
134 let XULAppInfo = newAppInfo({
136 ID: "{adb42a9a-0d19-4849-bf4d-627614ca19be}",
140 let XULAppInfoFactory = {
141 createInstance: function (iid) {
142 return XULAppInfo.QueryInterface(iid);
145 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
146 registrar.registerFactory(
149 XULAPPINFO_CONTRACTID,
153 // Load test components.
154 do_load_manifest("CatRegistrationComponents.manifest");
156 const expectedEntries = new Map([
157 ["CatRegisteredComponent", "@unit.test.com/cat-registered-component;1"],
158 ["CatAppRegisteredComponent", "@unit.test.com/cat-app-registered-component;1"],
161 // Verify the correct entries are registered in the "test-cat" category.
162 for (let {entry, value} of Services.catMan.enumerateCategory(CATEGORY_NAME)) {
163 ok(expectedEntries.has(entry), `${entry} is expected`);
164 Assert.equal(value, expectedEntries.get(entry), "${entry} has correct value.");
165 expectedEntries.delete(entry);
168 Array.from(expectedEntries.keys()),
170 "All expected entries have been deleted."
175 add_test(function test_categoryBackgroundTaskRegistration()
177 const CATEGORY_NAME = "test-cat1";
179 // Note that this test should succeed whether or not MOZ_BACKGROUNDTASKS is
180 // defined. If it's defined, there's no active task so the `backgroundtask`
181 // directive is processed, dropped, and always succeeds. If it's not defined,
182 // then the `backgroundtask` directive is processed and ignored.
184 // Load test components.
185 do_load_manifest("CatBackgroundTaskRegistrationComponents.manifest");
187 let expectedEntriesList = [
188 ["Cat1RegisteredComponent", "@unit.test.com/cat1-registered-component;1"],
189 ["Cat1BackgroundTaskNotRegisteredComponent", "@unit.test.com/cat1-backgroundtask-notregistered-component;1"],
191 if (!AppConstants.MOZ_BACKGROUNDTASKS) {
192 expectedEntriesList.push(...[
193 ["Cat1BackgroundTaskRegisteredComponent", "@unit.test.com/cat1-backgroundtask-registered-component;1"],
194 ["Cat1BackgroundTaskAlwaysRegisteredComponent", "@unit.test.com/cat1-backgroundtask-alwaysregistered-component;1"],
197 const expectedEntries = new Map(expectedEntriesList);
199 // Verify the correct entries are registered in the "test-cat" category.
200 for (let {entry, value} of Services.catMan.enumerateCategory(CATEGORY_NAME)) {
201 ok(expectedEntries.has(entry), `${entry} is expected`);
202 Assert.equal(value, expectedEntries.get(entry), "Verify that the value is correct in the expected entries.");
203 expectedEntries.delete(entry);
206 Array.from(expectedEntries.keys()),
208 "All expected entries have been deleted."
213 add_test(function test_generateSingletonFactory()
215 const XPCCOMPONENT_CONTRACTID = "@mozilla.org/singletonComponentTest;1";
216 const XPCCOMPONENT_CID = Components.ID("{31031c36-5e29-4dd9-9045-333a5d719a3e}");
218 function XPCComponent() {}
219 XPCComponent.prototype = {
220 classID: XPCCOMPONENT_CID,
221 QueryInterface: ChromeUtils.generateQI([])
223 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
224 registrar.registerFactory(
227 XPCCOMPONENT_CONTRACTID,
228 ComponentUtils.generateSingletonFactory(XPCComponent)
231 // First, try to instance the component.
232 let instance = Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports);
233 // Try again, check that it returns the same instance as before.
234 Assert.equal(instance,
235 Cc[XPCCOMPONENT_CONTRACTID].createInstance(Ci.nsISupports));
236 // Now, for sanity, check that getService is also returning the same instance.
237 Assert.equal(instance,
238 Cc[XPCCOMPONENT_CONTRACTID].getService(Ci.nsISupports));
243 ////////////////////////////////////////////////////////////////////////////////