Update MoreBottomSheetFragment Unit Tests (#4812)

* Update MoreBottomSheetFragment Unit Tests

* Fix failing tests
This commit is contained in:
Madhur Gupta 2022-02-15 15:10:34 +05:30 committed by GitHub
parent 4081bf6e61
commit 458024bdee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,21 +1,38 @@
package fr.free.nrw.commons.navtab package fr.free.nrw.commons.navtab
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Looper import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
import fr.free.nrw.commons.CommonsApplication
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.kvstore.JsonKvStore
import fr.free.nrw.commons.profile.ProfileActivity import fr.free.nrw.commons.profile.ProfileActivity
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.powermock.reflect.Whitebox
import org.robolectric.Robolectric import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment import org.robolectric.RuntimeEnvironment
import org.robolectric.Shadows import org.robolectric.Shadows
import org.robolectric.annotation.Config import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowActivity
import org.robolectric.shadows.ShadowAlertDialog
import java.lang.reflect.Method
@RunWith(RobolectricTestRunner::class) @RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class) @Config(sdk = [21], application = TestCommonsApplication::class)
@ -23,21 +40,38 @@ import org.robolectric.annotation.LooperMode
class MoreBottomSheetFragmentUnitTests { class MoreBottomSheetFragmentUnitTests {
private lateinit var fragment: MoreBottomSheetFragment private lateinit var fragment: MoreBottomSheetFragment
private lateinit var view: View
private lateinit var layoutInflater: LayoutInflater
private lateinit var context: Context private lateinit var context: Context
private lateinit var activity: ProfileActivity
@Mock
private lateinit var store: JsonKvStore
@Mock
private lateinit var morePeerReview: TextView
@Before @Before
fun setUp() { fun setUp() {
MockitoAnnotations.initMocks(this)
context = RuntimeEnvironment.application.applicationContext context = RuntimeEnvironment.application.applicationContext
val activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get() activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get()
fragment = MoreBottomSheetFragment() fragment = MoreBottomSheetFragment()
val fragmentManager: FragmentManager = activity.supportFragmentManager val fragmentManager: FragmentManager = activity.supportFragmentManager
val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction() val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(fragment, null) fragmentTransaction.add(fragment, null)
fragmentTransaction.commit() fragmentTransaction.commitNowAllowingStateLoss()
Whitebox.setInternalState(fragment, "store", store)
Whitebox.setInternalState(fragment, "morePeerReview", morePeerReview)
`when`(store.getBoolean(CommonsApplication.IS_LIMITED_CONNECTION_MODE_ENABLED)).thenReturn(
true
)
layoutInflater = LayoutInflater.from(activity)
view = fragment.onCreateView(layoutInflater, null, null) as View
} }
@Test @Test
@ -58,6 +92,8 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnLogoutClicked() { fun testOnLogoutClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onLogoutClicked() fragment.onLogoutClicked()
val dialog: AlertDialog = ShadowAlertDialog.getLatestDialog() as AlertDialog
Assert.assertEquals(dialog.isShowing, true)
} }
@Test @Test
@ -65,6 +101,8 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnFeedbackClicked() { fun testOnFeedbackClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onFeedbackClicked() fragment.onFeedbackClicked()
val dialog: AlertDialog = ShadowAlertDialog.getLatestDialog() as AlertDialog
Assert.assertEquals(dialog.isShowing, true)
} }
@Test @Test
@ -72,6 +110,11 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnAboutClicked() { fun testOnAboutClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onAboutClicked() fragment.onAboutClicked()
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val intentForResult: ShadowActivity.IntentForResult =
shadowActivity.nextStartedActivityForResult
val nextActivity: ComponentName? = intentForResult.intent.component
Assert.assertEquals(nextActivity?.className?.contains(".AboutActivity"), true)
} }
@Test @Test
@ -79,6 +122,11 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnTutorialClicked() { fun testOnTutorialClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onTutorialClicked() fragment.onTutorialClicked()
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val intentForResult: ShadowActivity.IntentForResult =
shadowActivity.nextStartedActivityForResult
val nextActivity: ComponentName? = intentForResult.intent.component
Assert.assertEquals(nextActivity?.className?.contains(".WelcomeActivity"), true)
} }
@Test @Test
@ -86,6 +134,11 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnSettingsClicked() { fun testOnSettingsClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onSettingsClicked() fragment.onSettingsClicked()
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val intentForResult: ShadowActivity.IntentForResult =
shadowActivity.nextStartedActivityForResult
val nextActivity: ComponentName? = intentForResult.intent.component
Assert.assertEquals(nextActivity?.className?.contains(".SettingsActivity"), true)
} }
@Test @Test
@ -93,6 +146,11 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnProfileClicked() { fun testOnProfileClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onProfileClicked() fragment.onProfileClicked()
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val intentForResult: ShadowActivity.IntentForResult =
shadowActivity.nextStartedActivityForResult
val nextActivity: ComponentName? = intentForResult.intent.component
Assert.assertEquals(nextActivity?.className?.contains(".ProfileActivity"), true)
} }
@Test @Test
@ -100,6 +158,29 @@ class MoreBottomSheetFragmentUnitTests {
fun testOnPeerReviewClicked() { fun testOnPeerReviewClicked() {
Shadows.shadowOf(Looper.getMainLooper()).idle() Shadows.shadowOf(Looper.getMainLooper()).idle()
fragment.onPeerReviewClicked() fragment.onPeerReviewClicked()
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val intentForResult: ShadowActivity.IntentForResult =
shadowActivity.nextStartedActivityForResult
val nextActivity: ComponentName? = intentForResult.intent.component
Assert.assertEquals(nextActivity?.className?.contains(".ReviewActivity"), true)
}
@Test
@Throws(Exception::class)
fun testSendFeedback() {
Shadows.shadowOf(Looper.getMainLooper()).idle()
val method: Method = MoreBottomSheetFragment::class.java.getDeclaredMethod("sendFeedback")
method.isAccessible = true
method.invoke(fragment)
val shadowActivity: ShadowActivity = Shadows.shadowOf(activity)
val startedIntent = shadowActivity.nextStartedActivity
Assert.assertEquals(startedIntent.action, Intent.ACTION_SENDTO)
Assert.assertEquals(startedIntent.type, null)
Assert.assertEquals(startedIntent.`data`, Uri.parse("mailto:"))
Assert.assertEquals(
startedIntent.extras?.get(Intent.EXTRA_SUBJECT),
CommonsApplication.FEEDBACK_EMAIL_SUBJECT
)
} }
} }