summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_mouse_enterleave_iframe.html
blob: 160174865f853f7eb3f895d0e7555bb234371cc6 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!doctype html>
<meta charset="utf-8">
<title>Test mouseenter and mouseleave for iframe.</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<script src="/tests/SimpleTest/paint_listener.js"></script>
<script src="/tests/gfx/layers/apz/test/mochitest/apz_test_utils.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<style>
#start {
  width: 300px;
  height: 30px;
}

#target, #target2 {
  width: 150px;
  height: 150px;
  background-color: #fcc;
  display: inline-block;
}

#frame, #frame2 {
  height: 100%;
  width: 100%;
}

#reflow, #div {
  width: 300px;
  height: 10px;
  background-color: lightgreen;
}
</style>
<div id="start">Start from here!!</div>
<div id="div"></div>
<div id="target">
  <iframe id="frame" frameborder="0" scrolling="no" src="http://example.com/tests/dom/events/test/file_mouse_enterleave.html"></iframe>
</div>
<div id="target2">
  <iframe id="frame2" frameborder="0" scrolling="no" src="http://example.com/tests/dom/events/test/file_mouse_enterleave.html"></iframe>
</div>
<div id="reflow"></div>
<script>

function reflow() {
  let div = document.getElementById("reflow");
  div.style.display = "none";
  div.getBoundingClientRect();
  div.style.display = "block";
  div.getBoundingClientRect();
}

function waitForMessage(aRemoteTarget, aEventType, aTargetName) {
  return new Promise(function(aResolve, aReject) {
    window.addEventListener("message", function listener(aEvent) {
      if (aEvent.source != aRemoteTarget.contentWindow) {
        return;
      }

      if (aEvent.data.eventType !== aEventType) {
        window.removeEventListener("message", listener);
        ok(false, `receive unexpected message ${JSON.stringify(aEvent.data)}`);
        aReject(new Error(`receive unexpected message ${JSON.stringify(aEvent.data)}`));
        return;
      }

      if (aEvent.data.targetName !== aTargetName) {
        return;
      }

      ok(true, `receive message ${JSON.stringify(aEvent.data)}`);
      // Trigger a reflow which will generate synthesized mouse move event.
      aRemoteTarget.contentWindow.postMessage("reflow", "*");
      // Now wait a bit to see if there is any unexpected message fired.
      setTimeout(function() {
        window.removeEventListener("message", listener);
        aResolve();
      }, 0);
    });
  });
}

function waitForLeaveEvent(aTarget) {
  return new Promise(function(aResolve) {
    aTarget.addEventListener("mouseleave", function(aEvent) {
      ok(true, `receive ${aEvent.type}`);
      aResolve();
    }, { once: true });
  });
}

function waitForEnterLeaveEvents(aEnterTarget, aLeaveTarget) {
  let expectedEvents = [{target: aEnterTarget, eventName: "mouseenter"}];
  if (aLeaveTarget) {
    expectedEvents.push({target: aLeaveTarget, eventName: "mouseleave"})
  }

  return new Promise(function(aResolve, aReject) {
    function cleanup() {
      aEnterTarget.removeEventListener("mouseenter", listener);
      aEnterTarget.removeEventListener("mouseleave", unexpectedEvent);
      if (aLeaveTarget) {
        aLeaveTarget.removeEventListener("mouseenter", unexpectedEvent);
        aLeaveTarget.removeEventListener("mouseleave", listener);
      }
    }

    function unexpectedEvent(aEvent) {
      cleanup();
      ok(false, `receive unexpected ${aEvent.type}`);
      aReject(new Error(`receive unexpected ${aEvent.type}`));
    }

    async function listener(aEvent) {
      if (expectedEvents.length <= 0) {
        unexpectedEvent(aEvent);
        return;
      }

      let expectedEvent = expectedEvents.pop();
      if (expectedEvent.target == aEvent.target &&
          expectedEvent.eventName == aEvent.type) {
        ok(true, `receive ${aEvent.type}`);
      } else {
        unexpectedEvent(aEvent);
        return;
      }

      if (!expectedEvents.length) {
        // Trigger a reflow which will generate synthesized mouse move event.
        reflow();
        // Now wait a bit to see if there is any unexpected event fired.
        setTimeout(function() {
          cleanup();
          aResolve();
        }, 0);
      }
    }

    aEnterTarget.addEventListener("mouseenter", listener);
    aEnterTarget.addEventListener("mouseleave", unexpectedEvent);
    if (aLeaveTarget) {
      aLeaveTarget.addEventListener("mouseenter", unexpectedEvent);
      aLeaveTarget.addEventListener("mouseleave", listener);
    }
  });
}

function moveMouseToInitialPosition() {
  info("Mouse moves to initial position");
  return promiseNativeMouseEvent({
    type: "mousemove",
    target: document.getElementById("start"),
    atCenter: true,
  });
}

add_setup(async function() {
  // Wait for focus before starting tests.
  await SimpleTest.promiseFocus();

  // Wait for apz getting stable.
  await waitUntilApzStable();

  // Move mouse to initial position.
  await moveMouseToInitialPosition();
});

add_task(async function testMouseEnterLeave() {
  let div = document.getElementById("div");
  let target = document.getElementById("target");
  let iframe = document.getElementById("frame");

  info("Mouse moves to the div above iframe");
  let promise = waitForEnterLeaveEvents(div);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target: div,
    atCenter: true,
  });
  await promise;

  info("Mouse moves into iframe");
  promise = Promise.all([waitForEnterLeaveEvents(target, div),
                         waitForMessage(iframe, "mouseenter", "div"),
                         waitForMessage(iframe, "mouseenter", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target,
    atCenter: true,
  });
  await promise;

  info("Mouse moves out from iframe to the div above iframe");
  promise = Promise.all([waitForEnterLeaveEvents(div, target),
                         waitForMessage(iframe, "mouseleave", "div"),
                         waitForMessage(iframe, "mouseleave", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target: div,
    atCenter: true,
  });
  await promise;

  // Move mouse back to initial position. This is to prevent unexpected
  // mouseleave event in initial steps for test-verify which runs same test
  // multiple times. 
  await moveMouseToInitialPosition();
});

add_task(async function testMouseEnterLeaveBetweenIframe() {
  let target = document.getElementById("target");
  let iframe = document.getElementById("frame");

  info("Mouse moves into the first iframe");
  let promise = Promise.all([waitForEnterLeaveEvents(target),
                             waitForMessage(iframe, "mouseenter", "div"),
                             waitForMessage(iframe, "mouseenter", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target,
    atCenter: true,
  });
  await promise;

  let target2 = document.getElementById("target2");
  let iframe2 = document.getElementById("frame2");

  info("Mouse moves out from the first iframe to the second iframe");
  promise = Promise.all([waitForEnterLeaveEvents(target2, target),
                         waitForMessage(iframe, "mouseleave", "div"),
                         waitForMessage(iframe, "mouseleave", "html"),
                         waitForMessage(iframe2, "mouseenter", "div"),
                         waitForMessage(iframe2, "mouseenter", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target: target2,
    atCenter: true,
  })
  await promise;

  info("Mouse moves out from the second iframe to the first iframe");
  promise = Promise.all([waitForEnterLeaveEvents(target, target2),
                         waitForMessage(iframe2, "mouseleave", "div"),
                         waitForMessage(iframe2, "mouseleave", "html"),
                         waitForMessage(iframe, "mouseenter", "div"),
                         waitForMessage(iframe, "mouseenter", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target,
    atCenter: true,
  });
  await promise;

  // Move mouse back to initial position.
  await Promise.all([waitForLeaveEvent(target),
                     waitForMessage(iframe, "mouseleave", "div"),
                     waitForMessage(iframe, "mouseleave", "html"),
                     moveMouseToInitialPosition()]);
});

add_task(async function testMouseEnterLeaveSwitchWindow() {
  let target = document.getElementById("target");
  let iframe = document.getElementById("frame");

  info("Mouse moves into iframe");
  let promise = Promise.all([waitForEnterLeaveEvents(target),
                             waitForMessage(iframe, "mouseenter", "div"),
                             waitForMessage(iframe, "mouseenter", "html")]);
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target,
    atCenter: true,
  });
  await promise;

  info("Open and switch to new window");
  promise = Promise.all([waitForLeaveEvent(target),
                         waitForMessage(iframe, "mouseleave", "div"),
                         waitForMessage(iframe, "mouseleave", "html")]);
  let win = window.open("http://example.com/tests/dom/events/test/file_mouse_enterleave.html");
  // Trigger a reflow which will generate synthesized mouse move event.
  win.postMessage("reflow", "*");
  await promise;

  info("Switch back to test window");
  promise = Promise.all([waitForEnterLeaveEvents(target),
                         waitForMessage(iframe, "mouseenter", "div"),
                         waitForMessage(iframe, "mouseenter", "html")]);
  win.close();
  // Trigger a reflow which will generate synthesized mouse move event.
  reflow();
  // Wait for apz getting stable.
  await waitUntilApzStable();
  synthesizeNativeMouseEvent({
    type: "mousemove",
    target,
    atCenter: true,
  });
  await promise;

  // Move mouse back to initial position.
  await Promise.all([waitForLeaveEvent(target),
                     moveMouseToInitialPosition()]);
});
</script>