From cb60e5c67b409da1b22e856446183bc7c82d828b Mon Sep 17 00:00:00 2001 From: bufgix Date: Wed, 9 Aug 2023 13:45:07 -0700 Subject: [PATCH] Convert ReactImagePropertyList to kotlin (#38845) Summary: Issue: https://github.com/facebook/react-native/issues/38825#issuecomment-1669205837 ## Changelog: [GENERAL] [CHANGED] - ReactImagePropertyList.java => ReactImagePropertyList.kt Pull Request resolved: https://github.com/facebook/react-native/pull/38845 Test Plan: `./gradlew :packages:react-native:ReactAndroid:test` Reviewed By: cortinico, NickGerleman Differential Revision: D48171953 Pulled By: mdvacca fbshipit-source-id: ae1fbf32f2ef6d18ebe593e31664e45ca339941c --- .../views/image/ReactImagePropertyTest.java | 186 ------------------ .../views/image/ReactImagePropertyTest.kt | 153 ++++++++++++++ 2 files changed, 153 insertions(+), 186 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.java deleted file mode 100644 index 107c1d0d4538ad..00000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * 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.image; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import android.graphics.Color; -import android.util.DisplayMetrics; -import com.facebook.drawee.backends.pipeline.Fresco; -import com.facebook.react.bridge.Arguments; -import com.facebook.react.bridge.CatalystInstance; -import com.facebook.react.bridge.JavaOnlyArray; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactTestHelper; -import com.facebook.react.bridge.WritableArray; -import com.facebook.react.bridge.WritableMap; -import com.facebook.react.uimanager.DisplayMetricsHolder; -import com.facebook.react.uimanager.ReactStylesDiffMap; -import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.util.RNLog; -import com.facebook.react.views.imagehelper.ImageSource; -import com.facebook.soloader.SoLoader; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.rule.PowerMockRule; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** - * Verify that {@link ScalingUtils} properties are being applied correctly by {@link - * ReactImageManager}. - */ -@PrepareForTest({Arguments.class, RNLog.class}) -@RunWith(RobolectricTestRunner.class) -@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"}) -@Ignore // TODO T110934492 -public class ReactImagePropertyTest { - - @Rule public PowerMockRule rule = new PowerMockRule(); - - private ReactApplicationContext mContext; - private CatalystInstance mCatalystInstanceMock; - private ThemedReactContext mThemeContext; - - @Before - public void setup() { - PowerMockito.mockStatic(Arguments.class); - PowerMockito.when(Arguments.createArray()) - .thenAnswer( - new Answer() { - @Override - public Object answer(InvocationOnMock invocation) { - return new JavaOnlyArray(); - } - }); - PowerMockito.when(Arguments.createMap()) - .thenAnswer( - new Answer() { - @Override - public Object answer(InvocationOnMock invocation) { - return new JavaOnlyMap(); - } - }); - - // RNLog is stubbed out and the whole class need to be mocked - PowerMockito.mockStatic(RNLog.class); - PowerMockito.doNothing().when(RNLog.class); - RNLog.w(null, ""); - - SoLoader.setInTestMode(); - mContext = new ReactApplicationContext(RuntimeEnvironment.getApplication()); - mCatalystInstanceMock = ReactTestHelper.createMockCatalystInstance(); - mContext.initializeWithInstance(mCatalystInstanceMock); - mThemeContext = new ThemedReactContext(mContext, mContext); - Fresco.initialize(mContext); - DisplayMetricsHolder.setWindowDisplayMetrics(new DisplayMetrics()); - } - - @After - public void teardown() { - DisplayMetricsHolder.setWindowDisplayMetrics(null); - } - - public ReactStylesDiffMap buildStyles(Object... keysAndValues) { - return new ReactStylesDiffMap(JavaOnlyMap.of(keysAndValues)); - } - - @Test - public void testBorderColor() { - ReactImageManager viewManager = new ReactImageManager(); - ReactImageView view = viewManager.createViewInstance(mThemeContext); - viewManager.updateProperties( - view, - buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))); - - viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 0, 255, 255))); - int borderColor = view.getHierarchy().getRoundingParams().getBorderColor(); - assertEquals(0, Color.alpha(borderColor)); - assertEquals(0, Color.red(borderColor)); - assertEquals(255, Color.green(borderColor)); - assertEquals(255, Color.blue(borderColor)); - - viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 255, 50, 128))); - borderColor = view.getHierarchy().getRoundingParams().getBorderColor(); - assertEquals(0, Color.alpha(borderColor)); - assertEquals(255, Color.red(borderColor)); - assertEquals(50, Color.green(borderColor)); - assertEquals(128, Color.blue(borderColor)); - - viewManager.updateProperties(view, buildStyles("borderColor", null)); - borderColor = view.getHierarchy().getRoundingParams().getBorderColor(); - assertEquals(0, Color.alpha(borderColor)); - assertEquals(0, Color.red(borderColor)); - assertEquals(0, Color.green(borderColor)); - assertEquals(0, Color.blue(borderColor)); - } - - @Test - public void testRoundedCorners() { - ReactImageManager viewManager = new ReactImageManager(); - ReactImageView view = viewManager.createViewInstance(mThemeContext); - viewManager.updateProperties( - view, - buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))); - - // We can't easily verify if rounded corner was honored or not, this tests simply verifies - // we're not crashing.. - viewManager.updateProperties(view, buildStyles("borderRadius", (double) 10)); - viewManager.updateProperties(view, buildStyles("borderRadius", (double) 0)); - viewManager.updateProperties(view, buildStyles("borderRadius", null)); - } - - @Test - public void testAccessibilityFocus() { - ReactImageManager viewManager = new ReactImageManager(); - ReactImageView view = viewManager.createViewInstance(mThemeContext); - viewManager.setAccessible(view, true); - assertEquals(true, view.isFocusable()); - } - - @Test - public void testTintColor() { - ReactImageManager viewManager = new ReactImageManager(); - ReactImageView view = viewManager.createViewInstance(mThemeContext); - assertNull(view.getColorFilter()); - viewManager.updateProperties(view, buildStyles("tintColor", Color.argb(50, 0, 0, 255))); - // Can't actually assert the specific color so this is the next best thing. - // Does the color filter now exist? - assertNotNull(view.getColorFilter()); - viewManager.updateProperties(view, buildStyles("tintColor", null)); - assertNull(view.getColorFilter()); - } - - @Test - public void testNullSrcs() { - ReactImageManager viewManager = new ReactImageManager(); - ReactImageView view = viewManager.createViewInstance(mThemeContext); - WritableArray sources = Arguments.createArray(); - WritableMap srcObj = Arguments.createMap(); - srcObj.putNull("uri"); - srcObj.putNull("width"); - srcObj.putNull("height"); - sources.pushMap(srcObj); - viewManager.setSource(view, sources); - view.maybeUpdateView(); - assertEquals( - ImageSource.getTransparentBitmapImageSource(view.getContext()), view.getImageSource()); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.kt new file mode 100644 index 00000000000000..3e9a4f98cf83e2 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/image/ReactImagePropertyTest.kt @@ -0,0 +1,153 @@ +/* + * 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.image + +import android.graphics.Color +import android.util.DisplayMetrics +import com.facebook.drawee.backends.pipeline.Fresco +import com.facebook.react.bridge.Arguments +import com.facebook.react.bridge.CatalystInstance +import com.facebook.react.bridge.JavaOnlyArray +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.uimanager.ReactStylesDiffMap +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.util.RNLog +import com.facebook.react.views.imagehelper.ImageSource +import com.facebook.soloader.SoLoader +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PowerMockIgnore +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.rule.PowerMockRule +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +/** Verify that [ScalingUtils] properties are being applied correctly by [ ]. */ +@PrepareForTest(Arguments::class, RNLog::class) +@RunWith(RobolectricTestRunner::class) +@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*") +class ReactImagePropertyTest { + + @get:Rule var rule = PowerMockRule() + + private var context: ReactApplicationContext? = null + private var catalystInstanceMock: CatalystInstance? = null + private var themeContext: ThemedReactContext? = null + + @Before + fun setup() { + PowerMockito.mockStatic(Arguments::class.java) + PowerMockito.`when`(Arguments.createArray()).thenAnswer { JavaOnlyArray() } + PowerMockito.`when`(Arguments.createMap()).thenAnswer { JavaOnlyMap() } + + // RNLog is stubbed out and the whole class need to be mocked + PowerMockito.mockStatic(RNLog::class.java) + SoLoader.setInTestMode() + context = ReactApplicationContext(RuntimeEnvironment.getApplication()) + catalystInstanceMock = createMockCatalystInstance() + context!!.initializeWithInstance(catalystInstanceMock) + themeContext = ThemedReactContext(context, context) + Fresco.initialize(context) + DisplayMetricsHolder.setWindowDisplayMetrics(DisplayMetrics()) + } + + @After + fun teardown() { + DisplayMetricsHolder.setWindowDisplayMetrics(null) + } + + private fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap { + return ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) + } + + @Test + fun testBorderColor() { + val viewManager = ReactImageManager() + val view = viewManager.createViewInstance(themeContext!!) + viewManager.updateProperties( + view, + buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))) + viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 0, 255, 255))) + var borderColor = view.hierarchy.roundingParams!!.borderColor + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) + Assert.assertEquals(0, Color.red(borderColor).toLong()) + Assert.assertEquals(255, Color.green(borderColor).toLong()) + Assert.assertEquals(255, Color.blue(borderColor).toLong()) + viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 255, 50, 128))) + borderColor = view.hierarchy.roundingParams!!.borderColor + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) + Assert.assertEquals(255, Color.red(borderColor).toLong()) + Assert.assertEquals(50, Color.green(borderColor).toLong()) + Assert.assertEquals(128, Color.blue(borderColor).toLong()) + viewManager.updateProperties(view, buildStyles("borderColor", null)) + borderColor = view.hierarchy.roundingParams!!.borderColor + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) + Assert.assertEquals(0, Color.red(borderColor).toLong()) + Assert.assertEquals(0, Color.green(borderColor).toLong()) + Assert.assertEquals(0, Color.blue(borderColor).toLong()) + } + + @Test + fun testRoundedCorners() { + val viewManager = ReactImageManager() + val view = viewManager.createViewInstance(themeContext!!) + viewManager.updateProperties( + view, + buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))) + + // We can't easily verify if rounded corner was honored or not, this tests simply verifies + // we're not crashing.. + viewManager.updateProperties(view, buildStyles("borderRadius", 10.0)) + viewManager.updateProperties(view, buildStyles("borderRadius", 0.0)) + viewManager.updateProperties(view, buildStyles("borderRadius", null)) + } + + @Test + fun testAccessibilityFocus() { + val viewManager = ReactImageManager() + val view = viewManager.createViewInstance(themeContext!!) + viewManager.setAccessible(view, true) + Assert.assertEquals(true, view.isFocusable) + } + + @Test + fun testTintColor() { + val viewManager = ReactImageManager() + val view = viewManager.createViewInstance(themeContext!!) + Assert.assertNull(view.colorFilter) + viewManager.updateProperties(view, buildStyles("tintColor", Color.argb(50, 0, 0, 255))) + // Can't actually assert the specific color so this is the next best thing. + // Does the color filter now exist? + Assert.assertNotNull(view.colorFilter) + viewManager.updateProperties(view, buildStyles("tintColor", null)) + Assert.assertNull(view.colorFilter) + } + + @Test + fun testNullSrcs() { + val viewManager = ReactImageManager() + val view = viewManager.createViewInstance(themeContext!!) + val sources = Arguments.createArray() + val srcObj = Arguments.createMap() + srcObj.putNull("uri") + srcObj.putNull("width") + srcObj.putNull("height") + sources.pushMap(srcObj) + viewManager.setSource(view, sources) + view.maybeUpdateView() + Assert.assertEquals(ImageSource.getTransparentBitmapImageSource(view.context), view.imageSource) + } +}