toolkit/components/places/tests/browser/browser_bug680727.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/browser/browser_bug680727.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/* Ensure that clicking the button in the Offline mode neterror page updates
     1.8 +   global history. See bug 680727. */
     1.9 +/* TEST_PATH=toolkit/components/places/tests/browser/browser_bug680727.js make -C $(OBJDIR) mochitest-browser-chrome */
    1.10 +
    1.11 +
    1.12 +const kUniqueURI = Services.io.newURI("http://mochi.test:8888/#bug_680727",
    1.13 +                                      null, null);
    1.14 +var gAsyncHistory = 
    1.15 +  Cc["@mozilla.org/browser/history;1"].getService(Ci.mozIAsyncHistory);
    1.16 +
    1.17 +let proxyPrefValue;
    1.18 +
    1.19 +function test() {
    1.20 +  waitForExplicitFinish();
    1.21 +
    1.22 +  gBrowser.selectedTab = gBrowser.addTab();
    1.23 +
    1.24 +  // Tests always connect to localhost, and per bug 87717, localhost is now
    1.25 +  // reachable in offline mode.  To avoid this, disable any proxy.
    1.26 +  proxyPrefValue = Services.prefs.getIntPref("network.proxy.type");
    1.27 +  Services.prefs.setIntPref("network.proxy.type", 0);
    1.28 +
    1.29 +  // Clear network cache.
    1.30 +  Components.classes["@mozilla.org/netwerk/cache-storage-service;1"]
    1.31 +            .getService(Components.interfaces.nsICacheStorageService)
    1.32 +            .clear();
    1.33 +
    1.34 +  // Go offline, expecting the error page.
    1.35 +  Services.io.offline = true;
    1.36 +  window.addEventListener("DOMContentLoaded", errorListener, false);
    1.37 +  content.location = kUniqueURI.spec;
    1.38 +}
    1.39 +
    1.40 +//------------------------------------------------------------------------------
    1.41 +// listen to loading the neterror page. (offline mode)
    1.42 +function errorListener() {
    1.43 +  if(content.location == "about:blank") {
    1.44 +    info("got about:blank, which is expected once, so return");
    1.45 +    return;
    1.46 +  }
    1.47 +
    1.48 +  window.removeEventListener("DOMContentLoaded", errorListener, false);
    1.49 +  ok(Services.io.offline, "Services.io.offline is true.");
    1.50 +
    1.51 +  // This is an error page.
    1.52 +  is(gBrowser.contentDocument.documentURI.substring(0, 27),
    1.53 +     "about:neterror?e=netOffline",
    1.54 +     "Document URI is the error page.");
    1.55 +
    1.56 +  // But location bar should show the original request.
    1.57 +  is(content.location.href, kUniqueURI.spec,
    1.58 +     "Docshell URI is the original URI.");
    1.59 +
    1.60 +  // Global history does not record URI of a failed request.
    1.61 +  promiseAsyncUpdates().then(function() {
    1.62 +    gAsyncHistory.isURIVisited(kUniqueURI, errorAsyncListener);
    1.63 +  });
    1.64 +}
    1.65 +
    1.66 +function errorAsyncListener(aURI, aIsVisited) {
    1.67 +  ok(kUniqueURI.equals(aURI) && !aIsVisited,
    1.68 +     "The neterror page is not listed in global history.");
    1.69 +
    1.70 +  Services.prefs.setIntPref("network.proxy.type", proxyPrefValue);
    1.71 +
    1.72 +  // Now press the "Try Again" button, with offline mode off.
    1.73 +  Services.io.offline = false;
    1.74 +
    1.75 +  window.addEventListener("DOMContentLoaded", reloadListener, false);
    1.76 +
    1.77 +  ok(gBrowser.contentDocument.getElementById("errorTryAgain"),
    1.78 +     "The error page has got a #errorTryAgain element");
    1.79 +  gBrowser.contentDocument.getElementById("errorTryAgain").click();
    1.80 +}
    1.81 +
    1.82 +//------------------------------------------------------------------------------
    1.83 +// listen to reload of neterror.
    1.84 +function reloadListener() {
    1.85 +  window.removeEventListener("DOMContentLoaded", reloadListener, false);
    1.86 +
    1.87 +  // This listener catches "DOMContentLoaded" on being called
    1.88 +  // nsIWPL::onLocationChange(...). That is right *AFTER*
    1.89 +  // IHistory::VisitURI(...) is called.
    1.90 +  ok(!Services.io.offline, "Services.io.offline is false.");
    1.91 +
    1.92 +  // This is not an error page.
    1.93 +  is(gBrowser.contentDocument.documentURI, kUniqueURI.spec, 
    1.94 +     "Document URI is not the offline-error page, but the original URI.");
    1.95 +
    1.96 +  // Check if global history remembers the successfully-requested URI.
    1.97 +  promiseAsyncUpdates().then(function() {
    1.98 +    gAsyncHistory.isURIVisited(kUniqueURI, reloadAsyncListener);
    1.99 +  });
   1.100 +}
   1.101 +
   1.102 +function reloadAsyncListener(aURI, aIsVisited) {
   1.103 +  ok(kUniqueURI.equals(aURI) && aIsVisited, "We have visited the URI.");
   1.104 +  promiseClearHistory().then(finish);
   1.105 +}
   1.106 +
   1.107 +registerCleanupFunction(function() {
   1.108 +  Services.prefs.setIntPref("network.proxy.type", proxyPrefValue);
   1.109 +  Services.io.offline = false;
   1.110 +  window.removeEventListener("DOMContentLoaded", errorListener, false);
   1.111 +  window.removeEventListener("DOMContentLoaded", reloadListener, false);
   1.112 +  gBrowser.removeCurrentTab();
   1.113 +});

mercurial