summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/pointerevents/pointerevent_capture_suppressing_mouse.html
blob: 0d7756335d1d9e7a5baf0a2f213963124d9850ef (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
<!doctype html>
<html>
  <head>
    <title>Set/Release capture</title>
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" type="text/css" href="pointerevent_styles.css">
    <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>
    <!-- Additional helper script for common checks across event types -->
    <script type="text/javascript" src="pointerevent_support.js"></script>
  </head>
  <body>
    <div class="spacer"></div>
    <div id="target0"></div>
    <div class="spacer"></div>
    <div id="target1"></div>
    <div class="spacer"></div>
    <input type="button" id="captureButton" value="Set Capture">
  </body>
  <script type='text/javascript'>
    window.onload = () => {
      let eventLog = [];
      let nextUncheckedEventIndex = 0;
      const target0 = document.getElementById('target0');
      const target1 = document.getElementById('target1');
      const captureButton = document.getElementById('captureButton');

      function eventLabel(target, eventName) {
        return `${eventName}@${target.id}`;
      }

      function recordEvent(target, eventName) {
        eventLog.push(eventLabel(target, eventName));
      }

      // 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 = eventLabel(target, expectedEventNames[i]);
          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(", ")}`);
      }

      // Adds listeners for all element-pointerevent combinations. Each listener
      // records the event for validation. the pointerdown event on the button
      // triggers an extra step of triggering pointer capture.
      function addEventListeners(t) {
        // Adds a single event that is removed at the conclusion of the test.
        const addListener = (target, eventName, fn) => {
          const callback = (e) => {
            recordEvent(target, eventName);
            // Additional event handling is optional.
            if (fn)
              fn(e);
          };
          target.addEventListener(eventName, callback);
          t.add_cleanup(() => {
             target.removeEventListener(eventName, callback);
          });
        };
        [target0, target1, captureButton].forEach(el => {
          ['gotpointercapture', 'lostpointercapture', 'pointerenter',
           'pointerleave', 'pointermove', 'pointerout',
           'pointerover'].forEach(eventName => {
             addListener(el, eventName);
           });
        });
        addListener(captureButton, 'pointerdown', (e) => {
          target0.setPointerCapture(e.pointerId);
        });
        t.add_cleanup(() => {
          eventLog = [];
          nextUncheckedEventIndex = 0;
        });
      }

      // Trigger and wait for a pointer move.  The wait is to ensure there is
      // no coalescence of pointer move events.
      async function moveTo(x, y, target) {
        const movePromise = getEvent('pointermove', target);
        let actions = new test_driver.Actions()
                      .pointerMove(x, y, { origin: target })
                      .send();
        await actions;
        await movePromise;
      }

      promise_test(async t => {
        // Reset pointer position.
        await moveTo(0, 0, document.body);
        addEventListeners(t);
        // Move to the first target.
        await moveTo(0, 0, target0);
        assert_next_events(target0,["pointerover", "pointerenter", "pointermove"],
                           'Move to first target');
        assert_empty_event_queue('Check after first move');

        // Move to the second taret.
        await moveTo(0, 0, target1);
        assert_next_events(target0, ['pointerout', 'pointerleave'],
                           'Exit first target');
        assert_next_events(target1,
                      ["pointerover", "pointerenter", "pointermove"],
                      'Move to second target');
        assert_empty_event_queue('Check after second move');

        // Move to the capture button.
        await moveTo(0, 0, captureButton);
        assert_next_events(target1, ['pointerout', 'pointerleave'],
                           'Exit second target');
        assert_next_events(
            captureButton, ["pointerover", "pointerenter", "pointermove"],
            'Move to button');
        assert_empty_event_queue('Check after third move');
      }, 'Validate pointer events track pointer movement without pointer '
         + 'capture.');

      async function runCaptureAndHoverTargetActionSequence(t, hoverTarget) {
        const pointerUpPromise = getEvent('pointerup', document);
        const actionsPromise =
            new test_driver.Actions()
                // Start outside capture button.
                .pointerMove(0, 0)
                .pointerDown()
                .pointerUp()
                // Move to the capture button
                .pointerMove(0, 0, {origin: captureButton})
                // Trigger pointer capture
                .pointerDown()
                // Hover over the target element
                .pointerMove(10, 0, {origin: hoverTarget})
                // Release capture
                .pointerUp()
                .send();
        await actionsPromise;
        await pointerUpPromise;
      }

      promise_test(async t => {
        // Reset pointer position.
        await moveTo(0, 0, document.body);
        addEventListeners(t);

        // On entry for this sub-test, the pointer is at the document origin.
        await runCaptureAndHoverTargetActionSequence(t, captureButton);
        assert_next_events(
            captureButton,
            ['pointerover', 'pointerenter', 'pointermove'],
            'Move to button from origin');
        assert_next_events(
            captureButton,
            ['pointerdown', 'pointerout', 'pointerleave'],
            'Pointer down on button to trigger capture');
        assert_next_events(
            target0,
            ['pointerover', 'pointerenter', 'gotpointercapture', 'pointermove'],
            'Capture triggered while hovering over button');
        assert_next_events(
            target0,
            ['lostpointercapture', 'pointerout', 'pointerleave'],
            'Lose pointer capture while hovering over button');
        // Post pointer capture.
        assert_next_events(
            captureButton,
            ['pointerover', 'pointerenter'],
            'Post capture while hovering over button');
        assert_empty_event_queue('Check after button hover');

        // On entry for this sub-test, the pointer is over the button.
        await runCaptureAndHoverTargetActionSequence(t, target0);
        assert_next_events(
            captureButton,
            ['pointerout', 'pointerleave'],
            'Move from button to document origin');
        assert_next_events(
            captureButton,
            ['pointerover', 'pointerenter', 'pointermove'],
            'Move from document origin to button');
        assert_next_events(
            captureButton,
            [ 'pointerdown', 'pointerout', 'pointerleave'],
            'Pointer down on button to trigger capture');
        assert_next_events(
            target0,
            ['pointerover', 'pointerenter', 'gotpointercapture', 'pointermove'],
            'Capture triggered while hovering over capture target');
        assert_next_events(
            target0,
            ['lostpointercapture'],
            'Lose pointer capture while hovering over capture target');
        assert_empty_event_queue('Check after hover on capture target');


        // On entry for this sub-test, the pointer is over the capture target.
        await runCaptureAndHoverTargetActionSequence(t, target1);
        assert_next_events(
            target0,
            ['pointerout', 'pointerleave'],
            'Move from capture target to button');
        assert_next_events(
            captureButton,
            ['pointerover', 'pointerenter', 'pointermove'],
            'Move from capture target to button');
        assert_next_events(
            captureButton,
            [ 'pointerdown', 'pointerout', 'pointerleave'],
            'Pointer down on button to trigger capture');
        assert_next_events(
            target0,
            ['pointerover', 'pointerenter', 'gotpointercapture', 'pointermove'],
            'Capture triggered while hovering over non-capture target');
        assert_next_events(
            target0,
            ['lostpointercapture', 'pointerout', 'pointerleave'],
            'Lose pointer capture while hovering over non-capture target');
        assert_next_events(
            target1,
            ["pointerover", "pointerenter"],
            'Post capture while hovering over non-capture target');
        assert_empty_event_queue('Check after hover on non-capture target ');
      }, 'Test pointer capture.');
    };
  </script>
</html>