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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: var Social, SocialService; michael@0: michael@0: let manifests = [ michael@0: { michael@0: name: "provider 1", michael@0: origin: "https://example1.com", michael@0: sidebarURL: "https://example1.com/sidebar/", michael@0: }, michael@0: { michael@0: name: "provider 2", michael@0: origin: "https://example2.com", michael@0: sidebarURL: "https://example1.com/sidebar/", michael@0: } michael@0: ]; michael@0: michael@0: const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest."); michael@0: michael@0: // SocialProvider class relies on blocklisting being enabled. To enable michael@0: // blocklisting, we have to setup an app and initialize the blocklist (see michael@0: // initApp below). michael@0: const gProfD = do_get_profile(); michael@0: michael@0: const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; michael@0: const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}"); michael@0: michael@0: function createAppInfo(id, name, version, platformVersion) { michael@0: gAppInfo = { michael@0: // nsIXULAppInfo michael@0: vendor: "Mozilla", michael@0: name: name, michael@0: ID: id, michael@0: version: version, michael@0: appBuildID: "2007010101", michael@0: platformVersion: platformVersion ? platformVersion : "1.0", michael@0: platformBuildID: "2007010101", michael@0: michael@0: // nsIXULRuntime michael@0: inSafeMode: false, michael@0: logConsoleErrors: true, michael@0: OS: "XPCShell", michael@0: XPCOMABI: "noarch-spidermonkey", michael@0: invalidateCachesOnRestart: function invalidateCachesOnRestart() { michael@0: // Do nothing michael@0: }, michael@0: michael@0: // nsICrashReporter michael@0: annotations: {}, michael@0: michael@0: annotateCrashReport: function(key, data) { michael@0: this.annotations[key] = data; michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, michael@0: Ci.nsIXULRuntime, michael@0: Ci.nsICrashReporter, michael@0: Ci.nsISupports]) michael@0: }; michael@0: michael@0: var XULAppInfoFactory = { michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return gAppInfo.QueryInterface(iid); michael@0: } michael@0: }; michael@0: var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo", michael@0: XULAPPINFO_CONTRACTID, XULAppInfoFactory); michael@0: } michael@0: michael@0: function initApp() { michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); michael@0: // prepare a blocklist file for the blocklist service michael@0: var blocklistFile = gProfD.clone(); michael@0: blocklistFile.append("blocklist.xml"); michael@0: if (blocklistFile.exists()) michael@0: blocklistFile.remove(false); michael@0: var source = do_get_file("blocklist.xml"); michael@0: source.copyTo(gProfD, "blocklist.xml"); michael@0: blocklistFile.lastModifiedTime = Date.now(); michael@0: michael@0: michael@0: let internalManager = Cc["@mozilla.org/addons/integration;1"]. michael@0: getService(Ci.nsIObserver). michael@0: QueryInterface(Ci.nsITimerCallback); michael@0: michael@0: internalManager.observe(null, "addons-startup", null); michael@0: } michael@0: michael@0: function setManifestPref(manifest) { michael@0: let string = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: string.data = JSON.stringify(manifest); michael@0: Services.prefs.setComplexValue("social.manifest." + manifest.origin, Ci.nsISupportsString, string); michael@0: } michael@0: michael@0: function do_wait_observer(topic, cb) { michael@0: function observer(subject, topic, data) { michael@0: Services.obs.removeObserver(observer, topic); michael@0: cb(); michael@0: } michael@0: Services.obs.addObserver(observer, topic, false); michael@0: } michael@0: michael@0: function do_add_providers(cb) { michael@0: // run only after social is already initialized michael@0: SocialService.addProvider(manifests[0], function() { michael@0: do_wait_observer("social:providers-changed", function() { michael@0: do_check_eq(Social.providers.length, 2, "2 providers installed"); michael@0: do_execute_soon(cb); michael@0: }); michael@0: SocialService.addProvider(manifests[1]); michael@0: }); michael@0: } michael@0: michael@0: function do_initialize_social(enabledOnStartup, cb) { michael@0: initApp(); michael@0: michael@0: if (enabledOnStartup) { michael@0: // set prefs before initializing social michael@0: manifests.forEach(function (manifest) { michael@0: setManifestPref(manifest); michael@0: }); michael@0: // Set both providers active and flag the first one as "current" michael@0: let activeVal = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: let active = {}; michael@0: for (let m of manifests) michael@0: active[m.origin] = 1; michael@0: activeVal.data = JSON.stringify(active); michael@0: Services.prefs.setComplexValue("social.activeProviders", michael@0: Ci.nsISupportsString, activeVal); michael@0: michael@0: do_register_cleanup(function() { michael@0: manifests.forEach(function (manifest) { michael@0: Services.prefs.clearUserPref("social.manifest." + manifest.origin); michael@0: }); michael@0: Services.prefs.clearUserPref("social.activeProviders"); michael@0: }); michael@0: michael@0: // expecting 2 providers installed michael@0: do_wait_observer("social:providers-changed", function() { michael@0: do_check_eq(Social.providers.length, 2, "2 providers installed"); michael@0: do_execute_soon(cb); michael@0: }); michael@0: } michael@0: michael@0: // import and initialize everything michael@0: SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: do_check_eq(enabledOnStartup, SocialService.hasEnabledProviders, "Service has enabled providers"); michael@0: Social = Cu.import("resource:///modules/Social.jsm", {}).Social; michael@0: do_check_false(Social.initialized, "Social is not initialized"); michael@0: Social.init(); michael@0: do_check_true(Social.initialized, "Social is initialized"); michael@0: if (!enabledOnStartup) michael@0: do_execute_soon(cb); michael@0: }