summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/pointerevents/pointerevent_touch-adjustment_click_target.html
blob: 46d5ac1793be0445bd2968c50ebf949e1ba2202f (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Touch-generated events should have the same target</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<p>Touch letter 'O' below to run the test. If a "PASS" result appears the test passes, otherwise it fails</p>
<p><a href="#" id="link">Link</a> <span id="target">O</span></p>
<div id="log"></div>
<script>
let input_gesture;
const target = document.getElementById('target');
const xPosition = Math.ceil(target.offsetLeft + 2);
const yPosition = Math.ceil(target.offsetTop + 2);

function inject_input() {
  let actions = new test_driver.Actions();
  return actions
        .addPointer("touchPointer", "touch")
        .setPointer("touchPointer")
        .pointerMove(xPosition, yPosition)
        .pointerDown()
        .pointerUp()
        .send();
}


addEventListener('load', () => {
  input_gesture = inject_input();
});

async_test(t => {
    const link = document.getElementById('link');
    const expectedEventLog = ['pointerdown-link', 'touchstart-link', 'pointerup-link', 'touchend-link', 'click-link'];
    const eventLogRecorder = [];

    const eventNames = ['touchstart', 'touchmove', 'touchend', 'pointerdown', 'pointermove', 'pointerup', 'click'];
    for (eventName of eventNames) {
        document.addEventListener(eventName, t.step_func(event => {
            // TouchEvent and PointerEvent should have the same un-adjusted coordinates.
            // click event should have coordinates adjusted to link element.
            const eventClientX = event.clientX || (event.touches.length > 0 ? event.touches[0].clientX : 0);
            const eventClientY = event.clientY || (event.touches.length > 0 ? event.touches[0].clientY : 0);

            if (event.type === 'click') {
                assert_equals(document.elementFromPoint(eventClientX, eventClientY), link,
                    'click should have clientX/Y adjusted to link.');
            } else if (event.type != 'touchend') {
                assert_equals(eventClientX, xPosition,
                    `${event.type} should have un-adjusted x coordinates.`);
                assert_equals(eventClientY, yPosition,
                    `${event.type} should have un-adjusted y coordinates.`);
            }

            // All events should have target adjusted to link.
            const targetName = event.target.id || event.target.nodeName || '[null]';
            eventLogRecorder.push(`${event.type}-${targetName}`);
            if (event.type === 'click') {
                assert_array_equals(eventLogRecorder, expectedEventLog);
                input_gesture.then(() => { t.done(); });
            }
        }));
    }
});
</script>