mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Use new result API (#5875)
* remove unused result expectancy for settings screen launch Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * initial refactor to new result api, wip Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * refactor camera launcher Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * revert callback for video handling Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * invoke callbacks when cancelled Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * handle gallery picker result based on preference Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * remove old method of refactoring for file picker Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * remove legacy result handling callback Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * request code used for handling result was never used for launching an activity, hence removed Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * extract voice result handling into function Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * refactor test Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * remove unused tests Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * cleanup Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * fix-docs Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> * add space after , Signed-off-by: parneet-guraya <gurayaparneet@gmail.com> --------- Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
parent
f1205c19be
commit
1e7aabad16
26 changed files with 407 additions and 487 deletions
|
|
@ -340,20 +340,6 @@ class MainActivityUnitTests {
|
|||
method.invoke(activity, null, true)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResult() {
|
||||
val method: Method =
|
||||
MainActivity::class.java.getDeclaredMethod(
|
||||
"onActivityResult",
|
||||
Int::class.java,
|
||||
Int::class.java,
|
||||
Intent::class.java,
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, 0, 0, null)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnResume() {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
package fr.free.nrw.commons.customselector.ui.selector
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.ActivityResult
|
||||
import fr.free.nrw.commons.OkHttpConnectionFactory
|
||||
import fr.free.nrw.commons.TestCommonsApplication
|
||||
import fr.free.nrw.commons.contributions.ContributionDao
|
||||
|
|
@ -98,20 +100,20 @@ class CustomSelectorActivityTest {
|
|||
}
|
||||
|
||||
/**
|
||||
* Test onActivityResult function.
|
||||
* Test callback when result received.
|
||||
*/
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResult() {
|
||||
fun testResultLauncher() {
|
||||
val intent = Mockito.mock(Intent::class.java)
|
||||
val activityResult = ActivityResult(Activity.RESULT_OK, intent)
|
||||
val func =
|
||||
activity.javaClass.getDeclaredMethod(
|
||||
"onActivityResult",
|
||||
Int::class.java,
|
||||
Int::class.java,
|
||||
Intent::class.java,
|
||||
"onFullScreenDataReceived",
|
||||
ActivityResult::class.java,
|
||||
)
|
||||
func.isAccessible = true
|
||||
func.invoke(activity, 512, -1, Mockito.mock(Intent::class.java))
|
||||
func.invoke(activity, activityResult)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -6,18 +6,19 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import android.provider.MediaStore
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.nhaarman.mockitokotlin2.KArgumentCaptor
|
||||
import com.nhaarman.mockitokotlin2.argumentCaptor
|
||||
import com.nhaarman.mockitokotlin2.verify
|
||||
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.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.ArgumentCaptor
|
||||
import org.mockito.ArgumentMatchers
|
||||
import org.mockito.Captor
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.Mockito.`when`
|
||||
|
|
@ -48,8 +49,10 @@ class FilePickerTest {
|
|||
@Mock
|
||||
var unit: Unit? = null
|
||||
|
||||
@Captor
|
||||
var requestCodeCaptor: ArgumentCaptor<Integer>? = null
|
||||
@Mock
|
||||
private lateinit var mockResultLauncher: ActivityResultLauncher<Intent>
|
||||
|
||||
private val intentCaptor: KArgumentCaptor<Intent> = argumentCaptor()
|
||||
|
||||
private lateinit var context: Context
|
||||
|
||||
|
|
@ -65,15 +68,17 @@ class FilePickerTest {
|
|||
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
|
||||
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
|
||||
val openDocumentPreferred = nextBoolean()
|
||||
FilePicker.openGallery(activity, 0, openDocumentPreferred)
|
||||
verify(activity).startActivityForResult(
|
||||
ArgumentMatchers.any(),
|
||||
requestCodeCaptor?.capture()?.toInt()!!,
|
||||
)
|
||||
if(openDocumentPreferred){
|
||||
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS)
|
||||
}else{
|
||||
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
|
||||
|
||||
FilePicker.openGallery(activity, mockResultLauncher, 0, openDocumentPreferred)
|
||||
|
||||
verify(mockResultLauncher).launch(intentCaptor.capture())
|
||||
|
||||
val capturedIntent = intentCaptor.firstValue
|
||||
|
||||
if (openDocumentPreferred) {
|
||||
assertEquals(Intent.ACTION_OPEN_DOCUMENT, capturedIntent.action)
|
||||
} else {
|
||||
assertEquals(Intent.ACTION_GET_CONTENT, capturedIntent.action)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -84,12 +89,13 @@ class FilePickerTest {
|
|||
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
|
||||
val mockApplication = mock(Application::class.java)
|
||||
`when`(activity.applicationContext).thenReturn(mockApplication)
|
||||
FilePicker.openCameraForImage(activity, 0)
|
||||
verify(activity).startActivityForResult(
|
||||
ArgumentMatchers.any(),
|
||||
requestCodeCaptor?.capture()?.toInt()!!,
|
||||
)
|
||||
assertEquals(requestCodeCaptor?.value, RequestCodes.TAKE_PICTURE)
|
||||
FilePicker.openCameraForImage(activity, mockResultLauncher, 0)
|
||||
|
||||
verify(mockResultLauncher).launch(intentCaptor.capture())
|
||||
|
||||
val capturedIntent = intentCaptor.firstValue
|
||||
|
||||
assertEquals(MediaStore.ACTION_IMAGE_CAPTURE, capturedIntent.action)
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -183,46 +189,20 @@ class FilePickerTest {
|
|||
method.invoke(mockFilePicker, mockIntent)
|
||||
}
|
||||
|
||||
@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
|
||||
fun testOpenCustomSelectorRequestCode() {
|
||||
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
|
||||
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
|
||||
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
|
||||
FilePicker.openCustomSelector(activity, 0)
|
||||
verify(activity).startActivityForResult(ArgumentMatchers.any(), requestCodeCaptor?.capture()?.toInt()!!)
|
||||
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_CUSTOM_SELECTOR)
|
||||
FilePicker.openCustomSelector(activity, mockResultLauncher, 0)
|
||||
|
||||
verify(mockResultLauncher).launch(intentCaptor.capture())
|
||||
|
||||
val capturedIntent = intentCaptor.firstValue
|
||||
|
||||
assertEquals(
|
||||
CustomSelectorActivity.Companion::class.java.declaringClass.name,
|
||||
capturedIntent.component?.className
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import android.widget.ProgressBar
|
|||
import android.widget.ScrollView
|
||||
import android.widget.Spinner
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.fragment.app.FragmentManager
|
||||
import androidx.fragment.app.FragmentTransaction
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
|
|
@ -76,7 +77,6 @@ import java.util.Locale
|
|||
@Config(sdk = [21], application = TestCommonsApplication::class)
|
||||
@LooperMode(LooperMode.Mode.PAUSED)
|
||||
class MediaDetailFragmentUnitTests {
|
||||
private val requestCode = 1001
|
||||
private val lastLocation = "last_location_while_uploading"
|
||||
private lateinit var fragment: MediaDetailFragment
|
||||
private lateinit var fragmentManager: FragmentManager
|
||||
|
|
@ -231,24 +231,6 @@ class MediaDetailFragmentUnitTests {
|
|||
fragment.onCreateView(layoutInflater, null, savedInstanceState)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResultLocationPickerActivity() {
|
||||
fragment.onActivityResult(requestCode, Activity.RESULT_CANCELED, intent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun `test OnActivity Result Cancelled LocationPickerActivity`() {
|
||||
fragment.onActivityResult(requestCode, Activity.RESULT_CANCELED, intent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun `test OnActivity Result Cancelled DescriptionEditActivity`() {
|
||||
fragment.onActivityResult(requestCode, Activity.RESULT_OK, intent)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnSaveInstanceState() {
|
||||
|
|
|
|||
|
|
@ -167,20 +167,6 @@ class UploadActivityUnitTests {
|
|||
activity.makeUploadRequest()
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResult() {
|
||||
val method: Method =
|
||||
UploadActivity::class.java.getDeclaredMethod(
|
||||
"onActivityResult",
|
||||
Int::class.java,
|
||||
Int::class.java,
|
||||
Intent::class.java,
|
||||
)
|
||||
method.isAccessible = true
|
||||
method.invoke(activity, CommonsApplication.OPEN_APPLICATION_DETAIL_SETTINGS, 0, Intent())
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testReceiveSharedItems() {
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@ package fr.free.nrw.commons.upload
|
|||
|
||||
import android.app.Dialog
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.view.View
|
||||
import android.widget.AdapterView
|
||||
import android.widget.GridLayout
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.nhaarman.mockitokotlin2.any
|
||||
import com.nhaarman.mockitokotlin2.times
|
||||
|
|
@ -67,13 +69,16 @@ class UploadMediaDetailAdapterUnitTest {
|
|||
@Mock
|
||||
private lateinit var adapterView: AdapterView<RecentLanguagesAdapter>
|
||||
|
||||
@Mock
|
||||
private lateinit var mockResultLauncher: ActivityResultLauncher<Intent>
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
MockitoAnnotations.openMocks(this)
|
||||
uploadMediaDetails = mutableListOf(uploadMediaDetail, uploadMediaDetail)
|
||||
activity = Robolectric.buildActivity(UploadActivity::class.java).get()
|
||||
fragment = mock(UploadMediaDetailFragment::class.java)
|
||||
adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao)
|
||||
adapter = UploadMediaDetailAdapter(fragment, "", recentLanguagesDao, mockResultLauncher)
|
||||
context = ApplicationProvider.getApplicationContext()
|
||||
Whitebox.setInternalState(adapter, "uploadMediaDetails", uploadMediaDetails)
|
||||
Whitebox.setInternalState(adapter, "eventListener", eventListener)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import android.view.View
|
|||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.appcompat.widget.AppCompatButton
|
||||
import androidx.appcompat.widget.AppCompatImageButton
|
||||
import androidx.fragment.app.FragmentManager
|
||||
|
|
@ -348,7 +349,7 @@ class UploadMediaDetailFragmentUnitTest {
|
|||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResultOnMapIconClicked() {
|
||||
fun testOnCameraPositionCallbackOnMapIconClicked() {
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
Mockito.mock(LocationPicker::class.java)
|
||||
val intent = Mockito.mock(Intent::class.java)
|
||||
|
|
@ -363,13 +364,18 @@ class UploadMediaDetailFragmentUnitTest {
|
|||
`when`(latLng.latitude).thenReturn(0.0)
|
||||
`when`(latLng.longitude).thenReturn(0.0)
|
||||
`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)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Throws(Exception::class)
|
||||
fun testOnActivityResultAddLocationDialog() {
|
||||
fun testOnCameraPositionCallbackAddLocationDialog() {
|
||||
shadowOf(Looper.getMainLooper()).idle()
|
||||
Mockito.mock(LocationPicker::class.java)
|
||||
val intent = Mockito.mock(Intent::class.java)
|
||||
|
|
@ -387,7 +393,13 @@ class UploadMediaDetailFragmentUnitTest {
|
|||
`when`(latLng.latitude).thenReturn(0.0)
|
||||
`when`(latLng.longitude).thenReturn(0.0)
|
||||
`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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue