Skip to content

Commit baa2714

Browse files
SimpleViewPropertyTest.java => SimpleViewPropertyTest.kt (#38856)
Summary: This PR converts SimpleViewPropertyTest into Kotlin as requested in [https://github.com/facebook/react-native/issues/38835](https://github.com/facebook/react-native/issues/38825#issuecomment-1669634951) ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [INTERNAL] [CHANGED] - Convert to SimpleViewPropertyTest Kotlin Pull Request resolved: #38856 Test Plan: 1. Run `./gradlew :packages:react-native:ReactAndroid:test.` 2. All tests should pass. Reviewed By: NickGerleman Differential Revision: D48171939 Pulled By: mdvacca fbshipit-source-id: eaa93a696faba1793b85186e6a072a7d3f043d0c
1 parent 5d67460 commit baa2714

2 files changed

Lines changed: 100 additions & 118 deletions

File tree

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.java

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.uimanager
9+
10+
import android.graphics.drawable.ColorDrawable
11+
import android.view.View
12+
import com.facebook.react.bridge.CatalystInstance
13+
import com.facebook.react.bridge.JavaOnlyMap
14+
import com.facebook.react.bridge.ReactApplicationContext
15+
import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance
16+
import com.facebook.react.bridge.ReadableMap
17+
import com.facebook.react.touch.JSResponderHandler
18+
import com.facebook.react.uimanager.annotations.ReactProp
19+
import org.assertj.core.api.Assertions
20+
import org.junit.Before
21+
import org.junit.Rule
22+
import org.junit.Test
23+
import org.junit.runner.RunWith
24+
import org.powermock.core.classloader.annotations.PowerMockIgnore
25+
import org.powermock.modules.junit4.rule.PowerMockRule
26+
import org.robolectric.RobolectricTestRunner
27+
import org.robolectric.RuntimeEnvironment
28+
29+
/** Verify [View] view property being applied properly by [SimpleViewManager] */
30+
@RunWith(RobolectricTestRunner::class)
31+
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*")
32+
class SimpleViewPropertyTest {
33+
34+
@get:Rule var rule = PowerMockRule()
35+
36+
private class ConcreteViewManager : SimpleViewManager<View?>() {
37+
@ReactProp(name = "foo") fun setFoo(view: View, foo: Boolean) {}
38+
39+
@ReactProp(name = "bar") fun setBar(view: View, bar: ReadableMap?) {}
40+
41+
override fun createViewInstance(reactContext: ThemedReactContext): View {
42+
return View(reactContext)
43+
}
44+
45+
override fun getName(): String {
46+
return "View"
47+
}
48+
}
49+
50+
private lateinit var context: ReactApplicationContext
51+
private lateinit var catalystInstanceMock: CatalystInstance
52+
private lateinit var themedContext: ThemedReactContext
53+
private lateinit var manager: ConcreteViewManager
54+
55+
@Before
56+
fun setup() {
57+
context = ReactApplicationContext(RuntimeEnvironment.getApplication())
58+
catalystInstanceMock = createMockCatalystInstance()
59+
context.initializeWithInstance(catalystInstanceMock)
60+
themedContext = ThemedReactContext(context, context)
61+
manager = ConcreteViewManager()
62+
}
63+
64+
fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap {
65+
return ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues))
66+
}
67+
68+
@Test
69+
fun testOpacity() {
70+
val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler())
71+
manager.updateProperties(view, buildStyles())
72+
Assertions.assertThat(view.alpha).isEqualTo(1.0f)
73+
manager.updateProperties(view, buildStyles("opacity", 0.31))
74+
Assertions.assertThat(view.alpha).isEqualTo(0.31f, Assertions.offset(1e-5f))
75+
manager.updateProperties(view, buildStyles("opacity", null))
76+
Assertions.assertThat(view.alpha).isEqualTo(1.0f)
77+
}
78+
79+
@Test
80+
fun testBackgroundColor() {
81+
val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler())
82+
manager.updateProperties(view, buildStyles())
83+
Assertions.assertThat(view.background).isEqualTo(null)
84+
manager.updateProperties(view, buildStyles("backgroundColor", 12))
85+
Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(12)
86+
manager.updateProperties(view, buildStyles("backgroundColor", null))
87+
Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(0)
88+
}
89+
90+
@Test
91+
fun testGetNativeProps() {
92+
val nativeProps = manager.nativeProps
93+
Assertions.assertThat(nativeProps["foo"]).isEqualTo("boolean")
94+
Assertions.assertThat(nativeProps["bar"]).isEqualTo("Map")
95+
}
96+
97+
companion object {
98+
private const val viewTag = 2
99+
}
100+
}

0 commit comments

Comments
 (0)