summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/uievents/mouse/mouseenter-mouseleave-on-drag.html
blob: c36a1501c0bb44a8c88f50e4e7a1337aee7b6e8c (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for redundant mouseenter or mouseleave events</title>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="/resources/testdriver.js"></script>
  <script src="/resources/testdriver-actions.js"></script>
  <script src="/resources/testdriver-vendor.js"></script>
</head>
<style>
#outer {
  background: grey;
  position: absolute;
  left: 100px;
  top: 100px;
  width: 100px;
  height: 100px;
}
#inner {
  background: red;
  position: absolute;
  left: 30px;
  top: 30px;
  width: 40px;
  height: 40px;
}
</style>

<body>
  <!-- Verifies that dragging mouse in/out of an element doesn't fire redundant
       mouseenter or mouseleave events (crbug.com/356090 & crbug.com/470258) -->
  <div id="outer">
    <div id="inner"></div>
  </div>
</body>
<script>
let eventLog = [];
let nextUncheckedEventIndex = 0;

// Ensure match to the next sequence of events in the event log.
function assert_next_events(target, expectedEventNames, message) {
  for (let i = 0; i < expectedEventNames.length; i++) {
    assert_true(nextUncheckedEventIndex < eventLog.length,
                `${message}: empty event queue`);
    const observed = eventLog[nextUncheckedEventIndex++];
    const expected = `${expectedEventNames[i]}@${target.id}`;
    assert_equals(observed, expected,`${message}: Event  mismatch`);
  }
}

// After validating the expected events, all entries in the event map
// must be false or we have recorded an unexpected event.
function assert_empty_event_queue(message) {
  const uncheckedEvents = eventLog.length - nextUncheckedEventIndex;
  assert_equals(uncheckedEvents, 0,
                `${message}: Unexpected events ` +
                `${eventLog.slice(-uncheckedEvents).join(", ")}`);
}

function addEventListeners(test) {
  const eventTypes = [
    'mousedown',
    'mouseenter',
    'mouseleave',
    'mousemove',
    'mouseout',
    'mouseover',
    'mouseup'
  ];
  ['inner', 'outer'].forEach(id => {
    const element = document.getElementById(id);
    eventTypes.forEach(eventType => {
      const listener = (e) => {
        if (e.eventPhase == Event.AT_TARGET) {
          eventLog.push(`${eventType}@${id}`);
        }
      };
      element.addEventListener(eventType, listener);
      test.add_cleanup(() => {
        element.removeEventListener(eventType, listener);
      });
    })
  });
}

// At the end of each action sequence we move the mouse over the root element.
// Once this event is detected, all other upstream events must be logged and
// we can proceed with the checks.
async function mousemoveOverRootElement() {
  return new Promise(resolve => {
    const listener = (e) => {
      if (e.eventPhase == Event.AT_TARGET) {
        document.documentElement.removeEventListener('mousemove', listener);
        resolve();
      }
    };
    document.documentElement.addEventListener('mousemove', listener);
  });
}

window.onload = async () => {
  const outer = document.getElementById('outer');
  const inner = document.getElementById('inner');
  const leftOuter = 100;
  const rightOuter = 200;
  const leftInner = 130;
  const rightInner = 170;
  const centerY = 150;

  promise_test(async t => {
    addEventListeners(t);
    const completionPromise = mousemoveOverRootElement();
    const actions =new test_driver.Actions();
    actions.pointerMove(leftOuter + 10, centerY)
           .pointerDown({button: actions.ButtonType.LEFT})
           .pointerMove(rightOuter - 10, centerY)
           .pointerUp({button: actions.ButtonType.LEFT})
           .pointerMove(0, 0)
           .send();
    await actions;
    await completionPromise;

    assert_next_events(outer, ['mouseover', 'mouseenter', 'mousemove'],
                       'Move over outer element');
    assert_next_events(outer, ['mousedown', 'mousemove', 'mouseup'],
                       'Drag across outer element');
    assert_next_events(outer, ['mouseout', 'mouseleave'],
                       'Move to origin');
    assert_empty_event_queue('Drag across outer element');
  }, 'Test dragging across inner div');

  promise_test(async t => {
    addEventListeners(t);
    const completionPromise = mousemoveOverRootElement();
    const actions =new test_driver.Actions();
    actions.pointerMove(leftOuter + 10, centerY)
           .pointerDown({button: actions.ButtonType.LEFT})
           .pointerMove(leftInner + 10, centerY)
           .pointerUp({button: actions.ButtonType.LEFT})
           .pointerMove(0, 0)
           .send();
    await actions;
    await completionPromise;

    assert_next_events(outer, ['mouseover', 'mouseenter', 'mousemove'],
                       'Move over outer element');
    assert_next_events(outer, ['mousedown', 'mouseout'],
                       'Initiate drag');
    assert_next_events(inner,
                       ['mouseover', 'mouseenter', 'mousemove', 'mouseup'],
                       'Drag into inner element');
    assert_next_events(inner, ['mouseout', 'mouseleave'],
                       'Move to origin');
    assert_next_events(outer, [ 'mouseleave'],
                       'Move to origin');
    assert_empty_event_queue('Drag into inner element');
  }, 'Test dragging into inner div');

  promise_test(async t => {
    addEventListeners(t);
    const completionPromise = mousemoveOverRootElement();
    const actions =new test_driver.Actions();
    actions.pointerMove(leftInner + 10, centerY)
           .pointerDown({button: actions.ButtonType.LEFT})
           .pointerMove(rightInner + 10, centerY)
           .pointerUp({button: actions.ButtonType.LEFT})
           .pointerMove(0, 0)
           .send();
    await actions;
    await completionPromise;

    assert_next_events(inner, ['mouseover'], 'Move over inner element');
    assert_next_events(outer, ['mouseenter'], 'Enter outer');
    assert_next_events(inner, ['mouseenter', 'mousemove'],
                              'Move across inner element');
    assert_next_events(inner, ['mousedown', 'mouseout', 'mouseleave'],
                              'Drag out of inner');
    assert_next_events(outer, ['mouseover', 'mousemove', 'mouseup'],
                               'Drag into outer');
    assert_next_events(outer, ['mouseout', 'mouseleave'],
                               'Move to origin');
    assert_empty_event_queue('Drag into inner element');
  }, 'Test dragging out of inner div');
};
</script>
</html>