dom/tests/mochitest/ajax/offline/test_offlineMode.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/ajax/offline/test_offlineMode.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs">
     1.5 +<head>
     1.6 +<title>Offline mode test</title>
     1.7 +
     1.8 +<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
    1.10 +<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.11 +
    1.12 +<script class="testbody" type="text/javascript">
    1.13 +
    1.14 +/**
    1.15 + * The test loads a manifest and cache it.
    1.16 + * Then tests if all works in online mode
    1.17 + * as expected. Then switches firefox to offline
    1.18 + * mode and tries the page load still works as
    1.19 + * expected, i.e. all items from the cache load.
    1.20 + */
    1.21 +
    1.22 +var gImplicitWindow = null;
    1.23 +var gCompleteTimeout = null;
    1.24 +var gGotExplicitVersion = 0;
    1.25 +var gGotImplicitVersion = 0;
    1.26 +var gGotDynamicVersion = 0;
    1.27 +var gGotOnError = false;
    1.28 +
    1.29 +function createURI(urispec)
    1.30 +{
    1.31 +  var ioServ = Components.classes["@mozilla.org/network/io-service;1"]
    1.32 +                         .getService(Components.interfaces.nsIIOService);
    1.33 +  return ioServ.newURI(urispec, null, null);
    1.34 +}
    1.35 +
    1.36 +// test
    1.37 +
    1.38 +function manifestUpdated()
    1.39 +{
    1.40 +  applicationCache.mozAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html");
    1.41 +  OfflineTest.waitForAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html", dynamicAdded);
    1.42 +}
    1.43 +
    1.44 +function dynamicAdded()
    1.45 +{
    1.46 +  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html";
    1.47 +}
    1.48 +
    1.49 +// Called by the dynamically added iframe on load
    1.50 +function notwhitelistOnLoad()
    1.51 +{
    1.52 +  gGotDynamicVersion = 1;
    1.53 +  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs";
    1.54 +}
    1.55 +
    1.56 +// Called by the explicit iframe on load
    1.57 +function frameLoad(version)
    1.58 +{
    1.59 +  gGotExplicitVersion = version;
    1.60 +  gImplicitWindow = window.open("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html");
    1.61 +}
    1.62 +
    1.63 +// Called by the implicit window on load
    1.64 +function implicitLoaded(aWindow, errorOccured)
    1.65 +{
    1.66 +  aWindow.close();
    1.67 +
    1.68 +  gGotImplicitVersion = 1;
    1.69 +  OfflineTest.waitForAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", implicitAdded);
    1.70 +}
    1.71 +
    1.72 +function implicitAdded()
    1.73 +{
    1.74 +  OfflineTest.priv(finalize)();
    1.75 +}
    1.76 +
    1.77 +function finalize()
    1.78 +{
    1.79 +  window.clearTimeout(gCompleteTimeout);
    1.80 +
    1.81 +  var ioserv = Cc["@mozilla.org/network/io-service;1"]
    1.82 +      .getService(Ci.nsIIOService);
    1.83 +
    1.84 +  if (!ioserv.offline)
    1.85 +  {
    1.86 +    OfflineTest.is(gGotExplicitVersion, 1, "Explicit entry loaded");
    1.87 +    OfflineTest.is(gGotImplicitVersion, 1, "Implicit entry loaded");
    1.88 +    OfflineTest.is(gGotDynamicVersion, 1, "Dynamic entry loaded");
    1.89 +
    1.90 +    gGotExplicitVersion = 0;
    1.91 +    gGotImplicitVersion = 0;
    1.92 +    gGotDynamicVersion = 0;
    1.93 +
    1.94 +    var entries = [
    1.95 +      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html", true],
    1.96 +      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs", true],
    1.97 +      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", true]
    1.98 +    ];
    1.99 +    OfflineTest.checkCacheEntries(entries, goOffline);
   1.100 +  }
   1.101 +  else
   1.102 +  {
   1.103 +    gImplicitWindow.close();
   1.104 +
   1.105 +    ioserv.offline = false;
   1.106 +
   1.107 +    OfflineTest.is(gGotExplicitVersion, 1, "Explicit entry loaded");
   1.108 +    OfflineTest.is(gGotImplicitVersion, 1, "Bug 461325 - Implicit entry loaded");
   1.109 +    OfflineTest.is(gGotDynamicVersion, 1, "Dynamic entry loaded");
   1.110 +    OfflineTest.ok(gGotOnError, "Got onerror event invoked by implicit page load in offline mode");
   1.111 +
   1.112 +    OfflineTest.teardownAndFinish();
   1.113 +  }
   1.114 +}
   1.115 +
   1.116 +function goOffline()
   1.117 +{
   1.118 +  var listener = {
   1.119 +    onCacheEntryDoomed: function (status) {
   1.120 +      OfflineTest.priv(goOfflineContinue)();
   1.121 +    }
   1.122 +  };
   1.123 +
   1.124 +  // Delete HTTP cache to ensure we are going from offline cache
   1.125 +  var cache = Cc["@mozilla.org/network/cache-storage-service;1"]
   1.126 +      .getService(Ci.nsICacheStorageService);
   1.127 +  var storage = cache.diskCacheStorage(LoadContextInfo.default, false);
   1.128 +  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html"), "", null);
   1.129 +  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs"), "", null);
   1.130 +  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html"), "", listener);
   1.131 +}
   1.132 +
   1.133 +function goOfflineContinue()
   1.134 +{
   1.135 +  var ioserv = Cc["@mozilla.org/network/io-service;1"]
   1.136 +      .getService(Ci.nsIIOService);
   1.137 +
   1.138 +  ioserv.offline = true;
   1.139 +
   1.140 +  gCompleteTimeout = window.setTimeout(OfflineTest.priv(finalize), 10000);
   1.141 +
   1.142 +  // remove error handling. in offline mode
   1.143 +  // is correct to get error message
   1.144 +  applicationCache.onerror = function() {gGotOnError = true;}
   1.145 +
   1.146 +  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html";
   1.147 +  // Starts the chain all over again but in offline mode.
   1.148 +}
   1.149 +
   1.150 +SimpleTest.waitForExplicitFinish();
   1.151 +
   1.152 +if (OfflineTest.setup()) {
   1.153 +  applicationCache.onerror = OfflineTest.failEvent;
   1.154 +  applicationCache.onupdateready = OfflineTest.failEvent;
   1.155 +  applicationCache.oncached = OfflineTest.priv(manifestUpdated);
   1.156 +}
   1.157 +
   1.158 +</script>
   1.159 +
   1.160 +</head>
   1.161 +
   1.162 +<body>
   1.163 +
   1.164 +<iframe name="aFrame" src=""></iframe>
   1.165 +
   1.166 +</body>
   1.167 +</html>

mercurial