<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs">
<head>
<title>Offline mode test</title>

<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/dom/tests/mochitest/ajax/offline/offlineTests.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

<script class="testbody" type="text/javascript">

/**
 * The test loads a manifest and cache it.
 * Then tests if all works in online mode
 * as expected. Then switches firefox to offline
 * mode and tries the page load still works as
 * expected, i.e. all items from the cache load.
 */

var gImplicitWindow = null;
var gCompleteTimeout = null;
var gGotExplicitVersion = 0;
var gGotImplicitVersion = 0;
var gGotDynamicVersion = 0;
var gGotOnError = false;

function createURI(urispec)
{
  var ioServ = Components.classes["@mozilla.org/network/io-service;1"]
                         .getService(Components.interfaces.nsIIOService);
  return ioServ.newURI(urispec, null, null);
}

// test

function manifestUpdated()
{
  applicationCache.mozAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html");
  OfflineTest.waitForAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html", dynamicAdded);
}

function dynamicAdded()
{
  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html";
}

// Called by the dynamically added iframe on load
function notwhitelistOnLoad()
{
  gGotDynamicVersion = 1;
  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs";
}

// Called by the explicit iframe on load
function frameLoad(version)
{
  gGotExplicitVersion = version;
  gImplicitWindow = window.open("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html");
}

// Called by the implicit window on load
function implicitLoaded(aWindow, errorOccured)
{
  aWindow.close();

  gGotImplicitVersion = 1;
  OfflineTest.waitForAdd("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", implicitAdded);
}

function implicitAdded()
{
  OfflineTest.priv(finalize)();
}

function finalize()
{
  window.clearTimeout(gCompleteTimeout);

  var ioserv = Cc["@mozilla.org/network/io-service;1"]
      .getService(Ci.nsIIOService);

  if (!ioserv.offline)
  {
    OfflineTest.is(gGotExplicitVersion, 1, "Explicit entry loaded");
    OfflineTest.is(gGotImplicitVersion, 1, "Implicit entry loaded");
    OfflineTest.is(gGotDynamicVersion, 1, "Dynamic entry loaded");

    gGotExplicitVersion = 0;
    gGotImplicitVersion = 0;
    gGotDynamicVersion = 0;

    var entries = [
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html", true],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs", true],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", true]
    ];
    OfflineTest.checkCacheEntries(entries, goOffline);
  }
  else
  {
    gImplicitWindow.close();

    ioserv.offline = false;

    OfflineTest.is(gGotExplicitVersion, 1, "Explicit entry loaded");
    OfflineTest.is(gGotImplicitVersion, 1, "Bug 461325 - Implicit entry loaded");
    OfflineTest.is(gGotDynamicVersion, 1, "Dynamic entry loaded");
    OfflineTest.ok(gGotOnError, "Got onerror event invoked by implicit page load in offline mode");

    OfflineTest.teardownAndFinish();
  }
}

function goOffline()
{
  var listener = {
    onCacheEntryDoomed: function (status) {
      OfflineTest.priv(goOfflineContinue)();
    }
  };

  // Delete HTTP cache to ensure we are going from offline cache
  var cache = Cc["@mozilla.org/network/cache-storage-service;1"]
      .getService(Ci.nsICacheStorageService);
  var storage = cache.diskCacheStorage(LoadContextInfo.default, false);
  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html"), "", null);
  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs"), "", null);
  storage.asyncDoomURI(createURI("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html"), "", listener);
}

function goOfflineContinue()
{
  var ioserv = Cc["@mozilla.org/network/io-service;1"]
      .getService(Ci.nsIIOService);

  ioserv.offline = true;

  gCompleteTimeout = window.setTimeout(OfflineTest.priv(finalize), 10000);

  // remove error handling. in offline mode
  // is correct to get error message
  applicationCache.onerror = function() {gGotOnError = true;}

  aFrame.location = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/notonwhitelist.html";
  // Starts the chain all over again but in offline mode.
}

SimpleTest.waitForExplicitFinish();

if (OfflineTest.setup()) {
  applicationCache.onerror = OfflineTest.failEvent;
  applicationCache.onupdateready = OfflineTest.failEvent;
  applicationCache.oncached = OfflineTest.priv(manifestUpdated);
}

</script>

</head>

<body>

<iframe name="aFrame" src=""></iframe>

</body>
</html>