Skip to content

Commit

Permalink
Migrate JSPackagerClientTest to Kotlin (#37747)
Browse files Browse the repository at this point in the history
Summary:
Migrate [JSPackagerClientTest.java](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/packagerconnection/JSPackagerClientTest.java) to Kotlin.  See #37708

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[INTERNAL] [CHANGED] - Migrate JSPackagerClientTest to Kotlin

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[INTERNAL] [CHANGED] - Migrate JSPackagerClientTest to Kotlin

Pull Request resolved: #37747

Test Plan:
Tests pass: ./gradlew :packages:react-native:ReactAndroid:test
Formatted with [KtFmt](https://facebook.github.io/ktfmt/)

Reviewed By: cortinico

Differential Revision: D46514210

Pulled By: rshest

fbshipit-source-id: ecec18a69b1401214e22c6cbea34d49528da1a79
  • Loading branch information
brunohensel authored and facebook-github-bot committed Jun 8, 2023
1 parent e1206da commit ad6718a
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 144 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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.packagerconnection

import com.facebook.react.packagerconnection.ReconnectingWebSocket.ConnectionCallback
import java.io.IOException
import okio.ByteString
import okio.ByteString.encodeUtf8
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.*
import org.mockito.Mockito.`when` as whenever
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class JSPackagerClientTest {

private lateinit var settings: PackagerConnectionSettings

@Before
fun setup() {
settings = mock(PackagerConnectionSettings::class.java)
whenever(settings.debugServerHost).thenReturn("ws://not_needed")
whenever(settings.packageName).thenReturn("my_test_package")
}

@Test
@Throws(IOException::class)
fun test_onMessage_ShouldTriggerNotification() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage("""{"version": 2, "method": "methodValue", "params": "paramsValue"}""")
verify(handler).onNotification(eq("paramsValue"))
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_ShouldTriggerRequest() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage(
"""{"version": 2, "id": "idValue", "method": "methodValue", "params": "paramsValue"}""")
verify(handler, never()).onNotification(any())
verify(handler).onRequest(eq("paramsValue"), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_WithoutParams_ShouldTriggerNotification() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage("""{"version": 2, "method": "methodValue"}""")
verify(handler).onNotification(ArgumentMatchers.isNull())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_WithInvalidContentType_ShouldNotTriggerCallback() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage(encodeUtf8("""{"version": 2, "method": "methodValue"}"""))
verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_WithoutMethod_ShouldNotTriggerCallback() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage("""{"version": 2}""")
verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_With_Null_Action_ShouldNotTriggerCallback() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage("""{"version": 2, "method": null}""")
verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_WithInvalidMethod_ShouldNotTriggerCallback() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage(ByteString.EMPTY)
verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onMessage_WrongVersion_ShouldNotTriggerCallback() {
val handler = mock(RequestHandler::class.java)
val client = getClient(createRequestHandler("methodValue", handler))

client.onMessage("""{"version": 1, "method": "methodValue"}""")
verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

@Test
@Throws(IOException::class)
fun test_onDisconnection_ShouldTriggerDisconnectionCallback() {
val connectionHandler = mock(ConnectionCallback::class.java)
val handler = mock(RequestHandler::class.java)
val client = getClient(requestHandlers = emptyMap(), connectionCallback = connectionHandler)

client.close()

verify(connectionHandler, never()).onConnected()
verify(connectionHandler, times(1)).onDisconnected()

verify(handler, never()).onNotification(any())
verify(handler, never()).onRequest(any(), any(Responder::class.java))
}

private fun getClient(
requestHandlers: Map<String, RequestHandler>,
clientId: String = "test_client",
settings: PackagerConnectionSettings = this.settings,
connectionCallback: ConnectionCallback? = null
): JSPackagerClient = JSPackagerClient(clientId, settings, requestHandlers, connectionCallback)

private fun createRequestHandler(
action: String,
handler: RequestHandler
): Map<String, RequestHandler> = mapOf(action to handler)
}

0 comments on commit ad6718a

Please sign in to comment.