Migrated helper modules to kotlin (#6007)

* Rename .java to .kt

* Migrated delete and description module to kotlin (WIP)

* Fix: Unit tests

* Fix: Unit tests

* Rename .java to .kt

* Migrated data, db, and converter module to kotlin

* Fix: Unit tests

* Fix: Unit tests

---------

Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
Saifuddin Adenwala 2024-12-11 04:13:36 +05:30 committed by GitHub
parent 73311970c5
commit 3030a6fca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 1168 additions and 973 deletions

View file

@ -15,11 +15,14 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Before
import fr.free.nrw.commons.R
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.Mockito.spy
import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito.`when`
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@ -44,7 +47,7 @@ class DeleteHelperTest {
@Mock
internal lateinit var media: Media
lateinit var deleteHelper: DeleteHelper
private lateinit var deleteHelper: DeleteHelper
/**
* Init mocks for test
@ -60,19 +63,46 @@ class DeleteHelperTest {
*/
@Test
fun makeDeletion() {
whenever(pageEditClient.prependEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
whenever(pageEditClient.appendEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
whenever(pageEditClient.edit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
whenever(pageEditClient.prependEdit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(true))
whenever(pageEditClient.appendEdit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(true))
whenever(pageEditClient.edit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(true))
whenever(media.displayTitle).thenReturn("Test file")
`when`(context.getString(R.string.delete_helper_show_deletion_title))
.thenReturn("Deletion Notification")
`when`(context.getString(R.string.delete_helper_show_deletion_title_success))
.thenReturn("Success")
`when`(context.getString(R.string.delete_helper_show_deletion_title_failed))
.thenReturn("Failed")
`when`(context.getString(R.string.delete_helper_show_deletion_message_else))
.thenReturn("Media deletion failed")
`when`(context.getString(
R.string.delete_helper_show_deletion_message_if, media.displayTitle)
).thenReturn("Media successfully deleted: Test Media Title")
val creatorName = "Creator"
whenever(media.author).thenReturn("$creatorName")
whenever(media.filename).thenReturn("Test file.jpg")
val makeDeletion = deleteHelper.makeDeletion(context, media, "Test reason")?.blockingGet()
val makeDeletion = deleteHelper.makeDeletion(
context,
media,
"Test reason"
)?.blockingGet()
assertNotNull(makeDeletion)
assertTrue(makeDeletion!!)
verify(pageEditClient).appendEdit(eq("User_Talk:$creatorName"), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())
@ -83,12 +113,24 @@ class DeleteHelperTest {
*/
@Test(expected = RuntimeException::class)
fun makeDeletionForPrependEditFailure() {
whenever(pageEditClient.prependEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(false))
whenever(pageEditClient.appendEdit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
whenever(pageEditClient.edit(ArgumentMatchers.anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString()))
.thenReturn(Observable.just(true))
whenever(pageEditClient.prependEdit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(false))
whenever(pageEditClient.appendEdit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(true))
whenever(pageEditClient.edit(
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString(),
ArgumentMatchers.anyString())
).thenReturn(Observable.just(true))
whenever(media.displayTitle).thenReturn("Test file")
whenever(media.filename).thenReturn("Test file.jpg")
whenever(media.author).thenReturn("Creator (page does not exist)")
@ -141,16 +183,30 @@ class DeleteHelperTest {
@Test
fun alertDialogPositiveButtonDisableTest() {
val mContext = RuntimeEnvironment.getApplication().applicationContext
deleteHelper.askReasonAndExecute(media, mContext, "My Question", ReviewController.DeleteReason.COPYRIGHT_VIOLATION, callback)
assertEquals(false, deleteHelper.dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled)
deleteHelper.askReasonAndExecute(
media,
mContext,
"My Question",
ReviewController.DeleteReason.COPYRIGHT_VIOLATION, callback
)
deleteHelper.getListener()?.onClick(
deleteHelper.getDialog(),
1,
true
)
assertEquals(
true,
deleteHelper.getDialog()?.getButton(AlertDialog.BUTTON_POSITIVE)?.isEnabled
)
}
@Test
fun alertDialogPositiveButtonEnableTest() {
val mContext = RuntimeEnvironment.getApplication().applicationContext
deleteHelper.askReasonAndExecute(media, mContext, "My Question", ReviewController.DeleteReason.COPYRIGHT_VIOLATION, callback)
deleteHelper.listener.onClick(deleteHelper.dialog, 1, true)
assertEquals(true, deleteHelper.dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled)
deleteHelper.getListener()?.onClick(deleteHelper.getDialog(), 1, true)
assertEquals(true, deleteHelper.getDialog()?.getButton(AlertDialog.BUTTON_POSITIVE)?.isEnabled)
}
@Test(expected = RuntimeException::class)

View file

@ -3,6 +3,7 @@ package fr.free.nrw.commons.delete
import android.content.Context
import android.content.res.Resources
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.profile.achievements.FeedbackResponse
@ -24,6 +25,7 @@ import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import org.powermock.api.mockito.PowerMockito
import java.util.Date
class ReasonBuilderTest {
@ -53,6 +55,9 @@ class ReasonBuilderTest {
@Test
fun forceLoginWhenAccountIsNull() {
PowerMockito.`when`(context?.getString(R.string.user_not_logged_in))
.thenReturn("Log-in expired. Please log in again.")
reasonBuilder!!.getReason(mock(Media::class.java), "test")
verify(sessionManager, times(1))!!.forceLogin(any(Context::class.java))
}

View file

@ -10,6 +10,7 @@ import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import fr.free.nrw.commons.R
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
@ -73,6 +74,15 @@ class DescriptionEditHelperUnitTest {
@Test
fun testShowCaptionEditNotificationCaseFalse() {
`when`(context.getString(R.string.caption_edit_helper_show_edit_title))
.thenReturn("Edit Caption")
`when`(context.getString(R.string.coordinates_edit_helper_show_edit_title_success))
.thenReturn("Success")
`when`(context.getString(R.string.caption_edit_helper_show_edit_message))
.thenReturn("Edit caption was successful")
`when`(context.getString(R.string.caption_edit_helper_edit_message_else))
.thenReturn("Edit caption failed")
val method: Method =
DescriptionEditHelper::class.java.getDeclaredMethod(
"showCaptionEditNotification",
@ -86,6 +96,15 @@ class DescriptionEditHelperUnitTest {
@Test
fun testShowCaptionEditNotificationCaseTrue() {
`when`(context.getString(R.string.caption_edit_helper_show_edit_title))
.thenReturn("Edit Caption")
`when`(context.getString(R.string.coordinates_edit_helper_show_edit_title_success))
.thenReturn("Success")
`when`(context.getString(R.string.caption_edit_helper_show_edit_message))
.thenReturn("Edit caption was successful")
`when`(context.getString(R.string.caption_edit_helper_edit_message_else))
.thenReturn("Edit caption failed")
val method: Method =
DescriptionEditHelper::class.java.getDeclaredMethod(
"showCaptionEditNotification",
@ -99,6 +118,15 @@ class DescriptionEditHelperUnitTest {
@Test
fun testShowDescriptionEditNotificationCaseFalse() {
`when`(context.getString(R.string.description_edit_helper_show_edit_title))
.thenReturn("Edit Description")
`when`(context.getString(R.string.coordinates_edit_helper_show_edit_title_success))
.thenReturn("Success")
`when`(context.getString(R.string.description_edit_helper_show_edit_message))
.thenReturn("Edit message")
`when`(context.getString(R.string.description_edit_helper_edit_message_else))
.thenReturn("Edit failed")
val method: Method =
DescriptionEditHelper::class.java.getDeclaredMethod(
"showDescriptionEditNotification",
@ -112,6 +140,15 @@ class DescriptionEditHelperUnitTest {
@Test
fun testShowDescriptionEditNotificationCaseTrue() {
`when`(context.getString(R.string.description_edit_helper_show_edit_title))
.thenReturn("Edit Description")
`when`(context.getString(R.string.coordinates_edit_helper_show_edit_title_success))
.thenReturn("Success")
`when`(context.getString(R.string.description_edit_helper_show_edit_message))
.thenReturn("Edit message")
`when`(context.getString(R.string.description_edit_helper_edit_message_else))
.thenReturn("Edit failed")
val method: Method =
DescriptionEditHelper::class.java.getDeclaredMethod(
"showDescriptionEditNotification",

View file

@ -27,6 +27,7 @@ import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.soloader.SoLoader
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.whenever
import fr.free.nrw.commons.LocationPicker.LocationPickerActivity
@ -768,9 +769,16 @@ class MediaDetailFragmentUnitTests {
).thenReturn(true)
doReturn(
Single.just(true),
).`when`(deleteHelper).makeDeletion(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())
).`when`(deleteHelper).makeDeletion(
ArgumentMatchers.any(),
ArgumentMatchers.any(),
ArgumentMatchers.any()
)
doReturn(Single.just("")).`when`(reasonBuilder).getReason(ArgumentMatchers.any(), ArgumentMatchers.any())
doReturn(Single.just("")).`when`(reasonBuilder).getReason(
ArgumentMatchers.any(),
ArgumentMatchers.any()
)
val method: Method =
MediaDetailFragment::class.java.getDeclaredMethod(