|
| 1 | +package com.shifthackz.aisdv1.presentation.screen.gallery.detail |
| 2 | + |
| 3 | +import android.graphics.Bitmap |
| 4 | +import com.shifthackz.aisdv1.core.imageprocessing.Base64ToBitmapConverter |
| 5 | +import com.shifthackz.aisdv1.core.model.asUiText |
| 6 | +import com.shifthackz.aisdv1.domain.usecase.caching.GetLastResultFromCacheUseCase |
| 7 | +import com.shifthackz.aisdv1.domain.usecase.gallery.DeleteGalleryItemUseCase |
| 8 | +import com.shifthackz.aisdv1.domain.usecase.generation.GetGenerationResultUseCase |
| 9 | +import com.shifthackz.aisdv1.presentation.core.CoreViewModelTest |
| 10 | +import com.shifthackz.aisdv1.presentation.core.GenerationFormUpdateEvent |
| 11 | +import com.shifthackz.aisdv1.presentation.extensions.mapToUi |
| 12 | +import com.shifthackz.aisdv1.presentation.mocks.mockAiGenerationResult |
| 13 | +import com.shifthackz.aisdv1.presentation.model.Modal |
| 14 | +import com.shifthackz.aisdv1.presentation.navigation.router.main.MainRouter |
| 15 | +import com.shifthackz.aisdv1.presentation.stub.stubSchedulersProvider |
| 16 | +import io.mockk.every |
| 17 | +import io.mockk.mockk |
| 18 | +import io.mockk.verify |
| 19 | +import io.reactivex.rxjava3.core.Completable |
| 20 | +import io.reactivex.rxjava3.core.Single |
| 21 | +import kotlinx.coroutines.flow.firstOrNull |
| 22 | +import kotlinx.coroutines.test.runTest |
| 23 | +import org.junit.Assert |
| 24 | +import org.junit.Before |
| 25 | +import org.junit.Test |
| 26 | + |
| 27 | +class GalleryDetailViewModelTest : CoreViewModelTest<GalleryDetailViewModel>() { |
| 28 | + |
| 29 | + private val stubBitmap = mockk<Bitmap>() |
| 30 | + private val stubGetGenerationResultUseCase = mockk<GetGenerationResultUseCase>() |
| 31 | + private val stubGetLastResultFromCacheUseCase = mockk<GetLastResultFromCacheUseCase>() |
| 32 | + private val stubDeleteGalleryItemUseCase = mockk<DeleteGalleryItemUseCase>() |
| 33 | + private val stubGalleryDetailBitmapExporter = mockk<GalleryDetailBitmapExporter>() |
| 34 | + private val stubBase64ToBitmapConverter = mockk<Base64ToBitmapConverter>() |
| 35 | + private val stubGenerationFormUpdateEvent = mockk<GenerationFormUpdateEvent>() |
| 36 | + private val stubMainRouter = mockk<MainRouter>() |
| 37 | + |
| 38 | + override fun initializeViewModel() = GalleryDetailViewModel( |
| 39 | + itemId = 5598L, |
| 40 | + getGenerationResultUseCase = stubGetGenerationResultUseCase, |
| 41 | + getLastResultFromCacheUseCase = stubGetLastResultFromCacheUseCase, |
| 42 | + deleteGalleryItemUseCase = stubDeleteGalleryItemUseCase, |
| 43 | + galleryDetailBitmapExporter = stubGalleryDetailBitmapExporter, |
| 44 | + base64ToBitmapConverter = stubBase64ToBitmapConverter, |
| 45 | + schedulersProvider = stubSchedulersProvider, |
| 46 | + generationFormUpdateEvent = stubGenerationFormUpdateEvent, |
| 47 | + mainRouter = stubMainRouter, |
| 48 | + ) |
| 49 | + |
| 50 | + @Before |
| 51 | + override fun initialize() { |
| 52 | + super.initialize() |
| 53 | + |
| 54 | + every { |
| 55 | + stubGetLastResultFromCacheUseCase() |
| 56 | + } returns Single.just(mockAiGenerationResult) |
| 57 | + |
| 58 | + every { |
| 59 | + stubGetGenerationResultUseCase(any()) |
| 60 | + } returns Single.just(mockAiGenerationResult) |
| 61 | + |
| 62 | + every { |
| 63 | + stubBase64ToBitmapConverter(any()) |
| 64 | + } returns Single.just(Base64ToBitmapConverter.Output(stubBitmap)) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `initialized, loaded ai generation result, expected UI state is Content`() { |
| 69 | + runTest { |
| 70 | + val expected = GalleryDetailState.Content( |
| 71 | + tabs = GalleryDetailState.Tab.consume(mockAiGenerationResult.type), |
| 72 | + generationType = mockAiGenerationResult.type, |
| 73 | + id = mockAiGenerationResult.id, |
| 74 | + bitmap = stubBitmap, |
| 75 | + inputBitmap = stubBitmap, |
| 76 | + createdAt = mockAiGenerationResult.createdAt.toString().asUiText(), |
| 77 | + type = mockAiGenerationResult.type.key.asUiText(), |
| 78 | + prompt = mockAiGenerationResult.prompt.asUiText(), |
| 79 | + negativePrompt = mockAiGenerationResult.negativePrompt.asUiText(), |
| 80 | + size = "512 X 512".asUiText(), |
| 81 | + samplingSteps = mockAiGenerationResult.samplingSteps.toString().asUiText(), |
| 82 | + cfgScale = mockAiGenerationResult.cfgScale.toString().asUiText(), |
| 83 | + restoreFaces = mockAiGenerationResult.restoreFaces.mapToUi(), |
| 84 | + sampler = mockAiGenerationResult.sampler.asUiText(), |
| 85 | + seed = mockAiGenerationResult.seed.asUiText(), |
| 86 | + subSeed = mockAiGenerationResult.subSeed.asUiText(), |
| 87 | + subSeedStrength = mockAiGenerationResult.subSeedStrength.toString().asUiText(), |
| 88 | + denoisingStrength = mockAiGenerationResult.denoisingStrength.toString().asUiText(), |
| 89 | + ) |
| 90 | + val actual = viewModel.state.value |
| 91 | + Assert.assertEquals(expected, actual) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `given received CopyToClipboard intent, expected ShareClipBoard effect delivered to effect collector`() { |
| 97 | + viewModel.processIntent(GalleryDetailIntent.CopyToClipboard("text")) |
| 98 | + runTest { |
| 99 | + val expected = GalleryDetailEffect.ShareClipBoard("text") |
| 100 | + val actual = viewModel.effect.firstOrNull() |
| 101 | + Assert.assertEquals(expected, actual) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `given received Delete_Request intent, expected modal field in UI state is DeleteImageConfirm`() { |
| 107 | + viewModel.processIntent(GalleryDetailIntent.Delete.Request) |
| 108 | + runTest { |
| 109 | + val expected = Modal.DeleteImageConfirm |
| 110 | + val actual = (viewModel.state.value as? GalleryDetailState.Content)?.screenModal |
| 111 | + Assert.assertEquals(expected, actual) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `given received Delete_Confirm intent, expected modal field in UI state is None, deleteGalleryItemUseCase() method is called`() { |
| 117 | + every { |
| 118 | + stubDeleteGalleryItemUseCase(any()) |
| 119 | + } returns Completable.complete() |
| 120 | + viewModel.processIntent(GalleryDetailIntent.Delete.Confirm) |
| 121 | + runTest { |
| 122 | + val expected = Modal.None |
| 123 | + val actual = (viewModel.state.value as? GalleryDetailState.Content)?.screenModal |
| 124 | + Assert.assertEquals(expected, actual) |
| 125 | + } |
| 126 | + verify { |
| 127 | + stubDeleteGalleryItemUseCase(5598L) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments