|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +import type {PlatformTestComponentBaseProps} from '../PlatformTest/RNTesterPlatformTestTypes'; |
| 12 | + |
| 13 | +import RNTesterPlatformTest from '../PlatformTest/RNTesterPlatformTest'; |
| 14 | +import RNTesterPlatformTestEventRecorder from '../PlatformTest/RNTesterPlatformTestEventRecorder'; |
| 15 | +import * as React from 'react'; |
| 16 | +import {useCallback, useState} from 'react'; |
| 17 | +import {View, StyleSheet} from 'react-native'; |
| 18 | + |
| 19 | +const styles = StyleSheet.create({ |
| 20 | + a: { |
| 21 | + width: 200, |
| 22 | + height: 120, |
| 23 | + backgroundColor: 'blue', |
| 24 | + flexDirection: 'row', |
| 25 | + justifyContent: 'center', |
| 26 | + alignItems: 'center', |
| 27 | + }, |
| 28 | + b: { |
| 29 | + height: 60, |
| 30 | + width: 100, |
| 31 | + backgroundColor: 'green', |
| 32 | + }, |
| 33 | + c: { |
| 34 | + height: 120, |
| 35 | + width: 200, |
| 36 | + backgroundColor: 'yellow', |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +const relevantEvents = [ |
| 41 | + 'pointerMove', |
| 42 | + 'pointerOver', |
| 43 | + 'pointerEnter', |
| 44 | + 'pointerOut', |
| 45 | + 'pointerLeave', |
| 46 | +]; |
| 47 | + |
| 48 | +const expected = [ |
| 49 | + {type: 'pointerOver', target: 'a'}, |
| 50 | + {type: 'pointerEnter', target: 'a'}, |
| 51 | + {type: 'pointerMove', target: 'a', optional: true}, |
| 52 | + {type: 'pointerOut', target: 'a'}, |
| 53 | + {type: 'pointerOver', target: 'b'}, |
| 54 | + {type: 'pointerEnter', target: 'b'}, |
| 55 | + {type: 'pointerMove', target: 'b', optional: true}, |
| 56 | + {type: 'pointerOut', target: 'b'}, |
| 57 | + {type: 'pointerLeave', target: 'b'}, |
| 58 | + {type: 'pointerOver', target: 'a'}, |
| 59 | + {type: 'pointerMove', target: 'a', optional: true}, |
| 60 | + {type: 'pointerOut', target: 'a'}, |
| 61 | + {type: 'pointerLeave', target: 'a'}, |
| 62 | + {type: 'pointerOver', target: 'c'}, |
| 63 | + {type: 'pointerEnter', target: 'c'}, |
| 64 | + {type: 'pointerMove', target: 'c', optional: true}, |
| 65 | + {type: 'pointerOut', target: 'c'}, |
| 66 | + {type: 'pointerLeave', target: 'c'}, |
| 67 | +]; |
| 68 | + |
| 69 | +const targetNames = ['a', 'b', 'c']; |
| 70 | + |
| 71 | +// adapted from https://github.com/web-platform-tests/wpt/blob/master/uievents/order-of-events/mouse-events/mousemove-between.html |
| 72 | +function PointerEventPointerMoveBetweenTestCase( |
| 73 | + props: PlatformTestComponentBaseProps, |
| 74 | +) { |
| 75 | + const {harness} = props; |
| 76 | + |
| 77 | + const pointermove_between = harness.useAsyncTest( |
| 78 | + 'Pointermove events between elements should fire in the correct order.', |
| 79 | + ); |
| 80 | + |
| 81 | + const [eventRecorder] = useState( |
| 82 | + () => |
| 83 | + new RNTesterPlatformTestEventRecorder({ |
| 84 | + mergeEventTypes: ['pointerMove'], |
| 85 | + relevantEvents, |
| 86 | + }), |
| 87 | + ); |
| 88 | + |
| 89 | + const eventHandler = useCallback( |
| 90 | + (event: PointerEvent, eventType: string, eventTarget: string) => { |
| 91 | + event.stopPropagation(); |
| 92 | + if (eventTarget === 'c' && eventType === 'pointerLeave') { |
| 93 | + pointermove_between.step(({assert_true}) => { |
| 94 | + assert_true( |
| 95 | + eventRecorder.checkRecords(expected), |
| 96 | + 'Expected events to occur in the correct order', |
| 97 | + ); |
| 98 | + }); |
| 99 | + pointermove_between.done(); |
| 100 | + } |
| 101 | + }, |
| 102 | + [eventRecorder, pointermove_between], |
| 103 | + ); |
| 104 | + |
| 105 | + const eventProps = eventRecorder.useRecorderTestEventHandlers( |
| 106 | + targetNames, |
| 107 | + eventHandler, |
| 108 | + ); |
| 109 | + |
| 110 | + return ( |
| 111 | + <> |
| 112 | + <View {...eventProps.a} style={styles.a}> |
| 113 | + <View {...eventProps.b} style={styles.b} /> |
| 114 | + </View> |
| 115 | + <View {...eventProps.c} style={styles.c} /> |
| 116 | + </> |
| 117 | + ); |
| 118 | +} |
| 119 | + |
| 120 | +type Props = $ReadOnly<{}>; |
| 121 | +export default function PointerEventPointerMoveBetween( |
| 122 | + props: Props, |
| 123 | +): React.MixedElement { |
| 124 | + return ( |
| 125 | + <RNTesterPlatformTest |
| 126 | + component={PointerEventPointerMoveBetweenTestCase} |
| 127 | + description="" |
| 128 | + instructions={[ |
| 129 | + 'Move your cursor over the blue element, later over the green one, later back to the blue one.', |
| 130 | + 'Move the mosue from the blue element to the yellow one, later to the white background.', |
| 131 | + ]} |
| 132 | + title="Pointermove handling between elements" |
| 133 | + /> |
| 134 | + ); |
| 135 | +} |
0 commit comments