Skip to content

Commit 3c504a7

Browse files
dalvesDavid Alves
andauthored
Wait for vertical scroll endpoints before visual captures (#60417)
Summary: The vertical scroll-to visual test can capture the previous scroll position instead of the requested one. 1. A button requests an animated scroll. 2. `waitForAnimationToEnd` can observe unchanged frames before that scroll starts and return immediately. 3. The screenshot is then taken at the old endpoint, without checking whether the scroll reached its destination. What changes The RNTester example exposes its current endpoint through a nonvisual `testID` on its existing outer container. The marker starts at top, matching the initial zero offset. Requests for a different endpoint clear it to pending; requests at an already-acknowledged endpoint preserve it, since a no-op scroll need not emit another event. Native scroll offsets update the marker, including the final momentum event when intermediate scroll events are throttled. The flow waits for the requested endpoint before its existing animation-settling check and screenshot. The list content, styles, animated scroll commands, screenshot IDs, and crops are unchanged. The horizontal and sticky-header flows are unchanged. Changelog: [Internal] Differential Revision: D134586364 Co-authored-by: David Alves <da@meta.com>
1 parent fa1dc61 commit 3c504a7

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
12+
import type {ScrollEvent} from 'react-native';
1213

1314
import RNTesterText from '../../components/RNTesterText';
1415
import ScrollViewPressableStickyHeaderExample from './ScrollViewPressableStickyHeaderExample';
@@ -238,9 +239,26 @@ function ContentOffsetList(): React.Node {
238239
function ScrollViewScrollToExample(): React.Node {
239240
let _scrollView: ?React.ElementRef<typeof ScrollView>;
240241
const [scrolledToTop, setScrolledToTop] = useState(false);
242+
const [scrollPosition, setScrollPosition] = useState<
243+
'pending' | 'top' | 'bottom',
244+
>('top');
245+
const updateScrollPosition = (event: ScrollEvent) => {
246+
const {contentOffset, contentSize, layoutMeasurement} = event.nativeEvent;
247+
// Native offsets can round by a fraction of a logical pixel.
248+
if (contentOffset.y <= 1) {
249+
setScrollPosition('top');
250+
} else if (
251+
contentOffset.y >=
252+
contentSize.height - layoutMeasurement.height - 1
253+
) {
254+
setScrollPosition('bottom');
255+
} else {
256+
setScrollPosition('pending');
257+
}
258+
};
241259
const textStyle = {color: 'blue', marginBottom: 10, textAlign: 'center'};
242260
return (
243-
<View>
261+
<View testID={`scroll_position_${scrollPosition}`}>
244262
{scrolledToTop ? (
245263
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
246264
* https://fburl.com/workplace/6291gfvu */
@@ -253,10 +271,12 @@ function ScrollViewScrollToExample(): React.Node {
253271
}}
254272
automaticallyAdjustContentInsets={false}
255273
nestedScrollEnabled
256-
onScroll={() => {
274+
onScroll={event => {
257275
console.log('onScroll!');
258276
setScrolledToTop(false);
277+
updateScrollPosition(event);
259278
}}
279+
onMomentumScrollEnd={updateScrollPosition}
260280
onScrollToTop={() => {
261281
setScrolledToTop(true);
262282
}}
@@ -268,13 +288,19 @@ function ScrollViewScrollToExample(): React.Node {
268288
<Button
269289
label="Scroll to top"
270290
onPress={() => {
291+
setScrollPosition(position =>
292+
position === 'top' ? position : 'pending',
293+
);
271294
nullthrows<$FlowFixMe>(_scrollView).scrollTo({y: 0});
272295
}}
273296
testID="scroll_to_top_button"
274297
/>
275298
<Button
276299
label="Scroll to bottom"
277300
onPress={() => {
301+
setScrollPosition(position =>
302+
position === 'bottom' ? position : 'pending',
303+
);
278304
nullthrows<$FlowFixMe>(_scrollView).scrollToEnd({animated: true});
279305
}}
280306
testID="scroll_to_bottom_button"

‎packages/rn-tester/visual-tests/flows/scroll-view.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ appId: ${APP_ID}
1010
timeout: 15000
1111
- tapOn:
1212
id: 'scroll_to_bottom_button'
13+
- extendedWaitUntil:
14+
visible:
15+
id: 'scroll_position_bottom'
16+
timeout: 5000
1317
- waitForAnimationToEnd:
1418
timeout: 5000
1519
- takeScreenshot:
@@ -18,6 +22,10 @@ appId: ${APP_ID}
1822
id: 'scroll_vertical'
1923
- tapOn:
2024
id: 'scroll_to_top_button'
25+
- extendedWaitUntil:
26+
visible:
27+
id: 'scroll_position_top'
28+
timeout: 5000
2129
- waitForAnimationToEnd:
2230
timeout: 5000
2331
- takeScreenshot:

0 commit comments

Comments
 (0)