f580f52ad0b8e4cdae0df94b3309f5766d85a918
[WebKit-https.git] / LayoutTests / fast / dom / location-hash.html
1 <html>
2 <head>
3 <script>
4     function print(message, color) 
5     {
6         var paragraph = document.createElement("div");
7         paragraph.appendChild(document.createTextNode(message));
8         paragraph.style.fontFamily = "monospace";
9         if (color)
10             paragraph.style.color = color;
11             document.getElementById("console").appendChild(paragraph);
12     }
13
14     function shouldBe(a, b)
15     {
16         var evalA = eval(a);
17         if (evalA == b) 
18             print("PASS: " + a + " should be " + b + " and is.", "green");
19         else {
20             print("FAIL: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
21             numErrors ++;
22         }
23     }
24     
25     function step() {
26         switch (state) {
27             case 0:
28             shouldBe('window.history.length == originalHistoryLength', true);
29             shouldBe('window.location.hash', '');
30             window.location.hash = 'foo';
31             shouldBe('window.location.hash', '#foo');
32             shouldBe("window.location == originalLocation + '#foo'", true);
33             shouldBe('window.history.length ==  originalHistoryLength + 1', true);
34             break;
35         case 1:
36             window.location.hash = 'bar';
37             shouldBe('window.location.hash', '#bar');
38             shouldBe("window.location == originalLocation + '#bar'", true);
39             shouldBe('window.history.length == originalHistoryLength + 2', true);
40             break;
41         case 2:
42             window.location.hash = 'bar';
43             shouldBe('window.location.hash', '#bar');
44             shouldBe("window.location == originalLocation + '#bar'", true);
45             shouldBe('window.history.length == originalHistoryLength + 2', true);
46             break;        
47         case 3:
48             window.history.back();
49             shouldBe('window.location.hash', '#foo');
50             shouldBe("window.location == originalLocation + '#foo'", true);
51             break;
52         case 4:
53             window.history.back();
54             shouldBe('window.location.hash', '');
55             shouldBe("window.location == originalLocation", true);
56             break;
57         case 5:
58             window.history.forward();
59             shouldBe('window.location.hash', '#foo');
60             shouldBe("window.location == originalLocation + '#foo'", true);
61             break;
62         case 6:
63             window.location.hash = '';
64             if (numErrors == 0)
65                 print("SUCCESS!", "green")
66             else
67                 print("FAILURE: one or more tests failed", "red");
68                 
69             if (window.layoutTestController)
70                 layoutTestController.notifyDone();
71                 
72             return;
73         }
74         
75         state ++;
76         step();
77     }
78         
79     function runTests() {
80         if (window.layoutTestController) {
81             layoutTestController.clearBackForwardList();
82             layoutTestController.dumpAsText();
83             layoutTestController.waitUntilDone();
84         }
85         
86         state = 0;
87         numErrors = 0;
88         originalLocation = window.location.href;
89         originalHistoryLength = window.history.length;
90
91         // Location changes need to happen outside the onload handler to generate history entries.
92         setTimeout(step, 0);
93     }
94     </script>
95 </head>
96 <body onload="runTests();">
97 <p>This tests that modifying location.hash works as it should</p>
98 <div id="console">
99 </div>
100 </body>
101 </html>