4 https://bugzilla.mozilla.org/show_bug.cgi?id=500328
7 <title>Test for Bug
500328</title>
8 <script type=
"application/javascript" src=
"/MochiKit/packed.js"></script>
9 <script type=
"application/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <script type=
"application/javascript" src=
"/tests/SimpleTest/EventUtils.js"></script>
11 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
14 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=500328">Mozilla Bug
500328</a>
16 <div id=
"status"></div>
18 <iframe id=
"iframe"></iframe>
19 <iframe id=
"iframe2"></iframe>
23 <script type=
"application/javascript;version=1.7">
25 /** Test for Bug
500328 **/
27 SimpleTest.waitForExplicitFinish();
29 var iframe = document.getElementById(
"iframe");
30 var iframeCw = iframe.contentWindow;
32 var iframe2 = document.getElementById(
"iframe2");
33 var iframe2Cw = iframe2.contentWindow;
35 const unvisitedColor =
"rgb(0, 0, 238)";
36 const visitedColor =
"rgb(85, 26, 139)";
38 var gCallbackOnIframeLoad = false;
39 var gCallbackOnPopState = false;
40 var gNumPopStates =
0;
41 var gLastPopStateEvent;
45 function statusMsg(msg) {
46 var msgElem = document.createElement(
"p");
47 msgElem.appendChild(document.createTextNode(msg));
49 document.getElementById(
"status").appendChild(msgElem);
53 setTimeout(function() { gGen.next(); },
1000);
56 function shortWait() {
57 setTimeout(function() { gGen.next(); },
0);
60 function onChildPopState(e) {
62 gLastPopStateEvent = e;
63 if (gCallbackOnPopState) {
64 statusMsg(
"Popstate(" + JSON.stringify(e.state) +
"). Calling gGen.next().");
65 gCallbackOnPopState = false;
69 statusMsg(
"Popstate(" + JSON.stringify(e.state) +
"). NOT calling gGen.next().");
73 function onChildLoad() {
74 if(gCallbackOnIframeLoad) {
75 statusMsg(
"Got load. About to call gGen.next().");
76 gCallbackOnIframeLoad = false;
80 statusMsg(
"Got load, but not calling gGen.next() because gCallbackOnIframeLoad was false.");
84 function enableChildLoadCallback() {
85 gCallbackOnIframeLoad = true;
88 function enableChildPopStateCallback() {
89 gCallbackOnPopState = true;
92 function clearPopStateCounter() {
96 function noPopStateExpected(msg) {
97 is(gNumPopStates,
0, msg);
99 // Even if there's an error, set gNumPopStates to
0 so other tests don't
104 function popstateExpected(msg) {
105 is(gNumPopStates,
1, msg);
109 function getColor(elem) {
110 netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
111 var utils = document.defaultView.
112 QueryInterface(Components.interfaces.nsIInterfaceRequestor).
113 getInterface(Components.interfaces.nsIDOMWindowUtils);
114 return utils.getVisitedDependentComputedStyle(elem,
"",
"color");
117 function getSHistory(theWindow)
119 netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
121 const Ci = Components.interfaces;
122 var sh = theWindow.QueryInterface(Ci.nsIInterfaceRequestor)
123 .getInterface(Ci.nsIWebNavigation)
125 if (!sh || sh == null)
126 throw(
"Couldn't get shistory for window!");
131 function getSHTitle(sh, offset)
133 netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
138 // False instructs the SHistory not to modify its current index.
139 return sh.getEntryAtIndex(sh.index + offset, false).title;
142 // Tests that win's location ends with str
143 function locationEndsWith(win, str) {
144 var exp = new RegExp(str +
"$");
145 ok(win.location.toString().match(exp),
146 "Wrong window location. Expected it to end with " +
147 str +
", but actuall was " + win.location);
150 function expectException(func, msg) {
158 ok(failed, msg +
" succeeded, but should have failed.");
162 // We can't enable universal XPConnect privleges in this function, because
163 // test
5 needs to be running at normal privleges in order to test the
164 // same-origin policy.
168 *
1. Clear the popstate counter
171 clearPopStateCounter();
173 // The URL of file_bug500328_1.html on http://localhost:
8888
176 // Now we can start the tests
179 * TEST
1 tests basic pushState functionality
181 enableChildLoadCallback();
182 iframeCw.location =
"file_bug500328_1.html";
184 innerLoc = iframeCw.location.toString();
185 // Load should be fired before popstate.
186 enableChildPopStateCallback();
188 popstateExpected(
"Expected initial popstate.");
189 statusMsg(
"Awake after first popstate.");
193 iframeCw.history.pushState(testObj1,
"test 1");
194 is(iframeCw.location.search,
"",
195 "First pushstate should leave us where we were.");
197 // Make sure that this pushstate doesn't trigger a hashchange.
198 iframeCw.onhashchange = function() {
199 ok(false,
"Pushstate shouldn't trigger a hashchange.");
202 iframeCw.history.pushState(testObj2,
"test 1#foo",
"?test1#foo");
203 is(iframeCw.location.search,
"?test1",
204 "Second pushstate should push us to '?test1'.");
205 is(iframeCw.location.hash,
"#foo",
206 "Second pushstate should push us to '#foo'");
210 // Let the hashchange event fire, if it's going to.
213 iframeCw.onhashchange = null;
215 statusMsg(
"About to go back to page 1.");
216 // We don't have to yield here because this back() and the resulting popstate
217 // are completely synchronous. In fact, if we did yield, JS would throw an
218 // error because we'd be calling gGen.next from within gGen.next.
219 iframeCw.history.back();
221 statusMsg(
"Awake after going back to page 1.");
222 popstateExpected(
"Going back to page 1 should trigger a popstate.");
223 is(JSON.stringify(gLastPopStateEvent.state), JSON.stringify(testObj1),
224 "Wrong state object popped after going back to page 1.");
225 ok(gLastPopStateEvent.state === iframeCw.document.mozCurrentStateObject,
226 "Wrong state object in document after going back to page 1.");
227 ok(iframeCw.location.toString().match(/file_bug500328_1.html$/),
228 "Going back to page 1 hould take us to original page.");
230 iframeCw.history.back();
231 popstateExpected(
"Going back to page 0 should trigger a popstate.");
232 is(gLastPopStateEvent.state, null,
233 "Going back to page 0 should pop a null state.");
234 is(iframeCw.document.mozCurrentStateObject, null,
235 "Going back to page 0 should pop a null state.");
236 is(iframeCw.location.search,
"",
237 "Going back to page 0 should clear the querystring.");
239 iframeCw.history.forward();
240 popstateExpected(
"Going forward to page 1 should trigger a popstate.");
241 is(JSON.stringify(gLastPopStateEvent.state), JSON.stringify(testObj1),
242 "Wrong state object popped after going forward to page 1.");
243 ok(gLastPopStateEvent.state === iframeCw.document.mozCurrentStateObject,
244 "Wrong state object in document after going forward to page 1.");
245 ok(iframeCw.location.toString().match(/file_bug500328_1.html$/),
246 "Going forward to page 1 should leave us at original page.");
249 statusMsg(
"About to go forward to page 2.");
250 iframeCw.history.forward();
251 statusMsg(
"Awake after going forward to page 2.");
252 popstateExpected(
"Going forward to page 2 should trigger a popstate.");
253 is(JSON.stringify(gLastPopStateEvent.state), JSON.stringify(testObj2),
254 "Wrong state object popped after going forward to page 2.");
255 ok(iframeCw.document.mozCurrentStateObject === gLastPopStateEvent.state,
256 "Wrong state object in document after going forward to page 2.");
257 ok(iframeCw.location.toString().match(/file_bug500328_1.html\?test1#foo$/),
258 "Going forward to page 2 took us to " + iframeCw.location.toString());
260 // The iframe's current location is file_bug500328_1.html?test1#foo.
261 // Clicking link1 should take us to file_bug500328_1.html?test1#
1.
263 enableChildPopStateCallback();
264 sendMouseEvent({type:'click'}, 'link-anchor1', iframeCw);
266 popstateExpected(
"Clicking on link-anchor1 should trigger a popstate.");
267 is(iframeCw.location.search,
"?test1",
268 "search should be ?test1 after clicking link.");
269 is(iframeCw.location.hash,
"#1",
270 "hash should be #1 after clicking link.");
273 * Reload file_bug500328_1.html; we're now going to test that link hrefs
274 * and colors are updated correctly on push/popstates.
277 iframe.onload = onChildLoad;
278 enableChildLoadCallback();
279 iframeCw.location =
"about:blank";
281 iframe.onload = null;
282 iframeCw.location =
"file_bug500328_1.html";
283 enableChildPopStateCallback();
285 popstateExpected(
"No popstate after re-loading file_bug500328_1.html");
286 statusMsg(
"Done loading file_bug500328_1.html for the second time.");
288 var ifLink = iframeCw.document.getElementById(
"link-anchor1");
289 var rand = Date.now() +
"-" + Math.random();
292 // Poll the document until the link has the correct color, or this test times
293 // out. Unfortunately I can't come up with a more elegant way to do this.
294 // We could listen to MozAfterPaint, but that doesn't guarantee that we'll
295 // observe the new color.
296 while (getColor(ifLink) != unvisitedColor) {
297 // Dump so something shows up in the mochitest logs if we spin here.
298 dump(
"ifLink has wrong initial color. Spinning...\n");
299 setTimeout(function() { gGen.next(); },
10);
303 // Navigate iframe2 to dir/${rand}
304 iframe2.onload = onChildLoad;
305 enableChildLoadCallback();
306 iframe2Cw.location =
"mytestdir/" + rand;
309 // PushState the iframe into the mytestdir directory. This should cause
310 // ifLink to turn purple, since we just visited mytestdir/${rand} in iframe2.
311 iframeCw.history.pushState(null,
"foo",
"mytestdir/foo");
313 // Check that the link's color is now visitedColor
314 while (getColor(ifLink) != visitedColor) {
315 dump(
"ifLink has wrong color after pushstate. Spinning...\n");
316 setTimeout(function() { gGen.next(); },
10);
320 ok(ifLink.href.match(
"mytestdir\\/" + rand +
"$"),
321 "inner frame's link should end with 'mytestdir/${rand}'");
323 // Navigate out of the mytestdir directory. This should cause ifLink to turn
325 iframeCw.history.pushState(null,
"bar",
"../file_bug500328_1.html");
327 // Check that the link's color is back to the unvisited color.
328 while (getColor(ifLink) != unvisitedColor) {
329 dump(
"ifLink has wrong color after pushstating out of dir. Spinning...\n");
330 setTimeout(function() { gGen.next(); },
10);
334 ok(!ifLink.href.match(
"mytestdir"),
335 "inner frame's link shouldn't contain 'mytestdir'.");
338 * TEST
2 tests that pushstate's same-origin checks are correct.
340 var filename = 'file_bug500328_2.html';
341 // Get the directory we're currently in
342 var dirname = document.location.pathname.replace(/[^\/]*$/, '');
343 statusMsg(
"Dirname is: " + dirname);
344 var loc = 'http://example.com' + dirname + filename;
345 statusMsg(
"About to transfer iframe to " + loc);
346 iframeCw.location = loc;
347 // We have to register a listener like this because this file is hosted on a
348 // different domain and can't notify us on load.
349 iframe.onload = onChildLoad;
350 enableChildLoadCallback();
353 // This function tries to pushstate and replacestate to the given URL and
354 // fails the test if the calls succeed.
355 var tryBadPushAndReplaceState = function(url) {
356 // XXX ex should be a SECURITY_ERR, not a plain Error.
358 var hist = iframeCw.history;
359 var url2 = url + dirname + filename;
361 expectException(function() { hist.pushState({},
"foo", url); },
362 'pushState to ' + url);
364 expectException(function() { hist.pushState({},
"foo", url2); },
365 'pushState to ' + url2);
367 expectException(function() { hist.replaceState({},
"foo", url); },
368 'replaceState to ' + url);
370 expectException(function() { hist.replaceState({},
"foo", url2); },
371 'replaceState to ' + url2);
374 // We're currently at http://example.com/[dirname]/[filename]
375 tryBadPushAndReplaceState(
"https://example.com");
376 tryBadPushAndReplaceState(
"http://foo.example.com");
377 tryBadPushAndReplaceState(
"http://example.com:1234");
378 tryBadPushAndReplaceState(
"http://example.com.a");
379 tryBadPushAndReplaceState(
"http://example.con");
380 tryBadPushAndReplaceState(
"http://eexample.com");
381 tryBadPushAndReplaceState(
"http://me@example.com");
384 * TEST
3 tests that the session history entries' titles are properly sync'ed
385 * after push/pop states.
387 * We have to run this test in a popup rather than an iframe because only the
388 * root docshell has a session history object.
390 statusMsg(
"About to open popup.");
391 var popup = window.open(
"file_bug500328_1.html",
"popup0",
392 "height=200,width=200,location=yes," +
393 "menubar=yes,status=yes,toolbar=yes,dependent=yes");
395 var shistory = getSHistory(popup);
397 enableChildPopStateCallback();
399 popstateExpected(
"Didn't get popstate after opening window.");
401 popup.history.pushState(null,
"title 0");
402 ok(SpecialPowers.isBackButtonEnabled(popup),
403 "Back button was not enabled after initial pushstate.");
405 popup.document.title =
"title 1";
407 // Yield to the event loop so listeners will be notified of the title change
408 // and so that the hash change we trigger below generates a new session
413 // Check that the current session history entry's title has been updated to
414 // reflect the new document title.
415 is(getSHTitle(shistory),
"title 1",
"SHEntry title test 1");
417 // Change the page's hash to #
1, which will trigger a popstate event.
418 // We don't have to wait, because this happens synchronously.
419 popup.location.hash =
"#1";
420 popstateExpected(
"Didn't get popstate after changing hash.");
422 popup.document.title =
"title 2";
424 // Yield so listeners will be notified of the title change we just performed.
428 is(getSHTitle(shistory),
"title 2",
"SHEntry title test 2");
430 // Go back. Happens synchronously. We should get a popstate.
431 statusMsg(
"About to go back.");
432 popup.history.go(-
1);
433 popstateExpected(
"Didn't get a popstate after going back.");
435 // Even though we went back, we expect the SHEntry title to remain the same
436 // because the document didn't change.
437 is(getSHTitle(shistory),
"title 2",
"SHEntry title test 3");
439 popup.document.title =
"Changed 1";
443 // This check is really a test of bug
509055.
444 is(getSHTitle(shistory),
"Changed 1",
"SHEntry title test 4");
449 * TEST
4 tests replaceState and that we don't get double popstates on
450 * window.open. It also stress-tests the system and its interaction with
451 * bfcache by making many push/replace state calls.
453 popup = window.open(
"file_bug500328_1.html",
"popup1",
454 "height=200,width=200,location=yes," +
455 "menubar=yes,status=yes,toolbar=yes,dependent=yes");
457 // The initial about:blank load into the new window shouldn't result in us
458 // seeing a popstate. Once file_bug500328_1.html is loaded, it'll overwrite
459 // popup.onpopstate, and this assertion won't fire for that popstate and
462 // If we fired the popstate event asynchronously, we'd expect this assert to
464 popup.onpopstate = function() {
465 ok(false,
"Initial load of popup shouldn't give us a popstate.");
468 shistory = getSHistory(popup);
470 enableChildPopStateCallback();
472 statusMsg(
"Awake after loading content into popup.");
474 popup.history.replaceState({n:
1, ok:true},
"state 1",
"good1.html");
475 locationEndsWith(popup,
"good1.html");
477 // Even though we replaceState with title
"state 1", the title should remain
478 //
"test 1" because we ignore the title argument in push/replaceState.
480 is(getSHTitle(shistory),
"test 1",
"SHEntry title 'state 1'");
482 // Flush the event loop so our next load creates a new session history entry.
486 enableChildPopStateCallback();
487 popup.location =
"file_bug500328_1.html";
490 // Flush the event loop so nsDocShell::OnNewURI runs and our load is recorded
491 // properly. OnNewURI is called after PopState fires, because OnNewURI
492 // results from an async event, while PopState is sync. We'd have to do the
493 // same thing if we were listening to onload here, so this isn't
498 // Now go back and make sure everything is as it should be.
499 enableChildPopStateCallback();
500 popup.history.back();
503 // Flush the event loop so the document's location is updated.
507 // We had some popstates above without corresponding popstateExpected()
508 // calls, so we need to clear the counter.
509 clearPopStateCounter();
511 locationEndsWith(popup,
"good1.html");
512 is(JSON.stringify(gLastPopStateEvent.state), '{
"n":
1,
"ok":true}',
513 "Wrong state popped after going back to initial state.");
515 // We're back at state
0, which was replaceState-ed to state1.html. Let's do
516 // some push/pop/replaces to make sure everything works OK when we involve
517 // large numbers of SHEntries.
518 for(var i =
2; i <=
30; i++) {
520 popup.history.pushState({n:i, ok:true},
"state " + i,
"good" + i +
".html");
523 popup.history.pushState({n:i},
"state " + i,
"state" + i +
".html");
524 for(var j =
0; j < i %
4; j++) {
525 popup.history.replaceState({n:i, nn:j},
"state " + i +
", " + j);
527 popup.history.replaceState({n:i, ok:true},
"state " + i,
"good" + i +
".html");
531 for(var i =
29; i
>=
1; i--) {
532 popup.history.back();
533 popstateExpected(
"Didn't get a popstate on iteration " + i);
534 locationEndsWith(popup,
"good" + i +
".html");
535 is(gLastPopStateEvent.state.n, i,
"Bad counter on last popstate event.");
536 ok(gLastPopStateEvent.state.ok,
537 "Last popstate event should have 'ok' set to true.");
543 * TEST
5 tests misc security features and implementation details of
548 * Test that we can't push/replace an object with a large (over
640k
549 * characters) JSON representation.
552 // (In case you're curious, this loop generates an object which serializes to
553 //
694581 characters.)
554 var bigObject = new Object();
555 for(var i =
0; i <
51200; i++) {
558 // statusMsg(
"Big object has size " + JSON.stringify(bigObject).length);
560 // We shouldn't be able to pushstate this large object, due to space
563 function() { iframeCw.history.pushState(bigObject,
"foo"); },
564 "pushState-ing large object");
567 function() { iframeCw.history.replaceState(bigObject,
"foo"); },
568 "replaceState-ing large object");
571 * Make sure we can't push/replace state on an iframe of a different origin.
572 * This will work if this function has requested Universal XPConnect
573 * privileges, so any code which needs those privileges can't be in this
576 enableChildLoadCallback();
577 iframeCw.location =
"http://example.com";
578 iframe.onload = onChildLoad;
580 iframe.onload = null;
583 function() { iframeCw.history.pushState({},
"foo"); },
584 "pushState-ing in a different origin");
587 function() { iframeCw.history.replaceState({},
"foo"); },
588 "replaceState-ing in a different origin");
591 * If we do the following:
593 * * PushState to page B.
594 * * Refresh. The server responds with a
404
596 * Then at the end, page A should be displayed, not the
404 page.
598 enableChildLoadCallback();
599 iframe.onload = onChildLoad;
600 iframeCw.location =
"about:blank";
602 iframe.onload = null;
604 enableChildPopStateCallback();
605 // navigate to http://localhost:
8888/[...]/file_bug500328_1.html
606 iframeCw.location = innerLoc;
609 // Let the PopState handler finish. If we call refresh (below) from within
610 // the handler, we get slightly confused and can't tell that we're at a
404
611 // after the refresh.
615 // PushState to a URL which doesn't exist
616 iframeCw.history.pushState({},
"", rand);
618 // Refresh. We'll end up a
404 page.
619 iframe.onload = onChildLoad;
620 enableChildLoadCallback();
621 iframeCw.location.reload(true);
624 // Since the last page was a
404, going back should actually show the
625 // contents of the old page, instead of persisting the contents of the
404
627 clearPopStateCounter();
628 enableChildPopStateCallback();
629 iframeCw.history.back();
631 popstateExpected(
"Didn't get popstate after going back.");
633 // Make sure that we're actually showing the contents of
634 // file_bug500328_1.html, as opposed to the
404 page.
635 var identifierElem = iframeCw.document.getElementById(
"link-anchor1");
636 ok(identifierElem != undefined && identifierElem != null,
637 "iframe didn't contain file_bug500328_1.html's contents.");
640 * TEST
6 tests that the referrer is set properly after push/replace states.
644 * First, a simple test:
645 * * Load file_bug500328_1.html into iframe
646 * * PushState to newpage1.html#foo
647 * * Instruct the iframe to load file_bug500328_1.html into itself.
648 * The referer should be newpage1.html, without the hash.
650 * This also tests that we can call pushState from within the onload handler.
652 enableChildLoadCallback();
653 iframeCw.location =
"file_bug500328_1.html";
656 // Run within the onload handler. This should work without issue.
657 iframeCw.history.pushState(null,
"",
"newpage1.html");
659 // iframeCw.navigateTo() causes the iframe to set its location on our
660 // behalf. We can't just set its location ourselves, because then *we*
661 // become the referrer.
662 enableChildPopStateCallback();
663 iframeCw.navigateTo(
"file_bug500328_1.html");
666 ok(iframeCw.document.referrer.toString().match(/newpage1.html$/),
667 "Wrong referrer after replaceState. Expected newpage1.html, but was " +
668 iframeCw.document.referrer);
671 * We're back at file_bug500328_1.html. Now do the following:
672 * * replaceState to newpage2.html#foo
673 * * Click a link back to file_bug500328_1.html
674 * The referrer should be newpage2.html, without the hash.
676 iframeCw.history.replaceState(null, null,
"newpage2.html#foo");
677 enableChildPopStateCallback();
678 sendMouseEvent({type:'click'}, 'link-self', iframeCw);
681 ok(iframeCw.document.referrer.toString().match(/newpage2.html$/),
682 "Wrong referrer after replaceState. Expected newpage2.html, but was " +
683 iframeCw.document.referrer);
687 * Set up a cycle with the popstate event to make sure it's properly
690 var evt = document.createEvent(
"popstateevent");
691 evt.initEvent(
"foo", false, false, evt);
695 statusMsg(
"********** Finished tests ***********");
700 // I don't think this will actually make the mochitest fail, but there's
701 // not much we can do about this. Realistically, though, regressions are
702 // not likely to fire extra events -- this trap is here mostly to catch
703 // errors made while wriring tests.
704 ok(false,
"Got extra event!");
708 statusMsg(
"XXXXXXXXXXXXXX");
711 statusMsg(
"Woken up.");
716 // Important: Wait to start the tests until the page has loaded. Otherwise,
717 // the test will occasionally fail when it begins running before the iframes
718 // have finished their initial load of about:blank.
719 window.addEventListener('load', function() {