|
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 |
|
3 // file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 // Test the top-level window UI for social. |
|
6 |
|
7 let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
8 |
|
9 // This function should "reset" Social such that the next time Social.init() |
|
10 // is called (eg, when a new window is opened), it re-performs all |
|
11 // initialization. |
|
12 function resetSocial() { |
|
13 Social.initialized = false; |
|
14 Social.providers = []; |
|
15 // *sob* - listeners keep getting added... |
|
16 SocialService._providerListeners.clear(); |
|
17 } |
|
18 |
|
19 let createdWindows = []; |
|
20 |
|
21 function openWindowAndWaitForInit(parentWin, callback) { |
|
22 // this notification tells us SocialUI.init() has been run... |
|
23 let topic = "browser-delayed-startup-finished"; |
|
24 let w = parentWin.OpenBrowserWindow(); |
|
25 createdWindows.push(w); |
|
26 Services.obs.addObserver(function providerSet(subject, topic, data) { |
|
27 Services.obs.removeObserver(providerSet, topic); |
|
28 info(topic + " observer was notified - continuing test"); |
|
29 executeSoon(() => callback(w)); |
|
30 }, topic, false); |
|
31 } |
|
32 |
|
33 function closeWindow(w, cb) { |
|
34 waitForNotification("domwindowclosed", cb); |
|
35 w.close(); |
|
36 } |
|
37 |
|
38 function closeOneWindow(cb) { |
|
39 let w = createdWindows.pop(); |
|
40 if (!w || w.closed) { |
|
41 cb(); |
|
42 return; |
|
43 } |
|
44 closeWindow(w, function() { |
|
45 closeOneWindow(cb); |
|
46 }); |
|
47 w.close(); |
|
48 } |
|
49 |
|
50 function postTestCleanup(cb) { |
|
51 closeOneWindow(cb); |
|
52 } |
|
53 |
|
54 let manifest = { // normal provider |
|
55 name: "provider 1", |
|
56 origin: "https://example.com", |
|
57 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", |
|
58 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", |
|
59 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" |
|
60 }; |
|
61 let manifest2 = { // used for testing install |
|
62 name: "provider test1", |
|
63 origin: "https://test1.example.com", |
|
64 workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", |
|
65 sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html", |
|
66 iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png", |
|
67 }; |
|
68 |
|
69 function test() { |
|
70 waitForExplicitFinish(); |
|
71 runSocialTests(tests, undefined, postTestCleanup); |
|
72 } |
|
73 |
|
74 let tests = { |
|
75 // check when social is totally disabled at startup (ie, no providers enabled) |
|
76 testInactiveStartup: function(cbnext) { |
|
77 is(Social.providers.length, 0, "needs zero providers to start this test."); |
|
78 ok(!SocialService.hasEnabledProviders, "no providers are enabled"); |
|
79 resetSocial(); |
|
80 openWindowAndWaitForInit(window, function(w1) { |
|
81 checkSocialUI(w1); |
|
82 // Now social is (re-)initialized, open a secondary window and check that. |
|
83 openWindowAndWaitForInit(window, function(w2) { |
|
84 checkSocialUI(w2); |
|
85 checkSocialUI(w1); |
|
86 cbnext(); |
|
87 }); |
|
88 }); |
|
89 }, |
|
90 |
|
91 // Check when providers are enabled and social is turned on at startup. |
|
92 testEnabledStartup: function(cbnext) { |
|
93 setManifestPref("social.manifest.test", manifest); |
|
94 ok(!SocialSidebar.opened, "sidebar is closed initially"); |
|
95 SocialService.addProvider(manifest, function() { |
|
96 SocialService.addProvider(manifest2, function (provider) { |
|
97 SocialSidebar.show(); |
|
98 waitForCondition(function() SocialSidebar.opened, |
|
99 function() { |
|
100 ok(SocialSidebar.opened, "first window sidebar is open"); |
|
101 openWindowAndWaitForInit(window, function(w1) { |
|
102 ok(w1.SocialSidebar.opened, "new window sidebar is open"); |
|
103 ok(SocialService.hasEnabledProviders, "providers are enabled"); |
|
104 checkSocialUI(w1); |
|
105 // now init is complete, open a second window |
|
106 openWindowAndWaitForInit(window, function(w2) { |
|
107 ok(w1.SocialSidebar.opened, "w1 sidebar is open"); |
|
108 ok(w2.SocialSidebar.opened, "w2 sidebar is open"); |
|
109 checkSocialUI(w2); |
|
110 checkSocialUI(w1); |
|
111 |
|
112 // disable social and re-check |
|
113 SocialService.removeProvider(manifest.origin, function() { |
|
114 SocialService.removeProvider(manifest2.origin, function() { |
|
115 ok(!Social.enabled, "social is disabled"); |
|
116 is(Social.providers.length, 0, "no providers"); |
|
117 ok(!w1.SocialSidebar.opened, "w1 sidebar is closed"); |
|
118 ok(!w2.SocialSidebar.opened, "w2 sidebar is closed"); |
|
119 checkSocialUI(w2); |
|
120 checkSocialUI(w1); |
|
121 Services.prefs.clearUserPref("social.manifest.test"); |
|
122 cbnext(); |
|
123 }); |
|
124 }); |
|
125 }); |
|
126 }); |
|
127 }, "sidebar did not open"); |
|
128 }, cbnext); |
|
129 }, cbnext); |
|
130 }, |
|
131 |
|
132 testGlobalState: function(cbnext) { |
|
133 setManifestPref("social.manifest.test", manifest); |
|
134 ok(!SocialSidebar.opened, "sidebar is closed initially"); |
|
135 ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state unset"); |
|
136 // mimick no session state in opener so we exercise the global state via pref |
|
137 SessionStore.deleteWindowValue(window, "socialSidebar"); |
|
138 ok(!SessionStore.getWindowValue(window, "socialSidebar"), "window state unset"); |
|
139 SocialService.addProvider(manifest, function() { |
|
140 openWindowAndWaitForInit(window, function(w1) { |
|
141 w1.SocialSidebar.show(); |
|
142 waitForCondition(function() w1.SocialSidebar.opened, |
|
143 function() { |
|
144 ok(Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state set"); |
|
145 ok(!SocialSidebar.opened, "1. main sidebar is still closed"); |
|
146 ok(w1.SocialSidebar.opened, "1. window sidebar is open"); |
|
147 closeWindow(w1, function() { |
|
148 // this time, the global state should cause the sidebar to be opened |
|
149 // in the new window |
|
150 openWindowAndWaitForInit(window, function(w1) { |
|
151 ok(!SocialSidebar.opened, "2. main sidebar is still closed"); |
|
152 ok(w1.SocialSidebar.opened, "2. window sidebar is open"); |
|
153 w1.SocialSidebar.hide(); |
|
154 ok(!w1.SocialSidebar.opened, "2. window sidebar is closed"); |
|
155 ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "2. global state unset"); |
|
156 // global state should now be no sidebar gets opened on new window |
|
157 closeWindow(w1, function() { |
|
158 ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "3. global state unset"); |
|
159 ok(!SocialSidebar.opened, "3. main sidebar is still closed"); |
|
160 openWindowAndWaitForInit(window, function(w1) { |
|
161 ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "4. global state unset"); |
|
162 ok(!SocialSidebar.opened, "4. main sidebar is still closed"); |
|
163 ok(!w1.SocialSidebar.opened, "4. window sidebar is closed"); |
|
164 SocialService.removeProvider(manifest.origin, function() { |
|
165 Services.prefs.clearUserPref("social.manifest.test"); |
|
166 cbnext(); |
|
167 }); |
|
168 }); |
|
169 }); |
|
170 }); |
|
171 }); |
|
172 }); |
|
173 }); |
|
174 }); |
|
175 }, |
|
176 |
|
177 // Check per window sidebar functionality, including migration from using |
|
178 // prefs to using session state, and state inheritance of windows (new windows |
|
179 // inherit state from the opener). |
|
180 testPerWindowSidebar: function(cbnext) { |
|
181 function finishCheck() { |
|
182 // disable social and re-check |
|
183 SocialService.removeProvider(manifest.origin, function() { |
|
184 SocialService.removeProvider(manifest2.origin, function() { |
|
185 ok(!Social.enabled, "social is disabled"); |
|
186 is(Social.providers.length, 0, "no providers"); |
|
187 Services.prefs.clearUserPref("social.manifest.test"); |
|
188 cbnext(); |
|
189 }); |
|
190 }); |
|
191 } |
|
192 |
|
193 setManifestPref("social.manifest.test", manifest); |
|
194 ok(!SocialSidebar.opened, "sidebar is closed initially"); |
|
195 SocialService.addProvider(manifest, function() { |
|
196 SocialService.addProvider(manifest2, function (provider) { |
|
197 // test the migration of the social.sidebar.open pref. We'll set a user |
|
198 // level pref to indicate it was open (along with the old |
|
199 // social.provider.current pref), then we'll open a window. During the |
|
200 // restoreState of the window, those prefs should be migrated, and the |
|
201 // sidebar should be opened. Both prefs are then removed. |
|
202 Services.prefs.setCharPref("social.provider.current", "https://example.com"); |
|
203 Services.prefs.setBoolPref("social.sidebar.open", true); |
|
204 |
|
205 openWindowAndWaitForInit(window, function(w1) { |
|
206 ok(w1.SocialSidebar.opened, "new window sidebar is open"); |
|
207 ok(SocialService.hasEnabledProviders, "providers are enabled"); |
|
208 ok(!Services.prefs.prefHasUserValue("social.provider.current"), "social.provider.current pref removed"); |
|
209 ok(!Services.prefs.prefHasUserValue("social.sidebar.open"), "social.sidebar.open pref removed"); |
|
210 checkSocialUI(w1); |
|
211 // now init is complete, open a second window, it's state should be the same as the opener |
|
212 openWindowAndWaitForInit(w1, function(w2) { |
|
213 ok(w1.SocialSidebar.opened, "w1 sidebar is open"); |
|
214 ok(w2.SocialSidebar.opened, "w2 sidebar is open"); |
|
215 checkSocialUI(w2); |
|
216 checkSocialUI(w1); |
|
217 |
|
218 // change the sidebar in w2 |
|
219 w2.SocialSidebar.show(manifest2.origin); |
|
220 let sbrowser1 = w1.document.getElementById("social-sidebar-browser"); |
|
221 is(manifest.origin, sbrowser1.getAttribute("origin"), "w1 sidebar origin matches"); |
|
222 let sbrowser2 = w2.document.getElementById("social-sidebar-browser"); |
|
223 is(manifest2.origin, sbrowser2.getAttribute("origin"), "w2 sidebar origin matches"); |
|
224 |
|
225 // hide sidebar, w1 sidebar should still be open |
|
226 w2.SocialSidebar.hide(); |
|
227 ok(w1.SocialSidebar.opened, "w1 sidebar is opened"); |
|
228 ok(!w2.SocialSidebar.opened, "w2 sidebar is closed"); |
|
229 ok(sbrowser2.parentNode.hidden, "w2 sidebar is hidden"); |
|
230 |
|
231 // open a 3rd window from w2, it should inherit the state of w2 |
|
232 openWindowAndWaitForInit(w2, function(w3) { |
|
233 // since the sidebar is not open, we need to ensure the provider |
|
234 // is selected to test we inherited the provider from the opener |
|
235 w3.SocialSidebar.ensureProvider(); |
|
236 is(w3.SocialSidebar.provider, w2.SocialSidebar.provider, "w3 has same provider as w2"); |
|
237 ok(!w3.SocialSidebar.opened, "w2 sidebar is closed"); |
|
238 |
|
239 // open a 4th window from w1, it should inherit the state of w1 |
|
240 openWindowAndWaitForInit(w1, function(w4) { |
|
241 is(w4.SocialSidebar.provider, w1.SocialSidebar.provider, "w4 has same provider as w1"); |
|
242 ok(w4.SocialSidebar.opened, "w4 sidebar is opened"); |
|
243 |
|
244 finishCheck(); |
|
245 }); |
|
246 }); |
|
247 |
|
248 }); |
|
249 }); |
|
250 }, cbnext); |
|
251 }, cbnext); |
|
252 } |
|
253 } |