Add tests for FilePicker public methods (#3948)

This commit is contained in:
neslihanturan 2020-10-06 18:34:39 +03:00 committed by GitHub
parent 67c0004062
commit b9de8dc145
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,57 @@
package fr.free.nrw.commons.filepicker
import android.app.Activity
import android.app.Application
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
import fr.free.nrw.commons.filepicker.Constants.RequestCodes
import fr.free.nrw.commons.filepicker.FilePicker.handleActivityResult
import junit.framework.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.*
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
class FilePickerTest {
@Mock
internal lateinit var activity: Activity
@Mock
internal lateinit var sharedPref: SharedPreferences
@Mock
var sharedPreferencesEditor: SharedPreferences.Editor? = null
@Mock
var unit: Unit? = null
@Captor
var requestCodeCaptor: ArgumentCaptor<Integer>? = null
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
}
@Test
fun testOpenGalleryRequestCode() {
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
`when`(sharedPref.edit().putInt("type", 0)).thenReturn(sharedPreferencesEditor)
FilePicker.openGallery(activity, 0)
verify(activity).startActivityForResult(ArgumentMatchers.anyObject(), requestCodeCaptor?.capture()?.toInt()!!)
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
}
@Test
fun testOpenCameraForImageCode() {
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
`when`(sharedPref.edit()).thenReturn(sharedPreferencesEditor)
`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.anyObject(), requestCodeCaptor?.capture()?.toInt()!!)
assertEquals(requestCodeCaptor?.value, RequestCodes.TAKE_PICTURE)
}
}