Bug 615061 - Dispatch the hashchange event synchronously. r=smaug, a2.0=blocking

This commit is contained in:
Justin Lebar 2010-11-29 11:13:12 -08:00
Родитель 88dcd8da4e
Коммит 5039e47c46
8 изменённых файлов: 69 добавлений и 42 удалений

Просмотреть файл

@ -8337,7 +8337,7 @@ nsDocShell::InternalLoad(nsIURI * aURI,
window->DispatchSyncPopState(); window->DispatchSyncPopState();
if (doHashchange) if (doHashchange)
window->DispatchSyncHashchange(); window->DispatchAsyncHashchange();
} }
return NS_OK; return NS_OK;

Просмотреть файл

@ -3,7 +3,7 @@
<head> <head>
<title>Test inner frame for bug 509055</title> <title>Test inner frame for bug 509055</title>
</head> </head>
<body> <body onhashchange="hashchangeCallback()">
file_bug509055.html file_bug509055.html
</body> </body>
</html> </html>

Просмотреть файл

@ -25,7 +25,6 @@ SimpleTest.waitForExplicitFinish();
var gNumHashchanges = 0; var gNumHashchanges = 0;
var gCallbackOnIframeLoad = false; var gCallbackOnIframeLoad = false;
var gCallbackOnHashchange = false;
function statusMsg(msg) { function statusMsg(msg) {
var msgElem = document.createElement("p"); var msgElem = document.createElement("p");
@ -43,14 +42,7 @@ function longWait() {
// event which was fired. // event which was fired.
function onIframeHashchange() { function onIframeHashchange() {
gNumHashchanges++; gNumHashchanges++;
if (gCallbackOnHashchange) { gGen.next();
gCallbackOnHashchange = false;
gGen.next();
}
}
function enableHashchangeCallback() {
gCallbackOnHashchange = true;
} }
function onIframeLoad() { function onIframeLoad() {
@ -121,35 +113,36 @@ function run_test() {
noEventExpected("No hashchange expected initially."); noEventExpected("No hashchange expected initially.");
enableHashchangeCallback();
sendMouseEvent({type: "click"}, "link1", frameCw); sendMouseEvent({type: "click"}, "link1", frameCw);
yield; yield;
eventExpected("Clicking link1 should trigger a hashchange."); eventExpected("Clicking link1 should trigger a hashchange.");
enableHashchangeCallback();
sendMouseEvent({type: "click"}, "link1", frameCw); sendMouseEvent({type: "click"}, "link1", frameCw);
longWait(); longWait();
yield; yield;
// succeed if a hashchange event wasn't triggered while we were waiting // succeed if a hashchange event wasn't triggered while we were waiting
noEventExpected("Clicking link1 again should not trigger a hashchange."); noEventExpected("Clicking link1 again should not trigger a hashchange.");
enableHashchangeCallback();
sendMouseEvent({type: "click"}, "link2", frameCw); sendMouseEvent({type: "click"}, "link2", frameCw);
yield; yield;
eventExpected("Clicking link2 should trigger a hashchange."); eventExpected("Clicking link2 should trigger a hashchange.");
frameCw.history.go(-1); frameCw.history.go(-1);
yield;
eventExpected("Going back should trigger a hashchange."); eventExpected("Going back should trigger a hashchange.");
frameCw.history.go(1); frameCw.history.go(1);
yield;
eventExpected("Going forward should trigger a hashchange."); eventExpected("Going forward should trigger a hashchange.");
frameCw.window.location.hash = ""; frameCw.window.location.hash = "";
yield;
eventExpected("Changing window.location.hash should trigger a hashchange."); eventExpected("Changing window.location.hash should trigger a hashchange.");
// window.location has a trailing '#' right now, so we append "link1", not // window.location has a trailing '#' right now, so we append "link1", not
// "#link1". // "#link1".
frameCw.window.location = frameCw.window.location + "link1"; frameCw.window.location = frameCw.window.location + "link1";
yield;
eventExpected("Assigning to window.location should trigger a hashchange."); eventExpected("Assigning to window.location should trigger a hashchange.");
// Set up history in the iframe which looks like: // Set up history in the iframe which looks like:
@ -184,8 +177,9 @@ function run_test() {
yield; yield;
frameCw.document.location = "file_bug385434_2.html#foo"; frameCw.document.location = "file_bug385434_2.html#foo";
eventExpected("frame onhashchange should fire events."); yield;
eventExpected("frame onhashchange should fire events.");
// iframe should set gSampleEvent // iframe should set gSampleEvent
is(gSampleEvent.target, frameCw, is(gSampleEvent.target, frameCw,
"The hashchange event should be targeted to the window."); "The hashchange event should be targeted to the window.");
@ -201,7 +195,6 @@ function run_test() {
* hashchange is dispatched if the current document readyState is * hashchange is dispatched if the current document readyState is
* not "complete" (bug 504837). * not "complete" (bug 504837).
*/ */
enableHashchangeCallback();
frameCw.document.location = "file_bug385434_3.html"; frameCw.document.location = "file_bug385434_3.html";
yield; yield;
eventExpected("Hashchange should fire even if the document " + eventExpected("Hashchange should fire even if the document " +

Просмотреть файл

@ -29,6 +29,13 @@ function shortWait() {
setTimeout(function() { gGen.next(); }, 0, false); setTimeout(function() { gGen.next(); }, 0, false);
} }
function onChildHashchange(e) {
// gGen might be undefined when we refresh the page, so we have to check here
dump("onChildHashchange() called.\n");
if(gGen)
gGen.next();
}
function onChildLoad(e) { function onChildLoad(e) {
if(gGen) if(gGen)
gGen.next(); gGen.next();
@ -40,23 +47,30 @@ function runTest() {
var popup = window.open("file_bug509055.html", "popup 0", var popup = window.open("file_bug509055.html", "popup 0",
"height=200,width=200,location=yes," + "height=200,width=200,location=yes," +
"menubar=yes,status=yes,toolbar=yes,dependent=yes"); "menubar=yes,status=yes,toolbar=yes,dependent=yes");
popup.hashchangeCallback = onChildHashchange;
popup.onload = onChildLoad; popup.onload = onChildLoad;
yield; // wait for load dump('Waiting for initial load.\n');
yield;
// Not sure why this wait is necessary, but without it, the change to
// location.hash below doesn't create a SHEntry or enable the back button. // Without this wait, the change to location.hash below doesn't create a
shortWait(); // SHEntry or enable the back button.
shortWait();
dump('Got initial load. Spinning event loop.\n');
yield; yield;
// Both setting location.hash and calling history.back() happen
// synchronously, so there's no need to yield here.
popup.location.hash = "#1"; popup.location.hash = "#1";
dump('Waiting for hashchange.\n');
yield;
popup.history.back(); popup.history.back();
dump('Waiting for second hashchange.\n');
yield; // wait for hashchange
popup.document.title = "Changed"; popup.document.title = "Changed";
// Wait for listeners to be notified of the title change. // Wait for listeners to be notified of the title change.
shortWait(); shortWait();
dump('Got second hashchange. Spinning event loop.\n');
yield; yield;
var sh = popup.QueryInterface(Components.interfaces.nsIInterfaceRequestor) var sh = popup.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
@ -70,6 +84,7 @@ function runTest() {
popup.close(); popup.close();
SimpleTest.finish(); SimpleTest.finish();
dump('Final yield.\n');
yield; yield;
} }

Просмотреть файл

@ -7506,11 +7506,20 @@ nsGlobalWindow::PageHidden()
} }
nsresult nsresult
nsGlobalWindow::DispatchSyncHashchange() nsGlobalWindow::DispatchAsyncHashchange()
{ {
FORWARD_TO_INNER(DispatchSyncHashchange, (), NS_OK); FORWARD_TO_INNER(DispatchAsyncHashchange, (), NS_OK);
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(),
"Must be safe to run script here."); nsCOMPtr<nsIRunnable> event =
NS_NewRunnableMethod(this, &nsGlobalWindow::FireHashchange);
return NS_DispatchToCurrentThread(event);
}
nsresult
nsGlobalWindow::FireHashchange()
{
NS_ENSURE_TRUE(IsInnerWindow(), NS_ERROR_FAILURE);
// Don't do anything if the window is frozen. // Don't do anything if the window is frozen.
if (IsFrozen()) if (IsFrozen())

Просмотреть файл

@ -546,7 +546,7 @@ public:
virtual PRBool TakeFocus(PRBool aFocus, PRUint32 aFocusMethod); virtual PRBool TakeFocus(PRBool aFocus, PRUint32 aFocusMethod);
virtual void SetReadyForFocus(); virtual void SetReadyForFocus();
virtual void PageHidden(); virtual void PageHidden();
virtual nsresult DispatchSyncHashchange(); virtual nsresult DispatchAsyncHashchange();
virtual nsresult DispatchSyncPopState(); virtual nsresult DispatchSyncPopState();
virtual nsresult SetArguments(nsIArray *aArguments, nsIPrincipal *aOrigin); virtual nsresult SetArguments(nsIArray *aArguments, nsIPrincipal *aOrigin);
@ -701,6 +701,7 @@ protected:
const nsAString &aPopupWindowName, const nsAString &aPopupWindowName,
const nsAString &aPopupWindowFeatures); const nsAString &aPopupWindowFeatures);
void FireOfflineStatusEvent(); void FireOfflineStatusEvent();
nsresult FireHashchange();
void FlushPendingNotifications(mozFlushType aType); void FlushPendingNotifications(mozFlushType aType);
void EnsureReflowFlushAndPaint(); void EnsureReflowFlushAndPaint();

Просмотреть файл

@ -524,9 +524,10 @@ public:
virtual void PageHidden() = 0; virtual void PageHidden() = 0;
/** /**
* Instructs this window to synchronously dispatch a hashchange event. * Instructs this window to asynchronously dispatch a hashchange event. This
* method must be called on an inner window.
*/ */
virtual nsresult DispatchSyncHashchange() = 0; virtual nsresult DispatchAsyncHashchange() = 0;
/** /**
* Instructs this window to synchronously dispatch a popState event. * Instructs this window to synchronously dispatch a popState event.

Просмотреть файл

@ -23,8 +23,6 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=504220
/** Test for Bug 504220 **/ /** Test for Bug 504220 **/
function run_test() { function run_test() {
SimpleTest.waitForExplicitFinish();
ok("onhashchange" in document.body, ok("onhashchange" in document.body,
"document.body should contain 'onhashchange'."); "document.body should contain 'onhashchange'.");
@ -37,20 +35,30 @@ function run_test() {
// Likewise, document.body.hashchange should mirror window.onhashchange // Likewise, document.body.hashchange should mirror window.onhashchange
numEvents = 0; numEvents = 0;
var func2 = function() { numEvents++; }; var func2 = function() { numEvents++; gGen.next() };
window.onhashchange = func2; window.onhashchange = func2;
is(document.body.onhashchange, func2); is(document.body.onhashchange, func2);
// Change the document's hash. If we've been running this test manually, SimpleTest.waitForExplicitFinish();
// the hash might already be "#foo", so we need to check in order to be
// sure we trigger a hashchange.
if (location.hash != "#foo")
location.hash = "#foo";
else
location.hash = "#bar";
is(numEvents, 1, "Exactly one hashchange should have been fired."); function waitForHashchange() {
SimpleTest.finish(); // Change the document's hash. If we've been running this test manually,
// the hash might already be "#foo", so we need to check in order to be
// sure we trigger a hashchange.
if (location.hash != "#foo")
location.hash = "#foo";
else
location.hash = "#bar";
yield;
is(numEvents, 1, "Exactly one hashchange should have been fired.");
SimpleTest.finish();
yield;
}
var gGen = waitForHashchange();
gGen.next();
} }
</script> </script>