Bug 795518 - window.close() in social windows should close social panel. r=jaws

This commit is contained in:
Mark Hammond 2012-10-02 17:57:20 +10:00
parent a521376e23
commit c1ed6de18a
5 changed files with 87 additions and 1 deletions

View File

@ -116,6 +116,33 @@ var tests = {
}
port.postMessage({topic: "test-worker-chat", data: chatUrl});
},
testCloseSelf: function(next) {
let chats = document.getElementById("pinnedchats");
let port = Social.provider.getWorkerPort();
ok(port, "provider has a port");
port.onmessage = function (e) {
let topic = e.data.topic;
switch (topic) {
case "test-init-done":
port.postMessage({topic: "test-chatbox-open"});
break;
case "got-chatbox-visibility":
is(e.data.result, "shown", "chatbox shown");
port.close(); // don't want any more visibility messages.
let chat = chats.selectedChat;
ok(chat.parentNode, "chat has a parent node before it is closed");
// ask it to close itself.
let doc = chat.iframe.contentDocument;
let evt = doc.createEvent("CustomEvent");
evt.initCustomEvent("socialTest-CloseSelf", true, true, {});
doc.documentElement.dispatchEvent(evt);
ok(!chat.parentNode, "chat is now closed");
next();
break;
}
}
port.postMessage({topic: "test-init", data: { id: 1 }});
},
testCloseOnLogout: function(next) {
const chatUrl = "https://example.com/browser/browser/base/content/test/social_chat.html";
let port = Social.provider.getWorkerPort();

View File

@ -67,6 +67,7 @@ var tests = {
cs = iframe.contentWindow.getComputedStyle(iframe.contentDocument.body);
is(cs.width, "500px", "should now be 500px wide");
panel.hidePopup();
port.close();
next();
}, false);
SocialFlyout.dispatchPanelEvent("socialTest-MakeWider");
@ -74,6 +75,31 @@ var tests = {
}
}
port.postMessage({topic: "test-init"});
},
testCloseSelf: function(next) {
let panel = document.getElementById("social-flyout-panel");
let port = Social.provider.getWorkerPort();
ok(port, "provider has a port");
port.onmessage = function (e) {
let topic = e.data.topic;
switch (topic) {
case "test-init-done":
port.postMessage({topic: "test-flyout-open"});
break;
case "got-flyout-visibility":
let iframe = panel.firstChild;
iframe.contentDocument.addEventListener("SocialTest-DoneCloseSelf", function _doneHandler() {
iframe.contentDocument.removeEventListener("SocialTest-DoneCloseSelf", _doneHandler, false);
is(panel.state, "closed", "flyout should have closed itself");
next();
}, false);
is(panel.state, "open", "flyout should be open");
port.close(); // so we don't get the -visibility message as it hides...
SocialFlyout.dispatchPanelEvent("socialTest-CloseSelf");
break;
}
}
port.postMessage({topic: "test-init"});
}
}

View File

@ -14,6 +14,9 @@
var port = navigator.mozSocial.getWorker().port;
port.postMessage({topic: "chatbox-visibility", result: "hidden"});
}, false);
window.addEventListener("socialTest-CloseSelf", function(e) {
window.close();
}, false);
</script>
</head>
<body onload="pingWorker();">

View File

@ -21,6 +21,12 @@
evt.initCustomEvent("SocialTest-DoneMakeWider", true, true, {});
document.documentElement.dispatchEvent(evt);
}, false);
window.addEventListener("socialTest-CloseSelf", function(e) {
window.close();
var evt = document.createEvent("CustomEvent");
evt.initCustomEvent("SocialTest-DoneCloseSelf", true, true, {});
document.documentElement.dispatchEvent(evt);
}, false);
</script>
</head>
<body style="max-width: 250px;" onload="pingWorker();">

View File

@ -174,6 +174,30 @@ function attachToWindow(provider, targetWindow) {
// set a timer which will fire after the unload events have all fired.
schedule(function () { port.close(); });
});
targetWindow.addEventListener("DOMWindowClose", function _mozSocialDOMWindowClose(evt) {
let elt = targetWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.chromeEventHandler;
while (elt) {
if (elt.nodeName == "panel") {
elt.hidePopup();
break;
} else if (elt.nodeName == "chatbox") {
elt.close();
break;
}
elt = elt.parentNode;
}
// preventDefault stops the default window.close() function being called,
// which doesn't actually close anything but causes things to get into
// a bad state (an internal 'closed' flag is set and debug builds start
// asserting as the window is used.).
// None of the windows we inject this API into are suitable for this
// default close behaviour, so even if we took no action above, we avoid
// the default close from doing anything.
evt.preventDefault();
}, true);
}
function schedule(callback) {