browser/base/content/test/social/browser_social_errorPage.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_errorPage.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,178 @@
     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 +function gc() {
     1.9 +  Cu.forceGC();
    1.10 +  let wu =  window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
    1.11 +                  .getInterface(Components.interfaces.nsIDOMWindowUtils);
    1.12 +  wu.garbageCollect();
    1.13 +}
    1.14 +
    1.15 +// Support for going on and offline.
    1.16 +// (via browser/base/content/test/browser_bookmark_titles.js)
    1.17 +let origProxyType = Services.prefs.getIntPref('network.proxy.type');
    1.18 +
    1.19 +function goOffline() {
    1.20 +  // Simulate a network outage with offline mode. (Localhost is still
    1.21 +  // accessible in offline mode, so disable the test proxy as well.)
    1.22 +  if (!Services.io.offline)
    1.23 +    BrowserOffline.toggleOfflineStatus();
    1.24 +  Services.prefs.setIntPref('network.proxy.type', 0);
    1.25 +  // LOAD_FLAGS_BYPASS_CACHE isn't good enough. So clear the cache.
    1.26 +  Services.cache2.clear();
    1.27 +}
    1.28 +
    1.29 +function goOnline(callback) {
    1.30 +  Services.prefs.setIntPref('network.proxy.type', origProxyType);
    1.31 +  if (Services.io.offline)
    1.32 +    BrowserOffline.toggleOfflineStatus();
    1.33 +  if (callback)
    1.34 +    callback();
    1.35 +}
    1.36 +
    1.37 +function openPanel(url, panelCallback, loadCallback) {
    1.38 +  // open a flyout
    1.39 +  SocialFlyout.open(url, 0, panelCallback);
    1.40 +  SocialFlyout.panel.firstChild.addEventListener("load", function panelLoad() {
    1.41 +    SocialFlyout.panel.firstChild.removeEventListener("load", panelLoad, true);
    1.42 +    loadCallback();
    1.43 +  }, true);
    1.44 +}
    1.45 +
    1.46 +function openChat(url, panelCallback, loadCallback) {
    1.47 +  // open a chat window
    1.48 +  SocialChatBar.openChat(SocialSidebar.provider, url, panelCallback);
    1.49 +  SocialChatBar.chatbar.firstChild.addEventListener("DOMContentLoaded", function panelLoad() {
    1.50 +    SocialChatBar.chatbar.firstChild.removeEventListener("DOMContentLoaded", panelLoad, true);
    1.51 +    loadCallback();
    1.52 +  }, true);
    1.53 +}
    1.54 +
    1.55 +function onSidebarLoad(callback) {
    1.56 +  let sbrowser = document.getElementById("social-sidebar-browser");
    1.57 +  sbrowser.addEventListener("load", function load() {
    1.58 +    sbrowser.removeEventListener("load", load, true);
    1.59 +    callback();
    1.60 +  }, true);
    1.61 +}
    1.62 +
    1.63 +function ensureWorkerLoaded(provider, callback) {
    1.64 +  // once the worker responds to a ping we know it must be up.
    1.65 +  let port = provider.getWorkerPort();
    1.66 +  port.onmessage = function(msg) {
    1.67 +    if (msg.data.topic == "pong") {
    1.68 +      port.close();
    1.69 +      callback();
    1.70 +    }
    1.71 +  }
    1.72 +  port.postMessage({topic: "ping"})
    1.73 +}
    1.74 +
    1.75 +let manifest = { // normal provider
    1.76 +  name: "provider 1",
    1.77 +  origin: "https://example.com",
    1.78 +  sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
    1.79 +  workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
    1.80 +  iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
    1.81 +};
    1.82 +
    1.83 +function test() {
    1.84 +  waitForExplicitFinish();
    1.85 +
    1.86 +  runSocialTestWithProvider(manifest, function (finishcb) {
    1.87 +    runSocialTests(tests, undefined, goOnline, finishcb);
    1.88 +  });
    1.89 +}
    1.90 +
    1.91 +var tests = {
    1.92 +  testSidebar: function(next) {
    1.93 +    let sbrowser = document.getElementById("social-sidebar-browser");
    1.94 +    onSidebarLoad(function() {
    1.95 +      ok(sbrowser.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
    1.96 +      gc();
    1.97 +      // Add a new load listener, then find and click the "try again" button.
    1.98 +      onSidebarLoad(function() {
    1.99 +        // should still be on the error page.
   1.100 +        ok(sbrowser.contentDocument.location.href.indexOf("about:socialerror?")==0, "is still on social error page");
   1.101 +        // go online and try again - this should work.
   1.102 +        goOnline();
   1.103 +        onSidebarLoad(function() {
   1.104 +          // should now be on the correct page.
   1.105 +          is(sbrowser.contentDocument.location.href, manifest.sidebarURL, "is now on social sidebar page");
   1.106 +          next();
   1.107 +        });
   1.108 +        sbrowser.contentDocument.getElementById("btnTryAgain").click();
   1.109 +      });
   1.110 +      sbrowser.contentDocument.getElementById("btnTryAgain").click();
   1.111 +    });
   1.112 +    // we want the worker to be fully loaded before going offline, otherwise
   1.113 +    // it might fail due to going offline.
   1.114 +    ensureWorkerLoaded(SocialSidebar.provider, function() {
   1.115 +      // go offline then attempt to load the sidebar - it should fail.
   1.116 +      goOffline();
   1.117 +      SocialSidebar.show();
   1.118 +  });
   1.119 +  },
   1.120 +
   1.121 +  testFlyout: function(next) {
   1.122 +    let panelCallbackCount = 0;
   1.123 +    let panel = document.getElementById("social-flyout-panel");
   1.124 +    // go offline and open a flyout.
   1.125 +    goOffline();
   1.126 +    openPanel(
   1.127 +      "https://example.com/browser/browser/base/content/test/social/social_panel.html",
   1.128 +      function() { // the panel api callback
   1.129 +        panelCallbackCount++;
   1.130 +      },
   1.131 +      function() { // the "load" callback.
   1.132 +        executeSoon(function() {
   1.133 +          todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
   1.134 +          ok(panel.firstChild.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
   1.135 +          // Bug 832943 - the listeners previously stopped working after a GC, so
   1.136 +          // force a GC now and try again.
   1.137 +          gc();
   1.138 +          openPanel(
   1.139 +            "https://example.com/browser/browser/base/content/test/social/social_panel.html",
   1.140 +            function() { // the panel api callback
   1.141 +              panelCallbackCount++;
   1.142 +            },
   1.143 +            function() { // the "load" callback.
   1.144 +              executeSoon(function() {
   1.145 +                todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
   1.146 +                ok(panel.firstChild.contentDocument.location.href.indexOf("about:socialerror?")==0, "is on social error page");
   1.147 +                gc();
   1.148 +                SocialFlyout.unload();
   1.149 +                next();
   1.150 +              });
   1.151 +            }
   1.152 +          );
   1.153 +        });
   1.154 +      }
   1.155 +    );
   1.156 +  },
   1.157 +
   1.158 +  testChatWindow: function(next) {
   1.159 +    let panelCallbackCount = 0;
   1.160 +    // go offline and open a flyout.
   1.161 +    goOffline();
   1.162 +    openChat(
   1.163 +      "https://example.com/browser/browser/base/content/test/social/social_chat.html",
   1.164 +      function() { // the panel api callback
   1.165 +        panelCallbackCount++;
   1.166 +      },
   1.167 +      function() { // the "load" callback.
   1.168 +        executeSoon(function() {
   1.169 +          todo_is(panelCallbackCount, 0, "Bug 833207 - should be no callback when error page loads.");
   1.170 +          let chat = SocialChatBar.chatbar.selectedChat;
   1.171 +          waitForCondition(function() chat.contentDocument.location.href.indexOf("about:socialerror?")==0,
   1.172 +                           function() {
   1.173 +                            chat.close();
   1.174 +                            next();
   1.175 +                            },
   1.176 +                           "error page didn't appear");
   1.177 +        });
   1.178 +      }
   1.179 +    );
   1.180 +  }
   1.181 +}

mercurial