|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
|
6 Cu.import("resource://gre/modules/Services.jsm"); |
|
7 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
8 |
|
9 var Social, SocialService; |
|
10 |
|
11 let manifests = [ |
|
12 { |
|
13 name: "provider 1", |
|
14 origin: "https://example1.com", |
|
15 sidebarURL: "https://example1.com/sidebar/", |
|
16 }, |
|
17 { |
|
18 name: "provider 2", |
|
19 origin: "https://example2.com", |
|
20 sidebarURL: "https://example1.com/sidebar/", |
|
21 } |
|
22 ]; |
|
23 |
|
24 const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest."); |
|
25 |
|
26 // SocialProvider class relies on blocklisting being enabled. To enable |
|
27 // blocklisting, we have to setup an app and initialize the blocklist (see |
|
28 // initApp below). |
|
29 const gProfD = do_get_profile(); |
|
30 |
|
31 const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; |
|
32 const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}"); |
|
33 |
|
34 function createAppInfo(id, name, version, platformVersion) { |
|
35 gAppInfo = { |
|
36 // nsIXULAppInfo |
|
37 vendor: "Mozilla", |
|
38 name: name, |
|
39 ID: id, |
|
40 version: version, |
|
41 appBuildID: "2007010101", |
|
42 platformVersion: platformVersion ? platformVersion : "1.0", |
|
43 platformBuildID: "2007010101", |
|
44 |
|
45 // nsIXULRuntime |
|
46 inSafeMode: false, |
|
47 logConsoleErrors: true, |
|
48 OS: "XPCShell", |
|
49 XPCOMABI: "noarch-spidermonkey", |
|
50 invalidateCachesOnRestart: function invalidateCachesOnRestart() { |
|
51 // Do nothing |
|
52 }, |
|
53 |
|
54 // nsICrashReporter |
|
55 annotations: {}, |
|
56 |
|
57 annotateCrashReport: function(key, data) { |
|
58 this.annotations[key] = data; |
|
59 }, |
|
60 |
|
61 QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, |
|
62 Ci.nsIXULRuntime, |
|
63 Ci.nsICrashReporter, |
|
64 Ci.nsISupports]) |
|
65 }; |
|
66 |
|
67 var XULAppInfoFactory = { |
|
68 createInstance: function (outer, iid) { |
|
69 if (outer != null) |
|
70 throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
71 return gAppInfo.QueryInterface(iid); |
|
72 } |
|
73 }; |
|
74 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
|
75 registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo", |
|
76 XULAPPINFO_CONTRACTID, XULAppInfoFactory); |
|
77 } |
|
78 |
|
79 function initApp() { |
|
80 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); |
|
81 // prepare a blocklist file for the blocklist service |
|
82 var blocklistFile = gProfD.clone(); |
|
83 blocklistFile.append("blocklist.xml"); |
|
84 if (blocklistFile.exists()) |
|
85 blocklistFile.remove(false); |
|
86 var source = do_get_file("blocklist.xml"); |
|
87 source.copyTo(gProfD, "blocklist.xml"); |
|
88 blocklistFile.lastModifiedTime = Date.now(); |
|
89 |
|
90 |
|
91 let internalManager = Cc["@mozilla.org/addons/integration;1"]. |
|
92 getService(Ci.nsIObserver). |
|
93 QueryInterface(Ci.nsITimerCallback); |
|
94 |
|
95 internalManager.observe(null, "addons-startup", null); |
|
96 } |
|
97 |
|
98 function setManifestPref(manifest) { |
|
99 let string = Cc["@mozilla.org/supports-string;1"]. |
|
100 createInstance(Ci.nsISupportsString); |
|
101 string.data = JSON.stringify(manifest); |
|
102 Services.prefs.setComplexValue("social.manifest." + manifest.origin, Ci.nsISupportsString, string); |
|
103 } |
|
104 |
|
105 function do_wait_observer(topic, cb) { |
|
106 function observer(subject, topic, data) { |
|
107 Services.obs.removeObserver(observer, topic); |
|
108 cb(); |
|
109 } |
|
110 Services.obs.addObserver(observer, topic, false); |
|
111 } |
|
112 |
|
113 function do_add_providers(cb) { |
|
114 // run only after social is already initialized |
|
115 SocialService.addProvider(manifests[0], function() { |
|
116 do_wait_observer("social:providers-changed", function() { |
|
117 do_check_eq(Social.providers.length, 2, "2 providers installed"); |
|
118 do_execute_soon(cb); |
|
119 }); |
|
120 SocialService.addProvider(manifests[1]); |
|
121 }); |
|
122 } |
|
123 |
|
124 function do_initialize_social(enabledOnStartup, cb) { |
|
125 initApp(); |
|
126 |
|
127 if (enabledOnStartup) { |
|
128 // set prefs before initializing social |
|
129 manifests.forEach(function (manifest) { |
|
130 setManifestPref(manifest); |
|
131 }); |
|
132 // Set both providers active and flag the first one as "current" |
|
133 let activeVal = Cc["@mozilla.org/supports-string;1"]. |
|
134 createInstance(Ci.nsISupportsString); |
|
135 let active = {}; |
|
136 for (let m of manifests) |
|
137 active[m.origin] = 1; |
|
138 activeVal.data = JSON.stringify(active); |
|
139 Services.prefs.setComplexValue("social.activeProviders", |
|
140 Ci.nsISupportsString, activeVal); |
|
141 |
|
142 do_register_cleanup(function() { |
|
143 manifests.forEach(function (manifest) { |
|
144 Services.prefs.clearUserPref("social.manifest." + manifest.origin); |
|
145 }); |
|
146 Services.prefs.clearUserPref("social.activeProviders"); |
|
147 }); |
|
148 |
|
149 // expecting 2 providers installed |
|
150 do_wait_observer("social:providers-changed", function() { |
|
151 do_check_eq(Social.providers.length, 2, "2 providers installed"); |
|
152 do_execute_soon(cb); |
|
153 }); |
|
154 } |
|
155 |
|
156 // import and initialize everything |
|
157 SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
158 do_check_eq(enabledOnStartup, SocialService.hasEnabledProviders, "Service has enabled providers"); |
|
159 Social = Cu.import("resource:///modules/Social.jsm", {}).Social; |
|
160 do_check_false(Social.initialized, "Social is not initialized"); |
|
161 Social.init(); |
|
162 do_check_true(Social.initialized, "Social is initialized"); |
|
163 if (!enabledOnStartup) |
|
164 do_execute_soon(cb); |
|
165 } |