From d1bf6d0e1d2054ea70f8e5413273d0fe9646d5fc Mon Sep 17 00:00:00 2001 From: Madhur Gupta <30932899+madhurgupta10@users.noreply.github.com> Date: Tue, 7 Dec 2021 01:58:58 +0530 Subject: [PATCH] Add CommonPlaceClickActions Unit Tests (#4674) --- .../fragments/CommonPlaceClickActions.kt | 22 ++-- .../nearby/CommonPlaceClickActionsUnitTest.kt | 102 ++++++++++++++++++ 2 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/nearby/CommonPlaceClickActionsUnitTest.kt diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/CommonPlaceClickActions.kt b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/CommonPlaceClickActions.kt index f815e5ab5..a06af32da 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/fragments/CommonPlaceClickActions.kt +++ b/app/src/main/java/fr/free/nrw/commons/nearby/fragments/CommonPlaceClickActions.kt @@ -74,7 +74,7 @@ class CommonPlaceClickActions @Inject constructor( private fun openWebView(link: Uri): Boolean { Utils.handleWebUrl(activity, link) - return true; + return true } private fun PopupMenu.enableBy(menuId: Int, hasLink: Boolean) { @@ -85,15 +85,19 @@ class CommonPlaceClickActions @Inject constructor( AlertDialog.Builder(activity) .setMessage(R.string.login_alert_message) .setPositiveButton(R.string.login) { dialog, which -> - ActivityUtils.startActivityWithFlags( - activity, - LoginActivity::class.java, - Intent.FLAG_ACTIVITY_CLEAR_TOP, - Intent.FLAG_ACTIVITY_SINGLE_TOP - ) - applicationKvStore.putBoolean("login_skipped", false) - activity.finish() + setPositiveButton() } .show() } + + private fun setPositiveButton() { + ActivityUtils.startActivityWithFlags( + activity, + LoginActivity::class.java, + Intent.FLAG_ACTIVITY_CLEAR_TOP, + Intent.FLAG_ACTIVITY_SINGLE_TOP + ) + applicationKvStore.putBoolean("login_skipped", false) + activity.finish() + } } diff --git a/app/src/test/kotlin/fr/free/nrw/commons/nearby/CommonPlaceClickActionsUnitTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/nearby/CommonPlaceClickActionsUnitTest.kt new file mode 100644 index 000000000..e2f959a42 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/nearby/CommonPlaceClickActionsUnitTest.kt @@ -0,0 +1,102 @@ +package fr.free.nrw.commons.nearby + +import android.net.Uri +import fr.free.nrw.commons.TestCommonsApplication +import fr.free.nrw.commons.contributions.ContributionController +import fr.free.nrw.commons.kvstore.JsonKvStore +import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions +import fr.free.nrw.commons.profile.ProfileActivity +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.robolectric.annotation.LooperMode +import java.lang.reflect.Method + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [21], application = TestCommonsApplication::class) +@LooperMode(LooperMode.Mode.PAUSED) +class CommonPlaceClickActionsUnitTest { + + private lateinit var commonPlaceClickActions: CommonPlaceClickActions + + @Mock + private lateinit var store: JsonKvStore + + @Mock + private lateinit var contributionController: ContributionController + + @Mock + private lateinit var place: Place + + @Mock + private lateinit var uri: Uri + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + val activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get() + commonPlaceClickActions = CommonPlaceClickActions(store, activity, contributionController) + } + + @Test + fun testNonNull() { + Assert.assertNotNull(commonPlaceClickActions) + } + + @Test + fun testFunctionDeclaration() { + Assert.assertNotNull(commonPlaceClickActions.onCameraClicked()) + Assert.assertNotNull(commonPlaceClickActions.onGalleryClicked()) + Assert.assertNotNull(commonPlaceClickActions.onOverflowClicked()) + Assert.assertNotNull(commonPlaceClickActions.onDirectionsClicked()) + } + + @Test + @Throws(Exception::class) + fun testStoreSharedPrefs() { + val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod( + "storeSharedPrefs", + Place::class.java + ) + method.isAccessible = true + method.invoke(commonPlaceClickActions, place) + } + + @Test + @Throws(Exception::class) + fun testOpenWebView() { + val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod( + "openWebView", + Uri::class.java + ) + method.isAccessible = true + Assert.assertEquals(method.invoke(commonPlaceClickActions, uri), true) + } + + @Test + @Throws(Exception::class) + fun testShowLoginDialog() { + val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod( + "showLoginDialog" + ) + method.isAccessible = true + method.invoke(commonPlaceClickActions) + } + + @Test + @Throws(Exception::class) + fun testSetPositiveButton() { + val method: Method = CommonPlaceClickActions::class.java.getDeclaredMethod( + "setPositiveButton" + ) + method.isAccessible = true + method.invoke(commonPlaceClickActions) + } + +} \ No newline at end of file