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 load(url)
     33 {
     34     try {
     35         var req = new XMLHttpRequest();
     36         req.open("GET", url, false);
     37         req.send("");
     38         return req.responseText;
     39     } catch (ex) {
     40         log("FAIL: Cannot load " + url + ", ex = " + ex);
     41         hadError = true;
     42         return ""; // This value should not be expected as the responseText for a url presented to this function.
     43     }
     44 }
     45 
     46 var testURL = "/resources/network-simulator.php?path=/appcache/resources/not-in-cache.txt";
     47 var nonexistentURL = "resources/does-not-exist";
     48 var redirectURL = "resources/fallback-redirect.php";
     49 
     50 function test()
     51 {
     52     applicationCache.onnoupdate = function() { log("FAIL: received unexpected noupdate event") }
     53     applicationCache.oncached = function() { log("FAIL: received unexpected cached event") }
     54     
     55     setNetworkEnabled(true);
     56 
     57     if (!/not in the cache/.test(load(testURL))) {
     58         log("FAIL: Cannot load an URL from fallback namespace when network is enabled");
     59         hadError = true;
     60     }
     61 
     62     if (load(nonexistentURL) != "Hello, World!") {
     63         log("FAIL: Fallback resource wasn't used for a 404 response");
     64         hadError = true;
     65     }
     66 
     67     if (load(redirectURL) != "Hello, World!") {
     68         log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin");
     69         hadError = true;
     70     }
     71 
     72     test2();
     73 }
     74 
     75 function test2()
     76 {
     77     var req = new XMLHttpRequest;
     78     req.open("GET", nonexistentURL);
     79     req.onerror = function() {
     80         log("FAIL: Fallback resource wasn't used for a 404 response (async)");
     81         hadError = true;
     82         test3();
     83     }
     84     req.onload = function() {
     85         if (req.responseText != "Hello, World!") {
     86             log("FAIL: Unexpected result for a 404 response (async)");
     87             hadError = true;
     88         }
     89         test3();
     90     }
     91 
     92     req.send("");
     93 }
     94 
     95 function test3()
     96 {
     97     var req = new XMLHttpRequest;
     98     req.open("GET", redirectURL);
     99     req.onerror = function() {
    100         log("FAIL: Fallback resource wasn't used for a redirect to a resource with another origin (async)");
    101         hadError = true;
    102         test4();
    103     }
    104     req.onload = function() {
    105         if (req.responseText != "Hello, World!") {
    106             log("FAIL: Unexpected result for a redirect response (async)");
    107             hadError = true;
    108         }
    109         test4();
    110     }
    111 
    112     req.send("");
    113 }
    114 
    115 function test4()
    116 {
    117     // Try loading a fallback resource as main one.
    118 
    119     applicationCache.onnoupdate = test5;
    120 
    121     var ifr = document.createElement("iframe");
    122     ifr.setAttribute("src", nonexistentURL);
    123     document.body.appendChild(ifr);
    124 }
    125 
    126 function test5()
    127 {
    128     if (!/Hello, World/.test(window.frames[0].document.documentElement.innerHTML)) {
    129         log("FAIL: Fallback URL was not honored for main resource");
    130         hadError = true;
    131     }
    132     test6();
    133 }
    134 
    135 function test6()
    136 {
    137     setNetworkEnabled(false);
    138 
    139     if (load(testURL) != load("resources/simple.txt")) {
    140         log("FAIL: Loading an URL from fallback namespace when network is disabled returned unexpected response");
    141         hadError = true;
    142     }
    143 
    144     log(hadError ? "FAILURE" : "SUCCESS");
    145     if (window.layoutTestController)
    146         layoutTestController.notifyDone();
    147 }
    148 
    149 applicationCache.onnoupdate = function() { test() }
    150 applicationCache.oncached = function() { test() }
    151 
    152 applicationCache.onupdateready = function() { log("FAIL: received unexpected updateready event") }
    153 applicationCache.onerror = function() { log("FAIL: received unexpected error event") }
    154 
    155 </script>
    156 </body>
    157 </html>
    158