1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/modules/tests/xpcshell/test_Services.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * This file tests the Services.jsm module. 1.9 + */ 1.10 + 1.11 +//////////////////////////////////////////////////////////////////////////////// 1.12 +/// Globals 1.13 + 1.14 +const Cc = Components.classes; 1.15 +const Ci = Components.interfaces; 1.16 +const Cu = Components.utils; 1.17 +const Cr = Components.results; 1.18 + 1.19 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.20 +Cu.import("resource://gre/modules/Services.jsm"); 1.21 + 1.22 +function checkService(service, interface) { 1.23 + do_print("Checking that Services." + service + " is an " + interface); 1.24 + do_check_true(service in Services); 1.25 + do_check_true(Services[service] instanceof interface); 1.26 +} 1.27 + 1.28 +//////////////////////////////////////////////////////////////////////////////// 1.29 +/// Tests 1.30 + 1.31 +function run_test() 1.32 +{ 1.33 + do_get_profile(); 1.34 + 1.35 + checkService("appShell", Ci.nsIAppShellService); 1.36 + checkService("appinfo", Ci.nsIXULRuntime); 1.37 + checkService("blocklist", Ci.nsIBlocklistService); 1.38 + checkService("cache", Ci.nsICacheService); 1.39 + checkService("cache2", Ci.nsICacheStorageService); 1.40 + checkService("clipboard", Ci.nsIClipboard); 1.41 + checkService("console", Ci.nsIConsoleService); 1.42 + checkService("contentPrefs", Ci.nsIContentPrefService); 1.43 + checkService("cookies", Ci.nsICookieManager2); 1.44 + checkService("dirsvc", Ci.nsIDirectoryService); 1.45 + checkService("dirsvc", Ci.nsIProperties); 1.46 + checkService("DOMRequest", Ci.nsIDOMRequestService); 1.47 + checkService("domStorageManager", Ci.nsIDOMStorageManager); 1.48 + checkService("downloads", Ci.nsIDownloadManager); 1.49 + checkService("droppedLinkHandler", Ci.nsIDroppedLinkHandler); 1.50 + checkService("eTLD", Ci.nsIEffectiveTLDService); 1.51 + checkService("focus", Ci.nsIFocusManager); 1.52 + checkService("io", Ci.nsIIOService); 1.53 + checkService("io", Ci.nsIIOService2); 1.54 + checkService("locale", Ci.nsILocaleService); 1.55 + checkService("logins", Ci.nsILoginManager); 1.56 + checkService("obs", Ci.nsIObserverService); 1.57 + checkService("perms", Ci.nsIPermissionManager); 1.58 + checkService("prefs", Ci.nsIPrefBranch); 1.59 + checkService("prefs", Ci.nsIPrefService); 1.60 + checkService("prompt", Ci.nsIPromptService); 1.61 + checkService("scriptSecurityManager", Ci.nsIScriptSecurityManager); 1.62 + checkService("scriptloader", Ci.mozIJSSubScriptLoader); 1.63 + checkService("startup", Ci.nsIAppStartup); 1.64 + checkService("storage", Ci.mozIStorageService); 1.65 + checkService("strings", Ci.nsIStringBundleService); 1.66 + checkService("sysinfo", Ci.nsIPropertyBag2); 1.67 + checkService("telemetry", Ci.nsITelemetry); 1.68 + checkService("tm", Ci.nsIThreadManager); 1.69 + checkService("uriFixup", Ci.nsIURIFixup); 1.70 + checkService("urlFormatter", Ci.nsIURLFormatter); 1.71 + checkService("vc", Ci.nsIVersionComparator); 1.72 + checkService("wm", Ci.nsIWindowMediator); 1.73 + checkService("ww", Ci.nsIWindowWatcher); 1.74 + if ("nsIBrowserSearchService" in Ci) { 1.75 + checkService("search", Ci.nsIBrowserSearchService); 1.76 + } 1.77 + if ("nsIAndroidBridge" in Ci) { 1.78 + checkService("androidBridge", Ci.nsIAndroidBridge); 1.79 + } 1.80 + 1.81 + // In xpcshell tests, the "@mozilla.org/xre/app-info;1" component implements 1.82 + // only the nsIXULRuntime interface, but not nsIXULAppInfo. To test the 1.83 + // service getter for the latter interface, we define a minimal mock factory 1.84 + // that returns an object defining both interfaces. 1.85 + let contractID = "@mozilla.org/xre/app-info;1"; 1.86 + let mockFactory = { 1.87 + createInstance: function (aOuter, aIid) { 1.88 + return { 1.89 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime, 1.90 + Ci.nsIXULAppInfo]), 1.91 + }.QueryInterface(aIid); 1.92 + } 1.93 + }; 1.94 + 1.95 + let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.96 + let cid = registrar.contractIDToCID(contractID); 1.97 + let oldFactory = Components.manager.getClassObject(Cc[contractID], 1.98 + Ci.nsIFactory); 1.99 + registrar.unregisterFactory(cid, oldFactory); 1.100 + registrar.registerFactory(cid, "", contractID, mockFactory); 1.101 + 1.102 + // We need to reload the module to update the lazy getter. 1.103 + Cu.unload("resource://gre/modules/Services.jsm"); 1.104 + Cu.import("resource://gre/modules/Services.jsm"); 1.105 + 1.106 + checkService("appinfo", Ci.nsIXULAppInfo); 1.107 + 1.108 + // Clean up. 1.109 + registrar.unregisterFactory(cid, mockFactory); 1.110 + registrar.registerFactory(cid, "", contractID, oldFactory); 1.111 + 1.112 + Cu.unload("resource://gre/modules/Services.jsm"); 1.113 +}