From c5e0e2d169162e9f87ee21ad7510886be6f0c26a Mon Sep 17 00:00:00 2001 From: fathonyfath Date: Fri, 9 Jun 2023 13:18:36 -0700 Subject: [PATCH] Convert DialogModuleTest to Kotlin (#37795) Summary: This PR converts DialogModuleTest into Kotlin as requested in [this issue](https://github.com/facebook/react-native/issues/37708). ## Changelog: [INTERNAL] [CHANGED] - Convert DialogModuleTest to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/37795 Test Plan: 1. Run `./gradlew :packages:react-native:ReactAndroid:test`. 2. All tests should pass. Reviewed By: cortinico Differential Revision: D46596235 Pulled By: mdvacca fbshipit-source-id: ef4184664ad885ebd2e8c1d51ca5bb7dc48f0610 --- .../modules/dialog/DialogModuleTest.java | 176 ------------------ .../react/modules/dialog/DialogModuleTest.kt | 161 ++++++++++++++++ 2 files changed, 161 insertions(+), 176 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.java deleted file mode 100644 index d93357166e415d..00000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.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.modules.dialog; - -import static android.os.Looper.getMainLooper; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.when; -import static org.robolectric.Shadows.shadowOf; - -import android.app.AlertDialog; -import android.content.DialogInterface; -import androidx.fragment.app.FragmentActivity; -import com.facebook.react.bridge.Callback; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReactApplicationContext; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.robolectric.Robolectric; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.android.controller.ActivityController; - -@RunWith(RobolectricTestRunner.class) -public class DialogModuleTest { - - private ActivityController mActivityController; - private FragmentActivity mActivity; - private DialogModule mDialogModule; - - static final class SimpleCallback implements Callback { - private Object[] mArgs; - private int mCalls; - - @Override - public void invoke(Object... args) { - mCalls++; - mArgs = args; - } - - public int getCalls() { - return mCalls; - } - - public Object[] getArgs() { - return mArgs; - } - } - - @Before - public void setUp() throws Exception { - mActivityController = Robolectric.buildActivity(FragmentActivity.class); - mActivity = mActivityController.create().start().resume().get(); - - final ReactApplicationContext context = Mockito.mock(ReactApplicationContext.class); - when(context.hasActiveReactInstance()).thenReturn(true); - when(context.getCurrentActivity()).thenReturn(mActivity); - - mDialogModule = new DialogModule(context); - mDialogModule.onHostResume(); - } - - @After - public void tearDown() { - mActivityController.pause().stop().destroy(); - - mActivityController = null; - mDialogModule = null; - } - - @Test - public void testAllOptions() { - final JavaOnlyMap options = new JavaOnlyMap(); - options.putString("title", "Title"); - options.putString("message", "Message"); - options.putString("buttonPositive", "OK"); - options.putString("buttonNegative", "Cancel"); - options.putString("buttonNeutral", "Later"); - options.putBoolean("cancelable", false); - - mDialogModule.showAlert(options, null, null); - shadowOf(getMainLooper()).idle(); - - final AlertFragment fragment = getFragment(); - - assertNotNull("Fragment was not displayed", fragment); - assertFalse(fragment.isCancelable()); - - final AlertDialog dialog = (AlertDialog) fragment.getDialog(); - assertEquals("OK", dialog.getButton(DialogInterface.BUTTON_POSITIVE).getText().toString()); - assertEquals("Cancel", dialog.getButton(DialogInterface.BUTTON_NEGATIVE).getText().toString()); - assertEquals("Later", dialog.getButton(DialogInterface.BUTTON_NEUTRAL).getText().toString()); - } - - @Test - public void testCallbackPositive() { - final JavaOnlyMap options = new JavaOnlyMap(); - options.putString("buttonPositive", "OK"); - - final SimpleCallback actionCallback = new SimpleCallback(); - mDialogModule.showAlert(options, null, actionCallback); - shadowOf(getMainLooper()).idle(); - - final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); - dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); - shadowOf(getMainLooper()).idle(); - - assertEquals(1, actionCallback.getCalls()); - assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); - assertEquals(DialogInterface.BUTTON_POSITIVE, actionCallback.getArgs()[1]); - } - - @Test - public void testCallbackNegative() { - final JavaOnlyMap options = new JavaOnlyMap(); - options.putString("buttonNegative", "Cancel"); - - final SimpleCallback actionCallback = new SimpleCallback(); - mDialogModule.showAlert(options, null, actionCallback); - shadowOf(getMainLooper()).idle(); - - final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); - dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick(); - shadowOf(getMainLooper()).idle(); - - assertEquals(1, actionCallback.getCalls()); - assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); - assertEquals(DialogInterface.BUTTON_NEGATIVE, actionCallback.getArgs()[1]); - } - - @Test - public void testCallbackNeutral() { - final JavaOnlyMap options = new JavaOnlyMap(); - options.putString("buttonNeutral", "Later"); - - final SimpleCallback actionCallback = new SimpleCallback(); - mDialogModule.showAlert(options, null, actionCallback); - shadowOf(getMainLooper()).idle(); - - final AlertDialog dialog = (AlertDialog) getFragment().getDialog(); - dialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); - shadowOf(getMainLooper()).idle(); - - assertEquals(1, actionCallback.getCalls()); - assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.getArgs()[0]); - assertEquals(DialogInterface.BUTTON_NEUTRAL, actionCallback.getArgs()[1]); - } - - @Test - public void testCallbackDismiss() { - final JavaOnlyMap options = new JavaOnlyMap(); - - final SimpleCallback actionCallback = new SimpleCallback(); - mDialogModule.showAlert(options, null, actionCallback); - shadowOf(getMainLooper()).idle(); - - getFragment().getDialog().dismiss(); - shadowOf(getMainLooper()).idle(); - - assertEquals(1, actionCallback.getCalls()); - assertEquals(DialogModule.ACTION_DISMISSED, actionCallback.getArgs()[0]); - } - - private AlertFragment getFragment() { - return (AlertFragment) - mActivity.getSupportFragmentManager().findFragmentByTag(DialogModule.FRAGMENT_TAG); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.kt new file mode 100644 index 00000000000000..94d4d63e664646 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/dialog/DialogModuleTest.kt @@ -0,0 +1,161 @@ +/* + * 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.modules.dialog + +import android.app.AlertDialog +import android.content.DialogInterface +import android.os.Looper.getMainLooper +import androidx.fragment.app.FragmentActivity +import com.facebook.react.bridge.Callback +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReactApplicationContext +import org.junit.* +import org.junit.Assert.* +import org.junit.runner.RunWith +import org.mockito.Mockito.mock +import org.powermock.api.mockito.PowerMockito.`when` as whenever +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.android.controller.ActivityController + +@RunWith(RobolectricTestRunner::class) +class DialogModuleTest { + + private lateinit var activityController: ActivityController + private lateinit var activity: FragmentActivity + private lateinit var dialogModule: DialogModule + + class SimpleCallback : Callback { + var args: Array? = null + private set + + var calls: Int = 0 + private set + + override fun invoke(vararg args: Any?) { + this.calls++ + this.args = args + } + } + + @Before + fun setUp() { + activityController = Robolectric.buildActivity(FragmentActivity::class.java) + activity = activityController.create().start().resume().get() + + val context: ReactApplicationContext = mock(ReactApplicationContext::class.java) + whenever(context.hasActiveReactInstance()).thenReturn(true) + whenever(context.currentActivity).thenReturn(activity) + + dialogModule = DialogModule(context) + dialogModule.onHostResume() + } + + @After + fun tearDown() { + activityController.pause().stop().destroy() + } + + @Test + fun testAllOptions() { + val options = + JavaOnlyMap().apply { + putString("title", "Title") + putString("message", "Message") + putString("buttonPositive", "OK") + putString("buttonNegative", "Cancel") + putString("buttonNeutral", "Later") + putBoolean("cancelable", false) + } + + dialogModule.showAlert(options, null, null) + shadowOf(getMainLooper()).idle() + + val fragment = getFragment() + + assertNotNull("Fragment was not displayed", fragment) + assertFalse(fragment!!.isCancelable) + + val dialog = fragment.dialog as AlertDialog + assertEquals("OK", dialog.getButton(DialogInterface.BUTTON_POSITIVE).text.toString()) + assertEquals("Cancel", dialog.getButton(DialogInterface.BUTTON_NEGATIVE).text.toString()) + assertEquals("Later", dialog.getButton(DialogInterface.BUTTON_NEUTRAL).text.toString()) + } + + @Test + fun testCallbackPositive() { + val options = JavaOnlyMap().apply { putString("buttonPositive", "OK") } + + val actionCallback = SimpleCallback() + dialogModule.showAlert(options, null, actionCallback) + shadowOf(getMainLooper()).idle() + + val dialog = getFragment()!!.dialog as AlertDialog + dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick() + shadowOf(getMainLooper()).idle() + + assertEquals(1, actionCallback.calls) + assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.args!![0]) + assertEquals(DialogInterface.BUTTON_POSITIVE, actionCallback.args!![1]) + } + + @Test + fun testCallbackNegative() { + val options = JavaOnlyMap().apply { putString("buttonNegative", "Cancel") } + + val actionCallback = SimpleCallback() + dialogModule.showAlert(options, null, actionCallback) + shadowOf(getMainLooper()).idle() + + val dialog = getFragment()!!.dialog as AlertDialog + dialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick() + shadowOf(getMainLooper()).idle() + + assertEquals(1, actionCallback.calls) + assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.args!![0]) + assertEquals(DialogInterface.BUTTON_NEGATIVE, actionCallback.args!![1]) + } + + @Test + fun testCallbackNeutral() { + val options = JavaOnlyMap().apply { putString("buttonNeutral", "Later") } + + val actionCallback = SimpleCallback() + dialogModule.showAlert(options, null, actionCallback) + shadowOf(getMainLooper()).idle() + + val dialog = getFragment()!!.dialog as AlertDialog + dialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick() + shadowOf(getMainLooper()).idle() + + assertEquals(1, actionCallback.calls) + assertEquals(DialogModule.ACTION_BUTTON_CLICKED, actionCallback.args!![0]) + assertEquals(DialogInterface.BUTTON_NEUTRAL, actionCallback.args!![1]) + } + + @Test + fun testCallbackDismiss() { + val options = JavaOnlyMap() + + val actionCallback = SimpleCallback() + dialogModule.showAlert(options, null, actionCallback) + shadowOf(getMainLooper()).idle() + + getFragment()!!.dialog!!.dismiss() + shadowOf(getMainLooper()).idle() + + assertEquals(1, actionCallback.calls) + assertEquals(DialogModule.ACTION_DISMISSED, actionCallback.args!![0]) + } + + private fun getFragment(): AlertFragment? { + return activity.supportFragmentManager.findFragmentByTag(DialogModule.FRAGMENT_TAG) + as? AlertFragment + } +}