summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/window_panel_anchoradjust.xhtml
blob: 735ecf26b6c914305cd017d55350ac197cc8659c (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
181
182
183
184
185
186
187
188
189
190
191
192
193
<?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 width="200" height="200"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

<deck id="deck">
  <hbox id="container">
    <button id="anchor" label="Anchor"/>
  </hbox>
  <button id="anchor3" label="Anchor3"/>
</deck>

<hbox id="container2">
  <button id="anchor2" label="Anchor2"/>
</hbox>

<button id="anchor4" label="Anchor4"/>

<panel id="panel" type="arrow">
  <button label="OK"/>
</panel>

<menupopup id="menupopup">
  <menuitem label="One"/>
  <menuitem id="menuanchor" label="Two"/>
  <menuitem label="Three"/>
</menupopup>

<script><![CDATA[


SimpleTest.waitForExplicitFinish();

function next()
{
  return new Promise(r => {
    requestAnimationFrame(() => requestAnimationFrame(r));
  })
}

function waitForPanel(panel, event)
{
  return new Promise(resolve => {
    panel.addEventListener(event, () => { resolve(); }, { once: true });
  });
}

function isWithinHalfPixel(a, b, message)
{
  ok(Math.abs(a - b) <= 0.5, `${message}: ${a}, ${b}`);
}

function getPanelPos(panel) {
  let {left, top, bottom, right} = panel.getBoundingClientRect();
  left -= parseFloat(getComputedStyle(panel).marginLeft);
  top -= parseFloat(getComputedStyle(panel).marginTop);
  bottom += parseFloat(getComputedStyle(panel).marginBottom);
  right += parseFloat(getComputedStyle(panel).marginRight);
  return {left, top, bottom, right};
}

async function runTests() {
  let panel = document.getElementById("panel");
  let anchor = document.getElementById("anchor");

  let popupshown = waitForPanel(panel, "popupshown");
  panel.openPopup(anchor, "after_start");
  info("popupshown");
  await popupshown;

  let anchorrect = anchor.getBoundingClientRect();
  let panelpos = getPanelPos(panel);
  let xarrowdiff = panelpos.left - anchorrect.left;

  // When the anchor is moved in some manner, the panel should be adjusted
  let popuppositioned = waitForPanel(panel, "popuppositioned");
  document.getElementById("anchor").style.marginLeft = "50px"
  info("before popuppositioned");
  await popuppositioned;
  info("after popuppositioned");

  anchorrect = anchor.getBoundingClientRect();
  panelpos = getPanelPos(panel);
  isWithinHalfPixel(anchorrect.left, panelpos.left - xarrowdiff, "anchor moved x");
  isWithinHalfPixel(anchorrect.bottom, panelpos.top, "anchor moved y");

  // moveToAnchor is used to change the anchor
  let anchor2 = document.getElementById("anchor2");
  popuppositioned = waitForPanel(panel, "popuppositioned");
  panel.moveToAnchor(anchor2, "after_end");
  info("before popuppositioned 2");
  await popuppositioned;
  info("after popuppositioned 2");

  let anchor2rect = anchor2.getBoundingClientRect();
  panelpos = getPanelPos(panel);
  isWithinHalfPixel(anchor2rect.right, panelpos.right + xarrowdiff, "new anchor x");
  isWithinHalfPixel(anchor2rect.bottom, panelpos.top, "new anchor y");

  // moveToAnchor is used to change the anchor with an x and y offset
  popuppositioned = waitForPanel(panel, "popuppositioned");
  panel.moveToAnchor(anchor2, "after_end", 7, 9);
  await popuppositioned;

  anchor2rect = anchor2.getBoundingClientRect();
  panelpos = getPanelPos(panel);
  isWithinHalfPixel(anchor2rect.right + 7, panelpos.right + xarrowdiff, "new anchor with offset x");
  isWithinHalfPixel(anchor2rect.bottom + 9, panelpos.top, "new anchor with offset y");

  // When the container of the anchor is collapsed, the panel should be hidden.
  let popuphidden = waitForPanel(panel, "popuphidden");
  anchor2.parentNode.collapsed = true;
  await popuphidden;

  popupshown = waitForPanel(panel, "popupshown");
  panel.openPopup(anchor, "after_start");
  await popupshown;

  // When the deck containing the anchor changes to a different page, the panel should be hidden.
  popuphidden = waitForPanel(panel, "popuphidden");
  document.getElementById("deck").selectedIndex = 1;
  await popuphidden;

  let anchor3 = document.getElementById("anchor3");
  popupshown = waitForPanel(panel, "popupshown");
  panel.openPopup(anchor3, "after_start");
  await popupshown;

  // When the anchor is hidden; the panel should be hidden.
  popuphidden = waitForPanel(panel, "popuphidden");
  anchor3.parentNode.hidden = true;
  await popuphidden;

  // When the panel is anchored to an element in a popup, the panel should
  // also be hidden when that popup is hidden.
  let menupopup = document.getElementById("menupopup");
  popupshown = waitForPanel(menupopup, "popupshown");
  menupopup.openPopupAtScreen(200, 200);
  await popupshown;

  popupshown = waitForPanel(panel, "popupshown");
  panel.openPopup(document.getElementById("menuanchor"), "after_start");
  await popupshown;

  popuphidden = waitForPanel(panel, "popuphidden");
  let menupopuphidden = waitForPanel(menupopup, "popuphidden");
  menupopup.hidePopup();
  await popuphidden;
  await menupopuphidden;

  // The panel should no longer follow anchors.
  panel.setAttribute("followanchor", "false");

  let anchor4 = document.getElementById("anchor4");
  popupshown = waitForPanel(panel, "popupshown");
  panel.openPopup(anchor4, "after_start");
  await popupshown;

  let anchor4rect = anchor4.getBoundingClientRect();

  anchor4.style.marginLeft = "50px"
  await next();

  panelpos = getPanelPos(panel);
  isWithinHalfPixel(anchor4rect.left, panelpos.left - xarrowdiff, "no follow anchor x");
  isWithinHalfPixel(anchor4rect.bottom, panelpos.top, "no follow anchor y");

  popuphidden = waitForPanel(panel, "popuphidden");
  panel.hidePopup();
  await popuphidden;

  window.close();
  window.arguments[0].SimpleTest.finish();
}

function ok(condition, message) {
  window.arguments[0].SimpleTest.ok(condition, message);
}

function is(left, right, message) {
  window.arguments[0].SimpleTest.is(left, right, message);
}

window.arguments[0].SimpleTest.waitForFocus(runTests, window);

]]>
</script>

</window>