|
| 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 | +package com.facebook.react.views.textinput |
| 9 | + |
| 10 | +import android.app.Activity |
| 11 | +import android.graphics.Color |
| 12 | +import android.graphics.Outline |
| 13 | +import android.graphics.Rect |
| 14 | +import android.graphics.drawable.Drawable |
| 15 | +import android.graphics.drawable.InsetDrawable |
| 16 | +import android.graphics.drawable.NinePatchDrawable |
| 17 | +import android.graphics.drawable.StateListDrawable |
| 18 | +import android.os.Build |
| 19 | +import android.os.Looper |
| 20 | +import android.util.DisplayMetrics |
| 21 | +import android.util.StateSet |
| 22 | +import android.view.ContextThemeWrapper |
| 23 | +import android.view.View |
| 24 | +import android.view.ViewGroup |
| 25 | +import android.view.ViewOutlineProvider |
| 26 | +import androidx.annotation.RequiresApi |
| 27 | +import androidx.core.graphics.drawable.toDrawable |
| 28 | +import com.facebook.react.bridge.ReactTestHelper.createCatalystContextForTest |
| 29 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 30 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 31 | +import com.facebook.react.uimanager.ThemedReactContext |
| 32 | +import org.assertj.core.api.Assertions.assertThat |
| 33 | +import org.junit.Before |
| 34 | +import org.junit.Test |
| 35 | +import org.junit.runner.RunWith |
| 36 | +import org.robolectric.Robolectric |
| 37 | +import org.robolectric.RobolectricTestRunner |
| 38 | +import org.robolectric.RuntimeEnvironment |
| 39 | +import org.robolectric.Shadows.shadowOf |
| 40 | + |
| 41 | +@RunWith(RobolectricTestRunner::class) |
| 42 | +@RequiresApi(Build.VERSION_CODES.N) |
| 43 | +class ReactEditTextOutlineTest { |
| 44 | + |
| 45 | + private lateinit var view: ReactEditText |
| 46 | + |
| 47 | + @Before |
| 48 | + fun setup() { |
| 49 | + ReactNativeFeatureFlagsForTests.setUp() |
| 50 | + val context = createCatalystContextForTest() |
| 51 | + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) |
| 52 | + view = |
| 53 | + ReactTextInputManager().createViewInstance(ThemedReactContext(context, context, null, -1)) |
| 54 | + view.layout(0, 0, WIDTH, HEIGHT) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun themeBackground_isInsetSelectorOfNinePatches() { |
| 59 | + val background = themeTextFieldBackground() |
| 60 | + |
| 61 | + assertThat(background).isInstanceOf(InsetDrawable::class.java) |
| 62 | + val selector = (background as InsetDrawable).drawable |
| 63 | + assertThat(selector).isInstanceOf(StateListDrawable::class.java) |
| 64 | + assertThat(selector?.current).isInstanceOf(NinePatchDrawable::class.java) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun themeBackgroundWithoutPadding_usesViewBounds() { |
| 69 | + view.background = themeTextFieldBackground() |
| 70 | + view.setPadding(0, 0, 0, 0) |
| 71 | + |
| 72 | + assertThat(outlineBounds()).isEqualTo(Rect(0, 0, WIDTH, HEIGHT)) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun themeBackgroundWithOneSideBelowThemePadding_usesViewBounds() { |
| 77 | + val background = themeTextFieldBackground() |
| 78 | + view.background = background |
| 79 | + val padding = Rect().also { background.getPadding(it) } |
| 80 | + view.setPadding(padding.left, 0, padding.right, padding.bottom) |
| 81 | + |
| 82 | + assertThat(outlineBounds()).isEqualTo(Rect(0, 0, WIDTH, HEIGHT)) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun themeBackgroundWithThemePadding_usesBackgroundOutline() { |
| 87 | + val background = themeTextFieldBackground() |
| 88 | + view.background = background |
| 89 | + val padding = Rect().also { background.getPadding(it) } |
| 90 | + view.setPadding(padding.left, padding.top, padding.right, padding.bottom) |
| 91 | + |
| 92 | + assertThat(outlineBounds()).isEqualTo(backgroundOutlineBounds(background)) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun paddingChangeWhileAttached_rebuildsOutline() { |
| 97 | + val background = themeTextFieldBackground() |
| 98 | + view.background = background |
| 99 | + val padding = Rect().also { background.getPadding(it) } |
| 100 | + view.setPadding(padding.left, padding.top, padding.right, padding.bottom) |
| 101 | + val rebuiltOutline = Rect() |
| 102 | + val provider = view.outlineProvider |
| 103 | + view.outlineProvider = |
| 104 | + object : ViewOutlineProvider() { |
| 105 | + override fun getOutline(view: View, outline: Outline) { |
| 106 | + provider.getOutline(view, outline) |
| 107 | + outline.getRect(rebuiltOutline) |
| 108 | + } |
| 109 | + } |
| 110 | + val activity = Robolectric.buildActivity(Activity::class.java).setup().get() |
| 111 | + activity.setContentView(view, ViewGroup.LayoutParams(WIDTH, HEIGHT)) |
| 112 | + shadowOf(Looper.getMainLooper()).idle() |
| 113 | + assertThat(rebuiltOutline).isEqualTo(backgroundOutlineBounds(background)) |
| 114 | + |
| 115 | + view.setPadding(0, 0, 0, 0) |
| 116 | + |
| 117 | + assertThat(rebuiltOutline).isEqualTo(Rect(0, 0, WIDTH, HEIGHT)) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun customInsetBackgroundWithoutPadding_usesBackgroundOutline() { |
| 122 | + val selector = StateListDrawable() |
| 123 | + selector.addState(StateSet.WILD_CARD, Color.BLACK.toDrawable()) |
| 124 | + val background = InsetDrawable(selector, 4, 10, 4, 7) |
| 125 | + background.setBounds(0, 0, WIDTH, HEIGHT) |
| 126 | + view.background = background |
| 127 | + view.setPadding(0, 0, 0, 0) |
| 128 | + |
| 129 | + assertThat(outlineBounds()).isEqualTo(Rect(4, 10, WIDTH - 4, HEIGHT - 7)) |
| 130 | + } |
| 131 | + |
| 132 | + private fun themeTextFieldBackground(): Drawable { |
| 133 | + val context = |
| 134 | + ContextThemeWrapper(RuntimeEnvironment.getApplication(), android.R.style.Theme_Material) |
| 135 | + val attrs = context.obtainStyledAttributes(intArrayOf(android.R.attr.editTextBackground)) |
| 136 | + val background = checkNotNull(attrs.getDrawable(0)) |
| 137 | + attrs.recycle() |
| 138 | + background.setBounds(0, 0, WIDTH, HEIGHT) |
| 139 | + return background |
| 140 | + } |
| 141 | + |
| 142 | + private fun outlineBounds(): Rect { |
| 143 | + val outline = Outline() |
| 144 | + view.outlineProvider.getOutline(view, outline) |
| 145 | + return Rect().also { outline.getRect(it) } |
| 146 | + } |
| 147 | + |
| 148 | + private fun backgroundOutlineBounds(background: Drawable): Rect { |
| 149 | + val outline = Outline() |
| 150 | + background.getOutline(outline) |
| 151 | + return Rect().also { outline.getRect(it) } |
| 152 | + } |
| 153 | + |
| 154 | + private companion object { |
| 155 | + const val WIDTH = 200 |
| 156 | + const val HEIGHT = 100 |
| 157 | + } |
| 158 | +} |
0 commit comments