summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/offline/test_updatingManifest.html
blob: 6d573a92663a218a10b0e62a2d735199e09256c3 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs">
<head>
<title>Cache update 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">

/**
 * This test loads manifest and checks presence of all items.
 * Then it modifies the manifest and updates the cache again.
 * Then test presence of items according to the spec and also
 * if the cache associated with the document is still the old
 * one. Then again modifies the manifest, checks items and finally
 * swaps cache for this document, reloads and checks document state.
 */

const NAMESPACE_BYPASS = SpecialPowers.Ci.nsIApplicationCacheNamespace.NAMESPACE_BYPASS;
const NAMESPACE_FALLBACK = SpecialPowers.Ci.nsIApplicationCacheNamespace.NAMESPACE_FALLBACK;

var gStep = 0;
var gGotFrameVersion = 0;
var gCallOnUpdatingFrameLoad = null;

// Helpers

function reloadLocations(frames)
{
  for (frame in frames)
    frames[frame].location.reload();
}

function waitForLocations(frames, doneFunc)
{
  frame = frames.shift();
  if (frame)
    // toString() might cause problems when this test will
    // completely be changed to test IDN behavior.
    OfflineTest.waitForAdd(frame, function()
      {
        waitForLocations(frames, doneFunc);
      }
    );
  else
  {
    doneFunc();
  }
}

function checkFallbackAndWhitelisting(key, fallback, onwhitelist)
{
  // Get matching namespace for the key
  var matchingNamespace = OfflineTest.getActiveCache()
    .getMatchingNamespace(key);

  // If we are not expecting the key is to be on white list or
  // has been assigned a fallback check there is not any matching
  // namespace found and exit
  if (!fallback && !onwhitelist) {
    is(matchingNamespace, null, "No namespace found for "+key);
    return;
  }

  // We expect this entry is on the white list or has a fallback
  // entry assigned, check we found a matching namespace
  ok(matchingNamespace, "We have a namespace for "+key);
  if (!matchingNamespace)
    return;

  // We expect the key be assigned a fallback URI, check the namespace
  // type is of fallback type
  OfflineTest.is(!!(matchingNamespace.itemType & NAMESPACE_FALLBACK), !!fallback,
    (fallback ? "Namespace type is fallback for " : "Namespace type is not fallback for ")+key);

  // We expect the key be assigned a fallback URI, check the URI is
  // equal to expected one
  OfflineTest.is(matchingNamespace.data, fallback,
    (fallback ? "Expected correct fallback for " : "No fallback for ")+key);

  // We expect the key be on the white list, check the namespace type
  // is of bypass type
  OfflineTest.is(!!(matchingNamespace.itemType & NAMESPACE_BYPASS), onwhitelist,
    (onwhitelist ? "On white list " : "Not on white list ")+key);
}

// Events

function frameLoad(version)
{
  gGotFrameVersion = version;
  if (gCallOnUpdatingFrameLoad)
  {
    var call = gCallOnUpdatingFrameLoad;
    gCallOnUpdatingFrameLoad = null;
    SimpleTest.executeSoon(OfflineTest.priv(call));
  }
}

function whitelistOnLoad(version)
{
  // Whitelisting is not tested by this test...
}


// Start of the test function chain
// ================================

function manifestCached()
{
  OfflineTest.is(gStep, 0, "Got manifestCached in step 0, gStep=" + gStep);

  reloadLocations([fallbackFrame1, fallbackFrame2]);
  waitForLocations(
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html"],
    fallbackLoaded
  );
}

function fallbackLoaded()
{
  dump("in fallbackLoaded\n");
  applicationCache.mozAdd("http://mochi.test:8888/tests/SimpleTest/EventUtils.js");
  OfflineTest.waitForAdd("http://mochi.test:8888/tests/SimpleTest/EventUtils.js",
      dynamicLoaded);
}

function dynamicLoaded()
{
  window.open("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html");
  // window.applicationCache.noupdate invokes implicitLoaded()
}

function implicitLoaded(aWindow, errorOccured)
{
  aWindow.close();

  OfflineTest.ok(!errorOccured, "No error on new implicit page manifest update");

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

function implicitCached()
{
  // Checking first version of the manifest + another implict page caching

  // Whitelist entries
  checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", "", true);

  // Fallback URI selection check
  checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/opp.html",
      "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", false);
  checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/opp.html",
      "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", false);

  // Cache object status
  OfflineTest.is(applicationCache.status, SpecialPowers.Ci.nsIDOMOfflineResourceList.IDLE,
      "we have associated application cache (1)");

  OfflineTest.is(gGotFrameVersion, 1, "IFrame version 1");

  var entries = [
    // Explicit entries
    ["http://mochi.test:8888/tests/SimpleTest/SimpleTest.js", false],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/offlineTests.js", true],

    // Fallback entries
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", true],
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback2.html", false],

    // Whitelist entries
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", false],

    // Implicit entries
    ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", true],

    // Dynamic entries
    ["http://mochi.test:8888/tests/SimpleTest/EventUtils.js", true]
  ];
  OfflineTest.checkCacheEntries(
    entries,
    function() {
      ++gStep;

      OfflineTest.setSJSState("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs", "second");

      applicationCache.update();
      // Invokes manifestUpdated()
    });
}

function manifestUpdated()
{
  OfflineTest.ok(gStep == 1 || gStep == 2, "Got manifestUpdated in step 1 or 2, gStep=" + gStep);

  switch (gStep)
  {
  case 1:
    // Processing second version of the manifest.

    // Whitelist entries
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", "", false);

    // Fallback URI selection check
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/opp.html",
        "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", false);
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/opp.html",
        "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback2.html", false);

    // Cache object status
    OfflineTest.is(applicationCache.status, SpecialPowers.Ci.nsIDOMOfflineResourceList.UPDATEREADY,
        "we have associated application cache and update is pending (2)");

    var entries = [
      // Explicit entries
      ["http://mochi.test:8888/tests/SimpleTest/SimpleTest.js", true],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/offlineTests.js", true],

      // Fallback entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", true],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback2.html", true],

      // Whitelist entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", false],

      // Implicit entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", true],

      // Dynamic entries
      ["http://mochi.test:8888/tests/SimpleTest/EventUtils.js", true]
    ];
    OfflineTest.checkCacheEntries(
      entries,
      function() {
        ++gStep;

        OfflineTest.setSJSState("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingManifest.sjs", "third");
        OfflineTest.setSJSState("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs", "second");

        gGotFrameVersion = 0;
        updatingFrame.location.reload();
        // Since the frame is offline-cached, reload of it invokes update
      });

    break;

  case 2:
    // Processing third version of the manifest.

    // Whitelist entries
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", "", true);

    // Fallback URI selection check
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/opp.html",
        "", false);
    checkFallbackAndWhitelisting("http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/opp.html",
        "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback2.html", false);

    // Cache object status
    OfflineTest.is(applicationCache.status, SpecialPowers.Ci.nsIDOMOfflineResourceList.UPDATEREADY,
        "we have associated application cache and update is pending (3)");

    OfflineTest.is(gGotFrameVersion, 1, "IFrame version 1 because cache was not swapped");

    var entries = [
      // Explicit entries
      ["http://mochi.test:8888/tests/SimpleTest/SimpleTest.js", false],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/offlineTests.js", true],

      // Fallback entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback.html", false],
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/fallback2.html", true],

      // Whitelist entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html", false],

      // Implicit entries
      ["http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingImplicit.html", true],

      // Dynamic entries
      ["http://mochi.test:8888/tests/SimpleTest/EventUtils.js", true]
    ];
    OfflineTest.checkCacheEntries(
      entries,
      function() {
        ++gStep;

        applicationCache.onnoupdate = OfflineTest.priv(manifestNoUpdate);
        applicationCache.update();
        // Invokes manifestNoUpdate()
      });

    break;
  }
}

function manifestNoUpdate()
{
  applicationCache.onnoupdate = null;

  OfflineTest.ok(gStep == 3, "Got manifestNoUpdate in step 3, gStep=" + gStep);
  ++gStep;

  OfflineTest.is(applicationCache.status, SpecialPowers.Ci.nsIDOMOfflineResourceList.UPDATEREADY,
        "we have associated application cache and update is pending (4)");
  applicationCache.swapCache();
  OfflineTest.is(applicationCache.status, SpecialPowers.Ci.nsIDOMOfflineResourceList.IDLE,
        "we have associated application cache (4)");

  gGotFrameVersion = 0;
  gCallOnUpdatingFrameLoad = checkNewVersionOfIFrame;
  updatingFrame.location.reload();
}

function checkNewVersionOfIFrame()
{
  OfflineTest.is(gGotFrameVersion, 2, "IFrame version 2");

  OfflineTest.teardownAndFinish();
}

// End of the test function chain
// ==============================

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");

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

</script>

</head>

<body>
  <iframe name="updatingFrame" src="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/updatingIframe.sjs"></iframe>
  <iframe name="fallbackFrame1" src="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/opp.html"></iframe>
  <iframe name="fallbackFrame2" src="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/namespace1/sub/opp.html"></iframe>
  <iframe name="whitelistFrame" src="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/onwhitelist.html"></iframe>
</body>
</html>