Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / location-hash.html
bloba6018ad84ba7ba2c084f9fe97817f4fb7e9026e3
1 <html>
2 <head>
3 <script>
4 function print(message, color)
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);
14 function shouldBe(a, b)
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 ++;
25 function step() {
26 state++;
27 switch (state) {
28 case 1:
29 shouldBe('window.history.length == originalHistoryLength', true);
30 shouldBe('window.location.hash', '');
31 window.location.hash = 'foo';
32 shouldBe('window.location.hash', '#foo');
33 shouldBe("window.location == originalLocation + '#foo'", true);
34 shouldBe('window.history.length == originalHistoryLength + 1', true);
35 // hashchange will jump to the next step.
36 break;
37 case 2:
38 window.location.hash = 'bar';
39 shouldBe('window.location.hash', '#bar');
40 shouldBe("window.location == originalLocation + '#bar'", true);
41 shouldBe('window.history.length == originalHistoryLength + 2', true);
42 // hashchange will jump to the next step.
43 break;
44 case 3:
45 window.location.hash = 'bar';
46 shouldBe('window.location.hash', '#bar');
47 shouldBe("window.location == originalLocation + '#bar'", true);
48 shouldBe('window.history.length == originalHistoryLength + 2', true);
49 step(); // No hashchange.
50 break;
51 case 4:
52 shouldBe('window.location.hash', '#bar');
53 shouldBe("window.location == originalLocation + '#bar'", true);
54 window.history.back();
55 // history.back() is asychronous, location should be unchanged
56 shouldBe('window.location.hash', '#bar');
57 shouldBe("window.location == originalLocation + '#bar'", true);
58 // hashchange will jump to the next step.
59 break;
60 case 5:
61 shouldBe('window.location.hash', '#foo');
62 shouldBe("window.location == originalLocation + '#foo'", true);
63 window.history.back();
64 // history.back() is asychronous, location should be unchanged
65 shouldBe('window.location.hash', '#foo');
66 shouldBe("window.location == originalLocation + '#foo'", true);
67 // hashchange will jump to the next step.
68 break;
69 case 6:
70 shouldBe('window.location.hash', '');
71 shouldBe("window.location == originalLocation", true);
72 window.history.forward();
73 // history.forward() is asychronous, location should be unchanged
74 shouldBe('window.location.hash', '');
75 shouldBe("window.location == originalLocation", true);
76 // hashchange will jump to the next step.
77 break;
78 case 7:
79 shouldBe('window.location.hash', '#foo');
80 shouldBe("window.location == originalLocation + '#foo'", true);
81 window.location.hash = '';
82 shouldBe('window.location.hash', '');
83 shouldBe("window.location == originalLocation + '#'", true);
84 window.location.hash = '#';
85 shouldBe('window.location.hash', '');
86 shouldBe("window.location == originalLocation + '#'", true);
87 if (numErrors == 0)
88 print("SUCCESS!", "green")
89 else
90 print("FAILURE: one or more tests failed", "red");
92 if (window.testRunner)
93 testRunner.notifyDone();
95 return;
99 function runTests() {
100 if (window.testRunner) {
101 testRunner.clearBackForwardList();
102 testRunner.dumpAsText();
103 testRunner.waitUntilDone();
106 state = 0;
107 numErrors = 0;
108 originalLocation = window.location.href;
109 originalHistoryLength = window.history.length;
111 window.onhashchange = step;
113 // Location changes need to happen outside the onload handler to generate history entries.
114 setTimeout(step, 0);
116 </script>
117 </head>
118 <body onload="runTests();">
119 <p>This tests that modifying location.hash works as it should</p>
120 <div id="console">
121 </div>
122 </body>
123 </html>