|
| 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 | + |
| 8 | +// The deprecated APIs exercised here remain public and required by this Robolectric setup. |
| 9 | +@file:Suppress("DEPRECATION") |
| 10 | + |
| 11 | +package com.facebook.react.views.scroll |
| 12 | + |
| 13 | +import android.annotation.SuppressLint |
| 14 | +import android.util.DisplayMetrics |
| 15 | +import android.view.InputDevice |
| 16 | +import android.view.MotionEvent |
| 17 | +import android.view.View |
| 18 | +import android.widget.FrameLayout |
| 19 | +import com.facebook.react.bridge.BridgeReactContext |
| 20 | +import com.facebook.react.bridge.JavaOnlyArray |
| 21 | +import com.facebook.react.bridge.JavaOnlyMap |
| 22 | +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance |
| 23 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 24 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 25 | +import com.facebook.react.uimanager.ReactStylesDiffMap |
| 26 | +import com.facebook.react.uimanager.ThemedReactContext |
| 27 | +import com.facebook.react.uimanager.UIManagerHelper |
| 28 | +import com.facebook.react.uimanager.events.EventDispatcher |
| 29 | +import com.facebook.soloader.SoLoader |
| 30 | +import org.assertj.core.api.Assertions.assertThat |
| 31 | +import org.junit.After |
| 32 | +import org.junit.Before |
| 33 | +import org.junit.Test |
| 34 | +import org.junit.runner.RunWith |
| 35 | +import org.mockito.MockedStatic |
| 36 | +import org.mockito.Mockito.mockStatic |
| 37 | +import org.mockito.kotlin.any |
| 38 | +import org.mockito.kotlin.mock |
| 39 | +import org.robolectric.RobolectricTestRunner |
| 40 | +import org.robolectric.RuntimeEnvironment |
| 41 | + |
| 42 | +/** Verifies the direction of wheel and joystick scrolling in regular and inverted ScrollViews. */ |
| 43 | +@SuppressLint("DeprecatedClass", "DeprecatedMethod") |
| 44 | +@RunWith(RobolectricTestRunner::class) |
| 45 | +class ReactScrollViewInvertedScrollTest { |
| 46 | + |
| 47 | + private lateinit var themedContext: ThemedReactContext |
| 48 | + private lateinit var uiManagerHelperMock: MockedStatic<UIManagerHelper> |
| 49 | + |
| 50 | + @Before |
| 51 | + fun setup() { |
| 52 | + ReactNativeFeatureFlagsForTests.setUp() |
| 53 | + SoLoader.setInTestMode() |
| 54 | + val context = BridgeReactContext(RuntimeEnvironment.getApplication()) |
| 55 | + context.initializeWithInstance(createMockCatalystInstance()) |
| 56 | + themedContext = ThemedReactContext(context, context, null, -1) |
| 57 | + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) |
| 58 | + uiManagerHelperMock = mockStatic(UIManagerHelper::class.java) |
| 59 | + uiManagerHelperMock |
| 60 | + .`when`<EventDispatcher?> { UIManagerHelper.getEventDispatcher(any()) } |
| 61 | + .thenReturn(mock()) |
| 62 | + } |
| 63 | + |
| 64 | + @After |
| 65 | + fun teardown() { |
| 66 | + uiManagerHelperMock.close() |
| 67 | + DisplayMetricsHolder.setScreenDisplayMetrics(null) |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun testWheelScrollUpScrollsTowardStart() { |
| 72 | + val view = createScrolledToMiddle() |
| 73 | + |
| 74 | + view.onGenericMotionEvent(obtainWheelScroll(1f)) |
| 75 | + |
| 76 | + assertThat(view.scrollY).isLessThan(MIDDLE_SCROLL_Y) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testInvertedWheelScrollUpScrollsTowardEnd() { |
| 81 | + // The transform an inverted VirtualizedList applies on Android. |
| 82 | + val view = createScrolledToMiddle(JavaOnlyArray.of(JavaOnlyMap.of("scale", -1.0))) |
| 83 | + |
| 84 | + view.onGenericMotionEvent(obtainWheelScroll(1f)) |
| 85 | + |
| 86 | + assertThat(view.scrollY).isGreaterThan(MIDDLE_SCROLL_Y) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun testInvertedParentWheelScrollUpScrollsTowardEnd() { |
| 91 | + val view = createScrolledToMiddle() |
| 92 | + // With a refreshControl, ScrollView moves its transform to the wrapping refresh layout. |
| 93 | + FrameLayout(themedContext).apply { scaleY = -1f }.addView(view) |
| 94 | + |
| 95 | + view.onGenericMotionEvent(obtainWheelScroll(1f)) |
| 96 | + |
| 97 | + assertThat(view.scrollY).isGreaterThan(MIDDLE_SCROLL_Y) |
| 98 | + } |
| 99 | + |
| 100 | + private fun createScrolledToMiddle(transform: JavaOnlyArray? = null): ReactScrollView { |
| 101 | + val manager = ReactScrollViewManager() |
| 102 | + val view = manager.createViewInstance(themedContext) |
| 103 | + if (transform != null) { |
| 104 | + manager.updateProperties(view, ReactStylesDiffMap(JavaOnlyMap.of("transform", transform))) |
| 105 | + } |
| 106 | + val content = View(themedContext) |
| 107 | + view.addView(content) |
| 108 | + content.layout(0, 0, VIEWPORT_SIZE, CONTENT_HEIGHT) |
| 109 | + view.layout(0, 0, VIEWPORT_SIZE, VIEWPORT_SIZE) |
| 110 | + view.scrollTo(0, MIDDLE_SCROLL_Y) |
| 111 | + assertThat(view.scrollY).isEqualTo(MIDDLE_SCROLL_Y) |
| 112 | + return view |
| 113 | + } |
| 114 | + |
| 115 | + private fun obtainWheelScroll(vScroll: Float): MotionEvent { |
| 116 | + val properties = |
| 117 | + MotionEvent.PointerProperties().apply { toolType = MotionEvent.TOOL_TYPE_MOUSE } |
| 118 | + val coords = |
| 119 | + MotionEvent.PointerCoords().apply { setAxisValue(MotionEvent.AXIS_VSCROLL, vScroll) } |
| 120 | + return MotionEvent.obtain( |
| 121 | + 0L, |
| 122 | + 0L, |
| 123 | + MotionEvent.ACTION_SCROLL, |
| 124 | + 1, |
| 125 | + arrayOf(properties), |
| 126 | + arrayOf(coords), |
| 127 | + 0, |
| 128 | + 0, |
| 129 | + 1f, |
| 130 | + 1f, |
| 131 | + 0, |
| 132 | + 0, |
| 133 | + InputDevice.SOURCE_MOUSE, |
| 134 | + 0, |
| 135 | + ) |
| 136 | + } |
| 137 | + |
| 138 | + private companion object { |
| 139 | + const val VIEWPORT_SIZE = 100 |
| 140 | + const val CONTENT_HEIGHT = 1000 |
| 141 | + const val MIDDLE_SCROLL_Y = 450 |
| 142 | + } |
| 143 | +} |
0 commit comments