mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Add some more File Picker Unit Tests (#4536)
* Add some more File Picker Unit Tests * Fix failing test * Fix syntax error
This commit is contained in:
parent
c6f4649fcf
commit
c1e83d7ee8
3 changed files with 205 additions and 4 deletions
|
|
@ -2,35 +2,57 @@ package fr.free.nrw.commons.filepicker
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import android.net.Uri
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.nhaarman.mockitokotlin2.verify
|
import com.nhaarman.mockitokotlin2.verify
|
||||||
import com.nhaarman.mockitokotlin2.verifyNoMoreInteractions
|
import fr.free.nrw.commons.TestCommonsApplication
|
||||||
import fr.free.nrw.commons.filepicker.Constants.RequestCodes
|
import fr.free.nrw.commons.filepicker.Constants.RequestCodes
|
||||||
import fr.free.nrw.commons.filepicker.FilePicker.handleActivityResult
|
|
||||||
import junit.framework.Assert.assertEquals
|
import junit.framework.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.mockito.*
|
import org.mockito.*
|
||||||
import org.mockito.Mockito.`when`
|
import org.mockito.Mockito.`when`
|
||||||
import org.mockito.Mockito.mock
|
import org.mockito.Mockito.mock
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.RuntimeEnvironment
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
import org.robolectric.annotation.LooperMode
|
||||||
|
import java.lang.reflect.Method
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(
|
||||||
|
sdk = [21],
|
||||||
|
application = TestCommonsApplication::class,
|
||||||
|
shadows = [ShadowFileProvider::class]
|
||||||
|
)
|
||||||
|
@LooperMode(LooperMode.Mode.PAUSED)
|
||||||
class FilePickerTest {
|
class FilePickerTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
internal lateinit var activity: Activity
|
internal lateinit var activity: Activity
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
internal lateinit var sharedPref: SharedPreferences
|
internal lateinit var sharedPref: SharedPreferences
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
var sharedPreferencesEditor: SharedPreferences.Editor? = null
|
var sharedPreferencesEditor: SharedPreferences.Editor? = null
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
var unit: Unit? = null
|
var unit: Unit? = null
|
||||||
|
|
||||||
@Captor
|
@Captor
|
||||||
var requestCodeCaptor: ArgumentCaptor<Integer>? = null
|
var requestCodeCaptor: ArgumentCaptor<Integer>? = null
|
||||||
|
|
||||||
|
private lateinit var context: Context
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setup() {
|
fun setup() {
|
||||||
MockitoAnnotations.initMocks(this)
|
MockitoAnnotations.initMocks(this)
|
||||||
|
context = RuntimeEnvironment.application.applicationContext
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -39,7 +61,10 @@ 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)
|
||||||
FilePicker.openGallery(activity, 0)
|
FilePicker.openGallery(activity, 0)
|
||||||
verify(activity).startActivityForResult(ArgumentMatchers.anyObject(), requestCodeCaptor?.capture()?.toInt()!!)
|
verify(activity).startActivityForResult(
|
||||||
|
ArgumentMatchers.anyObject(),
|
||||||
|
requestCodeCaptor?.capture()?.toInt()!!
|
||||||
|
)
|
||||||
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
|
assertEquals(requestCodeCaptor?.value, RequestCodes.PICK_PICTURE_FROM_GALLERY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,10 +76,150 @@ class FilePickerTest {
|
||||||
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, 0)
|
||||||
verify(activity).startActivityForResult(ArgumentMatchers.anyObject(), requestCodeCaptor?.capture()?.toInt()!!)
|
verify(activity).startActivityForResult(
|
||||||
|
ArgumentMatchers.anyObject(),
|
||||||
|
requestCodeCaptor?.capture()?.toInt()!!
|
||||||
|
)
|
||||||
assertEquals(requestCodeCaptor?.value, RequestCodes.TAKE_PICTURE)
|
assertEquals(requestCodeCaptor?.value, RequestCodes.TAKE_PICTURE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testCreateCameraPictureFile() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"createCameraPictureFile",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testCreateCameraForImageIntent() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"createCameraForImageIntent",
|
||||||
|
Context::class.java,
|
||||||
|
Int::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, context, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testRevokeWritePermission() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val mockUri = mock(Uri::class.java)
|
||||||
|
val mockContext = mock(Context::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"revokeWritePermission",
|
||||||
|
Context::class.java,
|
||||||
|
Uri::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, mockContext, mockUri)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testRestoreType() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"restoreType",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTakenCameraPicture() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"takenCameraPicture",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTakenCameraPictureCaseTrue() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
|
||||||
|
`when`(sharedPref.getString("last_photo", null)).thenReturn("")
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"takenCameraPicture",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, activity)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTakenCameraVideo() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"takenCameraVideo",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testTakenCameraVideoCaseTrue() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
|
||||||
|
`when`(sharedPref.getString("last_video", null)).thenReturn("")
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"takenCameraVideo",
|
||||||
|
Context::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
method.invoke(mockFilePicker, activity)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIsPhoto() {
|
||||||
|
val mockFilePicker = mock(FilePicker::class.java)
|
||||||
|
val mockIntent = mock(Intent::class.java)
|
||||||
|
val method: Method = FilePicker::class.java.getDeclaredMethod(
|
||||||
|
"isPhoto",
|
||||||
|
Intent::class.java
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
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
|
@Test
|
||||||
fun testOpenCustomSelectorRequestCode() {
|
fun testOpenCustomSelectorRequestCode() {
|
||||||
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
|
`when`(PreferenceManager.getDefaultSharedPreferences(activity)).thenReturn(sharedPref)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
package fr.free.nrw.commons.filepicker;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.MatrixCursor;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.provider.OpenableColumns;
|
||||||
|
import androidx.core.content.FileProvider;
|
||||||
|
import org.robolectric.annotation.Implementation;
|
||||||
|
import org.robolectric.annotation.Implements;
|
||||||
|
|
||||||
|
@Implements(FileProvider.class)
|
||||||
|
public class ShadowFileProvider {
|
||||||
|
|
||||||
|
@Implementation
|
||||||
|
public Cursor query(final Uri uri, final String[] projection, final String selection,
|
||||||
|
final String[] selectionArgs,
|
||||||
|
final String sortOrder) {
|
||||||
|
|
||||||
|
if (uri == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String[] columns = {OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE};
|
||||||
|
final Object[] values = {"dummy", 500};
|
||||||
|
final MatrixCursor cursor = new MatrixCursor(columns, 1);
|
||||||
|
|
||||||
|
if (!uri.equals(Uri.EMPTY)) {
|
||||||
|
cursor.addRow(values);
|
||||||
|
}
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -65,6 +65,9 @@ class UploadCategoriesFragmentUnitTests {
|
||||||
@Mock
|
@Mock
|
||||||
private lateinit var tvTitle: TextView
|
private lateinit var tvTitle: TextView
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private lateinit var tvSubTitle: TextView
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private lateinit var tooltip: ImageView
|
private lateinit var tooltip: ImageView
|
||||||
|
|
||||||
|
|
@ -104,6 +107,7 @@ class UploadCategoriesFragmentUnitTests {
|
||||||
Whitebox.setInternalState(fragment, "rvCategories", rvCategories)
|
Whitebox.setInternalState(fragment, "rvCategories", rvCategories)
|
||||||
Whitebox.setInternalState(fragment, "tvTitle", tvTitle)
|
Whitebox.setInternalState(fragment, "tvTitle", tvTitle)
|
||||||
Whitebox.setInternalState(fragment, "tooltip", tooltip)
|
Whitebox.setInternalState(fragment, "tooltip", tooltip)
|
||||||
|
Whitebox.setInternalState(fragment, "tvSubTitle", tvSubTitle)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue