From e5dfe9964f60a0210ea3ee8a86af75f017c0649a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Henriques?= Date: Wed, 7 Jun 2023 19:00:18 -0700 Subject: [PATCH] Convert FallbackJSBundleLoaderTest to Kotlin (#37750) Summary: This PR converts FallbackJSBundleLoaderTest.java to Kotlin as requested in [this issue](https://github.com/facebook/react-native/issues/37708). ## Changelog: [INTERNAL] [CHANGED] - Convert FallbackJSBundleLoaderTest to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/37750 Test Plan: 1. Run `./gradlew :packages:react-native:ReactAndroid:test`. 2. All tests should pass. Reviewed By: cortinico Differential Revision: D46513866 Pulled By: rshest fbshipit-source-id: fecdb38243e0195c4099a5c0bb54d2e61ca08e79 --- .../bridge/FallbackJSBundleLoaderTest.java | 176 ------------------ .../bridge/FallbackJSBundleLoaderTest.kt | 153 +++++++++++++++ 2 files changed, 153 insertions(+), 176 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.java deleted file mode 100644 index 7c877bff842..00000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.java +++ /dev/null @@ -1,176 +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.bridge; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.facebook.common.logging.FLog; -import com.facebook.common.logging.FakeLoggingDelegate; -import java.util.ArrayList; -import java.util.Arrays; -import org.junit.Before; -import org.junit.Test; - -public class FallbackJSBundleLoaderTest { - - private static final String UNRECOVERABLE; - - static { - String prefix = FallbackJSBundleLoader.RECOVERABLE; - char first = prefix.charAt(0); - - UNRECOVERABLE = prefix.replace(first, (char) (first + 1)); - } - - private FakeLoggingDelegate mLoggingDelegate; - - @Before - public void setup() { - mLoggingDelegate = new FakeLoggingDelegate(); - FLog.setLoggingDelegate(mLoggingDelegate); - } - - @Test - public void firstLoaderSucceeds() { - JSBundleLoader delegates[] = - new JSBundleLoader[] {successfulLoader("url1"), successfulLoader("url2")}; - - FallbackJSBundleLoader fallbackLoader = - new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates))); - - assertThat(fallbackLoader.loadScript(null)).isEqualTo("url1"); - - verify(delegates[0], times(1)).loadScript(null); - verify(delegates[1], never()).loadScript(null); - - assertThat( - mLoggingDelegate.logContains(FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, null)) - .isFalse(); - } - - @Test - public void fallingBackSuccessfully() { - JSBundleLoader delegates[] = - new JSBundleLoader[] { - recoverableLoader("url1", "error1"), successfulLoader("url2"), successfulLoader("url3") - }; - - FallbackJSBundleLoader fallbackLoader = - new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates))); - - assertThat(fallbackLoader.loadScript(null)).isEqualTo("url2"); - - verify(delegates[0], times(1)).loadScript(null); - verify(delegates[1], times(1)).loadScript(null); - verify(delegates[2], never()).loadScript(null); - - assertThat( - mLoggingDelegate.logContains( - FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error1"))) - .isTrue(); - } - - @Test - public void fallingbackUnsuccessfully() { - JSBundleLoader delegates[] = - new JSBundleLoader[] { - recoverableLoader("url1", "error1"), recoverableLoader("url2", "error2") - }; - - FallbackJSBundleLoader fallbackLoader = - new FallbackJSBundleLoader(new ArrayList<>(Arrays.asList(delegates))); - - try { - fallbackLoader.loadScript(null); - fail("expect throw"); - } catch (Exception e) { - assertThat(e).isInstanceOf(RuntimeException.class); - - Throwable cause = e.getCause(); - ArrayList msgs = new ArrayList<>(); - while (cause != null) { - msgs.add(cause.getMessage()); - cause = cause.getCause(); - } - - assertThat(msgs).containsExactly(recoverableMsg("error1"), recoverableMsg("error2")); - } - - verify(delegates[0], times(1)).loadScript(null); - verify(delegates[1], times(1)).loadScript(null); - - assertThat( - mLoggingDelegate.logContains( - FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error1"))) - .isTrue(); - - assertThat( - mLoggingDelegate.logContains( - FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error2"))) - .isTrue(); - } - - @Test - public void unrecoverable() { - JSBundleLoader delegates[] = - new JSBundleLoader[] {fatalLoader("url1", "error1"), recoverableLoader("url2", "error2")}; - - FallbackJSBundleLoader fallbackLoader = - new FallbackJSBundleLoader(new ArrayList(Arrays.asList(delegates))); - - try { - fallbackLoader.loadScript(null); - fail("expect throw"); - } catch (Exception e) { - assertThat(e.getMessage()).isEqualTo(fatalMsg("error1")); - } - - verify(delegates[0], times(1)).loadScript(null); - verify(delegates[1], never()).loadScript(null); - - assertThat( - mLoggingDelegate.logContains(FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, null)) - .isFalse(); - } - - private static JSBundleLoader successfulLoader(String url) { - JSBundleLoader loader = mock(JSBundleLoader.class); - when(loader.loadScript(null)).thenReturn(url); - - return loader; - } - - private static String recoverableMsg(String errMsg) { - return FallbackJSBundleLoader.RECOVERABLE + errMsg; - } - - private static JSBundleLoader recoverableLoader(String url, String errMsg) { - JSBundleLoader loader = mock(JSBundleLoader.class); - when(loader.loadScript(null)) - .thenThrow(new RuntimeException(FallbackJSBundleLoader.RECOVERABLE + errMsg)); - - return loader; - } - - private static String fatalMsg(String errMsg) { - return UNRECOVERABLE + errMsg; - } - - private static JSBundleLoader fatalLoader(String url, String errMsg) { - JSBundleLoader loader = mock(JSBundleLoader.class); - when(loader.loadScript(null)).thenThrow(new RuntimeException(UNRECOVERABLE + errMsg)); - - return loader; - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.kt new file mode 100644 index 00000000000..62c0b4d46ba --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/FallbackJSBundleLoaderTest.kt @@ -0,0 +1,153 @@ +/* + * 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.bridge + +import com.facebook.common.logging.FLog +import com.facebook.common.logging.FakeLoggingDelegate +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.fail +import org.junit.Before +import org.junit.Test +import org.mockito.Mockito.* +import org.mockito.Mockito.`when` as whenever + +class FallbackJSBundleLoaderTest { + private lateinit var UNRECOVERABLE: String + private lateinit var loggingDelegate: FakeLoggingDelegate + + @Before + fun setup() { + val prefix = FallbackJSBundleLoader.RECOVERABLE + val first = prefix[0] + UNRECOVERABLE = prefix.replace(first, (first.code + 1).toChar()) + + loggingDelegate = FakeLoggingDelegate() + FLog.setLoggingDelegate(loggingDelegate) + } + + @Test + fun firstLoaderSucceeds() { + val delegates = arrayOf(successfulLoader("url1"), successfulLoader("url2")) + + val fallbackLoader = FallbackJSBundleLoader(listOf(*delegates)) + + assertThat(fallbackLoader.loadScript(null)).isEqualTo("url1") + + verify(delegates[0], times(1)).loadScript(null) + verify(delegates[1], never()).loadScript(null) + + assertThat( + loggingDelegate.logContains(FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, null)) + .isFalse + } + + @Test + fun fallingBackSuccessfully() { + val delegates = + arrayOf( + recoverableLoader("url1", "error1"), successfulLoader("url2"), successfulLoader("url3")) + + val fallbackLoader = FallbackJSBundleLoader(listOf(*delegates)) + + assertThat(fallbackLoader.loadScript(null)).isEqualTo("url2") + + verify(delegates[0], times(1)).loadScript(null) + verify(delegates[1], times(1)).loadScript(null) + verify(delegates[2], never()).loadScript(null) + + assertThat( + loggingDelegate.logContains( + FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error1"))) + .isTrue + } + + @Test + fun fallingbackUnsuccessfully() { + val delegates = + arrayOf(recoverableLoader("url1", "error1"), recoverableLoader("url2", "error2")) + + val fallbackLoader = FallbackJSBundleLoader(listOf(*delegates)) + + try { + fallbackLoader.loadScript(null) + fail("expect throw") + } catch (e: Exception) { + assertThat(e).isInstanceOf(RuntimeException::class.java) + + var cause = e.cause + val msgs = mutableListOf() + + while (cause != null) { + msgs.add(cause.message) + cause = cause.cause + } + + assertThat(msgs).containsExactly(recoverableMsg("error1"), recoverableMsg("error2")) + } + + verify(delegates[0], times(1)).loadScript(null) + verify(delegates[1], times(1)).loadScript(null) + + assertThat( + loggingDelegate.logContains( + FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error1"))) + .isTrue + + assertThat( + loggingDelegate.logContains( + FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, recoverableMsg("error2"))) + .isTrue + } + + @Test + fun unrecoverable() { + val delegates = arrayOf(fatalLoader("url1", "error1"), recoverableLoader("url2", "error2")) + + val fallbackLoader = FallbackJSBundleLoader(listOf(*delegates)) + + try { + fallbackLoader.loadScript(null) + fail("expect throw") + } catch (e: Exception) { + assertThat(e.message).isEqualTo(fatalMsg("error1")) + } + + verify(delegates[0], times(1)).loadScript(null) + verify(delegates[1], never()).loadScript(null) + + assertThat( + loggingDelegate.logContains(FakeLoggingDelegate.WTF, FallbackJSBundleLoader.TAG, null)) + .isFalse + } + + private fun successfulLoader(url: String): JSBundleLoader { + val loader = mock(JSBundleLoader::class.java) + whenever(loader.loadScript(null)).thenReturn(url) + + return loader + } + + private fun recoverableMsg(errMsg: String): String = FallbackJSBundleLoader.RECOVERABLE + errMsg + + private fun recoverableLoader(url: String, errMsg: String): JSBundleLoader { + val loader = mock(JSBundleLoader::class.java) + whenever(loader.loadScript(null)) + .thenThrow(RuntimeException(FallbackJSBundleLoader.RECOVERABLE + errMsg)) + + return loader + } + + private fun fatalMsg(errMsg: String): String = UNRECOVERABLE + errMsg + + private fun fatalLoader(url: String, errMsg: String): JSBundleLoader { + val loader = mock(JSBundleLoader::class.java) + whenever(loader.loadScript(null)).thenThrow(RuntimeException(UNRECOVERABLE + errMsg)) + + return loader + } +}