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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/social/browser_social_window.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,253 @@
     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
     1.6 +// file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +// Test the top-level window UI for social.
     1.9 +
    1.10 +let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService;
    1.11 +
    1.12 +// This function should "reset" Social such that the next time Social.init()
    1.13 +// is called (eg, when a new window is opened), it re-performs all
    1.14 +// initialization.
    1.15 +function resetSocial() {
    1.16 +  Social.initialized = false;
    1.17 +  Social.providers = [];
    1.18 +  // *sob* - listeners keep getting added...
    1.19 +  SocialService._providerListeners.clear();
    1.20 +}
    1.21 +
    1.22 +let createdWindows = [];
    1.23 +
    1.24 +function openWindowAndWaitForInit(parentWin, callback) {
    1.25 +  // this notification tells us SocialUI.init() has been run...
    1.26 +  let topic = "browser-delayed-startup-finished";
    1.27 +  let w = parentWin.OpenBrowserWindow();
    1.28 +  createdWindows.push(w);
    1.29 +  Services.obs.addObserver(function providerSet(subject, topic, data) {
    1.30 +    Services.obs.removeObserver(providerSet, topic);
    1.31 +    info(topic + " observer was notified - continuing test");
    1.32 +    executeSoon(() => callback(w));
    1.33 +  }, topic, false);
    1.34 +}
    1.35 +
    1.36 +function closeWindow(w, cb) {
    1.37 +  waitForNotification("domwindowclosed", cb);
    1.38 +  w.close();
    1.39 +}
    1.40 +
    1.41 +function closeOneWindow(cb) {
    1.42 +  let w = createdWindows.pop();
    1.43 +  if (!w || w.closed) {
    1.44 +    cb();
    1.45 +    return;
    1.46 +  }
    1.47 +  closeWindow(w, function() {
    1.48 +    closeOneWindow(cb);
    1.49 +  });
    1.50 +  w.close();
    1.51 +}
    1.52 +
    1.53 +function postTestCleanup(cb) {
    1.54 +  closeOneWindow(cb);
    1.55 +}
    1.56 +
    1.57 +let manifest = { // normal provider
    1.58 +  name: "provider 1",
    1.59 +  origin: "https://example.com",
    1.60 +  sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
    1.61 +  workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
    1.62 +  iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
    1.63 +};
    1.64 +let manifest2 = { // used for testing install
    1.65 +  name: "provider test1",
    1.66 +  origin: "https://test1.example.com",
    1.67 +  workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js",
    1.68 +  sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html",
    1.69 +  iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png",
    1.70 +};
    1.71 +
    1.72 +function test() {
    1.73 +  waitForExplicitFinish();
    1.74 +  runSocialTests(tests, undefined, postTestCleanup);
    1.75 +}
    1.76 +
    1.77 +let tests = {
    1.78 +  // check when social is totally disabled at startup (ie, no providers enabled)
    1.79 +  testInactiveStartup: function(cbnext) {
    1.80 +    is(Social.providers.length, 0, "needs zero providers to start this test.");
    1.81 +    ok(!SocialService.hasEnabledProviders, "no providers are enabled");
    1.82 +    resetSocial();
    1.83 +    openWindowAndWaitForInit(window, function(w1) {
    1.84 +      checkSocialUI(w1);
    1.85 +      // Now social is (re-)initialized, open a secondary window and check that.
    1.86 +      openWindowAndWaitForInit(window, function(w2) {
    1.87 +        checkSocialUI(w2);
    1.88 +        checkSocialUI(w1);
    1.89 +        cbnext();
    1.90 +      });
    1.91 +    });
    1.92 +  },
    1.93 +
    1.94 +  // Check when providers are enabled and social is turned on at startup.
    1.95 +  testEnabledStartup: function(cbnext) {
    1.96 +    setManifestPref("social.manifest.test", manifest);
    1.97 +    ok(!SocialSidebar.opened, "sidebar is closed initially");
    1.98 +    SocialService.addProvider(manifest, function() {
    1.99 +      SocialService.addProvider(manifest2, function (provider) {
   1.100 +        SocialSidebar.show();
   1.101 +        waitForCondition(function() SocialSidebar.opened,
   1.102 +                     function() {
   1.103 +          ok(SocialSidebar.opened, "first window sidebar is open");
   1.104 +          openWindowAndWaitForInit(window, function(w1) {
   1.105 +            ok(w1.SocialSidebar.opened, "new window sidebar is open");
   1.106 +            ok(SocialService.hasEnabledProviders, "providers are enabled");
   1.107 +            checkSocialUI(w1);
   1.108 +            // now init is complete, open a second window
   1.109 +            openWindowAndWaitForInit(window, function(w2) {
   1.110 +              ok(w1.SocialSidebar.opened, "w1 sidebar is open");
   1.111 +              ok(w2.SocialSidebar.opened, "w2 sidebar is open");
   1.112 +              checkSocialUI(w2);
   1.113 +              checkSocialUI(w1);
   1.114 +
   1.115 +              // disable social and re-check
   1.116 +              SocialService.removeProvider(manifest.origin, function() {
   1.117 +                SocialService.removeProvider(manifest2.origin, function() {
   1.118 +                  ok(!Social.enabled, "social is disabled");
   1.119 +                  is(Social.providers.length, 0, "no providers");
   1.120 +                  ok(!w1.SocialSidebar.opened, "w1 sidebar is closed");
   1.121 +                  ok(!w2.SocialSidebar.opened, "w2 sidebar is closed");
   1.122 +                  checkSocialUI(w2);
   1.123 +                  checkSocialUI(w1);
   1.124 +                  Services.prefs.clearUserPref("social.manifest.test");
   1.125 +                  cbnext();
   1.126 +                });
   1.127 +              });
   1.128 +            });
   1.129 +          });
   1.130 +        }, "sidebar did not open");
   1.131 +      }, cbnext);
   1.132 +    }, cbnext);
   1.133 +  },
   1.134 +
   1.135 +  testGlobalState: function(cbnext) {
   1.136 +    setManifestPref("social.manifest.test", manifest);
   1.137 +    ok(!SocialSidebar.opened, "sidebar is closed initially");
   1.138 +    ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state unset");
   1.139 +    // mimick no session state in opener so we exercise the global state via pref
   1.140 +    SessionStore.deleteWindowValue(window, "socialSidebar");
   1.141 +    ok(!SessionStore.getWindowValue(window, "socialSidebar"), "window state unset");
   1.142 +    SocialService.addProvider(manifest, function() {
   1.143 +      openWindowAndWaitForInit(window, function(w1) {
   1.144 +        w1.SocialSidebar.show();
   1.145 +        waitForCondition(function() w1.SocialSidebar.opened,
   1.146 +                     function() {
   1.147 +          ok(Services.prefs.prefHasUserValue("social.sidebar.provider"), "global state set");
   1.148 +          ok(!SocialSidebar.opened, "1. main sidebar is still closed");
   1.149 +          ok(w1.SocialSidebar.opened, "1. window sidebar is open");
   1.150 +          closeWindow(w1, function() {
   1.151 +            // this time, the global state should cause the sidebar to be opened
   1.152 +            // in the new window
   1.153 +            openWindowAndWaitForInit(window, function(w1) {
   1.154 +              ok(!SocialSidebar.opened, "2. main sidebar is still closed");
   1.155 +              ok(w1.SocialSidebar.opened, "2. window sidebar is open");
   1.156 +              w1.SocialSidebar.hide();
   1.157 +              ok(!w1.SocialSidebar.opened, "2. window sidebar is closed");
   1.158 +              ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "2. global state unset");
   1.159 +              // global state should now be no sidebar gets opened on new window
   1.160 +              closeWindow(w1, function() {
   1.161 +                ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "3. global state unset");
   1.162 +                ok(!SocialSidebar.opened, "3. main sidebar is still closed");
   1.163 +                openWindowAndWaitForInit(window, function(w1) {
   1.164 +                  ok(!Services.prefs.prefHasUserValue("social.sidebar.provider"), "4. global state unset");
   1.165 +                  ok(!SocialSidebar.opened, "4. main sidebar is still closed");
   1.166 +                  ok(!w1.SocialSidebar.opened, "4. window sidebar is closed");
   1.167 +                  SocialService.removeProvider(manifest.origin, function() {
   1.168 +                    Services.prefs.clearUserPref("social.manifest.test");
   1.169 +                    cbnext();
   1.170 +                  });
   1.171 +                });
   1.172 +              });
   1.173 +            });
   1.174 +          });
   1.175 +        });        
   1.176 +      });
   1.177 +    });
   1.178 +  },
   1.179 +
   1.180 +  // Check per window sidebar functionality, including migration from using
   1.181 +  // prefs to using session state, and state inheritance of windows (new windows
   1.182 +  // inherit state from the opener).
   1.183 +  testPerWindowSidebar: function(cbnext) {
   1.184 +    function finishCheck() {
   1.185 +      // disable social and re-check
   1.186 +      SocialService.removeProvider(manifest.origin, function() {
   1.187 +        SocialService.removeProvider(manifest2.origin, function() {
   1.188 +          ok(!Social.enabled, "social is disabled");
   1.189 +          is(Social.providers.length, 0, "no providers");
   1.190 +          Services.prefs.clearUserPref("social.manifest.test");
   1.191 +          cbnext();
   1.192 +        });
   1.193 +      });
   1.194 +    }
   1.195 +
   1.196 +    setManifestPref("social.manifest.test", manifest);
   1.197 +    ok(!SocialSidebar.opened, "sidebar is closed initially");
   1.198 +    SocialService.addProvider(manifest, function() {
   1.199 +      SocialService.addProvider(manifest2, function (provider) {
   1.200 +        // test the migration of the social.sidebar.open pref. We'll set a user
   1.201 +        // level pref to indicate it was open (along with the old
   1.202 +        // social.provider.current pref), then we'll open a window. During the
   1.203 +        // restoreState of the window, those prefs should be migrated, and the
   1.204 +        // sidebar should be opened.  Both prefs are then removed.
   1.205 +        Services.prefs.setCharPref("social.provider.current", "https://example.com");
   1.206 +        Services.prefs.setBoolPref("social.sidebar.open", true);
   1.207 +
   1.208 +        openWindowAndWaitForInit(window, function(w1) {
   1.209 +          ok(w1.SocialSidebar.opened, "new window sidebar is open");
   1.210 +          ok(SocialService.hasEnabledProviders, "providers are enabled");
   1.211 +          ok(!Services.prefs.prefHasUserValue("social.provider.current"), "social.provider.current pref removed");
   1.212 +          ok(!Services.prefs.prefHasUserValue("social.sidebar.open"), "social.sidebar.open pref removed");
   1.213 +          checkSocialUI(w1);
   1.214 +          // now init is complete, open a second window, it's state should be the same as the opener
   1.215 +          openWindowAndWaitForInit(w1, function(w2) {
   1.216 +            ok(w1.SocialSidebar.opened, "w1 sidebar is open");
   1.217 +            ok(w2.SocialSidebar.opened, "w2 sidebar is open");
   1.218 +            checkSocialUI(w2);
   1.219 +            checkSocialUI(w1);
   1.220 +
   1.221 +            // change the sidebar in w2
   1.222 +            w2.SocialSidebar.show(manifest2.origin);
   1.223 +            let sbrowser1 = w1.document.getElementById("social-sidebar-browser");
   1.224 +            is(manifest.origin, sbrowser1.getAttribute("origin"), "w1 sidebar origin matches");
   1.225 +            let sbrowser2 = w2.document.getElementById("social-sidebar-browser");
   1.226 +            is(manifest2.origin, sbrowser2.getAttribute("origin"), "w2 sidebar origin matches");
   1.227 +
   1.228 +            // hide sidebar, w1 sidebar should still be open
   1.229 +            w2.SocialSidebar.hide();
   1.230 +            ok(w1.SocialSidebar.opened, "w1 sidebar is opened");
   1.231 +            ok(!w2.SocialSidebar.opened, "w2 sidebar is closed");
   1.232 +            ok(sbrowser2.parentNode.hidden, "w2 sidebar is hidden");
   1.233 +
   1.234 +            // open a 3rd window from w2, it should inherit the state of w2
   1.235 +            openWindowAndWaitForInit(w2, function(w3) {
   1.236 +              // since the sidebar is not open, we need to ensure the provider
   1.237 +              // is selected to test we inherited the provider from the opener
   1.238 +              w3.SocialSidebar.ensureProvider();
   1.239 +              is(w3.SocialSidebar.provider, w2.SocialSidebar.provider, "w3 has same provider as w2");
   1.240 +              ok(!w3.SocialSidebar.opened, "w2 sidebar is closed");
   1.241 +
   1.242 +              // open a 4th window from w1, it should inherit the state of w1
   1.243 +              openWindowAndWaitForInit(w1, function(w4) {
   1.244 +                is(w4.SocialSidebar.provider, w1.SocialSidebar.provider, "w4 has same provider as w1");
   1.245 +                ok(w4.SocialSidebar.opened, "w4 sidebar is opened");
   1.246 +
   1.247 +                finishCheck();
   1.248 +              });
   1.249 +            });
   1.250 +
   1.251 +          });
   1.252 +        });
   1.253 +      }, cbnext);
   1.254 +    }, cbnext);
   1.255 +  }
   1.256 +}

mercurial