Home | History | Annotate | Download | only in appcache
      1 <body>
      2 <p>Test the behavior of DOMApplicationCache methods related to dynamic entries when the context
      3 is not associated with any cache.</p>
      4 <div id="result"></div>
      5 <script>
      6 if (window.layoutTestController)
      7     layoutTestController.dumpAsText();
      8 
      9 function log(message)
     10 {
     11     document.getElementById("result").innerHTML += message + "<br>";
     12 }
     13 
     14 
     15 function isResultCorrect(_actual, _expected)
     16 {
     17     if (_expected === 0)
     18         return _actual === _expected && (1/_actual) === (1/_expected);
     19     if (_actual === _expected)
     20         return true;
     21     if (typeof(_expected) == "number" && isNaN(_expected))
     22         return typeof(_actual) == "number" && isNaN(_actual);
     23     if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([])) {
     24         log("Array comparison is not supported");
     25         return false;
     26     }
     27     return false;
     28 }
     29 
     30 function shouldBe(_a, _b)
     31 {
     32   if (typeof _a != "string" || typeof _b != "string")
     33     log("WARN: shouldBe() expects string arguments");
     34   var exception;
     35   var _av;
     36   try {
     37      _av = eval(_a);
     38   } catch (e) {
     39      exception = e;
     40   }
     41   var _bv = eval(_b);
     42 
     43   if (exception)
     44     log("FAIL: " + _a + " should be " + _bv + ". Threw exception " + exception);
     45   else if (isResultCorrect(_av, _bv))
     46     log("PASS: " + _a + " is " + _b);
     47   else if (typeof(_av) == typeof(_bv))
     48     log("FAIL: " + _a + " should be " + _bv + ". Was " + stringify(_av) + ".");
     49   else
     50     log("FAIL: " + _a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
     51 }
     52 
     53 
     54 shouldBe("applicationCache.items.length", "0");
     55 shouldBe("applicationCache.items.item(0)", "null");
     56 shouldBe("applicationCache.items.item(1)", "null");
     57 shouldBe("applicationCache.items.item(0xffffffff)", "null");
     58 shouldBe("applicationCache.items.item(0xfffffffe)", "null");
     59 shouldBe("applicationCache.items.item(-1)", "null");
     60 shouldBe("applicationCache.items.item(-2)", "null");
     61 
     62 shouldBe("applicationCache.items[0xfffffffe]", "undefined");
     63 shouldBe("applicationCache.items[0xffffffff]", "undefined");
     64 
     65 // The below index access tests give different results in WebKit and Firefox 3.1b2 (undefined vs. empty string).
     66 shouldBe("applicationCache.items['0']", "undefined");
     67 shouldBe("applicationCache.items['']", "undefined");
     68 shouldBe("applicationCache.items[0]", "undefined");
     69 shouldBe("applicationCache.items[1]", "undefined");
     70 shouldBe("applicationCache.items[-1]", "undefined");
     71 shouldBe("applicationCache.items[-2]", "undefined");
     72 applicationCache.items[100] = "foobar";
     73 applicationCache.items['100'] = "foobar";
     74 applicationCache.items['foo'] = "bar";
     75 shouldBe("applicationCache.items[100]", "undefined");
     76 shouldBe("applicationCache.items['100']", "undefined");
     77 
     78 shouldBe("applicationCache.items['foo']", "undefined");
     79 shouldBe("applicationCache.items[0.1]", "undefined");
     80 
     81 try {
     82     applicationCache.hasItem('foo');
     83     log("FAIL: hasItem didn't raise an exception");
     84 } catch (ex) {
     85     if (ex.code == 11)
     86         log("PASS: hasItem raised INVALID_STATE_ERR");
     87     else
     88         log("FAIL: hasItem raised unexpected exception " + ex);
     89 }
     90 
     91 try {
     92     applicationCache.add('foo');
     93     log("FAIL: add didn't raise an exception");
     94 } catch (ex) {
     95     if (ex.code == 11)
     96         log("PASS: add raised INVALID_STATE_ERR");
     97     else
     98         log("FAIL: add raised unexpected exception " + ex);
     99 }
    100 
    101 try {
    102     applicationCache.remove('bar');
    103     log("FAIL: remove didn't raise an exception");
    104 } catch (ex) {
    105     if (ex.code == 11)
    106         log("PASS: remove raised INVALID_STATE_ERR");
    107     else
    108         log("FAIL: remove raised unexpected exception " + ex);
    109 }
    110 
    111 shouldBe("applicationCache.items.length", "0");
    112 
    113 
    114 
    115 log("DONE");
    116 
    117 </script>
    118 </head>
    119 </body>
    120