From 647542c6c858daeed317d9b56d2d67f26d17ad33 Mon Sep 17 00:00:00 2001 From: Vincent Riemer Date: Tue, 9 Aug 2022 16:37:56 -0700 Subject: [PATCH] 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 --- ...PointerEventAttributesHoverablePointers.js | 1 - .../PointerEventAttributesNoHoverPointers.js | 247 ++++++++++++++++++ .../Experimental/W3CPointerEventsExample.js | 9 + 3 files changed, 256 insertions(+), 1 deletion(-) create mode 100644 packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesNoHoverPointers.js diff --git a/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesHoverablePointers.js b/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesHoverablePointers.js index 0e69dd2b376..4ae94049dbb 100644 --- a/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesHoverablePointers.js +++ b/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesHoverablePointers.js @@ -139,7 +139,6 @@ function PointerEventAttributesHoverablePointersTestCase( }, pointerTestName + "'s ClientX and ClientY attributes are correct."); } - // TODO: check_PointerEvent check_PointerEvent(harness, event, eventType, { testNamePrefix, }); diff --git a/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesNoHoverPointers.js b/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesNoHoverPointers.js new file mode 100644 index 00000000000..dccd815c6e5 --- /dev/null +++ b/packages/rn-tester/js/examples/Experimental/W3CPointerEventPlatformTests/PointerEventAttributesNoHoverPointers.js @@ -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 ( + + + {square1Visible && ( + + )} + + + {square2Visible && ( + + )} + + + ); +} + +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 ( + + ); +} diff --git a/packages/rn-tester/js/examples/Experimental/W3CPointerEventsExample.js b/packages/rn-tester/js/examples/Experimental/W3CPointerEventsExample.js index 2663e95fb22..3fbc4e4c0c8 100644 --- a/packages/rn-tester/js/examples/Experimental/W3CPointerEventsExample.js +++ b/packages/rn-tester/js/examples/Experimental/W3CPointerEventsExample.js @@ -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 ; }, }, + { + name: 'pointerevent_attributes_nohover_pointers', + description: '', + title: 'Pointer Events no-hover pointer attributes test', + render(): React.Node { + return ; + }, + }, { name: 'pointerevent_pointermove', description: '',