Skip to content

Commit 491ede6

Browse files
mateoguzmanafacebook-github-bot
authored andcommitted
Migrate com.facebook.react.packagerconnection interfaces to Kotlin (#49025)
Summary: Migrate com.facebook.react.packagerconnection interfaces to Kotlin. Also, moving to `org.mockito.kotlin` instead of `org.mockito.Mockito` for JSPackagerClientTest to make it compatible with the migrated files. ## Changelog: [INTERNAL] - Migrate com.facebook.react.packagerconnection interfaces to Kotlin Pull Request resolved: #49025 Test Plan: ```bash yarn test-android yarn android ``` Reviewed By: cortinico, Abbondanzo Differential Revision: D68842336 Pulled By: tdn120 fbshipit-source-id: c3062675b5535277970fd97490720739fc748f2a
1 parent cd78f44 commit 491ede6

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/RequestHandler.java packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/RequestHandler.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
package com.facebook.react.packagerconnection;
9-
10-
import androidx.annotation.Nullable;
8+
package com.facebook.react.packagerconnection
119

1210
public interface RequestHandler {
13-
void onRequest(@Nullable Object params, Responder responder);
11+
public fun onRequest(params: Any?, responder: Responder)
1412

15-
void onNotification(@Nullable Object params);
13+
public fun onNotification(params: Any?)
1614
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/Responder.java packages/react-native/ReactAndroid/src/main/java/com/facebook/react/packagerconnection/Responder.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
package com.facebook.react.packagerconnection;
8+
package com.facebook.react.packagerconnection
99

1010
public interface Responder {
11-
void respond(Object result);
11+
public fun respond(result: Any)
1212

13-
void error(Object error);
13+
public fun error(error: Any)
1414
}

packages/react-native/ReactAndroid/src/test/java/com/facebook/react/packagerconnection/JSPackagerClientTest.kt

+28-24
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import okio.ByteString
1313
import org.junit.Before
1414
import org.junit.Test
1515
import org.junit.runner.RunWith
16-
import org.mockito.ArgumentMatchers
17-
import org.mockito.Mockito.*
18-
import org.mockito.Mockito.`when` as whenever
16+
import org.mockito.kotlin.any
17+
import org.mockito.kotlin.eq
18+
import org.mockito.kotlin.mock
19+
import org.mockito.kotlin.never
20+
import org.mockito.kotlin.times
21+
import org.mockito.kotlin.verify
22+
import org.mockito.kotlin.whenever
1923
import org.robolectric.RobolectricTestRunner
2024

2125
@RunWith(RobolectricTestRunner::class)
@@ -25,105 +29,105 @@ class JSPackagerClientTest {
2529

2630
@Before
2731
fun setup() {
28-
settings = mock(PackagerConnectionSettings::class.java)
32+
settings = mock<PackagerConnectionSettings>()
2933
whenever(settings.debugServerHost).thenReturn("ws://not_needed")
3034
whenever(settings.packageName).thenReturn("my_test_package")
3135
}
3236

3337
@Test
3438
@Throws(IOException::class)
3539
fun test_onMessage_ShouldTriggerNotification() {
36-
val handler = mock(RequestHandler::class.java)
40+
val handler = mock<RequestHandler>()
3741
val client = getClient(createRequestHandler("methodValue", handler))
3842

3943
client.onMessage("""{"version": 2, "method": "methodValue", "params": "paramsValue"}""")
4044
verify(handler).onNotification(eq("paramsValue"))
41-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
45+
verify(handler, never()).onRequest(any(), any())
4246
}
4347

4448
@Test
4549
@Throws(IOException::class)
4650
fun test_onMessage_ShouldTriggerRequest() {
47-
val handler = mock(RequestHandler::class.java)
51+
val handler = mock<RequestHandler>()
4852
val client = getClient(createRequestHandler("methodValue", handler))
4953

5054
client.onMessage(
5155
"""{"version": 2, "id": "idValue", "method": "methodValue", "params": "paramsValue"}""")
5256
verify(handler, never()).onNotification(any())
53-
verify(handler).onRequest(eq("paramsValue"), any(Responder::class.java))
57+
verify(handler).onRequest(eq("paramsValue"), any())
5458
}
5559

5660
@Test
5761
@Throws(IOException::class)
5862
fun test_onMessage_WithoutParams_ShouldTriggerNotification() {
59-
val handler = mock(RequestHandler::class.java)
63+
val handler = mock<RequestHandler>()
6064
val client = getClient(createRequestHandler("methodValue", handler))
6165

6266
client.onMessage("""{"version": 2, "method": "methodValue"}""")
63-
verify(handler).onNotification(ArgumentMatchers.isNull())
64-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
67+
verify(handler).onNotification(eq(null))
68+
verify(handler, never()).onRequest(any(), any())
6569
}
6670

6771
@Test
6872
@Throws(IOException::class)
6973
fun test_onMessage_WithInvalidContentType_ShouldNotTriggerCallback() {
70-
val handler = mock(RequestHandler::class.java)
74+
val handler = mock<RequestHandler>()
7175
val client = getClient(createRequestHandler("methodValue", handler))
7276

7377
client.onMessage(encodeUtf8("""{"version": 2, "method": "methodValue"}"""))
7478
verify(handler, never()).onNotification(any())
75-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
79+
verify(handler, never()).onRequest(any(), any())
7680
}
7781

7882
@Test
7983
@Throws(IOException::class)
8084
fun test_onMessage_WithoutMethod_ShouldNotTriggerCallback() {
81-
val handler = mock(RequestHandler::class.java)
85+
val handler = mock<RequestHandler>()
8286
val client = getClient(createRequestHandler("methodValue", handler))
8387

8488
client.onMessage("""{"version": 2}""")
8589
verify(handler, never()).onNotification(any())
86-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
90+
verify(handler, never()).onRequest(any(), any())
8791
}
8892

8993
@Test
9094
@Throws(IOException::class)
9195
fun test_onMessage_With_Null_Action_ShouldNotTriggerCallback() {
92-
val handler = mock(RequestHandler::class.java)
96+
val handler = mock<RequestHandler>()
9397
val client = getClient(createRequestHandler("methodValue", handler))
9498

9599
client.onMessage("""{"version": 2, "method": null}""")
96100
verify(handler, never()).onNotification(any())
97-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
101+
verify(handler, never()).onRequest(any(), any())
98102
}
99103

100104
@Test
101105
@Throws(IOException::class)
102106
fun test_onMessage_WithInvalidMethod_ShouldNotTriggerCallback() {
103-
val handler = mock(RequestHandler::class.java)
107+
val handler = mock<RequestHandler>()
104108
val client = getClient(createRequestHandler("methodValue", handler))
105109

106110
client.onMessage(ByteString.EMPTY)
107111
verify(handler, never()).onNotification(any())
108-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
112+
verify(handler, never()).onRequest(any(), any())
109113
}
110114

111115
@Test
112116
@Throws(IOException::class)
113117
fun test_onMessage_WrongVersion_ShouldNotTriggerCallback() {
114-
val handler = mock(RequestHandler::class.java)
118+
val handler = mock<RequestHandler>()
115119
val client = getClient(createRequestHandler("methodValue", handler))
116120

117121
client.onMessage("""{"version": 1, "method": "methodValue"}""")
118122
verify(handler, never()).onNotification(any())
119-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
123+
verify(handler, never()).onRequest(any(), any())
120124
}
121125

122126
@Test
123127
@Throws(IOException::class)
124128
fun test_onDisconnection_ShouldTriggerDisconnectionCallback() {
125-
val connectionHandler = mock(ConnectionCallback::class.java)
126-
val handler = mock(RequestHandler::class.java)
129+
val connectionHandler = mock<ConnectionCallback>()
130+
val handler = mock<RequestHandler>()
127131
val client = getClient(requestHandlers = emptyMap(), connectionCallback = connectionHandler)
128132

129133
client.close()
@@ -132,7 +136,7 @@ class JSPackagerClientTest {
132136
verify(connectionHandler, times(1)).onDisconnected()
133137

134138
verify(handler, never()).onNotification(any())
135-
verify(handler, never()).onRequest(any(), any(Responder::class.java))
139+
verify(handler, never()).onRequest(any(), any())
136140
}
137141

138142
private fun getClient(

0 commit comments

Comments
 (0)