diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java index 130630ef577..98abf37c6b2 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/JSDebuggerWebSocketClient.java @@ -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 { diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.java deleted file mode 100644 index 25d7f418971..00000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.java +++ /dev/null @@ -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 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)); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.kt new file mode 100644 index 00000000000..fbef2c7dc9e --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/devsupport/JSDebuggerWebSocketClientTest.kt @@ -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?, 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)) +}