Convert Java tests for JSDebuggerWebSocketClient to Kotlin (#38941)

Summary:
Migrate Java tests to Kotlin for the JSDebuggerWebSocketClient class.

## Changelog:

Pick one each for the category and type tags:

[ANDROID] [CHANGED] - Update Java tests to Kotlin for the referenced file

Pull Request resolved: https://github.com/facebook/react-native/pull/38941

Test Plan: All unit tests passed.

Reviewed By: mdvacca, rshest

Differential Revision: D48354172

Pulled By: cortinico

fbshipit-source-id: e8d3c98216b4e2c3ff462433c7e37d628d06f0c6
This commit is contained in:
Stewart Sum
2023-08-18 05:22:09 -07:00
committed by Facebook GitHub Bot
parent c326ec0475
commit 3dbb759506
3 changed files with 118 additions and 134 deletions
@@ -173,7 +173,7 @@ public class JSDebuggerWebSocketClient extends WebSocketListener {
}
@Override
public void onMessage(WebSocket webSocket, String text) {
public void onMessage(@Nullable WebSocket webSocket, String text) {
Integer replyID = null;
try {
@@ -1,133 +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.devsupport;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import com.facebook.react.common.JavascriptException;
import java.util.HashMap;
import okhttp3.WebSocket;
import okio.ByteString;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
@Ignore("Ignored due to unsupported mocking mechanism with JDK 18")
public class JSDebuggerWebSocketClientTest {
@Test
public void test_prepareJSRuntime_ShouldSendCorrectMessage() throws Exception {
final JSDebuggerWebSocketClient.JSDebuggerCallback cb =
mock(JSDebuggerWebSocketClient.JSDebuggerCallback.class);
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.prepareJSRuntime(cb);
verify(client).sendMessage(0, "{\"id\":0,\"method\":\"prepareJSRuntime\"}");
}
@Test
public void test_loadBundle_ShouldSendCorrectMessage() throws Exception {
final JSDebuggerWebSocketClient.JSDebuggerCallback cb =
mock(JSDebuggerWebSocketClient.JSDebuggerCallback.class);
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
HashMap<String, String> injectedObjects = new HashMap<>();
injectedObjects.put("key1", "value1");
injectedObjects.put("key2", "value2");
client.loadBundle("http://localhost:8080/index.js", injectedObjects, cb);
verify(client)
.sendMessage(
0,
"{\"id\":0,\"method\":\"executeApplicationScript\",\"url\":\"http://localhost:8080/index.js\""
+ ",\"inject\":{\"key1\":\"value1\",\"key2\":\"value2\"}}");
}
@Test
public void test_executeJSCall_ShouldSendCorrectMessage() throws Exception {
final JSDebuggerWebSocketClient.JSDebuggerCallback cb =
mock(JSDebuggerWebSocketClient.JSDebuggerCallback.class);
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.executeJSCall("foo", "[1,2,3]", cb);
verify(client).sendMessage(0, "{\"id\":0,\"method\":\"foo\",\"arguments\":[1,2,3]}");
}
@Test
public void test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(
mock(WebSocket.class), ByteString.encodeUtf8("{\"replyID\":0, \"result\":\"OK\"}"));
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String.class));
verify(client, never()).triggerRequestFailure(anyInt(), any());
}
@Test
public void test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"result\":\"OK\"}");
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String.class));
verify(client, never()).triggerRequestFailure(anyInt(), any());
}
@Test
public void test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":null, \"result\":\"OK\"}");
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String.class));
verify(client, never()).triggerRequestFailure(anyInt(), any());
}
@Test
public void test_onMessage_WithResult_ShouldTriggerRequestSuccess() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":0, \"result\":\"OK\"}");
verify(client).triggerRequestSuccess(0, "OK");
verify(client, never()).triggerRequestFailure(anyInt(), any());
}
@Test
public void test_onMessage_With_Null_Result_ShouldTriggerRequestSuccess() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":0, \"result\":null}");
verify(client).triggerRequestSuccess(0, null);
verify(client, never()).triggerRequestFailure(anyInt(), any());
}
@Test
public void test_onMessage_WithError_ShouldCallAbort() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":0, \"error\":\"BOOM\"}");
verify(client).abort(eq("BOOM"), isA(JavascriptException.class));
}
@Test
public void test_onMessage_With_Null_Error_ShouldTriggerRequestSuccess() throws Exception {
JSDebuggerWebSocketClient client = spy(new JSDebuggerWebSocketClient());
client.onMessage(null, "{\"replyID\":0, \"error\":null}");
verify(client).triggerRequestSuccess(anyInt(), nullable(String.class));
}
}
@@ -0,0 +1,117 @@
/*
* 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.devsupport
import com.facebook.react.common.JavascriptException
import com.facebook.react.devsupport.JSDebuggerWebSocketClient.JSDebuggerCallback
import okhttp3.WebSocket
import okio.ByteString
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.eq
import org.mockito.ArgumentMatchers.isA
import org.mockito.ArgumentMatchers.nullable
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class JSDebuggerWebSocketClientTest {
@Test
fun test_prepareJSRuntime_ShouldSendCorrectMessage() {
val cb = mock(JSDebuggerCallback::class.java)
val client = spy(JSDebuggerWebSocketClient())
client.prepareJSRuntime(cb)
verify(client).sendMessage(0, "{\"id\":0,\"method\":\"prepareJSRuntime\"}")
}
@Test
fun test_loadBundle_ShouldSendCorrectMessage() {
val cb = mock(JSDebuggerCallback::class.java)
val client = spy(JSDebuggerWebSocketClient())
val injectedObjects = mapOf("key1" to "value1", "key2" to "value2")
client.loadBundle(
"http://localhost:8080/index.js", injectedObjects as HashMap<String, String>?, cb)
verify(client)
.sendMessage(
0,
"{\"id\":0,\"method\":\"executeApplicationScript\",\"url\":\"http://localhost:8080/index.js\"" +
",\"inject\":{\"key1\":\"value1\",\"key2\":\"value2\"}}")
}
@Test
fun test_executeJSCall_ShouldSendCorrectMessage() {
val cb = mock(JSDebuggerCallback::class.java)
val client = spy(JSDebuggerWebSocketClient())
client.executeJSCall("foo", "[1,2,3]", cb)
verify(client).sendMessage(0, "{\"id\":0,\"method\":\"foo\",\"arguments\":[1,2,3]}")
}
@Test
fun test_onMessage_WithInvalidContentType_ShouldNotTriggerCallbacks() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(mock(WebSocket::class.java), encodeUtf8("{\"replyID\":0, \"result\":\"OK\"}"))
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String::class.java))
verify(client, never()).triggerRequestFailure(anyInt(), any())
}
@Test
fun test_onMessage_WithoutReplyId_ShouldNotTriggerCallbacks() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"result\":\"OK\"}")
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String::class.java))
verify(client, never()).triggerRequestFailure(anyInt(), any())
}
@Test
fun test_onMessage_With_Null_ReplyId_ShouldNotTriggerCallbacks() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"replyID\":null, \"result\":\"OK\"}")
verify(client, never()).triggerRequestSuccess(anyInt(), nullable(String::class.java))
verify(client, never()).triggerRequestFailure(anyInt(), any())
}
@Test
fun test_onMessage_WithResult_ShouldTriggerRequestSuccess() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"replyID\":0, \"result\":\"OK\"}")
verify(client).triggerRequestSuccess(0, "OK")
verify(client, never()).triggerRequestFailure(anyInt(), any())
}
@Test
fun test_onMessage_With_Null_Result_ShouldTriggerRequestSuccess() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"replyID\":0, \"result\":null}")
verify(client).triggerRequestSuccess(0, null)
verify(client, never()).triggerRequestFailure(anyInt(), any())
}
@Test
fun test_onMessage_WithError_ShouldCallAbort() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"replyID\":0, \"error\":\"BOOM\"}")
verify(client).abort(eq("BOOM"), isA(JavascriptException::class.java))
}
@Test
fun test_onMessage_With_Null_Error_ShouldTriggerRequestSuccess() {
val client = spy(JSDebuggerWebSocketClient())
client.onMessage(null, "{\"replyID\":0, \"error\":null}")
verify(client).triggerRequestSuccess(anyInt(), nullable(String::class.java))
}
private fun encodeUtf8(input: String): ByteString =
ByteString.of(*input.toByteArray(Charsets.UTF_8))
}