Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import com.facebook.react.uimanager.ViewDefaults
import com.facebook.react.uimanager.ViewProps
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.annotations.ReactPropGroup
import com.facebook.react.uimanager.drawable.CompositeBackgroundDrawable
import com.facebook.react.uimanager.events.BlurEvent
import com.facebook.react.uimanager.events.EventDispatcher
import com.facebook.react.uimanager.events.FocusEvent
Expand Down Expand Up @@ -532,15 +533,16 @@ public open class ReactTextInputManager public constructor() :

@ReactProp(name = "underlineColorAndroid", customType = "Color")
public fun setUnderlineColor(view: ReactEditText, underlineColor: Int?) {
// The underline is the EditText's own background. Once BackgroundStyleApplicator wraps it, tint
// only that drawable, so the filter does not also tint box shadows and outlines.
val background =
view.background.let { if (it is CompositeBackgroundDrawable) it.originalBackground else it }
?: return

// Drawable.mutate() can sometimes crash due to an AOSP bug:
// See https://code.google.com/p/android/issues/detail?id=191754 for more info
val background = view.background
var drawableToMutate = background

if (background == null) {
return
}

if (background.constantState != null) {
try {
drawableToMutate = checkNotNull(background.mutate())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.textinput

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.util.DisplayMetrics
import androidx.core.content.res.ResourcesCompat.ID_NULL
import androidx.core.graphics.createBitmap
import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import com.facebook.react.uimanager.BackgroundStyleApplicator
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.style.BoxShadow
import com.facebook.testutils.shadows.ShadowSoLoader
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode

/** Tests that `underlineColorAndroid` tints only the underline, not the other background layers. */
@RunWith(RobolectricTestRunner::class)
@Config(shadows = [ShadowSoLoader::class], sdk = [Build.VERSION_CODES.UPSIDE_DOWN_CAKE])
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class ReactTextInputUnderlineColorTest {

private lateinit var manager: ReactTextInputManager
private lateinit var view: ReactEditText
private lateinit var underline: GradientDrawable

@Before
fun setUp() {
ReactNativeFeatureFlagsForTests.setUp()
DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics().apply { density = 1f })
val themedContext =
ThemedReactContext(
createTestReactApplicationContext(),
RuntimeEnvironment.getApplication(),
null,
ID_NULL,
)
manager = ReactTextInputManager()
view = manager.createViewInstance(themedContext)
underline = GradientDrawable()
view.background = underline
}

@After
fun tearDown() {
DisplayMetricsHolder.setScreenDisplayMetrics(null)
ReactNativeFeatureFlags.dangerouslyReset()
}

@Test
fun underlineColorSetAfterBoxShadowKeepsTheShadow() {
setBoxShadow()
manager.setUnderlineColor(view, Color.TRANSPARENT)

assertThat(underline.colorFilter)
.isEqualTo(PorterDuffColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_IN))
assertThat(drawBackground().getPixel(15, 50)).isEqualTo(Color.BLACK)
}

@Test
fun underlineColorSetBeforeBoxShadowStaysOnTheUnderline() {
manager.setUnderlineColor(view, Color.RED)
setBoxShadow()
assertThat(underline.colorFilter)
.isEqualTo(PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN))

manager.setUnderlineColor(view, null)

assertThat(underline.colorFilter).isNull()
assertThat(drawBackground().getPixel(15, 50)).isEqualTo(Color.BLACK)
}

/** Gives the view a black 10px shadow on every side, without blur. */
private fun setBoxShadow() {
BackgroundStyleApplicator.setBoxShadow(
view,
listOf(BoxShadow(offsetX = 0f, offsetY = 0f, color = Color.BLACK, spreadDistance = 10f)),
)
}

/** Draws the background into a 100px square, inset by 20px so the 10px shadow spread fits. */
private fun drawBackground(): Bitmap {
val bitmap = createBitmap(100, 100, Bitmap.Config.ARGB_8888)
val background = checkNotNull(view.background)
background.setBounds(20, 20, 80, 80)
background.draw(Canvas(bitmap))
return bitmap
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import ExampleTextInput from './ExampleTextInput';
import TextInputSharedExamples from './TextInputSharedExamples';
import * as React from 'react';
import {useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
import {Pressable, StyleSheet, Text, View} from 'react-native';

const ToggleDefaultPaddingExample = (): React.Node => {
const [hasPadding, setHasPadding] = useState(false);
Expand All @@ -35,6 +35,35 @@ const ToggleDefaultPaddingExample = (): React.Node => {
);
};

const BoxShadowUnderlineColorExample = (): React.Node => {
const [underlineColor, setUnderlineColor] = useState<'transparent' | 'red'>(
'transparent',
);

return (
<View
testID="textinput-box-shadow-underline"
style={styles.boxShadowContainer}>
<Pressable
testID="textinput-box-shadow-underline-toggle"
onPress={() =>
setUnderlineColor(color => (color === 'red' ? 'transparent' : 'red'))
}>
<RNTesterText testID="textinput-box-shadow-underline-label">
{`underlineColorAndroid="${underlineColor}" (tap to change)`}
</RNTesterText>
</Pressable>
<View style={styles.boxShadowBackdrop}>
<ExampleTextInput
placeholder="The shadow stays black"
style={styles.boxShadow}
underlineColorAndroid={underlineColor}
/>
</View>
</View>
);
};

const styles = StyleSheet.create({
multiline: {
height: 60,
Expand All @@ -49,6 +78,18 @@ const styles = StyleSheet.create({
wrappedText: {
maxWidth: 300,
},
boxShadowContainer: {
gap: 10,
},
boxShadowBackdrop: {
backgroundColor: '#e5e5e5',
padding: 20,
},
boxShadow: {
backgroundColor: 'white',
borderRadius: 8,
boxShadow: '0px 6px 10px 0px rgba(0, 0, 0, 0.4)',
},
});

const examples: Array<RNTesterModuleExample> = [
Expand Down Expand Up @@ -490,6 +531,15 @@ const examples: Array<RNTesterModuleExample> = [
);
},
},
{
title: 'boxShadow and underlineColorAndroid',
name: 'boxShadowUnderlineColor',
description:
'Changing underlineColorAndroid tints the underline, not the box shadow',
render: function (): React.Node {
return <BoxShadowUnderlineColorExample />;
},
},
{
title: 'Toggle Default Padding',
render: function (): React.Node {
Expand Down
34 changes: 34 additions & 0 deletions packages/rn-tester/visual-tests/flows/text-input.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,37 @@ appId: ${APP_ID}
path: ${VISUAL_OUTPUT_DIR}/text-input/text-styles/cycle-6
cropOn:
id: 'text-styles'
- runFlow:
when:
platform: Android
commands:
- runFlow:
file: ../helpers/open-example.yml
env:
EXAMPLE_URL: rntester://example/TextInputExample/boxShadowUnderlineColor
- extendedWaitUntil:
visible:
id: 'textinput-box-shadow-underline'
timeout: 15000
- tapOn:
id: 'textinput-box-shadow-underline-toggle'
- assertVisible:
id: 'textinput-box-shadow-underline-label'
text: 'underlineColorAndroid="red" (tap to change)'
- waitForAnimationToEnd:
timeout: 5000
- takeScreenshot:
path: ${VISUAL_OUTPUT_DIR}/text-input/box-shadow-underline-color/red
cropOn:
id: 'textinput-box-shadow-underline'
- tapOn:
id: 'textinput-box-shadow-underline-toggle'
- assertVisible:
id: 'textinput-box-shadow-underline-label'
text: 'underlineColorAndroid="transparent" (tap to change)'
- waitForAnimationToEnd:
timeout: 5000
- takeScreenshot:
path: ${VISUAL_OUTPUT_DIR}/text-input/box-shadow-underline-color/transparent
cropOn:
id: 'textinput-box-shadow-underline'
1 change: 1 addition & 0 deletions packages/rn-tester/visual-tests/suites/text-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
| `clipping` | Initial rendering |
| `placeholderEllipsizing` | Single-line and multiline placeholders |
| `textStyles` | Cycles 1–6 of nested single-line and multiline styles |
| `boxShadowUnderlineColor` | `underlineColorAndroid` changed to `red`, then back to `transparent`: the box shadow stays black (Android only) |

Screenshots use the existing RNTester example crops. Each example starts from a fresh app launch; captures wait for the target and for animations to settle.