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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:710102231d34
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">
12
13 /** Test for Bug 903332 **/
14
15 var watch1Called;
16 function watch1(prop, oldValue, newValue)
17 {
18 is(watch1Called, false, "watch1Called not reset properly?");
19 watch1Called = true;
20
21 is(prop, "cookie", "wrong property name passed to watch1");
22 return newValue;
23 }
24
25 var watch2Called;
26 function watch2(prop, oldValue, newValue)
27 {
28 is(watch2Called, false, "watch2Called not reset properly?");
29 watch2Called = true;
30
31 is(prop, "cookie", "wrong property name passed to watch2");
32 return newValue;
33 }
34
35 // Just in case subsequent tests depend on a particular value...
36 var originalValue = document.cookie;
37 ok(true, "originalValue: " + originalValue);
38
39 var originalPrefix = originalValue.length > 0 ? originalValue + "; " : "";
40
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");
47
48 // add a watch
49 document.watch("cookie", watch1);
50
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");
57
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");
64
65 // overwrite the current watch with a new one
66 document.watch("cookie", watch2);
67
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");
76
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");
85
86 // remove the watch
87 document.unwatch("cookie");
88
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
102
103 var d = new Date();
104 d.setTime(0);
105 var suffix = "=; expires=" + d.toGMTString();
106
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 }
114
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">
124
125 </div>
126 <pre id="test">
127 </pre>
128 </body>
129 </html>

mercurial