refactor test

Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parneet-guraya 2024-10-18 21:00:18 +05:30
parent d6116fdaa3
commit d0efe134d9
No known key found for this signature in database
GPG key ID: 63B807C4B2A9064B
5 changed files with 66 additions and 68 deletions

View file

@ -1,8 +1,10 @@
package fr.free.nrw.commons.customselector.ui.selector package fr.free.nrw.commons.customselector.ui.selector
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import androidx.activity.result.ActivityResult
import fr.free.nrw.commons.OkHttpConnectionFactory import fr.free.nrw.commons.OkHttpConnectionFactory
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.contributions.ContributionDao import fr.free.nrw.commons.contributions.ContributionDao
@ -102,16 +104,16 @@ class CustomSelectorActivityTest {
*/ */
@Test @Test
@Throws(Exception::class) @Throws(Exception::class)
fun testOnActivityResult() { fun testResultLauncher() {
val intent = Mockito.mock(Intent::class.java)
val activityResult = ActivityResult(Activity.RESULT_OK,intent)
val func = val func =
activity.javaClass.getDeclaredMethod( activity.javaClass.getDeclaredMethod(
"onActivityResult", "onFullScreenDataReceived",
Int::class.java, ActivityResult::class.java,
Int::class.java,
Intent::class.java,
) )
func.isAccessible = true func.isAccessible = true
func.invoke(activity, 512, -1, Mockito.mock(Intent::class.java)) func.invoke(activity, activityResult)
} }
/** /**

View file

@ -6,18 +6,19 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri import android.net.Uri
import android.provider.MediaStore
import androidx.activity.result.ActivityResultLauncher
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import com.nhaarman.mockitokotlin2.KArgumentCaptor
import com.nhaarman.mockitokotlin2.argumentCaptor
import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.verify
import fr.free.nrw.commons.TestCommonsApplication import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.filepicker.Constants.RequestCodes import fr.free.nrw.commons.customselector.ui.selector.CustomSelectorActivity
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
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.ArgumentCaptor
import org.mockito.ArgumentMatchers
import org.mockito.Captor
import org.mockito.Mock import org.mockito.Mock
import org.mockito.Mockito.mock import org.mockito.Mockito.mock
import org.mockito.Mockito.`when` import org.mockito.Mockito.`when`
@ -48,8 +49,10 @@ class FilePickerTest {
@Mock @Mock
var unit: Unit? = null var unit: Unit? = null
@Captor @Mock
var requestCodeCaptor: ArgumentCaptor<Integer>? = null private lateinit var mockResultLauncher: ActivityResultLauncher<Intent>
private val intentCaptor: KArgumentCaptor<Intent> = argumentCaptor()
private lateinit var context: Context private lateinit var context: Context
@ -65,15 +68,17 @@ class FilePickerTest {
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor) `when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor) `when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
val openDocumentPreferred = nextBoolean() val openDocumentPreferred = nextBoolean()
FilePicker.openGallery(activity, 0, openDocumentPreferred)
verify(activity).startActivityForResult( FilePicker.openGallery(activity, mockResultLauncher, 0, openDocumentPreferred)
ArgumentMatchers.any(),
requestCodeCaptor?.capture()?.toInt()!!, verify(mockResultLauncher).launch(intentCaptor.capture())
)
val capturedIntent = intentCaptor.firstValue
if (openDocumentPreferred) { if (openDocumentPreferred) {
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) assertEquals(Intent.ACTION_OPEN_DOCUMENT, capturedIntent.action)
} else { } else {
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY) assertEquals(Intent.ACTION_GET_CONTENT, capturedIntent.action)
} }
} }
@ -84,12 +89,13 @@ class FilePickerTest {
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor) `when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
val mockApplication = mock(Application::class.java) val mockApplication = mock(Application::class.java)
`when`(activity.applicationContext).thenReturn(mockApplication) `when`(activity.applicationContext).thenReturn(mockApplication)
FilePicker.openCameraForImage(activity, 0) FilePicker.openCameraForImage(activity, mockResultLauncher, 0)
verify(activity).startActivityForResult(
ArgumentMatchers.any(), verify(mockResultLauncher).launch(intentCaptor.capture())
requestCodeCaptor?.capture()?.toInt()!!,
) val capturedIntent = intentCaptor.firstValue
assertEquals(requestCodeCaptor?.value, RequestCodes.TAKE_PICTURE)
assertEquals(MediaStore.ACTION_IMAGE_CAPTURE, capturedIntent.action)
} }
@Test @Test
@ -183,47 +189,20 @@ class FilePickerTest {
method.invoke(mockFilePicker, mockIntent) method.invoke(mockFilePicker, mockIntent)
} }
//TODO [Parry] adapt tests
// @Test
// fun testHandleActivityResultCaseOne() {
// val mockIntent = mock(Intent::class.java)
// FilePicker.handleActivityResult(
// RequestCodes.FILE_PICKER_IMAGE_IDENTIFICATOR,
// Activity.RESULT_OK,
// mockIntent,
// activity,
// object : DefaultCallback() {
// override fun onCanceled(
// source: FilePicker.ImageSource,
// type: Int,
// ) {
// super.onCanceled(source, type)
// }
//
// override fun onImagePickerError(
// e: Exception,
// source: FilePicker.ImageSource,
// type: Int,
// ) {
// }
//
// override fun onImagesPicked(
// imagesFiles: List<UploadableFile>,
// source: FilePicker.ImageSource,
// type: Int,
// ) {
// }
// },
// )
// }
@Test @Test
fun testOpenCustomSelectorRequestCode() { fun testOpenCustomSelectorRequestCode() {
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref) `when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor) `when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor) `when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
FilePicker.openCustomSelector(activity, 0) FilePicker.openCustomSelector(activity, mockResultLauncher, 0)
verify(activity).startActivityForResult(ArgumentMatchers.any(), requestCodeCaptor?.capture()?.toInt()!!)
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR) verify(mockResultLauncher).launch(intentCaptor.capture())
val capturedIntent = intentCaptor.firstValue
assertEquals(
CustomSelectorActivity.Companion::class.java.declaringClass.name,
capturedIntent.component?.className
)
} }
} }

View file

@ -19,6 +19,7 @@ import android.widget.ProgressBar
import android.widget.ScrollView import android.widget.ScrollView
import android.widget.Spinner import android.widget.Spinner
import android.widget.TextView import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider

View file

@ -2,11 +2,13 @@ package fr.free.nrw.commons.upload
import android.app.Dialog import android.app.Dialog
import android.content.Context import android.content.Context
import android.content.Intent
import android.view.View import android.view.View
import android.widget.AdapterView import android.widget.AdapterView
import android.widget.GridLayout import android.widget.GridLayout
import android.widget.ListView import android.widget.ListView
import android.widget.TextView import android.widget.TextView
import androidx.activity.result.ActivityResultLauncher
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.times import com.nhaarman.mockitokotlin2.times
@ -67,14 +69,16 @@ class UploadMediaDetailAdapterUnitTest {
@Mock @Mock
private lateinit var adapterView: AdapterView<RecentLanguagesAdapter> private lateinit var adapterView: AdapterView<RecentLanguagesAdapter>
@Mock
private lateinit var mockResultLauncher: ActivityResultLauncher<Intent>
@Before @Before
fun setUp() { fun setUp() {
MockitoAnnotations.openMocks(this) MockitoAnnotations.openMocks(this)
uploadMediaDetails = mutableListOf(uploadMediaDetail, uploadMediaDetail) uploadMediaDetails = mutableListOf(uploadMediaDetail, uploadMediaDetail)
activity = Robolectric.buildActivity(UploadActivity::class.java).get() activity = Robolectric.buildActivity(UploadActivity::class.java).get()
fragment = mock(UploadMediaDetailFragment::class.java) fragment = mock(UploadMediaDetailFragment::class.java)
//TODO[Parry] Adapt tests to new result api adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao, mockResultLauncher)
// adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao)
context = ApplicationProvider.getApplicationContext() context = ApplicationProvider.getApplicationContext()
Whitebox.setInternalState(adapter, "uploadMediaDetails", uploadMediaDetails) Whitebox.setInternalState(adapter, "uploadMediaDetails", uploadMediaDetails)
Whitebox.setInternalState(adapter, "eventListener", eventListener) Whitebox.setInternalState(adapter, "eventListener", eventListener)

View file

@ -11,6 +11,7 @@ import android.view.View
import android.widget.ImageView import android.widget.ImageView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.appcompat.widget.AppCompatButton import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.AppCompatImageButton import androidx.appcompat.widget.AppCompatImageButton
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
@ -363,7 +364,12 @@ class UploadMediaDetailFragmentUnitTest {
`when`(latLng.latitude).thenReturn(0.0) `when`(latLng.latitude).thenReturn(0.0)
`when`(latLng.longitude).thenReturn(0.0) `when`(latLng.longitude).thenReturn(0.0)
`when`(uploadItem.gpsCoords).thenReturn(imageCoordinates) `when`(uploadItem.gpsCoords).thenReturn(imageCoordinates)
fragment.onActivityResult(1211, Activity.RESULT_OK, intent) val activityResult = ActivityResult(Activity.RESULT_OK,intent)
val handleResultMethod = UploadMediaDetailFragment::class.java.getDeclaredMethod("onCameraPosition", ActivityResult::class.java)
handleResultMethod.isAccessible = true
handleResultMethod.invoke(fragment,activityResult)
Mockito.verify(presenter, Mockito.times(0)).getImageQuality(0, location, activity) Mockito.verify(presenter, Mockito.times(0)).getImageQuality(0, location, activity)
} }
@ -387,7 +393,13 @@ class UploadMediaDetailFragmentUnitTest {
`when`(latLng.latitude).thenReturn(0.0) `when`(latLng.latitude).thenReturn(0.0)
`when`(latLng.longitude).thenReturn(0.0) `when`(latLng.longitude).thenReturn(0.0)
`when`(uploadItem.gpsCoords).thenReturn(imageCoordinates) `when`(uploadItem.gpsCoords).thenReturn(imageCoordinates)
fragment.onActivityResult(1211, Activity.RESULT_OK, intent)
val activityResult = ActivityResult(Activity.RESULT_OK,intent)
val handleResultMethod = UploadMediaDetailFragment::class.java.getDeclaredMethod("onCameraPosition", ActivityResult::class.java)
handleResultMethod.isAccessible = true
handleResultMethod.invoke(fragment, activityResult)
Mockito.verify(presenter, Mockito.times(1)).displayLocDialog(0, null, false) Mockito.verify(presenter, Mockito.times(1)).displayLocDialog(0, null, false)
} }