|
| 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.image |
| 9 | + |
| 10 | +import android.graphics.Color |
| 11 | +import android.util.DisplayMetrics |
| 12 | +import com.facebook.drawee.backends.pipeline.Fresco |
| 13 | +import com.facebook.react.bridge.Arguments |
| 14 | +import com.facebook.react.bridge.CatalystInstance |
| 15 | +import com.facebook.react.bridge.JavaOnlyArray |
| 16 | +import com.facebook.react.bridge.JavaOnlyMap |
| 17 | +import com.facebook.react.bridge.ReactApplicationContext |
| 18 | +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance |
| 19 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 20 | +import com.facebook.react.uimanager.ReactStylesDiffMap |
| 21 | +import com.facebook.react.uimanager.ThemedReactContext |
| 22 | +import com.facebook.react.util.RNLog |
| 23 | +import com.facebook.react.views.imagehelper.ImageSource |
| 24 | +import com.facebook.soloader.SoLoader |
| 25 | +import org.junit.After |
| 26 | +import org.junit.Assert |
| 27 | +import org.junit.Before |
| 28 | +import org.junit.Rule |
| 29 | +import org.junit.Test |
| 30 | +import org.junit.runner.RunWith |
| 31 | +import org.powermock.api.mockito.PowerMockito |
| 32 | +import org.powermock.core.classloader.annotations.PowerMockIgnore |
| 33 | +import org.powermock.core.classloader.annotations.PrepareForTest |
| 34 | +import org.powermock.modules.junit4.rule.PowerMockRule |
| 35 | +import org.robolectric.RobolectricTestRunner |
| 36 | +import org.robolectric.RuntimeEnvironment |
| 37 | + |
| 38 | +/** Verify that [ScalingUtils] properties are being applied correctly by [ ]. */ |
| 39 | +@PrepareForTest(Arguments::class, RNLog::class) |
| 40 | +@RunWith(RobolectricTestRunner::class) |
| 41 | +@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*") |
| 42 | +class ReactImagePropertyTest { |
| 43 | + |
| 44 | + @get:Rule var rule = PowerMockRule() |
| 45 | + |
| 46 | + private var context: ReactApplicationContext? = null |
| 47 | + private var catalystInstanceMock: CatalystInstance? = null |
| 48 | + private var themeContext: ThemedReactContext? = null |
| 49 | + |
| 50 | + @Before |
| 51 | + fun setup() { |
| 52 | + PowerMockito.mockStatic(Arguments::class.java) |
| 53 | + PowerMockito.`when`(Arguments.createArray()).thenAnswer { JavaOnlyArray() } |
| 54 | + PowerMockito.`when`(Arguments.createMap()).thenAnswer { JavaOnlyMap() } |
| 55 | + |
| 56 | + // RNLog is stubbed out and the whole class need to be mocked |
| 57 | + PowerMockito.mockStatic(RNLog::class.java) |
| 58 | + SoLoader.setInTestMode() |
| 59 | + context = ReactApplicationContext(RuntimeEnvironment.getApplication()) |
| 60 | + catalystInstanceMock = createMockCatalystInstance() |
| 61 | + context!!.initializeWithInstance(catalystInstanceMock) |
| 62 | + themeContext = ThemedReactContext(context, context) |
| 63 | + Fresco.initialize(context) |
| 64 | + DisplayMetricsHolder.setWindowDisplayMetrics(DisplayMetrics()) |
| 65 | + } |
| 66 | + |
| 67 | + @After |
| 68 | + fun teardown() { |
| 69 | + DisplayMetricsHolder.setWindowDisplayMetrics(null) |
| 70 | + } |
| 71 | + |
| 72 | + private fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap { |
| 73 | + return ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun testBorderColor() { |
| 78 | + val viewManager = ReactImageManager() |
| 79 | + val view = viewManager.createViewInstance(themeContext!!) |
| 80 | + viewManager.updateProperties( |
| 81 | + view, |
| 82 | + buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))) |
| 83 | + viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 0, 255, 255))) |
| 84 | + var borderColor = view.hierarchy.roundingParams!!.borderColor |
| 85 | + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) |
| 86 | + Assert.assertEquals(0, Color.red(borderColor).toLong()) |
| 87 | + Assert.assertEquals(255, Color.green(borderColor).toLong()) |
| 88 | + Assert.assertEquals(255, Color.blue(borderColor).toLong()) |
| 89 | + viewManager.updateProperties(view, buildStyles("borderColor", Color.argb(0, 255, 50, 128))) |
| 90 | + borderColor = view.hierarchy.roundingParams!!.borderColor |
| 91 | + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) |
| 92 | + Assert.assertEquals(255, Color.red(borderColor).toLong()) |
| 93 | + Assert.assertEquals(50, Color.green(borderColor).toLong()) |
| 94 | + Assert.assertEquals(128, Color.blue(borderColor).toLong()) |
| 95 | + viewManager.updateProperties(view, buildStyles("borderColor", null)) |
| 96 | + borderColor = view.hierarchy.roundingParams!!.borderColor |
| 97 | + Assert.assertEquals(0, Color.alpha(borderColor).toLong()) |
| 98 | + Assert.assertEquals(0, Color.red(borderColor).toLong()) |
| 99 | + Assert.assertEquals(0, Color.green(borderColor).toLong()) |
| 100 | + Assert.assertEquals(0, Color.blue(borderColor).toLong()) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun testRoundedCorners() { |
| 105 | + val viewManager = ReactImageManager() |
| 106 | + val view = viewManager.createViewInstance(themeContext!!) |
| 107 | + viewManager.updateProperties( |
| 108 | + view, |
| 109 | + buildStyles("src", JavaOnlyArray.of(JavaOnlyMap.of("uri", "http://mysite.com/mypic.jpg")))) |
| 110 | + |
| 111 | + // We can't easily verify if rounded corner was honored or not, this tests simply verifies |
| 112 | + // we're not crashing.. |
| 113 | + viewManager.updateProperties(view, buildStyles("borderRadius", 10.0)) |
| 114 | + viewManager.updateProperties(view, buildStyles("borderRadius", 0.0)) |
| 115 | + viewManager.updateProperties(view, buildStyles("borderRadius", null)) |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + fun testAccessibilityFocus() { |
| 120 | + val viewManager = ReactImageManager() |
| 121 | + val view = viewManager.createViewInstance(themeContext!!) |
| 122 | + viewManager.setAccessible(view, true) |
| 123 | + Assert.assertEquals(true, view.isFocusable) |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + fun testTintColor() { |
| 128 | + val viewManager = ReactImageManager() |
| 129 | + val view = viewManager.createViewInstance(themeContext!!) |
| 130 | + Assert.assertNull(view.colorFilter) |
| 131 | + viewManager.updateProperties(view, buildStyles("tintColor", Color.argb(50, 0, 0, 255))) |
| 132 | + // Can't actually assert the specific color so this is the next best thing. |
| 133 | + // Does the color filter now exist? |
| 134 | + Assert.assertNotNull(view.colorFilter) |
| 135 | + viewManager.updateProperties(view, buildStyles("tintColor", null)) |
| 136 | + Assert.assertNull(view.colorFilter) |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun testNullSrcs() { |
| 141 | + val viewManager = ReactImageManager() |
| 142 | + val view = viewManager.createViewInstance(themeContext!!) |
| 143 | + val sources = Arguments.createArray() |
| 144 | + val srcObj = Arguments.createMap() |
| 145 | + srcObj.putNull("uri") |
| 146 | + srcObj.putNull("width") |
| 147 | + srcObj.putNull("height") |
| 148 | + sources.pushMap(srcObj) |
| 149 | + viewManager.setSource(view, sources) |
| 150 | + view.maybeUpdateView() |
| 151 | + Assert.assertEquals(ImageSource.getTransparentBitmapImageSource(view.context), view.imageSource) |
| 152 | + } |
| 153 | +} |
0 commit comments