@@ -6,18 +6,19 @@ import android.content.Context
6
6
import android.content.Intent
7
7
import android.content.SharedPreferences
8
8
import android.net.Uri
9
+ import android.provider.MediaStore
10
+ import androidx.activity.result.ActivityResultLauncher
9
11
import androidx.preference.PreferenceManager
10
12
import androidx.test.core.app.ApplicationProvider
13
+ import com.nhaarman.mockitokotlin2.KArgumentCaptor
14
+ import com.nhaarman.mockitokotlin2.argumentCaptor
11
15
import com.nhaarman.mockitokotlin2.verify
12
16
import fr.free.nrw.commons.TestCommonsApplication
13
- import fr.free.nrw.commons.filepicker.Constants.RequestCodes
17
+ import fr.free.nrw.commons.customselector.ui.selector.CustomSelectorActivity
14
18
import org.junit.Assert.assertEquals
15
19
import org.junit.Before
16
20
import org.junit.Test
17
21
import org.junit.runner.RunWith
18
- import org.mockito.ArgumentCaptor
19
- import org.mockito.ArgumentMatchers
20
- import org.mockito.Captor
21
22
import org.mockito.Mock
22
23
import org.mockito.Mockito.mock
23
24
import org.mockito.Mockito.`when`
@@ -48,8 +49,10 @@ class FilePickerTest {
48
49
@Mock
49
50
var unit: Unit? = null
50
51
51
- @Captor
52
- var requestCodeCaptor: ArgumentCaptor <Integer >? = null
52
+ @Mock
53
+ private lateinit var mockResultLauncher: ActivityResultLauncher <Intent >
54
+
55
+ private val intentCaptor: KArgumentCaptor <Intent > = argumentCaptor()
53
56
54
57
private lateinit var context: Context
55
58
@@ -65,15 +68,17 @@ class FilePickerTest {
65
68
`when `(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
66
69
`when `(sharedPref.edit().putInt(" type" , 0 )).thenReturn(sharedPreferencesEditor)
67
70
val openDocumentPreferred = nextBoolean()
68
- FilePicker .openGallery(activity, 0 , openDocumentPreferred)
69
- verify(activity).startActivityForResult(
70
- ArgumentMatchers .any(),
71
- requestCodeCaptor?.capture()?.toInt()!! ,
72
- )
73
- if (openDocumentPreferred){
74
- assertEquals(requestCodeCaptor?.value, RequestCodes .PICK_PICTURE_FROM_DOCUMENTS )
75
- }else {
76
- assertEquals(requestCodeCaptor?.value, RequestCodes .PICK_PICTURE_FROM_GALLERY )
71
+
72
+ FilePicker .openGallery(activity, mockResultLauncher, 0 , openDocumentPreferred)
73
+
74
+ verify(mockResultLauncher).launch(intentCaptor.capture())
75
+
76
+ val capturedIntent = intentCaptor.firstValue
77
+
78
+ if (openDocumentPreferred) {
79
+ assertEquals(Intent .ACTION_OPEN_DOCUMENT , capturedIntent.action)
80
+ } else {
81
+ assertEquals(Intent .ACTION_GET_CONTENT , capturedIntent.action)
77
82
}
78
83
}
79
84
@@ -84,12 +89,13 @@ class FilePickerTest {
84
89
`when `(sharedPref.edit().putInt(" type" , 0 )).thenReturn(sharedPreferencesEditor)
85
90
val mockApplication = mock(Application ::class .java)
86
91
`when `(activity.applicationContext).thenReturn(mockApplication)
87
- FilePicker .openCameraForImage(activity, 0 )
88
- verify(activity).startActivityForResult(
89
- ArgumentMatchers .any(),
90
- requestCodeCaptor?.capture()?.toInt()!! ,
91
- )
92
- assertEquals(requestCodeCaptor?.value, RequestCodes .TAKE_PICTURE )
92
+ FilePicker .openCameraForImage(activity, mockResultLauncher, 0 )
93
+
94
+ verify(mockResultLauncher).launch(intentCaptor.capture())
95
+
96
+ val capturedIntent = intentCaptor.firstValue
97
+
98
+ assertEquals(MediaStore .ACTION_IMAGE_CAPTURE , capturedIntent.action)
93
99
}
94
100
95
101
@Test
@@ -183,47 +189,20 @@ class FilePickerTest {
183
189
method.invoke(mockFilePicker, mockIntent)
184
190
}
185
191
186
- // TODO [Parry] adapt tests
187
- // @Test
188
- // fun testHandleActivityResultCaseOne() {
189
- // val mockIntent = mock(Intent::class.java)
190
- // FilePicker.handleActivityResult(
191
- // RequestCodes.FILE_PICKER_IMAGE_IDENTIFICATOR,
192
- // Activity.RESULT_OK,
193
- // mockIntent,
194
- // activity,
195
- // object : DefaultCallback() {
196
- // override fun onCanceled(
197
- // source: FilePicker.ImageSource,
198
- // type: Int,
199
- // ) {
200
- // super.onCanceled(source, type)
201
- // }
202
- //
203
- // override fun onImagePickerError(
204
- // e: Exception,
205
- // source: FilePicker.ImageSource,
206
- // type: Int,
207
- // ) {
208
- // }
209
- //
210
- // override fun onImagesPicked(
211
- // imagesFiles: List<UploadableFile>,
212
- // source: FilePicker.ImageSource,
213
- // type: Int,
214
- // ) {
215
- // }
216
- // },
217
- // )
218
- // }
219
-
220
192
@Test
221
193
fun testOpenCustomSelectorRequestCode () {
222
194
`when `(PreferenceManager .getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
223
195
`when `(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
224
196
`when `(sharedPref.edit().putInt(" type" , 0 )).thenReturn(sharedPreferencesEditor)
225
- FilePicker .openCustomSelector(activity, 0 )
226
- verify(activity).startActivityForResult(ArgumentMatchers .any(), requestCodeCaptor?.capture()?.toInt()!! )
227
- assertEquals(requestCodeCaptor?.value, RequestCodes .PICK_PICTURE_FROM_CUSTOM_SELECTOR )
197
+ FilePicker .openCustomSelector(activity, mockResultLauncher, 0 )
198
+
199
+ verify(mockResultLauncher).launch(intentCaptor.capture())
200
+
201
+ val capturedIntent = intentCaptor.firstValue
202
+
203
+ assertEquals(
204
+ CustomSelectorActivity .Companion ::class .java.declaringClass.name,
205
+ capturedIntent.component?.className
206
+ )
228
207
}
229
208
}
0 commit comments