content/html/document/test/test_document.watch.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/html/document/test/test_document.watch.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +<!DOCTYPE html>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=903332
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug 903332</title>
    1.12 +  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    1.14 +  <script type="application/javascript">
    1.15 +
    1.16 +  /** Test for Bug 903332 **/
    1.17 +
    1.18 +  var watch1Called;
    1.19 +  function watch1(prop, oldValue, newValue)
    1.20 +  {
    1.21 +    is(watch1Called, false, "watch1Called not reset properly?");
    1.22 +    watch1Called = true;
    1.23 +
    1.24 +    is(prop, "cookie", "wrong property name passed to watch1");
    1.25 +    return newValue;
    1.26 +  }
    1.27 +
    1.28 +  var watch2Called;
    1.29 +  function watch2(prop, oldValue, newValue)
    1.30 +  {
    1.31 +    is(watch2Called, false, "watch2Called not reset properly?");
    1.32 +    watch2Called = true;
    1.33 +
    1.34 +    is(prop, "cookie", "wrong property name passed to watch2");
    1.35 +    return newValue;
    1.36 +  }
    1.37 +
    1.38 +  // Just in case subsequent tests depend on a particular value...
    1.39 +  var originalValue = document.cookie;
    1.40 +  ok(true, "originalValue: " + originalValue);
    1.41 +
    1.42 +  var originalPrefix = originalValue.length > 0 ? originalValue + "; " : "";
    1.43 +
    1.44 +  try
    1.45 +  {
    1.46 +    // trial set (no watch) to verify things work
    1.47 +    document.cookie = "first=set";
    1.48 +    is(document.cookie, originalPrefix + "first=set",
    1.49 +       "first value correct");
    1.50 +
    1.51 +    // add a watch
    1.52 +    document.watch("cookie", watch1);
    1.53 +
    1.54 +    // set, check for watch invoked
    1.55 +    watch1Called = false;
    1.56 +    document.cookie = "second=set";
    1.57 +    is(watch1Called, true, "watch1 function should be called");
    1.58 +    is(document.cookie, originalPrefix + "first=set; second=set",
    1.59 +       "second value correct");
    1.60 +
    1.61 +    // and a second time, just in case
    1.62 +    watch1Called = false;
    1.63 +    document.cookie = "third=set";
    1.64 +    is(watch1Called, true, "watch1 function should be called");
    1.65 +    is(document.cookie, originalPrefix + "first=set; second=set; third=set",
    1.66 +       "third value correct");
    1.67 +
    1.68 +    // overwrite the current watch with a new one
    1.69 +    document.watch("cookie", watch2);
    1.70 +
    1.71 +    // set, check for watch invoked
    1.72 +    watch1Called = false;
    1.73 +    watch2Called = false;
    1.74 +    document.cookie = "fourth=set";
    1.75 +    is(watch1Called, false, "watch1 invoked erroneously");
    1.76 +    is(watch2Called, true, "watch2 function should be called");
    1.77 +    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set",
    1.78 +       "fourth value correct");
    1.79 +
    1.80 +    // and a second time, just in case
    1.81 +    watch1Called = false;
    1.82 +    watch2Called = false;
    1.83 +    document.cookie = "fifth=set";
    1.84 +    is(watch1Called, false, "watch1 invoked erroneously");
    1.85 +    is(watch2Called, true, "watch2 function should be called");
    1.86 +    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set",
    1.87 +       "fifth value correct");
    1.88 +
    1.89 +    // remove the watch
    1.90 +    document.unwatch("cookie");
    1.91 +
    1.92 +    // check for non-invocation now
    1.93 +    watch1Called = false;
    1.94 +    watch2Called = false;
    1.95 +    document.cookie = "sixth=set";
    1.96 +    is(watch1Called, false, "watch1 shouldn't be called");
    1.97 +    is(watch2Called, false, "watch2 shouldn't be called");
    1.98 +    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set; sixth=set",
    1.99 +       "sixth value correct");
   1.100 +  }
   1.101 +  finally
   1.102 +  {
   1.103 +    // reset
   1.104 +    document.unwatch("cookie"); // harmless, should be no-op except if bugs
   1.105 +
   1.106 +    var d = new Date();
   1.107 +    d.setTime(0);
   1.108 +    var suffix = "=; expires=" + d.toGMTString();
   1.109 +
   1.110 +    document.cookie = "first" + suffix;
   1.111 +    document.cookie = "second" + suffix;
   1.112 +    document.cookie = "third" + suffix;
   1.113 +    document.cookie = "fourth" + suffix;
   1.114 +    document.cookie = "fifth" + suffix;
   1.115 +    document.cookie = "sixth" + suffix;
   1.116 +  }
   1.117 +
   1.118 +  is(document.cookie, originalValue,
   1.119 +     "document.cookie isn't what it was initially!  expect bustage further " +
   1.120 +     "down the line");
   1.121 +  </script>
   1.122 +</head>
   1.123 +<body>
   1.124 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=903332">Mozilla Bug 903332</a>
   1.125 +<p id="display"></p>
   1.126 +<div id="content" style="display: none">
   1.127 +
   1.128 +</div>
   1.129 +<pre id="test">
   1.130 +</pre>
   1.131 +</body>
   1.132 +</html>

mercurial