docshell/test/test_bug385434.html

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=385434
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for Bug 385434</title>
michael@0 8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
michael@0 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
michael@0 11 </head>
michael@0 12 <body>
michael@0 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=385434">Mozilla Bug 385434</a>
michael@0 14 <p id="display"></p>
michael@0 15 <div id="content">
michael@0 16 <iframe id="frame" style="height:100px; width:100px; border:0"></iframe>
michael@0 17 <div id="status" style="display: none"></div>
michael@0 18 </div>
michael@0 19 <pre id="test">
michael@0 20 <script type="application/javascript;version=1.7">
michael@0 21
michael@0 22 /** Test for Bug 385434 **/
michael@0 23 SimpleTest.waitForExplicitFinish();
michael@0 24
michael@0 25 var gNumHashchanges = 0;
michael@0 26 var gCallbackOnIframeLoad = false;
michael@0 27
michael@0 28 function statusMsg(msg) {
michael@0 29 var msgElem = document.createElement("p");
michael@0 30 msgElem.appendChild(document.createTextNode(msg));
michael@0 31
michael@0 32 document.getElementById("status").appendChild(msgElem);
michael@0 33 }
michael@0 34
michael@0 35 function longWait() {
michael@0 36 setTimeout(function() { gGen.next() }, 1000);
michael@0 37 }
michael@0 38
michael@0 39 // onIframeHashchange, onIframeLoad, and onIframeScroll are all called by the
michael@0 40 // content we load into our iframe in order to notify the parent frame of an
michael@0 41 // event which was fired.
michael@0 42 function onIframeHashchange() {
michael@0 43 gNumHashchanges++;
michael@0 44 gGen.next();
michael@0 45 }
michael@0 46
michael@0 47 function onIframeLoad() {
michael@0 48 if (gCallbackOnIframeLoad) {
michael@0 49 gCallbackOnIframeLoad = false;
michael@0 50 gGen.next();
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 function onIframeScroll() {
michael@0 55 is(gNumHashchanges, 0, "onscroll should fire before onhashchange.");
michael@0 56 }
michael@0 57
michael@0 58 function enableIframeLoadCallback() {
michael@0 59 gCallbackOnIframeLoad = true;
michael@0 60 }
michael@0 61
michael@0 62 function noEventExpected(msg) {
michael@0 63 is(gNumHashchanges, 0, msg);
michael@0 64
michael@0 65 // Even if there's an error, set gNumHashchanges to 0 so other tests don't
michael@0 66 // fail.
michael@0 67 gNumHashchanges = 0;
michael@0 68 }
michael@0 69
michael@0 70 function eventExpected(msg) {
michael@0 71 is(gNumHashchanges, 1, msg);
michael@0 72
michael@0 73 // Eat up this event, whether the test above was true or not
michael@0 74 gNumHashchanges = 0;
michael@0 75 }
michael@0 76
michael@0 77 /*
michael@0 78 * The hashchange event is dispatched asynchronously, so if we want to observe
michael@0 79 * it, we have to yield within run_test(), transferring control back to the
michael@0 80 * event loop.
michael@0 81 *
michael@0 82 * When we're expecting our iframe to observe a hashchange event after we poke
michael@0 83 * it, we just yield and wait for onIframeHashchange() to call gGen.next() and
michael@0 84 * wake us up.
michael@0 85 *
michael@0 86 * When we're testing to ensure that the iframe doesn't dispatch a hashchange
michael@0 87 * event, we try to hook onto the iframe's load event. We call
michael@0 88 * enableIframeLoadCallback(), which causes onIframeLoad() to call gGen.next()
michael@0 89 * upon the next observed load. After we get our callback, we check that a
michael@0 90 * hashchange didn't occur.
michael@0 91 *
michael@0 92 * We can't always just wait for page load in order to observe that a
michael@0 93 * hashchange didn't happen. In these cases, we call longWait() and yield
michael@0 94 * until either a hashchange occurs or longWait's callback is scheduled. This
michael@0 95 * is something of a hack; it's entirely possible that longWait won't wait long
michael@0 96 * enough, and we won't observe what should have been a failure of the test.
michael@0 97 * But it shouldn't happen that good code will randomly *fail* this test.
michael@0 98 */
michael@0 99 function run_test() {
michael@0 100 /*
michael@0 101 * TEST 1 tests that:
michael@0 102 * <body onhashchange = ... > works,
michael@0 103 * the event is (not) fired at the correct times
michael@0 104 */
michael@0 105 var frame = document.getElementById("frame");
michael@0 106 var frameCw = frame.contentWindow;
michael@0 107
michael@0 108 enableIframeLoadCallback();
michael@0 109 frameCw.document.location = "file_bug385434_1.html";
michael@0 110 // Wait for the iframe to load and for our callback to fire
michael@0 111 yield undefined;
michael@0 112
michael@0 113 noEventExpected("No hashchange expected initially.");
michael@0 114
michael@0 115 sendMouseEvent({type: "click"}, "link1", frameCw);
michael@0 116 yield undefined;
michael@0 117 eventExpected("Clicking link1 should trigger a hashchange.");
michael@0 118
michael@0 119 sendMouseEvent({type: "click"}, "link1", frameCw);
michael@0 120 longWait();
michael@0 121 yield undefined;
michael@0 122 // succeed if a hashchange event wasn't triggered while we were waiting
michael@0 123 noEventExpected("Clicking link1 again should not trigger a hashchange.");
michael@0 124
michael@0 125 sendMouseEvent({type: "click"}, "link2", frameCw);
michael@0 126 yield undefined;
michael@0 127 eventExpected("Clicking link2 should trigger a hashchange.");
michael@0 128
michael@0 129 frameCw.history.go(-1);
michael@0 130 yield undefined;
michael@0 131 eventExpected("Going back should trigger a hashchange.");
michael@0 132
michael@0 133 frameCw.history.go(1);
michael@0 134 yield undefined;
michael@0 135 eventExpected("Going forward should trigger a hashchange.");
michael@0 136
michael@0 137 // window.location has a trailing '#' right now, so we append "link1", not
michael@0 138 // "#link1".
michael@0 139 frameCw.window.location = frameCw.window.location + "link1";
michael@0 140 yield undefined;
michael@0 141 eventExpected("Assigning to window.location should trigger a hashchange.");
michael@0 142
michael@0 143 // Set up history in the iframe which looks like:
michael@0 144 // file_bug385434_1.html#link1
michael@0 145 // file_bug385434_2.html
michael@0 146 // file_bug385434_1.html#foo <-- current page
michael@0 147 enableIframeLoadCallback();
michael@0 148 frameCw.window.location = "file_bug385434_2.html";
michael@0 149 yield undefined;
michael@0 150
michael@0 151 enableIframeLoadCallback();
michael@0 152 frameCw.window.location = "file_bug385434_1.html#foo";
michael@0 153 yield undefined;
michael@0 154
michael@0 155 // Now when we do history.go(-2) on the frame, it *shouldn't* fire a
michael@0 156 // hashchange. Although the URIs differ only by their hashes, they belong to
michael@0 157 // two different Documents.
michael@0 158 frameCw.history.go(-2);
michael@0 159 longWait();
michael@0 160 yield undefined;
michael@0 161 noEventExpected("Moving between different Documents shouldn't " +
michael@0 162 "trigger a hashchange.");
michael@0 163
michael@0 164 /*
michael@0 165 * TEST 2 tests that:
michael@0 166 * <frameset onhashchange = ... > works,
michael@0 167 * the event is targeted at the window object
michael@0 168 * the event's cancelable, bubbles settings are correct
michael@0 169 */
michael@0 170 enableIframeLoadCallback();
michael@0 171 frameCw.document.location = "file_bug385434_2.html";
michael@0 172 yield undefined;
michael@0 173
michael@0 174 frameCw.document.location = "file_bug385434_2.html#foo";
michael@0 175 yield undefined;
michael@0 176
michael@0 177 eventExpected("frame onhashchange should fire events.");
michael@0 178 // iframe should set gSampleEvent
michael@0 179 is(gSampleEvent.target, frameCw,
michael@0 180 "The hashchange event should be targeted to the window.");
michael@0 181 is(gSampleEvent.type, "hashchange",
michael@0 182 "Event type should be 'hashchange'.");
michael@0 183 is(gSampleEvent.cancelable, false,
michael@0 184 "The hashchange event shouldn't be cancelable.");
michael@0 185 is(gSampleEvent.bubbles, true,
michael@0 186 "The hashchange event should bubble.");
michael@0 187
michael@0 188 /*
michael@0 189 * TEST 3 tests that:
michael@0 190 * hashchange is dispatched if the current document readyState is
michael@0 191 * not "complete" (bug 504837).
michael@0 192 */
michael@0 193 frameCw.document.location = "file_bug385434_3.html";
michael@0 194 yield undefined;
michael@0 195 eventExpected("Hashchange should fire even if the document " +
michael@0 196 "hasn't finished loading.");
michael@0 197
michael@0 198 SimpleTest.finish();
michael@0 199 yield undefined;
michael@0 200 }
michael@0 201
michael@0 202 var gGen = run_test();
michael@0 203 gGen.next();
michael@0 204
michael@0 205 </script>
michael@0 206 </pre>
michael@0 207 </body>
michael@0 208 </html>

mercurial