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 michael@0: // file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: // Test the top-level window UI for social. michael@0: michael@0: let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: // This function should "reset" Social such that the next time Social.init() michael@0: // is called (eg, when a new window is opened), it re-performs all michael@0: // initialization. michael@0: function resetSocial() { michael@0: Social.initialized = false; michael@0: Social.providers = []; michael@0: // *sob* - listeners keep getting added... michael@0: SocialService._providerListeners.clear(); michael@0: } michael@0: michael@0: let createdWindows = []; michael@0: michael@0: function openWindowAndWaitForInit(parentWin, callback) { michael@0: // this notification tells us SocialUI.init() has been run... michael@0: let topic = "browser-delayed-startup-finished"; michael@0: let w = parentWin.OpenBrowserWindow(); michael@0: createdWindows.push(w); michael@0: Services.obs.addObserver(function providerSet(subject, topic, data) { michael@0: Services.obs.removeObserver(providerSet, topic); michael@0: info(topic + " observer was notified - continuing test"); michael@0: executeSoon(() => callback(w)); michael@0: }, topic, false); michael@0: } michael@0: michael@0: function closeWindow(w, cb) { michael@0: waitForNotification("domwindowclosed", cb); michael@0: w.close(); michael@0: } michael@0: michael@0: function closeOneWindow(cb) { michael@0: let w = createdWindows.pop(); michael@0: if (!w || w.closed) { michael@0: cb(); michael@0: return; michael@0: } michael@0: closeWindow(w, function() { michael@0: closeOneWindow(cb); michael@0: }); michael@0: w.close(); michael@0: } michael@0: michael@0: function postTestCleanup(cb) { michael@0: closeOneWindow(cb); michael@0: } michael@0: michael@0: let manifest = { // normal provider michael@0: name: "provider 1", michael@0: origin: "https://example.com", michael@0: sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" michael@0: }; michael@0: let manifest2 = { // used for testing install michael@0: name: "provider test1", michael@0: origin: "https://test1.example.com", michael@0: workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png", michael@0: }; michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: runSocialTests(tests, undefined, postTestCleanup); michael@0: } michael@0: michael@0: let tests = { michael@0: // check when social is totally disabled at startup (ie, no providers enabled) michael@0: testInactiveStartup: function(cbnext) { michael@0: is(Social.providers.length, 0, "needs zero providers to start this test."); michael@0: ok(!SocialService.hasEnabledProviders, "no providers are enabled"); michael@0: resetSocial(); michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: checkSocialUI(w1); michael@0: // Now social is (re-)initialized, open a secondary window and check that. michael@0: openWindowAndWaitForInit(window, function(w2) { michael@0: checkSocialUI(w2); michael@0: checkSocialUI(w1); michael@0: cbnext(); michael@0: }); michael@0: }); michael@0: }, michael@0: michael@0: // Check when providers are enabled and social is turned on at startup. michael@0: testEnabledStartup: function(cbnext) { michael@0: setManifestPref("social.manifest.test", manifest); michael@0: ok(!SocialSidebar.opened, "sidebar is closed initially"); michael@0: SocialService.addProvider(manifest, function() { michael@0: SocialService.addProvider(manifest2, function (provider) { michael@0: SocialSidebar.show(); michael@0: waitForCondition(function() SocialSidebar.opened, michael@0: function() { michael@0: ok(SocialSidebar.opened, "first window sidebar is open"); michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: ok(w1.SocialSidebar.opened, "new window sidebar is open"); michael@0: ok(SocialService.hasEnabledProviders, "providers are enabled"); michael@0: checkSocialUI(w1); michael@0: // now init is complete, open a second window michael@0: openWindowAndWaitForInit(window, function(w2) { michael@0: ok(w1.SocialSidebar.opened, "w1 sidebar is open"); michael@0: ok(w2.SocialSidebar.opened, "w2 sidebar is open"); michael@0: checkSocialUI(w2); michael@0: checkSocialUI(w1); michael@0: michael@0: // disable social and re-check michael@0: SocialService.removeProvider(manifest.origin, function() { michael@0: SocialService.removeProvider(manifest2.origin, function() { michael@0: ok(!Social.enabled, "social is disabled"); michael@0: is(Social.providers.length, 0, "no providers"); michael@0: ok(!w1.SocialSidebar.opened, "w1 sidebar is closed"); michael@0: ok(!w2.SocialSidebar.opened, "w2 sidebar is closed"); michael@0: checkSocialUI(w2); michael@0: checkSocialUI(w1); michael@0: Services.prefs.clearUserPref("social.manifest.test"); michael@0: cbnext(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }, "sidebar did not open"); michael@0: }, cbnext); michael@0: }, cbnext); michael@0: }, michael@0: michael@0: testGlobalState: function(cbnext) { michael@0: setManifestPref("social.manifest.test", manifest); michael@0: ok(!SocialSidebar.opened, "sidebar is closed initially"); michael@0: ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state unset"); michael@0: // mimick no session state in opener so we exercise the global state via pref michael@0: SessionStore.deleteWindowValue(window, "socialSidebar"); michael@0: ok(!SessionStore.getWindowValue(window, "socialSidebar"), "window state unset"); michael@0: SocialService.addProvider(manifest, function() { michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: w1.SocialSidebar.show(); michael@0: waitForCondition(function() w1.SocialSidebar.opened, michael@0: function() { michael@0: ok(Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state set"); michael@0: ok(!SocialSidebar.opened, "1. main sidebar is still closed"); michael@0: ok(w1.SocialSidebar.opened, "1. window sidebar is open"); michael@0: closeWindow(w1, function() { michael@0: // this time, the global state should cause the sidebar to be opened michael@0: // in the new window michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: ok(!SocialSidebar.opened, "2. main sidebar is still closed"); michael@0: ok(w1.SocialSidebar.opened, "2. window sidebar is open"); michael@0: w1.SocialSidebar.hide(); michael@0: ok(!w1.SocialSidebar.opened, "2. window sidebar is closed"); michael@0: ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "2. global state unset"); michael@0: // global state should now be no sidebar gets opened on new window michael@0: closeWindow(w1, function() { michael@0: ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "3. global state unset"); michael@0: ok(!SocialSidebar.opened, "3. main sidebar is still closed"); michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "4. global state unset"); michael@0: ok(!SocialSidebar.opened, "4. main sidebar is still closed"); michael@0: ok(!w1.SocialSidebar.opened, "4. window sidebar is closed"); michael@0: SocialService.removeProvider(manifest.origin, function() { michael@0: Services.prefs.clearUserPref("social.manifest.test"); michael@0: cbnext(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: michael@0: // Check per window sidebar functionality, including migration from using michael@0: // prefs to using session state, and state inheritance of windows (new windows michael@0: // inherit state from the opener). michael@0: testPerWindowSidebar: function(cbnext) { michael@0: function finishCheck() { michael@0: // disable social and re-check michael@0: SocialService.removeProvider(manifest.origin, function() { michael@0: SocialService.removeProvider(manifest2.origin, function() { michael@0: ok(!Social.enabled, "social is disabled"); michael@0: is(Social.providers.length, 0, "no providers"); michael@0: Services.prefs.clearUserPref("social.manifest.test"); michael@0: cbnext(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: setManifestPref("social.manifest.test", manifest); michael@0: ok(!SocialSidebar.opened, "sidebar is closed initially"); michael@0: SocialService.addProvider(manifest, function() { michael@0: SocialService.addProvider(manifest2, function (provider) { michael@0: // test the migration of the social.sidebar.open pref. We'll set a user michael@0: // level pref to indicate it was open (along with the old michael@0: // social.provider.current pref), then we'll open a window. During the michael@0: // restoreState of the window, those prefs should be migrated, and the michael@0: // sidebar should be opened. Both prefs are then removed. michael@0: Services.prefs.setCharPref("social.provider.current", "https://example.com"); michael@0: Services.prefs.setBoolPref("social.sidebar.open", true); michael@0: michael@0: openWindowAndWaitForInit(window, function(w1) { michael@0: ok(w1.SocialSidebar.opened, "new window sidebar is open"); michael@0: ok(SocialService.hasEnabledProviders, "providers are enabled"); michael@0: ok(!Services.prefs.prefHasUserValue("social.provider.current"), "social.provider.current pref removed"); michael@0: ok(!Services.prefs.prefHasUserValue("social.sidebar.open"), "social.sidebar.open pref removed"); michael@0: checkSocialUI(w1); michael@0: // now init is complete, open a second window, it's state should be the same as the opener michael@0: openWindowAndWaitForInit(w1, function(w2) { michael@0: ok(w1.SocialSidebar.opened, "w1 sidebar is open"); michael@0: ok(w2.SocialSidebar.opened, "w2 sidebar is open"); michael@0: checkSocialUI(w2); michael@0: checkSocialUI(w1); michael@0: michael@0: // change the sidebar in w2 michael@0: w2.SocialSidebar.show(manifest2.origin); michael@0: let sbrowser1 = w1.document.getElementById("social-sidebar-browser"); michael@0: is(manifest.origin, sbrowser1.getAttribute("origin"), "w1 sidebar origin matches"); michael@0: let sbrowser2 = w2.document.getElementById("social-sidebar-browser"); michael@0: is(manifest2.origin, sbrowser2.getAttribute("origin"), "w2 sidebar origin matches"); michael@0: michael@0: // hide sidebar, w1 sidebar should still be open michael@0: w2.SocialSidebar.hide(); michael@0: ok(w1.SocialSidebar.opened, "w1 sidebar is opened"); michael@0: ok(!w2.SocialSidebar.opened, "w2 sidebar is closed"); michael@0: ok(sbrowser2.parentNode.hidden, "w2 sidebar is hidden"); michael@0: michael@0: // open a 3rd window from w2, it should inherit the state of w2 michael@0: openWindowAndWaitForInit(w2, function(w3) { michael@0: // since the sidebar is not open, we need to ensure the provider michael@0: // is selected to test we inherited the provider from the opener michael@0: w3.SocialSidebar.ensureProvider(); michael@0: is(w3.SocialSidebar.provider, w2.SocialSidebar.provider, "w3 has same provider as w2"); michael@0: ok(!w3.SocialSidebar.opened, "w2 sidebar is closed"); michael@0: michael@0: // open a 4th window from w1, it should inherit the state of w1 michael@0: openWindowAndWaitForInit(w1, function(w4) { michael@0: is(w4.SocialSidebar.provider, w1.SocialSidebar.provider, "w4 has same provider as w1"); michael@0: ok(w4.SocialSidebar.opened, "w4 sidebar is opened"); michael@0: michael@0: finishCheck(); michael@0: }); michael@0: }); michael@0: michael@0: }); michael@0: }); michael@0: }, cbnext); michael@0: }, cbnext); michael@0: } michael@0: }