|
| 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.View |
| 16 | +import com.facebook.react.bridge.BridgeReactContext |
| 17 | +import com.facebook.react.bridge.CatalystInstance |
| 18 | +import com.facebook.react.bridge.JavaOnlyMap |
| 19 | +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance |
| 20 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 21 | +import com.facebook.react.uimanager.BackgroundStyleApplicator |
| 22 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 23 | +import com.facebook.react.uimanager.LengthPercentage |
| 24 | +import com.facebook.react.uimanager.LengthPercentageType |
| 25 | +import com.facebook.react.uimanager.ReactStylesDiffMap |
| 26 | +import com.facebook.react.uimanager.ThemedReactContext |
| 27 | +import com.facebook.react.uimanager.style.BorderRadiusProp |
| 28 | +import org.assertj.core.api.Assertions.assertThat |
| 29 | +import org.junit.After |
| 30 | +import org.junit.Before |
| 31 | +import org.junit.Test |
| 32 | +import org.junit.runner.RunWith |
| 33 | +import org.robolectric.RobolectricTestRunner |
| 34 | +import org.robolectric.RuntimeEnvironment |
| 35 | + |
| 36 | +/** Verifies border radius properties applied by the vertical and horizontal ScrollView managers. */ |
| 37 | +@SuppressLint("DeprecatedClass", "DeprecatedMethod") |
| 38 | +@RunWith(RobolectricTestRunner::class) |
| 39 | +class ReactScrollViewPropertyTest { |
| 40 | + |
| 41 | + private lateinit var context: BridgeReactContext |
| 42 | + private lateinit var catalystInstanceMock: CatalystInstance |
| 43 | + private lateinit var themedContext: ThemedReactContext |
| 44 | + |
| 45 | + @Before |
| 46 | + fun setup() { |
| 47 | + ReactNativeFeatureFlagsForTests.setUp() |
| 48 | + context = BridgeReactContext(RuntimeEnvironment.getApplication()) |
| 49 | + catalystInstanceMock = createMockCatalystInstance() |
| 50 | + context.initializeWithInstance(catalystInstanceMock) |
| 51 | + themedContext = ThemedReactContext(context, context, null, -1) |
| 52 | + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + fun teardown() { |
| 57 | + DisplayMetricsHolder.setScreenDisplayMetrics(null) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun testVerticalBorderRadius() { |
| 62 | + val manager = ReactScrollViewManager() |
| 63 | + val view = manager.createViewInstance(themedContext) |
| 64 | + |
| 65 | + assertBorderRadiusUpdates(view) { manager.updateProperties(view, it) } |
| 66 | + assertDeprecatedFloatBorderRadius(view) { index, radius -> |
| 67 | + manager.setBorderRadius(view, index, radius) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun testHorizontalBorderRadius() { |
| 73 | + val manager = ReactHorizontalScrollViewManager() |
| 74 | + val view = manager.createViewInstance(themedContext) |
| 75 | + |
| 76 | + assertBorderRadiusUpdates(view) { manager.updateProperties(view, it) } |
| 77 | + assertDeprecatedFloatBorderRadius(view) { index, radius -> |
| 78 | + manager.setBorderRadius(view, index, radius) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private fun assertBorderRadiusUpdates( |
| 83 | + view: View, |
| 84 | + updateProperties: (ReactStylesDiffMap) -> Unit, |
| 85 | + ) { |
| 86 | + val percentageRadii = |
| 87 | + listOf( |
| 88 | + Triple("borderRadius", BorderRadiusProp.BORDER_RADIUS, 10f), |
| 89 | + Triple("borderTopLeftRadius", BorderRadiusProp.BORDER_TOP_LEFT_RADIUS, 20f), |
| 90 | + Triple("borderTopRightRadius", BorderRadiusProp.BORDER_TOP_RIGHT_RADIUS, 30f), |
| 91 | + Triple("borderBottomRightRadius", BorderRadiusProp.BORDER_BOTTOM_RIGHT_RADIUS, 40f), |
| 92 | + Triple("borderBottomLeftRadius", BorderRadiusProp.BORDER_BOTTOM_LEFT_RADIUS, 50f), |
| 93 | + ) |
| 94 | + |
| 95 | + percentageRadii.forEach { (name, property, value) -> |
| 96 | + updateProperties(buildStyles(name, "$value%")) |
| 97 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, property)) |
| 98 | + .isEqualTo(LengthPercentage(value, LengthPercentageType.PERCENT)) |
| 99 | + } |
| 100 | + |
| 101 | + updateProperties(buildStyles("borderRadius", 12.0)) |
| 102 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 103 | + .isEqualTo(LengthPercentage(12f, LengthPercentageType.POINT)) |
| 104 | + |
| 105 | + updateProperties(buildStyles("borderRadius", -1.0)) |
| 106 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 107 | + .isNull() |
| 108 | + |
| 109 | + updateProperties(buildStyles("borderRadius", 13.0)) |
| 110 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 111 | + .isEqualTo(LengthPercentage(13f, LengthPercentageType.POINT)) |
| 112 | + |
| 113 | + updateProperties(buildStyles("borderRadius", null)) |
| 114 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 115 | + .isNull() |
| 116 | + } |
| 117 | + |
| 118 | + private fun assertDeprecatedFloatBorderRadius( |
| 119 | + view: View, |
| 120 | + setBorderRadius: (Int, Float) -> Unit, |
| 121 | + ) { |
| 122 | + setBorderRadius(0, 8f) |
| 123 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 124 | + .isEqualTo(LengthPercentage(8f, LengthPercentageType.POINT)) |
| 125 | + |
| 126 | + setBorderRadius(0, Float.NaN) |
| 127 | + assertThat(BackgroundStyleApplicator.getBorderRadius(view, BorderRadiusProp.BORDER_RADIUS)) |
| 128 | + .isNull() |
| 129 | + } |
| 130 | + |
| 131 | + private fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap = |
| 132 | + ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) |
| 133 | +} |
0 commit comments