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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 <!DOCTYPE html>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=903332
     5 -->
     6 <head>
     7   <meta charset="utf-8">
     8   <title>Test for Bug 903332</title>
     9   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    11   <script type="application/javascript">
    13   /** Test for Bug 903332 **/
    15   var watch1Called;
    16   function watch1(prop, oldValue, newValue)
    17   {
    18     is(watch1Called, false, "watch1Called not reset properly?");
    19     watch1Called = true;
    21     is(prop, "cookie", "wrong property name passed to watch1");
    22     return newValue;
    23   }
    25   var watch2Called;
    26   function watch2(prop, oldValue, newValue)
    27   {
    28     is(watch2Called, false, "watch2Called not reset properly?");
    29     watch2Called = true;
    31     is(prop, "cookie", "wrong property name passed to watch2");
    32     return newValue;
    33   }
    35   // Just in case subsequent tests depend on a particular value...
    36   var originalValue = document.cookie;
    37   ok(true, "originalValue: " + originalValue);
    39   var originalPrefix = originalValue.length > 0 ? originalValue + "; " : "";
    41   try
    42   {
    43     // trial set (no watch) to verify things work
    44     document.cookie = "first=set";
    45     is(document.cookie, originalPrefix + "first=set",
    46        "first value correct");
    48     // add a watch
    49     document.watch("cookie", watch1);
    51     // set, check for watch invoked
    52     watch1Called = false;
    53     document.cookie = "second=set";
    54     is(watch1Called, true, "watch1 function should be called");
    55     is(document.cookie, originalPrefix + "first=set; second=set",
    56        "second value correct");
    58     // and a second time, just in case
    59     watch1Called = false;
    60     document.cookie = "third=set";
    61     is(watch1Called, true, "watch1 function should be called");
    62     is(document.cookie, originalPrefix + "first=set; second=set; third=set",
    63        "third value correct");
    65     // overwrite the current watch with a new one
    66     document.watch("cookie", watch2);
    68     // set, check for watch invoked
    69     watch1Called = false;
    70     watch2Called = false;
    71     document.cookie = "fourth=set";
    72     is(watch1Called, false, "watch1 invoked erroneously");
    73     is(watch2Called, true, "watch2 function should be called");
    74     is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set",
    75        "fourth value correct");
    77     // and a second time, just in case
    78     watch1Called = false;
    79     watch2Called = false;
    80     document.cookie = "fifth=set";
    81     is(watch1Called, false, "watch1 invoked erroneously");
    82     is(watch2Called, true, "watch2 function should be called");
    83     is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set",
    84        "fifth value correct");
    86     // remove the watch
    87     document.unwatch("cookie");
    89     // check for non-invocation now
    90     watch1Called = false;
    91     watch2Called = false;
    92     document.cookie = "sixth=set";
    93     is(watch1Called, false, "watch1 shouldn't be called");
    94     is(watch2Called, false, "watch2 shouldn't be called");
    95     is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set; sixth=set",
    96        "sixth value correct");
    97   }
    98   finally
    99   {
   100     // reset
   101     document.unwatch("cookie"); // harmless, should be no-op except if bugs
   103     var d = new Date();
   104     d.setTime(0);
   105     var suffix = "=; expires=" + d.toGMTString();
   107     document.cookie = "first" + suffix;
   108     document.cookie = "second" + suffix;
   109     document.cookie = "third" + suffix;
   110     document.cookie = "fourth" + suffix;
   111     document.cookie = "fifth" + suffix;
   112     document.cookie = "sixth" + suffix;
   113   }
   115   is(document.cookie, originalValue,
   116      "document.cookie isn't what it was initially!  expect bustage further " +
   117      "down the line");
   118   </script>
   119 </head>
   120 <body>
   121 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=903332">Mozilla Bug 903332</a>
   122 <p id="display"></p>
   123 <div id="content" style="display: none">
   125 </div>
   126 <pre id="test">
   127 </pre>
   128 </body>
   129 </html>

mercurial