Add a pointer event attributes test variant for no-hover pointers

Summary:
Changelog: [RNTester][Internal] - Add a pointer event attributes test variant for no-hover pointers

This adds a slightly different version of the existing PointerEventAttributesHoverablePointers test but for pointers that cannot hover (such as touches)

Reviewed By: lunaleaps

Differential Revision: D38401888

fbshipit-source-id: 4ed782cc2157dcaa8f6c294ae3589843d020bc92
This commit is contained in:
Vincent Riemer authored and Facebook GitHub Bot committed 2022-08-09 16:37:56 -07:00
1 parent 477663cba8
commit 647542c6c8
3 files changed
+256 -1

No files matched your search

@@ -139,7 +139,6 @@ function PointerEventAttributesHoverablePointersTestCase(
}, pointerTestName + "'s ClientX and ClientY attributes are correct.");
}
// TODO: check_PointerEvent
check_PointerEvent(harness, event, eventType, {
testNamePrefix,
});
@@ -0,0 +1,247 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
import type {
Layout,
PointerEvent,
} from 'react-native/Libraries/Types/CoreEventTypes';
import type {PlatformTestComponentBaseProps} from '../PlatformTest/RNTesterPlatformTestTypes';
import * as React from 'react';
import {useCallback, useRef, useState} from 'react';
import {View, StyleSheet} from 'react-native';
import RNTesterPlatformTest from '../PlatformTest/RNTesterPlatformTest';
import {check_PointerEvent, useTestEventHandler} from './PointerEventSupport';
const eventList = [
'pointerOver',
'pointerEnter',
'pointerMove',
'pointerDown',
'pointerUp',
'pointerOut',
'pointerLeave',
];
function PointerEventAttributesNoHoverPointersTestCase(
props: PlatformTestComponentBaseProps,
) {
const {harness} = props;
const detected_pointertypesRef = useRef({});
const detected_eventTypesRef = useRef({});
const expectedPointerIdRef = useRef(NaN);
const [square1Visible, setSquare1Visible] = useState(true);
const [square2Visible, setSquare2Visible] = useState(false);
// Adapted from https://github.com/web-platform-tests/wpt/blob/8222ceeeebd4beab32f101ebf651a5dd7c5e8365/pointerevents/pointerevent_attributes_nohover_pointers.html#L25
const checkPointerEventAttributes = useCallback(
(
event: PointerEvent,
eventType: string,
targetLayout: Layout,
testNamePrefix: string,
expectedPointerType: string,
) => {
const detected_pointertypes = detected_pointertypesRef.current;
const detected_eventTypes = detected_eventTypesRef.current;
const expectedPointerId = expectedPointerIdRef.current;
if (detected_eventTypes[eventType]) {
return;
}
const expectedEventType =
eventList[Object.keys(detected_eventTypes).length].toLowerCase();
detected_eventTypes[eventType] = true;
const pointerTestName =
testNamePrefix + ' ' + expectedPointerType + ' ' + expectedEventType;
detected_pointertypes[event.nativeEvent.pointerType] = true;
harness.test(({assert_equals}) => {
assert_equals(
eventType,
expectedEventType,
'Event.type should be ' + expectedEventType,
);
}, pointerTestName + "'s type should be " + expectedEventType);
// Test button and buttons
harness.test(({assert_equals}) => {
assert_equals(event.nativeEvent.button, 0, 'Button attribute is 0');
}, pointerTestName + "'s button attribute is 0 when left mouse button is pressed.");
if (
eventType === 'pointerdown' ||
eventType === 'pointerover' ||
eventType === 'pointerenter'
) {
harness.test(({assert_equals}) => {
assert_equals(event.nativeEvent.buttons, 1, 'Buttons attribute is 1');
}, pointerTestName + "'s buttons attribute is 1 when left mouse button is pressed.");
} else {
harness.test(({assert_equals}) => {
assert_equals(event.nativeEvent.buttons, 0, 'Buttons attribute is 0');
}, pointerTestName + "'s buttons is 0 when mouse buttons are in released state.");
}
const left = targetLayout.x;
const top = targetLayout.y;
const right = targetLayout.x + targetLayout.width;
const bottom = targetLayout.y + targetLayout.height;
// Test clientX and clientY
harness.test(({assert_true}) => {
assert_true(
event.nativeEvent.clientX >= left &&
event.nativeEvent.clientX < right &&
event.nativeEvent.clientY >= top &&
event.nativeEvent.clientY < bottom,
'ClientX/Y should be in the boundaries of the box',
);
}, pointerTestName + "'s ClientX and ClientY attributes are correct.");
check_PointerEvent(harness, event, eventType, {
testNamePrefix,
});
// Test isPrimary value
harness.test(({assert_equals}) => {
assert_equals(
event.nativeEvent.isPrimary,
true,
'isPrimary should be true',
);
}, pointerTestName + '.isPrimary attribute is correct.');
// Test pointerId value
if (isNaN(expectedPointerId)) {
expectedPointerIdRef.current = event.nativeEvent.pointerId;
} else {
harness.test(({assert_equals}) => {
assert_equals(
event.nativeEvent.pointerId,
expectedPointerId,
'pointerId should remain the same for the same active pointer',
);
}, pointerTestName + '.pointerId should be the same as previous pointer events for this active pointer.');
}
},
[harness],
);
const square1Ref = useRef();
const square1Handlers = useTestEventHandler(eventList, (event, eventType) => {
if (!square1Visible) {
return;
}
const square1Elem = square1Ref.current;
if (square1Elem != null) {
square1Elem.measure((x, y, width, height, pageX, pageY) => {
checkPointerEventAttributes(
event,
eventType,
{x: pageX, y: pageY, width, height},
'',
'mouse',
);
if (
Object.keys(detected_eventTypesRef.current).length ===
eventList.length
) {
setSquare1Visible(false);
detected_eventTypesRef.current = {};
setSquare2Visible(true);
expectedPointerIdRef.current = NaN;
}
});
}
});
const square2Ref = useRef();
const square2Handlers = useTestEventHandler(eventList, (event, eventType) => {
const square2Elem = square2Ref.current;
if (square2Elem != null) {
square2Elem.measure((x, y, width, height, pageX, pageY) => {
checkPointerEventAttributes(
event,
eventType,
{x: pageX, y: pageY, width, height},
'Inner frame ',
'mouse',
);
if (
Object.keys(detected_eventTypesRef.current).length ===
eventList.length
) {
setSquare2Visible(false);
// TODO: Mark test as done
}
});
}
});
return (
<View style={styles.root}>
<View style={styles.squareContainer}>
{square1Visible && (
<View ref={square1Ref} style={styles.square1} {...square1Handlers} />
)}
</View>
<View style={styles.squareContainer}>
{square2Visible && (
<View ref={square2Ref} style={styles.square2} {...square2Handlers} />
)}
</View>
</View>
);
}
const styles = StyleSheet.create({
root: {
height: 150,
flexDirection: 'row',
alignItems: 'center',
},
square1: {
width: 40,
height: 40,
backgroundColor: 'black',
},
square2: {
width: 40,
height: 40,
backgroundColor: 'red',
},
squareContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
type Props = $ReadOnly<{}>;
export default function PointerEventAttributesNoHoverPointers(
props: Props,
): React.MixedElement {
return (
<RNTesterPlatformTest
component={PointerEventAttributesNoHoverPointersTestCase}
description="This test checks the properties of pointer events that do not support hover."
instructions={[
'Tap the black square.',
'Then move it off the black square so that it disappears.',
'When the red square appears tap on that as well.',
]}
title="Pointer Events no-hover pointer attributes test"
/>
);
}
@@ -17,6 +17,7 @@ import PointerEventAttributesHoverablePointers from './W3CPointerEventPlatformTe
import PointerEventPointerMove from './W3CPointerEventPlatformTests/PointerEventPointerMove';
import CompatibilityAnimatedPointerMove from './Compatibility/CompatibilityAnimatedPointerMove';
import PointerEventPrimaryTouchPointer from './W3CPointerEventPlatformTests/PointerEventPrimaryTouchPointer';
import PointerEventAttributesNoHoverPointers from './W3CPointerEventPlatformTests/PointerEventAttributesNoHoverPointers';
function EventfulView(props: {|
name: string,
@@ -240,6 +241,14 @@ export default {
return <PointerEventAttributesHoverablePointers />;
},
},
{
name: 'pointerevent_attributes_nohover_pointers',
description: '',
title: 'Pointer Events no-hover pointer attributes test',
render(): React.Node {
return <PointerEventAttributesNoHoverPointers />;
},
},
{
name: 'pointerevent_pointermove',
description: '',