summaryrefslogtreecommitdiffstats
path: root/dom/html/test/test_document.watch.html
blob: 54509823bef007f71d8be630ca6b5c8848e904e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=903332
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 903332</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 903332 **/

  var watch1Called;
  function watch1(prop, oldValue, newValue)
  {
    is(watch1Called, false, "watch1Called not reset properly?");
    watch1Called = true;

    is(prop, "cookie", "wrong property name passed to watch1");
    return newValue;
  }

  var watch2Called;
  function watch2(prop, oldValue, newValue)
  {
    is(watch2Called, false, "watch2Called not reset properly?");
    watch2Called = true;

    is(prop, "cookie", "wrong property name passed to watch2");
    return newValue;
  }

  // Just in case subsequent tests depend on a particular value...
  var originalValue = document.cookie;
  ok(true, "originalValue: " + originalValue);

  var originalPrefix = originalValue.length > 0 ? originalValue + "; " : "";

  try
  {
    // trial set (no watch) to verify things work
    document.cookie = "first=set";
    is(document.cookie, originalPrefix + "first=set",
       "first value correct");

    // add a watch
    document.watch("cookie", watch1);

    // set, check for watch invoked
    watch1Called = false;
    document.cookie = "second=set";
    is(watch1Called, true, "watch1 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set",
       "second value correct");

    // and a second time, just in case
    watch1Called = false;
    document.cookie = "third=set";
    is(watch1Called, true, "watch1 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set",
       "third value correct");

    // overwrite the current watch with a new one
    document.watch("cookie", watch2);

    // set, check for watch invoked
    watch1Called = false;
    watch2Called = false;
    document.cookie = "fourth=set";
    is(watch1Called, false, "watch1 invoked erroneously");
    is(watch2Called, true, "watch2 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set",
       "fourth value correct");

    // and a second time, just in case
    watch1Called = false;
    watch2Called = false;
    document.cookie = "fifth=set";
    is(watch1Called, false, "watch1 invoked erroneously");
    is(watch2Called, true, "watch2 function should be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set",
       "fifth value correct");

    // remove the watch
    document.unwatch("cookie");

    // check for non-invocation now
    watch1Called = false;
    watch2Called = false;
    document.cookie = "sixth=set";
    is(watch1Called, false, "watch1 shouldn't be called");
    is(watch2Called, false, "watch2 shouldn't be called");
    is(document.cookie, originalPrefix + "first=set; second=set; third=set; fourth=set; fifth=set; sixth=set",
       "sixth value correct");
  }
  finally
  {
    // reset
    document.unwatch("cookie"); // harmless, should be no-op except if bugs

    var d = new Date();
    d.setTime(0);
    var suffix = "=; expires=" + d.toGMTString();

    document.cookie = "first" + suffix;
    document.cookie = "second" + suffix;
    document.cookie = "third" + suffix;
    document.cookie = "fourth" + suffix;
    document.cookie = "fifth" + suffix;
    document.cookie = "sixth" + suffix;
  }

  is(document.cookie, originalValue,
     "document.cookie isn't what it was initially!  expect bustage further " +
     "down the line");
  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=903332">Mozilla Bug 903332</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>