Unit tests failing (#4889)

* tests

* fixed tests
This commit is contained in:
Devarsh Mavani 2022-03-13 06:11:45 +05:30 committed by GitHub
parent ed572aab6f
commit caad8644a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package fr.free.nrw.commons.leaderboard
import android.accounts.Account import android.accounts.Account
import android.content.Context import android.content.Context
import android.os.Looper.getMainLooper
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -26,11 +27,14 @@ import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
import org.mockito.Mockito.doReturn
import org.mockito.MockitoAnnotations import org.mockito.MockitoAnnotations
import org.powermock.reflect.Whitebox 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.shadowOf
import org.robolectric.annotation.Config import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode import org.robolectric.annotation.LooperMode
import org.robolectric.shadows.ShadowToast import org.robolectric.shadows.ShadowToast
@ -222,6 +226,7 @@ class LeaderboardFragmentUnitTests {
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testMenuVisibilityOverrideVisibleWithContext() { fun testMenuVisibilityOverrideVisibleWithContext() {
shadowOf(getMainLooper()).idle()
`when`(parentView.context).thenReturn(context) `when`(parentView.context).thenReturn(context)
val method: Method = LeaderboardFragment::class.java.getDeclaredMethod( val method: Method = LeaderboardFragment::class.java.getDeclaredMethod(
"setMenuVisibility", "setMenuVisibility",
@ -229,7 +234,17 @@ class LeaderboardFragmentUnitTests {
) )
method.isAccessible = true method.isAccessible = true
method.invoke(fragment, true) method.invoke(fragment, true)
Assert.assertEquals(ShadowToast.getTextOfLatestToast().toString(), context.getString(R.string.leaderboard_unavailable_beta)) if(isBetaFlavour) {
Assert.assertEquals(
ShadowToast.getTextOfLatestToast().toString(),
context.getString(R.string.leaderboard_unavailable_beta)
)
} else {
Assert.assertNull(
ShadowToast.getTextOfLatestToast()
)
}
} }
} }

View file

@ -17,6 +17,7 @@ import fr.free.nrw.commons.TestAppAdapter
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.auth.SessionManager import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.profile.ProfileActivity import fr.free.nrw.commons.profile.ProfileActivity
import fr.free.nrw.commons.utils.ConfigUtils
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
@ -372,7 +373,16 @@ class AchievementsFragmentUnitTests {
) )
method.isAccessible = true method.isAccessible = true
method.invoke(fragment, true) method.invoke(fragment, true)
Assert.assertEquals(ShadowToast.getTextOfLatestToast().toString(), context.getString(R.string.achievements_unavailable_beta)) if(ConfigUtils.isBetaFlavour) {
Assert.assertEquals(
ShadowToast.getTextOfLatestToast().toString(),
context.getString(R.string.achievements_unavailable_beta)
)
} else {
Assert.assertNull(
ShadowToast.getTextOfLatestToast()
)
}
} }
} }