Skip to content

Commit

Permalink
test(image): [android] improving image property test cases (facebook#…
Browse files Browse the repository at this point in the history
…47433)

Summary:
I've doing some enhancements around the Image component for Android, and in the process of understanding the codebase I saw some opportunity to improve the test cases as there is no much coverage around the underlying functionality.

## Changelog:

[INTERNAL] [ADDED] - Improving Android Image property test cases

Pull Request resolved: facebook#47433

Test Plan:
```bash
yarn test-android
```

Reviewed By: javache

Differential Revision: D65530813

Pulled By: cortinico

fbshipit-source-id: b314eaf7493c6f3c859b7cb0d06771c109e5e854
  • Loading branch information
mateoguzmana authored and facebook-github-bot committed Nov 6, 2024
1 parent a3ab789 commit 17c1646
Showing 1 changed file with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package com.facebook.react.views.image

import android.graphics.Color
import android.util.DisplayMetrics
import com.facebook.common.logging.FLog
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.react.bridge.Arguments
Expand All @@ -19,6 +20,7 @@ import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance
import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.common.ReactConstants
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.ReactStylesDiffMap
Expand All @@ -34,7 +36,11 @@ import org.junit.runner.RunWith
import org.mockito.MockedStatic
import org.mockito.Mockito.any
import org.mockito.Mockito.anyString
import org.mockito.Mockito.mock
import org.mockito.Mockito.mockStatic
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.verify
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

Expand All @@ -47,6 +53,7 @@ class ReactImagePropertyTest {
private lateinit var themeContext: ThemedReactContext
private lateinit var arguments: MockedStatic<Arguments>
private lateinit var rnLog: MockedStatic<RNLog>
private lateinit var flogMock: MockedStatic<FLog>

@Before
fun setup() {
Expand All @@ -57,6 +64,8 @@ class ReactImagePropertyTest {
rnLog = mockStatic(RNLog::class.java)
rnLog.`when`<Boolean> { RNLog.w(any(), anyString()) }.thenAnswer {}

flogMock = mockStatic(FLog::class.java)

SoLoader.setInTestMode()
context = BridgeReactContext(RuntimeEnvironment.getApplication())
catalystInstanceMock = createMockCatalystInstance()
Expand All @@ -73,6 +82,7 @@ class ReactImagePropertyTest {
DisplayMetricsHolder.setWindowDisplayMetrics(null)
arguments.close()
rnLog.close()
flogMock.close()
}

private fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap {
Expand All @@ -87,6 +97,20 @@ class ReactImagePropertyTest {
assertThat(view.isFocusable).isTrue()
}

@Test
fun testOverlayColor() {
val viewManager = ReactImageManager()
val mockView = mock(ReactImageView::class.java)

viewManager.setOverlayColor(mockView, null)
verify(mockView).setOverlayColor(Color.TRANSPARENT)
reset(mockView)

viewManager.setOverlayColor(mockView, Color.argb(50, 0, 0, 255))
verify(mockView).setOverlayColor(Color.argb(50, 0, 0, 255))
reset(mockView)
}

@Test
fun testTintColor() {
val viewManager = ReactImageManager()
Expand Down Expand Up @@ -115,4 +139,95 @@ class ReactImagePropertyTest {
assertThat(ImageSource.getTransparentBitmapImageSource(view.context))
.isEqualTo(view.imageSource)
}

@Test
fun testResizeMode() {
val viewManager = ReactImageManager()
val mockView = mock(ReactImageView::class.java)

viewManager.setResizeMode(mockView, null)
verify(mockView).setScaleType(ScalingUtils.ScaleType.CENTER_CROP)
reset(mockView)

viewManager.setResizeMode(mockView, "cover")
verify(mockView).setScaleType(ScalingUtils.ScaleType.CENTER_CROP)
reset(mockView)

viewManager.setResizeMode(mockView, "contain")
verify(mockView).setScaleType(ScalingUtils.ScaleType.FIT_CENTER)
reset(mockView)

viewManager.setResizeMode(mockView, "stretch")
verify(mockView).setScaleType(ScalingUtils.ScaleType.FIT_XY)
reset(mockView)

viewManager.setResizeMode(mockView, "repeat")
verify(mockView).setScaleType(ScaleTypeStartInside.INSTANCE)
reset(mockView)

viewManager.setResizeMode(mockView, "center")
verify(mockView).setScaleType(ScalingUtils.ScaleType.CENTER_INSIDE)
reset(mockView)

viewManager.setResizeMode(mockView, "invalid")
verify(mockView).setScaleType(ScalingUtils.ScaleType.CENTER_CROP)
}

@Test
fun testResizeMethod() {
val viewManager = ReactImageManager()
val mockView = mock(ReactImageView::class.java)

viewManager.setResizeMethod(mockView, null)
verify(mockView).setResizeMethod(ImageResizeMethod.AUTO)
reset(mockView)

viewManager.setResizeMethod(mockView, "auto")
verify(mockView).setResizeMethod(ImageResizeMethod.AUTO)
reset(mockView)

viewManager.setResizeMethod(mockView, "resize")
verify(mockView).setResizeMethod(ImageResizeMethod.RESIZE)
reset(mockView)

viewManager.setResizeMethod(mockView, "scale")
verify(mockView).setResizeMethod(ImageResizeMethod.SCALE)
reset(mockView)

viewManager.setResizeMethod(mockView, "none")
verify(mockView).setResizeMethod(ImageResizeMethod.NONE)
reset(mockView)

viewManager.setResizeMethod(mockView, "invalid")
verify(mockView).setResizeMethod(ImageResizeMethod.AUTO)
flogMock.verify { FLog.w(ReactConstants.TAG, "Invalid resize method: 'invalid'") }
}

@Test
fun testResizeMultiplier() {
val viewManager = ReactImageManager()
val mockView = mock(ReactImageView::class.java)

viewManager.setResizeMultiplier(mockView, 0.01f)
verify(mockView).setResizeMultiplier(0.01f)
reset(mockView)

viewManager.setResizeMultiplier(mockView, 0.009f)
verify(mockView).setResizeMultiplier(0.009f)
flogMock.verify { FLog.w(ReactConstants.TAG, "Invalid resize multiplier: '0.009'") }
}

@Test
fun testHeaders() {
val viewManager = ReactImageManager()
val mockView = mock(ReactImageView::class.java)

viewManager.setHeaders(mockView, null)
verify(mockView, never()).setHeaders(any())

val headers = JavaOnlyMap()
headers.putString("key", "value")
viewManager.setHeaders(mockView, headers)
verify(mockView).setHeaders(headers)
}
}

0 comments on commit 17c1646

Please sign in to comment.