summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_popupremoving.xhtml
blob: babd62c18bcf7c651a587c5c51c9a995cd292566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?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)"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<!--
  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.
  -->

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>      
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>      

<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>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

</window>