summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/offline/test_changingManifest.html
blob: 2ff365a4ab275ba3827182847884c0f076bfc0a4 (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
<html xmlns="http://www.w3.org/1999/xhtml" manifest="http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changingManifest.sjs">
<head>
<title>changing manifest 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 type="text/javascript">

var gGotChecking = false;
var gGotDownloading = false;

var g1SecUrl =  "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changing1Sec.sjs";
var g1HourUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/ajax/offline/changing1Hour.sjs";

var gCacheContents = null;

function finish()
{
  OfflineTest.teardownAndFinish();
}

function manifestUpdatedAgain()
{
  OfflineTest.ok(gGotChecking, "Should get a checking event on the second update");
  OfflineTest.ok(gGotDownloading, "Should get a downloading event on the second update");

  // The second manifest has no NETWORK entry, but we are still
  // associated with the first version of the manifest, so we should
  // be able to access the whitelisted entry.
  try {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "onwhitelist.html", false);
    xhr.send();
    OfflineTest.ok(true, "Fetched from the initial cache's whitelist.");
  } catch (e) {
    OfflineTest.ok(false, "Failed to fetch from the initial cache's whitelist.");
  }

  // Get the initial contents of the first two files.
  fetcher = new OfflineCacheContents([g1SecUrl, g1HourUrl]);
  fetcher.fetch(function(contents) {
    // Make sure the contents of the 1-second-expiration file have changed,
    // but that the 1-hour-expiration has not.
    OfflineTest.isnot(gCacheContents[g1SecUrl], contents[g1SecUrl], "1-second expiration should have changed");
    OfflineTest.is(gCacheContents[g1HourUrl], contents[g1HourUrl], "1-hour expiration should not have changed");

    finish();
  });
}

function failAndFinish(e) {
  OfflineTest.ok(false, "Unexpected event: " + e.type);
  finish();
}

function manifestUpdated()
{
  OfflineTest.ok(gGotChecking, "Should get a checking event");
  OfflineTest.ok(gGotDownloading, "Should get a downloading event");

  // Replace this manifest with a new one.
  OfflineTest.setSJSState("changingManifest.sjs", "2");

  // Get the initial contents of the first two files.
  fetcher = new OfflineCacheContents([g1SecUrl, g1HourUrl]);
  fetcher.fetch(function(contents) {
    gCacheContents = contents;

    // Now make sure applicationCache.update() does what we expect.
    applicationCache.onchecking = function() {
      OfflineTest.is(applicationCache.status, applicationCache.CHECKING,
                     "CHECKING state during update");
      gGotChecking = true;
    };
    applicationCache.ondownloading = function() {
      OfflineTest.is(applicationCache.status, applicationCache.DOWNLOADING,
                     "DOWNLOADING state during update");
      gGotDownloading = true;
    };

    applicationCache.onupdateready = OfflineTest.priv(manifestUpdatedAgain);
    applicationCache.onnoupdate = failAndFinish;
    applicationCache.oncached = failAndFinish;

    gGotChecking = false;
    gGotDownloading = false;

    // The changing versions give out a new version each second,
    // make sure it has time to grab a new version, and for the
    // 1-second cache timeout to pass.
    window.setTimeout("applicationCache.update()", 5000);
  });
}

if (OfflineTest.setup()) {
  applicationCache.onerror = failAndFinish;
  applicationCache.onnoupdate = failAndFinish;

  applicationCache.onchecking = function() { gGotChecking = true; };
  applicationCache.ondownloading = function() { gGotDownloading = true; };
  applicationCache.oncached = OfflineTest.priv(manifestUpdated);
}

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

</script>

</head>

<body>

</body>
</html>