4 https://bugzilla.mozilla.org/show_bug.cgi?id=385434
7 <title>Test for Bug
385434</title>
8 <script type=
"application/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
9 <script type=
"application/javascript" src=
"/tests/SimpleTest/EventUtils.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
13 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=385434">Mozilla Bug
385434</a>
16 <iframe id=
"frame" style=
"height:100px; width:100px; border:0"></iframe>
17 <div id=
"status" style=
"display: none"></div>
20 <script type=
"application/javascript;version=1.7">
22 /** Test for Bug
385434 **/
23 SimpleTest.waitForExplicitFinish();
25 var gNumHashchanges =
0;
26 var gCallbackOnIframeLoad = false;
28 function statusMsg(msg) {
29 var msgElem = document.createElement(
"p");
30 msgElem.appendChild(document.createTextNode(msg));
32 document.getElementById(
"status").appendChild(msgElem);
36 setTimeout(function() { gGen.next() },
1000);
39 // onIframeHashchange, onIframeLoad, and onIframeScroll are all called by the
40 // content we load into our iframe in order to notify the parent frame of an
41 // event which was fired.
42 function onIframeHashchange() {
47 function onIframeLoad() {
48 if (gCallbackOnIframeLoad) {
49 gCallbackOnIframeLoad = false;
54 function onIframeScroll() {
55 is(gNumHashchanges,
0,
"onscroll should fire before onhashchange.");
58 function enableIframeLoadCallback() {
59 gCallbackOnIframeLoad = true;
62 function noEventExpected(msg) {
63 is(gNumHashchanges,
0, msg);
65 // Even if there's an error, set gNumHashchanges to
0 so other tests don't
70 function eventExpected(msg) {
71 is(gNumHashchanges,
1, msg);
73 // Eat up this event, whether the test above was true or not
78 * The hashchange event is dispatched asynchronously, so if we want to observe
79 * it, we have to yield within run_test(), transferring control back to the
82 * When we're expecting our iframe to observe a hashchange event after we poke
83 * it, we just yield and wait for onIframeHashchange() to call gGen.next() and
86 * When we're testing to ensure that the iframe doesn't dispatch a hashchange
87 * event, we try to hook onto the iframe's load event. We call
88 * enableIframeLoadCallback(), which causes onIframeLoad() to call gGen.next()
89 * upon the next observed load. After we get our callback, we check that a
90 * hashchange didn't occur.
92 * We can't always just wait for page load in order to observe that a
93 * hashchange didn't happen. In these cases, we call longWait() and yield
94 * until either a hashchange occurs or longWait's callback is scheduled. This
95 * is something of a hack; it's entirely possible that longWait won't wait long
96 * enough, and we won't observe what should have been a failure of the test.
97 * But it shouldn't happen that good code will randomly *fail* this test.
102 *
<body onhashchange = ...
> works,
103 * the event is (not) fired at the correct times
105 var frame = document.getElementById(
"frame");
106 var frameCw = frame.contentWindow;
108 enableIframeLoadCallback();
109 frameCw.document.location =
"file_bug385434_1.html";
110 // Wait for the iframe to load and for our callback to fire
113 noEventExpected(
"No hashchange expected initially.");
115 sendMouseEvent({type:
"click"},
"link1", frameCw);
117 eventExpected(
"Clicking link1 should trigger a hashchange.");
119 sendMouseEvent({type:
"click"},
"link1", frameCw);
122 // succeed if a hashchange event wasn't triggered while we were waiting
123 noEventExpected(
"Clicking link1 again should not trigger a hashchange.");
125 sendMouseEvent({type:
"click"},
"link2", frameCw);
127 eventExpected(
"Clicking link2 should trigger a hashchange.");
129 frameCw.history.go(-
1);
131 eventExpected(
"Going back should trigger a hashchange.");
133 frameCw.history.go(
1);
135 eventExpected(
"Going forward should trigger a hashchange.");
137 // window.location has a trailing '#' right now, so we append
"link1", not
139 frameCw.window.location = frameCw.window.location +
"link1";
141 eventExpected(
"Assigning to window.location should trigger a hashchange.");
143 // Set up history in the iframe which looks like:
144 // file_bug385434_1.html#link1
145 // file_bug385434_2.html
146 // file_bug385434_1.html#foo <-- current page
147 enableIframeLoadCallback();
148 frameCw.window.location =
"file_bug385434_2.html";
151 enableIframeLoadCallback();
152 frameCw.window.location =
"file_bug385434_1.html#foo";
155 // Now when we do history.go(-
2) on the frame, it *shouldn't* fire a
156 // hashchange. Although the URIs differ only by their hashes, they belong to
157 // two different Documents.
158 frameCw.history.go(-
2);
161 noEventExpected(
"Moving between different Documents shouldn't " +
162 "trigger a hashchange.");
166 *
<frameset onhashchange = ...
> works,
167 * the event is targeted at the window object
168 * the event's cancelable, bubbles settings are correct
170 enableIframeLoadCallback();
171 frameCw.document.location =
"file_bug385434_2.html";
174 frameCw.document.location =
"file_bug385434_2.html#foo";
177 eventExpected(
"frame onhashchange should fire events.");
178 // iframe should set gSampleEvent
179 is(gSampleEvent.target, frameCw,
180 "The hashchange event should be targeted to the window.");
181 is(gSampleEvent.type,
"hashchange",
182 "Event type should be 'hashchange'.");
183 is(gSampleEvent.cancelable, false,
184 "The hashchange event shouldn't be cancelable.");
185 is(gSampleEvent.bubbles, true,
186 "The hashchange event should bubble.");
190 * hashchange is dispatched if the current document readyState is
191 * not
"complete" (bug
504837).
193 frameCw.document.location =
"file_bug385434_3.html";
195 eventExpected(
"Hashchange should fire even if the document " +
196 "hasn't finished loading.");
202 var gGen = run_test();