1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/modules/test/unit/social/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; 1.9 +Cu.import("resource://gre/modules/Services.jsm"); 1.10 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.11 + 1.12 +var Social, SocialService; 1.13 + 1.14 +let manifests = [ 1.15 + { 1.16 + name: "provider 1", 1.17 + origin: "https://example1.com", 1.18 + sidebarURL: "https://example1.com/sidebar/", 1.19 + }, 1.20 + { 1.21 + name: "provider 2", 1.22 + origin: "https://example2.com", 1.23 + sidebarURL: "https://example1.com/sidebar/", 1.24 + } 1.25 +]; 1.26 + 1.27 +const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest."); 1.28 + 1.29 +// SocialProvider class relies on blocklisting being enabled. To enable 1.30 +// blocklisting, we have to setup an app and initialize the blocklist (see 1.31 +// initApp below). 1.32 +const gProfD = do_get_profile(); 1.33 + 1.34 +const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; 1.35 +const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}"); 1.36 + 1.37 +function createAppInfo(id, name, version, platformVersion) { 1.38 + gAppInfo = { 1.39 + // nsIXULAppInfo 1.40 + vendor: "Mozilla", 1.41 + name: name, 1.42 + ID: id, 1.43 + version: version, 1.44 + appBuildID: "2007010101", 1.45 + platformVersion: platformVersion ? platformVersion : "1.0", 1.46 + platformBuildID: "2007010101", 1.47 + 1.48 + // nsIXULRuntime 1.49 + inSafeMode: false, 1.50 + logConsoleErrors: true, 1.51 + OS: "XPCShell", 1.52 + XPCOMABI: "noarch-spidermonkey", 1.53 + invalidateCachesOnRestart: function invalidateCachesOnRestart() { 1.54 + // Do nothing 1.55 + }, 1.56 + 1.57 + // nsICrashReporter 1.58 + annotations: {}, 1.59 + 1.60 + annotateCrashReport: function(key, data) { 1.61 + this.annotations[key] = data; 1.62 + }, 1.63 + 1.64 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, 1.65 + Ci.nsIXULRuntime, 1.66 + Ci.nsICrashReporter, 1.67 + Ci.nsISupports]) 1.68 + }; 1.69 + 1.70 + var XULAppInfoFactory = { 1.71 + createInstance: function (outer, iid) { 1.72 + if (outer != null) 1.73 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.74 + return gAppInfo.QueryInterface(iid); 1.75 + } 1.76 + }; 1.77 + var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.78 + registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo", 1.79 + XULAPPINFO_CONTRACTID, XULAppInfoFactory); 1.80 +} 1.81 + 1.82 +function initApp() { 1.83 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); 1.84 + // prepare a blocklist file for the blocklist service 1.85 + var blocklistFile = gProfD.clone(); 1.86 + blocklistFile.append("blocklist.xml"); 1.87 + if (blocklistFile.exists()) 1.88 + blocklistFile.remove(false); 1.89 + var source = do_get_file("blocklist.xml"); 1.90 + source.copyTo(gProfD, "blocklist.xml"); 1.91 + blocklistFile.lastModifiedTime = Date.now(); 1.92 + 1.93 + 1.94 + let internalManager = Cc["@mozilla.org/addons/integration;1"]. 1.95 + getService(Ci.nsIObserver). 1.96 + QueryInterface(Ci.nsITimerCallback); 1.97 + 1.98 + internalManager.observe(null, "addons-startup", null); 1.99 +} 1.100 + 1.101 +function setManifestPref(manifest) { 1.102 + let string = Cc["@mozilla.org/supports-string;1"]. 1.103 + createInstance(Ci.nsISupportsString); 1.104 + string.data = JSON.stringify(manifest); 1.105 + Services.prefs.setComplexValue("social.manifest." + manifest.origin, Ci.nsISupportsString, string); 1.106 +} 1.107 + 1.108 +function do_wait_observer(topic, cb) { 1.109 + function observer(subject, topic, data) { 1.110 + Services.obs.removeObserver(observer, topic); 1.111 + cb(); 1.112 + } 1.113 + Services.obs.addObserver(observer, topic, false); 1.114 +} 1.115 + 1.116 +function do_add_providers(cb) { 1.117 + // run only after social is already initialized 1.118 + SocialService.addProvider(manifests[0], function() { 1.119 + do_wait_observer("social:providers-changed", function() { 1.120 + do_check_eq(Social.providers.length, 2, "2 providers installed"); 1.121 + do_execute_soon(cb); 1.122 + }); 1.123 + SocialService.addProvider(manifests[1]); 1.124 + }); 1.125 +} 1.126 + 1.127 +function do_initialize_social(enabledOnStartup, cb) { 1.128 + initApp(); 1.129 + 1.130 + if (enabledOnStartup) { 1.131 + // set prefs before initializing social 1.132 + manifests.forEach(function (manifest) { 1.133 + setManifestPref(manifest); 1.134 + }); 1.135 + // Set both providers active and flag the first one as "current" 1.136 + let activeVal = Cc["@mozilla.org/supports-string;1"]. 1.137 + createInstance(Ci.nsISupportsString); 1.138 + let active = {}; 1.139 + for (let m of manifests) 1.140 + active[m.origin] = 1; 1.141 + activeVal.data = JSON.stringify(active); 1.142 + Services.prefs.setComplexValue("social.activeProviders", 1.143 + Ci.nsISupportsString, activeVal); 1.144 + 1.145 + do_register_cleanup(function() { 1.146 + manifests.forEach(function (manifest) { 1.147 + Services.prefs.clearUserPref("social.manifest." + manifest.origin); 1.148 + }); 1.149 + Services.prefs.clearUserPref("social.activeProviders"); 1.150 + }); 1.151 + 1.152 + // expecting 2 providers installed 1.153 + do_wait_observer("social:providers-changed", function() { 1.154 + do_check_eq(Social.providers.length, 2, "2 providers installed"); 1.155 + do_execute_soon(cb); 1.156 + }); 1.157 + } 1.158 + 1.159 + // import and initialize everything 1.160 + SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; 1.161 + do_check_eq(enabledOnStartup, SocialService.hasEnabledProviders, "Service has enabled providers"); 1.162 + Social = Cu.import("resource:///modules/Social.jsm", {}).Social; 1.163 + do_check_false(Social.initialized, "Social is not initialized"); 1.164 + Social.init(); 1.165 + do_check_true(Social.initialized, "Social is initialized"); 1.166 + if (!enabledOnStartup) 1.167 + do_execute_soon(cb); 1.168 +}