mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 06:43:56 +01:00 
			
		
		
		
	refactor test
Signed-off-by: parneet-guraya <gurayaparneet@gmail.com>
This commit is contained in:
		
							parent
							
								
									d6116fdaa3
								
							
						
					
					
						commit
						d0efe134d9
					
				
					 5 changed files with 66 additions and 68 deletions
				
			
		|  | @ -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 | ||||
|  | @ -102,16 +104,16 @@ class CustomSelectorActivityTest { | |||
|      */ | ||||
|     @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()!!, | ||||
|         ) | ||||
| 
 | ||||
|         FilePicker.openGallery(activity, mockResultLauncher, 0, openDocumentPreferred) | ||||
| 
 | ||||
|         verify(mockResultLauncher).launch(intentCaptor.capture()) | ||||
| 
 | ||||
|         val capturedIntent = intentCaptor.firstValue | ||||
| 
 | ||||
|         if (openDocumentPreferred) { | ||||
|             assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) | ||||
|             assertEquals(Intent.ACTION_OPEN_DOCUMENT, capturedIntent.action) | ||||
|         } 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) | ||||
|         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,47 +189,20 @@ class FilePickerTest { | |||
|         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 | ||||
|     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 | ||||
|  |  | |||
|  | @ -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,14 +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) | ||||
|         //TODO[Parry] Adapt tests to new result api | ||||
| //        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 | ||||
|  | @ -363,7 +364,12 @@ 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) | ||||
|     } | ||||
| 
 | ||||
|  | @ -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
	
	 parneet-guraya
						parneet-guraya