Home | History | Annotate | Download | only in appcache
      1 <html manifest="resources/fallback.manifest">
      2 <body>
      3 <p>Test application cache fallback entries.</p>
      4 <p>Should say SUCCESS:</p>
      5 <div id=result></div>
      6 
      7 <script>
      8 if (window.layoutTestController) {
      9     layoutTestController.dumpAsText();
     10     layoutTestController.waitUntilDone();
     11 }
     12 
     13 var hadError = false;
     14 
     15 function log(message)
     16 {
     17     document.getElementById("result").innerHTML += message + "<br>";
     18 }
     19 
     20 function setNetworkEnabled(state)
     21 {
     22     try {
     23         var req = new XMLHttpRequest;
     24         req.open("GET", "/resources/network-simulator.php?command=" + (state ? "connect" : "disconnect"), false);
     25         req.send("");
     26     } catch (ex) {
     27         log("Cannot access network simulator URL");
     28         hadError = true;
     29     }
     30 }
     31 
     32 function canLoad(url)
     33 {
     34     try {
     35         var req = new XMLHttpRequest();
     36         req.open("GET", url, false);
     37         req.send("");
     38         return true;
     39     } catch (e) {
     40         return false;
     41     }
     42 }
     43 
     44 function load(url)
     45 {
     46     try {
     47         var req = new XMLHttpRequest();
     48         req.open("GET", url, false);
     49         req.send("");
     50         return req.responseText;
     51     } catch (ex) {
     52         alert("Unexpected error loading " + url + ": " + ex);
     53         hadError = true;
     54     }
     55 }
     56 
     57 var testURL = "/resources/network-simulator.php?path=/appcache/resources/not-in-cache.txt";
     58 var nonexistentURL = "resources/does-not-exist";
     59 var redirectURL = "resources/fallback-redirect.php";
     60 
     61 function test()
     62 {
     63     applicationCache.onnoupdate = function() { log("FAIL: received unexpected noupdate event") }
     64     applicationCache.oncached = function() { log("FAIL: received unexpected cached event") }
     65     
     66     setNetworkEnabled(true);
     67 
     68     if (!canLoad(testURL) || !/not in the cache/.test(load(testURL))) {
     69         log("FAIL: Cannot load an URL from fallback namespace when network is enabled");
     70         hadError = true;
     71     }
     72 
     73     if (!canLoad(nonexistentURL) || load(nonexistentURL) != "Hello, World!") {
     74         log("FAIL: Fallback resource wasn't used for a 404 response");
     75         hadError = true;
     76     }
     77 
     78     if (!canLoad(redirectURL) || load(redirectURL) != "Hello, World!") {
     79         log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin");
     80         hadError = true;
     81     }
     82 
     83     test2();
     84 }
     85 
     86 function test2()
     87 {
     88     var req = new XMLHttpRequest;
     89     req.open("GET", nonexistentURL);
     90     req.onerror = function() {
     91         log("FAIL: Fallback resource wasn't used for a 404 response (async)");
     92         hadError = true;
     93         test3();
     94     }
     95     req.onload = function() {
     96         if (req.responseText != "Hello, World!") {
     97             log("FAIL: Unexpected result for a 404 response (async)");
     98             hadError = true;
     99         }
    100         test3();
    101     }
    102 
    103     req.send("");
    104 }
    105 
    106 function test3()
    107 {
    108     var req = new XMLHttpRequest;
    109     req.open("GET", redirectURL);
    110     req.onerror = function() {
    111         log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin (async)");
    112         hadError = true;
    113         test4();
    114     }
    115     req.onload = function() {
    116         if (req.responseText != "Hello, World!") {
    117             log("FAIL: Unexpected result for a redirect response (async)");
    118             hadError = true;
    119         }
    120         test4();
    121     }
    122 
    123     req.send("");
    124 }
    125 
    126 function test4()
    127 {
    128     // Try loading a fallback resource as main one.
    129 
    130     applicationCache.onnoupdate = test5;
    131 
    132     var ifr = document.createElement("iframe");
    133     ifr.setAttribute("src", nonexistentURL);
    134     document.body.appendChild(ifr);
    135 }
    136 
    137 function test5()
    138 {
    139     if (!/Hello, World/.test(window.frames[0].document.documentElement.innerHTML)) {
    140         log("FAIL: Fallback URL was not honored for main resource");
    141         hadError = true;
    142     }
    143     test6();
    144 }
    145 
    146 function test6()
    147 {
    148     setNetworkEnabled(false);
    149 
    150     if (!canLoad(testURL)) {
    151         log("FAIL: Cannot load an URL from fallback namespace when network is disabled");
    152         hadError = true;
    153     } else if (load(testURL) != load("resources/simple.txt")) {
    154         log("FAIL: Loading an URL from fallback namespace when network is disabled returned unexpected response");
    155         hadError = true;
    156     }
    157 
    158     log(hadError ? "FAILURE" : "SUCCESS");
    159     if (window.layoutTestController)
    160         layoutTestController.notifyDone();
    161 }
    162 
    163 applicationCache.onnoupdate = function() { test() }
    164 applicationCache.oncached = function() { test() }
    165 
    166 applicationCache.onupdateready = function() { log("FAIL: received unexpected updateready event") }
    167 applicationCache.onerror = function() { log("FAIL: received unexpected error event") }
    168 
    169 </script>
    170 </body>
    171 </html>
    172