-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix maintainVisibleContentPosition calculations and add tests + docs [fixes #25239] #57294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aarononeal
wants to merge
14
commits into
react:main
Choose a base branch
from
aarononeal:fix/mvcp-corrections-and-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
338385f
test(rn-tester): add maestro tests and refactor maintainVisibleConten…
aarononeal 6ddef2d
fix(virtualized-lists): clear cell metrics on orientation change and …
aarononeal d7feaee
fix(ios): MVCP correction fails when anchor view is deleted or recycled
aarononeal 7493059
fix(android): MVCP scroll position not updating due to event throttle
aarononeal f0f51e9
docs(virtualized-lists): document MVCP architecture, history, and tes…
aarononeal 6af58c8
fix: dispatch unthrottled scroll event on fling animation cancel
aarononeal b8ad2ec
fix: remove premature scroll events from MaintainVisibleScrollPositio…
aarononeal a1e4e81
docs(virtualized-lists): fold MVCP history into DESIGN.md as design r…
aarononeal 03b52d7
docs: colocate MVCP test coverage comments into test source files
aarononeal 4488b23
fix(lint): address lint warnings
aarononeal 1165571
style(rn-tester): improve button text styling
aarononeal 50c46f3
test(lists): sanitize snapshots by removing _owner
aarononeal 91dd3a4
test(ScrollView): cleanup and type improvements
aarononeal 2ff07e1
fix(metrics): revise divide-by-zero branch in aggregator
aarononeal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
1,881 changes: 1,881 additions & 0 deletions
1,881
...raries/Components/ScrollView/__tests__/ScrollView-maintainVisibleContentPosition-itest.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
223 changes: 223 additions & 0 deletions
223
...d/src/test/java/com/facebook/react/views/scroll/ReactScrollViewHelperFlingAnimatorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package com.facebook.react.views.scroll | ||
|
|
||
| import android.animation.ValueAnimator | ||
| import android.content.Context | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import com.facebook.react.bridge.ReactContext | ||
| import com.facebook.react.bridge.ReactTestHelper | ||
| import com.facebook.react.uimanager.StateWrapper | ||
| import com.facebook.react.uimanager.UIManagerHelper | ||
| import com.facebook.react.uimanager.events.Event | ||
| import com.facebook.react.uimanager.events.EventDispatcher | ||
| import com.facebook.testutils.shadows.ShadowNativeLoader | ||
| import com.facebook.testutils.shadows.ShadowSoLoader | ||
| import com.facebook.react.views.scroll.ReactScrollViewHelper.HasFlingAnimator | ||
| import com.facebook.react.views.scroll.ReactScrollViewHelper.HasScrollEventThrottle | ||
| import com.facebook.react.views.scroll.ReactScrollViewHelper.HasScrollState | ||
| import com.facebook.react.views.scroll.ReactScrollViewHelper.HasStateWrapper | ||
| import org.junit.After | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.MockedStatic | ||
| import org.mockito.Mockito.`when` | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.Mockito.mockStatic | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.argumentCaptor | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.verify | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
| import org.robolectric.annotation.LooperMode | ||
| import org.robolectric.RuntimeEnvironment | ||
|
|
||
| /** | ||
| * Tests for [ReactScrollViewHelper.registerFlingAnimator], verifying that unthrottled scroll | ||
| * events are dispatched when fling animations end or are cancelled. | ||
| * | ||
| * These events ensure JS state is updated with the final scroll position after programmatic | ||
| * scroll animations complete, preventing stale scroll position data. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @LooperMode(LooperMode.Mode.PAUSED) | ||
| @Config(shadows = [ShadowSoLoader::class, ShadowNativeLoader::class]) | ||
| class ReactScrollViewHelperFlingAnimatorTest { | ||
|
|
||
| private lateinit var mockScrollView: MockScrollView | ||
| private lateinit var mockAnimator: ValueAnimator | ||
| private lateinit var mockChild: View | ||
| private lateinit var mockEventDispatcher: EventDispatcher | ||
| private lateinit var mockContext: ReactContext | ||
| private lateinit var uiManagerHelperMock: MockedStatic<UIManagerHelper> | ||
| private val scrollListener = TestScrollListener() | ||
|
|
||
| @Before | ||
| @Suppress("UNCHECKED_CAST") | ||
| fun setUp() { | ||
| mockChild = mock() | ||
| mockAnimator = ValueAnimator() | ||
| mockEventDispatcher = mock() | ||
| mockContext = ReactTestHelper.createCatalystContextForTest() | ||
|
|
||
| mockScrollView = mock<MockScrollView>() | ||
|
|
||
| `when`(mockScrollView.context).thenReturn(mockContext) | ||
| `when`(mockScrollView.id).thenReturn(42) | ||
| `when`(mockScrollView.scrollX).thenReturn(0) | ||
| `when`(mockScrollView.scrollY).thenReturn(0) | ||
| `when`(mockScrollView.width).thenReturn(500) | ||
| `when`(mockScrollView.height).thenReturn(800) | ||
| `when`(mockScrollView.paddingStart).thenReturn(0) | ||
| `when`(mockScrollView.paddingEnd).thenReturn(0) | ||
| `when`(mockScrollView.paddingTop).thenReturn(0) | ||
| `when`(mockScrollView.paddingBottom).thenReturn(0) | ||
| `when`(mockScrollView.scrollEventThrottle).thenReturn(0) | ||
| `when`(mockScrollView.lastScrollDispatchTime).thenReturn(0L) | ||
| `when`(mockScrollView.stateWrapper).thenReturn(null) | ||
| `when`(mockScrollView.reactScrollViewScrollState).thenReturn( | ||
| ReactScrollViewHelper.ReactScrollViewScrollState() | ||
| ) | ||
| `when`(mockScrollView.getChildAt(0)).thenReturn(mockChild) | ||
| `when`(mockChild.width).thenReturn(1000) | ||
| `when`(mockChild.height).thenReturn(2000) | ||
| `when`(mockScrollView.getFlingAnimator()).thenReturn(mockAnimator) | ||
|
|
||
| uiManagerHelperMock = mockStatic(UIManagerHelper::class.java) | ||
| uiManagerHelperMock.`when`<ReactContext> { UIManagerHelper.getReactContext(any()) } | ||
| .thenReturn(mockContext) | ||
| uiManagerHelperMock.`when`<Int> { UIManagerHelper.getSurfaceId(any<Context>()) } | ||
| .thenReturn(1) | ||
| uiManagerHelperMock.`when`<EventDispatcher?> { UIManagerHelper.getEventDispatcher(any()) } | ||
| .thenReturn(mockEventDispatcher) | ||
|
|
||
| ReactScrollViewHelper.addScrollListener(scrollListener) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| ReactScrollViewHelper.removeScrollListener(scrollListener) | ||
| uiManagerHelperMock.close() | ||
| } | ||
|
|
||
| @Test | ||
| fun registerFlingAnimator_emitsScrollEventOnAnimationEnd() { | ||
| ReactScrollViewHelper.registerFlingAnimator(mockScrollView) | ||
|
|
||
| mockAnimator.setIntValues(0, 100) | ||
| mockAnimator.start() | ||
| mockAnimator.end() | ||
|
|
||
| val captor = argumentCaptor<Event<*>>() | ||
| verify(mockEventDispatcher, org.mockito.kotlin.atLeast(1)).dispatchEvent(captor.capture()) | ||
|
|
||
| val scrollEvents = captor.allValues.filterIsInstance<ScrollEvent>() | ||
| assert(scrollEvents.isNotEmpty()) | ||
| assert(scrollEvents.all { it.eventName == "topScroll" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun registerFlingAnimator_emitsScrollEventOnAnimationCancel() { | ||
| ReactScrollViewHelper.registerFlingAnimator(mockScrollView) | ||
|
|
||
| mockAnimator.setIntValues(0, 100) | ||
| mockAnimator.start() | ||
| mockAnimator.cancel() | ||
|
|
||
| val captor = argumentCaptor<Event<*>>() | ||
| verify(mockEventDispatcher, org.mockito.kotlin.atLeast(1)).dispatchEvent(captor.capture()) | ||
|
|
||
| val scrollEvents = captor.allValues.filterIsInstance<ScrollEvent>() | ||
| assert(scrollEvents.isNotEmpty()) | ||
| assert(scrollEvents.all { it.eventName == "topScroll" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun registerFlingAnimator_onAnimationEnd_notifiesScrollListener() { | ||
| ReactScrollViewHelper.registerFlingAnimator(mockScrollView) | ||
|
|
||
| mockAnimator.setIntValues(0, 100) | ||
| mockAnimator.start() | ||
| mockAnimator.end() | ||
|
|
||
| assert(scrollListener.scrollEventType == ScrollEventType.SCROLL) | ||
| assert(scrollListener.xVelocity == 0f) | ||
| assert(scrollListener.yVelocity == 0f) | ||
| } | ||
|
|
||
| @Test | ||
| fun registerFlingAnimator_onAnimationCancel_notifiesScrollListener() { | ||
| ReactScrollViewHelper.registerFlingAnimator(mockScrollView) | ||
|
|
||
| mockAnimator.setIntValues(0, 100) | ||
| mockAnimator.start() | ||
| mockAnimator.cancel() | ||
|
|
||
| assert(scrollListener.scrollEventType == ScrollEventType.SCROLL) | ||
| assert(scrollListener.xVelocity == 0f) | ||
| assert(scrollListener.yVelocity == 0f) | ||
| } | ||
|
|
||
| private class TestScrollListener : ReactScrollViewHelper.ScrollListener { | ||
| var scrollEventType: ScrollEventType? = null | ||
| var xVelocity: Float = 0f | ||
| var yVelocity: Float = 0f | ||
|
|
||
| override fun onScroll( | ||
| scrollView: ViewGroup?, | ||
| scrollEventType: ScrollEventType?, | ||
| xVelocity: Float, | ||
| yVelocity: Float, | ||
| ) { | ||
| this.scrollEventType = scrollEventType | ||
| this.xVelocity = xVelocity | ||
| this.yVelocity = yVelocity | ||
| } | ||
|
|
||
| override fun onLayout(scrollView: ViewGroup?) { | ||
| // no-op | ||
| } | ||
| } | ||
|
|
||
| private class MockScrollView : | ||
| ViewGroup(RuntimeEnvironment.getApplication()), | ||
| HasFlingAnimator, | ||
| HasScrollEventThrottle, | ||
| HasScrollState, | ||
| HasStateWrapper { | ||
|
|
||
| override var reactScrollViewScrollState = | ||
| ReactScrollViewHelper.ReactScrollViewScrollState() | ||
| override var scrollEventThrottle: Int = 0 | ||
| override var lastScrollDispatchTime: Long = 0 | ||
| override var stateWrapper: StateWrapper? = null | ||
| private val _animator: ValueAnimator = ValueAnimator() | ||
|
|
||
| override fun startFlingAnimator(start: Int, end: Int) { | ||
| _animator.setIntValues(start, end) | ||
| _animator.start() | ||
| } | ||
|
|
||
| override fun getFlingAnimator(): ValueAnimator = _animator | ||
|
|
||
| override fun getFlingExtrapolatedDistance(velocity: Int): Int = 0 | ||
|
|
||
| init { | ||
| super.setLayoutParams( | ||
| ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) | ||
| ) | ||
| } | ||
|
|
||
| override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { | ||
| // no-op | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @sammy-SC is this check safe to remove?