Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
<window title="Popup Removing Tests"
onload="setTimeout(nextTest, 0)"
<!--
This test checks that popup elements can be removed in various ways without
crashing. It tests two situations, one with menus that are 'separate', and
one with menus that are 'nested'. In each case, there are four levels of menu.
The nextTest function starts the process by opening the first menu. A set of
popupshown event listeners are used to open the next menu until all four are
showing. This last one calls removePopup to remove the menu node from the
tree. This should hide the popups as they are no longer in a document.
A mutation listener is triggered when the fourth menu closes by having its
open attribute cleared. This listener hides the third popup which causes
its frame to be removed. Naturally, we want to ensure that this doesn't
crash when the third menu is removed.
-->
<hbox>
<menu id="nestedmenu1" label="1">
<menupopup id="nestedpopup1" onpopupshown="if (event.target == this) this.firstChild.open = true">
<menu id="nestedmenu2" label="2">
<menupopup id="nestedpopup2" onpopupshown="if (event.target == this) this.firstChild.open = true">
<menu id="nestedmenu3" label="3">
<menupopup id="nestedpopup3" onpopupshown="if (event.target == this) this.firstChild.open = true">
<menu id="nestedmenu4" label="4">
<menupopup id="nestedpopup4">
<menuitem label="Nested 1"/>
<menuitem label="Nested 2"/>
<menuitem label="Nested 3"/>
</menupopup>
</menu>
</menupopup>
</menu>
</menupopup>
</menu>
</menupopup>
</menu>
<menu id="separatemenu1" label="1">
<menupopup id="separatepopup1" onpopupshown="$('separatemenu2').open = true">
<menuitem label="L1 One"/>
<menuitem label="L1 Two"/>
<menuitem label="L1 Three"/>
</menupopup>
</menu>
<menu id="separatemenu2" label="2">
<menupopup id="separatepopup2" onpopupshown="$('separatemenu3').open = true"
onpopuphidden="popup2Hidden()">
<menuitem label="L2 One"/>
<menuitem label="L2 Two"/>
<menuitem label="L2 Three"/>
</menupopup>
</menu>
<menu id="separatemenu3" label="3" onpopupshown="$('separatemenu4').open = true">
<menupopup id="separatepopup3">
<menuitem label="L3 One"/>
<menuitem label="L3 Two"/>
<menuitem label="L3 Three"/>
</menupopup>
</menu>
<menu id="separatemenu4" label="4" onpopuphidden="$('separatemenu2').open = false">
<menupopup id="separatepopup3">
<menuitem label="L4 One"/>
<menuitem label="L4 Two"/>
<menuitem label="L4 Three"/>
</menupopup>
</menu>
</hbox>
<script class="testbody" type="application/javascript">
<![CDATA[
SimpleTest.waitForExplicitFinish();
let gKey = "";
let gTriggerMutation = null;
let gChangeMutation = null;
let gResolveRemovePopups = null;
let gResolvePopup2Hidden = null;
function nextTest()
{
let promiseRemovePopups, promisePopup2Hidden;
if (gKey == "") {
gKey = "separate";
$("separatemenu4").addEventListener("popupshown", removePopups);
promiseRemovePopups = new Promise(resolve => gResolveRemovePopups = resolve);
promisePopup2Hidden = new Promise(resolve => gResolvePopup2Hidden = resolve);
}
else if (gKey == "separate") {
gKey = "nested";
$("nestedmenu4").addEventListener("popupshown", removePopups);
promiseRemovePopups = new Promise(resolve => gResolveRemovePopups = resolve);
gResolvePopup2Hidden = null;
}
else {
SimpleTest.finish();
return;
}
SimpleTest.executeSoon(async () => {
$(gKey + "menu1").open = true;
await Promise.all([promiseRemovePopups, promisePopup2Hidden]);
nextTest();
});
}
function modified(aMutationList, aObserver) {
// use this mutation listener to hide the third popup, destroying its frame.
// It gets triggered when the open attribute is cleared on the fourth menu.
for (const mutation of aMutationList) {
if (mutation.attributeName != "open") {
continue;
}
gChangeMutation.hidden = true;
// force a layout flush
document.documentElement.clientWidth;
gChangeMutation = null;
aObserver.disconnect();
break;
}
}
async function removePopups()
{
var menu2 = $(gKey + "menu2");
var menu3 = $(gKey + "menu3");
is(menu2.getAttribute("open"), "true", gKey + " menu 2 open before");
is(menu3.getAttribute("open"), "true", gKey + " menu 3 open before");
const observer = new MutationObserver(modified);
observer.observe(menu3, { attributes: true });
gChangeMutation = $(gKey + "menu4");
var menu = $(gKey + "menu1");
menu.remove();
const key = gKey;
await new Promise (SimpleTest.executeSoon);
if (key == "nested") {
// the 'separate' test checks this during the popup2 hidden event handler
is(menu2.hasAttribute("open"), false, gKey + " menu 2 open after");
is(menu3.hasAttribute("open"), false, gKey + " menu 3 open after");
}
gResolveRemovePopups();
}
function popup2Hidden() {
is($(gKey + "menu2").hasAttribute("open"), false, gKey + " menu 2 open after");
if (gResolvePopup2Hidden) {
gResolvePopup2Hidden();
}
}
]]>
</script>
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</window>