From 8c851ba02a4359fd75300b7e60b98acd0e723068 Mon Sep 17 00:00:00 2001 From: parneet-guraya Date: Sun, 8 Dec 2024 03:24:00 +0530 Subject: [PATCH] fix CI Signed-off-by: parneet-guraya --- .../ui/adapter/ImageAdapterTest.kt | 6 +-- .../ui/selector/ImageLoaderTest.kt | 49 ++++++++++++++----- .../media/MediaDetailFragmentUnitTests.kt | 24 ++++----- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/adapter/ImageAdapterTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/adapter/ImageAdapterTest.kt index 2a4c8c7915..6f2f709172 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/adapter/ImageAdapterTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/adapter/ImageAdapterTest.kt @@ -17,7 +17,7 @@ import fr.free.nrw.commons.customselector.ui.selector.ImageLoader import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain import org.junit.After @@ -34,7 +34,6 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import java.lang.reflect.Field import java.util.TreeMap -import kotlin.collections.ArrayList /** * Custom Selector image adapter test. @@ -65,7 +64,7 @@ class ImageAdapterTest { private lateinit var selectedImageField: Field private var uri: Uri = Mockito.mock(Uri::class.java) private lateinit var image: Image - private val testDispatcher = TestCoroutineDispatcher() + private val testDispatcher = StandardTestDispatcher() /** * Set up variables. @@ -91,7 +90,6 @@ class ImageAdapterTest { @After fun tearDown() { Dispatchers.resetMain() - testDispatcher.cleanupTestCoroutines() } /** diff --git a/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/selector/ImageLoaderTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/selector/ImageLoaderTest.kt index 64447384b8..3111031dea 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/selector/ImageLoaderTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/customselector/ui/selector/ImageLoaderTest.kt @@ -21,9 +21,10 @@ import fr.free.nrw.commons.upload.FileUtilsWrapper import io.reactivex.Single import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.runBlockingTest +import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.setMain import org.junit.After import org.junit.Assert @@ -45,7 +46,6 @@ import java.io.File import java.io.FileInputStream import java.util.Calendar import java.util.Date -import kotlin.collections.HashMap /** * Image Loader Test. @@ -92,7 +92,7 @@ class ImageLoaderTest { private lateinit var contentResolver: ContentResolver @ExperimentalCoroutinesApi - private val testDispacher = TestCoroutineDispatcher() + private val testDispacher = StandardTestDispatcher() private lateinit var imageLoader: ImageLoader private var mapImageSHA1: HashMap = HashMap() @@ -153,7 +153,6 @@ class ImageLoaderTest { @ExperimentalCoroutinesApi fun tearDown() { Dispatchers.resetMain() - testDispacher.cleanupTestCoroutines() mockedPickedFiles.close() } @@ -162,7 +161,7 @@ class ImageLoaderTest { */ @Test fun testQueryAndSetViewUploadedStatusNull() = - testDispacher.runBlockingTest { + TestScope(testDispacher).runTest { whenever(uploadedStatusDao.getUploadedFromImageSHA1(any())).thenReturn(null) whenever(notForUploadStatusDao.find(any())).thenReturn(0) mapModifiedImageSHA1[image] = "testSha1" @@ -182,7 +181,7 @@ class ImageLoaderTest { */ @Test fun testQueryAndSetViewUploadedStatusNotNull() = - testDispacher.runBlockingTest { + TestScope(testDispacher).runTest { whenever(uploadedStatusDao.getUploadedFromImageSHA1(any())).thenReturn(uploadedStatus) whenever(notForUploadStatusDao.find(any())).thenReturn(0) whenever(context.getSharedPreferences("custom_selector", 0)) @@ -195,7 +194,7 @@ class ImageLoaderTest { */ @Test fun testNextActionableImage() = - testDispacher.runBlockingTest { + TestScope(testDispacher).runTest { whenever(notForUploadStatusDao.find(any())).thenReturn(0) whenever(uploadedStatusDao.findByImageSHA1(any(), any())).thenReturn(0) whenever(uploadedStatusDao.findByModifiedImageSHA1(any(), any())).thenReturn(0) @@ -208,16 +207,40 @@ class ImageLoaderTest { whenever(PickedFiles.pickedExistingPicture(context, Uri.parse("test"))).thenReturn( uploadableFile, ) - imageLoader.nextActionableImage(listOf(image), testDispacher, testDispacher, 0, emptyList()) + imageLoader.nextActionableImage( + listOf(image), + testDispacher, + testDispacher, + 0, + emptyList() + ) whenever(notForUploadStatusDao.find(any())).thenReturn(1) - imageLoader.nextActionableImage(listOf(image), testDispacher, testDispacher, 0, emptyList()) + imageLoader.nextActionableImage( + listOf(image), + testDispacher, + testDispacher, + 0, + emptyList() + ) whenever(uploadedStatusDao.findByImageSHA1(any(), any())).thenReturn(2) - imageLoader.nextActionableImage(listOf(image), testDispacher, testDispacher, 0, emptyList()) + imageLoader.nextActionableImage( + listOf(image), + testDispacher, + testDispacher, + 0, + emptyList() + ) whenever(uploadedStatusDao.findByModifiedImageSHA1(any(), any())).thenReturn(2) - imageLoader.nextActionableImage(listOf(image), testDispacher, testDispacher, 0, emptyList()) + imageLoader.nextActionableImage( + listOf(image), + testDispacher, + testDispacher, + 0, + emptyList() + ) } /** @@ -226,7 +249,7 @@ class ImageLoaderTest { @Test @ExperimentalCoroutinesApi fun testGetSha1() = - testDispacher.runBlockingTest { + TestScope(testDispacher).runTest { BDDMockito .given(PickedFiles.pickedExistingPicture(context, image.uri)) .willReturn(UploadableFile(uri, File("ABC"))) diff --git a/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt b/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt index 9f73d2b81d..37b5277f47 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/media/MediaDetailFragmentUnitTests.kt @@ -162,7 +162,7 @@ class MediaDetailFragmentUnitTests { @Mock private lateinit var mockSharedPreferencesEditor: SharedPreferences.Editor - private lateinit var binding: FragmentMediaDetailBinding + private lateinit var _binding: FragmentMediaDetailBinding @Before fun setUp() { @@ -185,12 +185,12 @@ class MediaDetailFragmentUnitTests { layoutInflater = LayoutInflater.from(activity) - binding = FragmentMediaDetailBinding.inflate(layoutInflater) + _binding = FragmentMediaDetailBinding.inflate(layoutInflater) - scrollView = binding.mediaDetailScrollView + scrollView = _binding.mediaDetailScrollView - progressBarDeletion = binding.progressBarDeletion - delete = binding.nominateDeletion + progressBarDeletion = _binding.progressBarDeletion + delete = _binding.nominateDeletion Whitebox.setInternalState(fragment, "media", media) Whitebox.setInternalState(fragment, "isDeleted", isDeleted) @@ -198,13 +198,13 @@ class MediaDetailFragmentUnitTests { Whitebox.setInternalState(fragment, "reasonListEnglishMappings", reasonListEnglishMappings) Whitebox.setInternalState(fragment, "reasonBuilder", reasonBuilder) Whitebox.setInternalState(fragment, "deleteHelper", deleteHelper) - Whitebox.setInternalState(fragment, "binding", binding) + Whitebox.setInternalState(fragment, "_binding", _binding) Whitebox.setInternalState(fragment, "detailProvider", detailProvider) - Whitebox.setInternalState(binding, "mediaDetailImageView", simpleDraweeView) - Whitebox.setInternalState(binding, "mediaDetailTitle", textView) - Whitebox.setInternalState(binding, "mediaDetailDepictionContainer", linearLayout) - Whitebox.setInternalState(binding, "dummyCaptionDescriptionContainer", linearLayout) - Whitebox.setInternalState(binding, "depictionsEditButton", button) + Whitebox.setInternalState(_binding, "mediaDetailImageView", simpleDraweeView) + Whitebox.setInternalState(_binding, "mediaDetailTitle", textView) + Whitebox.setInternalState(_binding, "mediaDetailDepictionContainer", linearLayout) + Whitebox.setInternalState(_binding, "dummyCaptionDescriptionContainer", linearLayout) + Whitebox.setInternalState(_binding, "depictionsEditButton", button) Whitebox.setInternalState(fragment, "locationManager", locationManager) `when`(simpleDraweeView.hierarchy).thenReturn(genericDraweeHierarchy) @@ -241,7 +241,7 @@ class MediaDetailFragmentUnitTests { @Throws(Exception::class) fun testLaunchZoomActivity() { `when`(media.imageUrl).thenReturn("") - fragment.launchZoomActivity(binding.root) + fragment.launchZoomActivity(_binding.root) } @Test