browser/base/content/test/social/browser_social_window.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial