summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/offline/test_offlineMode.html
blob: 1d4610fb5013c344943449c16df622ff466ef280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<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>