Skip to content

Commit 6a385e3

Browse files
committed
Unit Testing | Patch 30
1 parent 562e93f commit 6a385e3

File tree

3 files changed

+310
-2
lines changed

3 files changed

+310
-2
lines changed

presentation/src/main/java/com/shifthackz/aisdv1/presentation/screen/inpaint/InPaintViewModel.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.shifthackz.aisdv1.presentation.screen.inpaint
22

3-
import com.shifthackz.aisdv1.core.common.log.debugLog
43
import com.shifthackz.aisdv1.core.common.log.errorLog
54
import com.shifthackz.aisdv1.core.common.schedulers.SchedulersProvider
65
import com.shifthackz.aisdv1.core.common.schedulers.subscribeOnMainThread
@@ -32,7 +31,6 @@ class InPaintViewModel(
3231
}
3332

3433
override fun processIntent(intent: InPaintIntent) {
35-
debugLog("INTENT : $intent")
3634
when (intent) {
3735
is InPaintIntent.DrawPath -> updateState { state ->
3836
state.copy(
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package com.shifthackz.aisdv1.presentation.screen.inpaint
2+
3+
import android.graphics.Bitmap
4+
import androidx.compose.ui.graphics.Path
5+
import com.shifthackz.aisdv1.presentation.core.CoreViewModelTest
6+
import com.shifthackz.aisdv1.presentation.model.InPaintModel
7+
import com.shifthackz.aisdv1.presentation.model.Modal
8+
import com.shifthackz.aisdv1.presentation.navigation.router.main.MainRouter
9+
import com.shifthackz.aisdv1.presentation.stub.stubSchedulersProvider
10+
import io.mockk.every
11+
import io.mockk.mockk
12+
import io.mockk.verify
13+
import io.reactivex.rxjava3.core.BackpressureStrategy
14+
import io.reactivex.rxjava3.subjects.BehaviorSubject
15+
import kotlinx.coroutines.test.runTest
16+
import org.junit.Assert
17+
import org.junit.Before
18+
import org.junit.Test
19+
20+
class InPaintViewModelTest : CoreViewModelTest<InPaintViewModel>() {
21+
22+
private val stubBitmap = mockk<Bitmap>()
23+
private val stubPath = mockk<Path>()
24+
private val stubInPainSubject = BehaviorSubject.create<InPaintModel>()
25+
private val stubBitmapSubject = BehaviorSubject.create<Bitmap>()
26+
private val stubInPaintStateProducer = mockk<InPaintStateProducer>()
27+
private val stubMainRouter = mockk<MainRouter>()
28+
29+
override fun initializeViewModel() = InPaintViewModel(
30+
schedulersProvider = stubSchedulersProvider,
31+
stateProducer = stubInPaintStateProducer,
32+
mainRouter = stubMainRouter,
33+
)
34+
35+
@Before
36+
override fun initialize() {
37+
super.initialize()
38+
39+
every {
40+
stubInPaintStateProducer.observeInPaint()
41+
} returns stubInPainSubject.toFlowable(BackpressureStrategy.LATEST)
42+
43+
every {
44+
stubInPaintStateProducer.observeBitmap()
45+
} returns stubBitmapSubject.toFlowable(BackpressureStrategy.LATEST)
46+
}
47+
48+
@Test
49+
fun `initialized, expected UI state updated with correct stub values`() {
50+
stubBitmapSubject.onNext(stubBitmap)
51+
stubInPainSubject.onNext(InPaintModel())
52+
runTest {
53+
val state = viewModel.state.value
54+
Assert.assertEquals(InPaintModel(), state.model)
55+
Assert.assertEquals(stubBitmap, state.bitmap)
56+
}
57+
}
58+
59+
@Test
60+
fun `given received DrawPath intent, expected last path in UI state added from intent`() {
61+
viewModel.processIntent(InPaintIntent.DrawPath(stubPath))
62+
runTest {
63+
val expected = stubPath to 16
64+
val actual = viewModel.state.value.model.paths.last()
65+
Assert.assertEquals(expected, actual)
66+
}
67+
}
68+
69+
@Test
70+
fun `given received DrawPathBmp intent, expected model bitmap updated in UI state from intent`() {
71+
viewModel.processIntent(InPaintIntent.DrawPathBmp(stubBitmap))
72+
runTest {
73+
val expected = stubBitmap
74+
val actual = viewModel.state.value.model.bitmap
75+
Assert.assertEquals(expected, actual)
76+
}
77+
}
78+
79+
@Test
80+
fun `given received NavigateBack intent, expected state producer updateInPaint(), router navigateBack() methods called`() {
81+
every {
82+
stubInPaintStateProducer.updateInPaint(any())
83+
} returns Unit
84+
85+
every {
86+
stubMainRouter.navigateBack()
87+
} returns Unit
88+
89+
viewModel.processIntent(InPaintIntent.NavigateBack)
90+
91+
verify {
92+
stubInPaintStateProducer.updateInPaint(viewModel.state.value.model)
93+
}
94+
verify {
95+
stubMainRouter.navigateBack()
96+
}
97+
}
98+
99+
@Test
100+
fun `given received SelectTab intent, expected selectedTab field updated in UI state`() {
101+
viewModel.processIntent(InPaintIntent.SelectTab(InPaintState.Tab.FORM))
102+
runTest {
103+
val expected = InPaintState.Tab.FORM
104+
val actual = viewModel.state.value.selectedTab
105+
Assert.assertEquals(expected, actual)
106+
}
107+
}
108+
109+
@Test
110+
fun `given received ChangeCapSize intent, expected size field updated in UI state`() {
111+
viewModel.processIntent(InPaintIntent.ChangeCapSize(5598))
112+
runTest {
113+
val expected = 5598
114+
val actual = viewModel.state.value.size
115+
Assert.assertEquals(expected, actual)
116+
}
117+
}
118+
119+
@Test
120+
fun `given received Action Undo intent, expected last path in UI state removed`() {
121+
viewModel.processIntent(InPaintIntent.Action.Undo)
122+
runTest {
123+
val expected = emptyList<Pair<Path, Int>>()
124+
val actual = viewModel.state.value.model.paths
125+
Assert.assertEquals(expected, actual)
126+
}
127+
}
128+
129+
@Test
130+
fun `given received ScreenModal Dismiss intent, expected screenModal in UI state is None`() {
131+
viewModel.processIntent(InPaintIntent.ScreenModal.Dismiss)
132+
runTest {
133+
val expected = Modal.None
134+
val actual = viewModel.state.value.screenModal
135+
Assert.assertEquals(expected, actual)
136+
}
137+
}
138+
139+
@Test
140+
fun `given received ScreenModal Show intent, expected screenModal in UI state is updated`() {
141+
viewModel.processIntent(InPaintIntent.ScreenModal.Show(Modal.Language))
142+
runTest {
143+
val expected = Modal.Language
144+
val actual = viewModel.state.value.screenModal
145+
Assert.assertEquals(expected, actual)
146+
}
147+
}
148+
149+
@Test
150+
fun `given received Action Clear intent, expected screenModal is None, paths is empty in UI state`() {
151+
viewModel.processIntent(InPaintIntent.Action.Clear)
152+
runTest {
153+
val state = viewModel.state.value
154+
Assert.assertEquals(Modal.None, state.screenModal)
155+
Assert.assertEquals(emptyList<Pair<Path, Int>>(), state.model.paths)
156+
}
157+
}
158+
159+
@Test
160+
fun `given received Update MaskBlur intent, expected maskBlur is updated in UI state`() {
161+
viewModel.processIntent(InPaintIntent.Update.MaskBlur(5598))
162+
runTest {
163+
val expected = 5598
164+
val actual = viewModel.state.value.model.maskBlur
165+
Assert.assertEquals(expected, actual)
166+
}
167+
}
168+
169+
@Test
170+
fun `given received Update OnlyMaskedPadding intent, expected onlyMaskedPaddingPx is updated in UI state`() {
171+
viewModel.processIntent(InPaintIntent.Update.OnlyMaskedPadding(5598))
172+
runTest {
173+
val expected = 5598
174+
val actual = viewModel.state.value.model.onlyMaskedPaddingPx
175+
Assert.assertEquals(expected, actual)
176+
}
177+
}
178+
179+
@Test
180+
fun `given received Update Area intent, expected inPaintArea is updated in UI state`() {
181+
viewModel.processIntent(InPaintIntent.Update.Area(InPaintModel.Area.WholePicture))
182+
runTest {
183+
val expected = InPaintModel.Area.WholePicture
184+
val actual = viewModel.state.value.model.inPaintArea
185+
Assert.assertEquals(expected, actual)
186+
}
187+
}
188+
189+
@Test
190+
fun `given received Update MaskContent intent, expected maskContent is updated in UI state`() {
191+
viewModel.processIntent(InPaintIntent.Update.MaskContent(InPaintModel.MaskContent.Fill))
192+
runTest {
193+
val expected = InPaintModel.MaskContent.Fill
194+
val actual = viewModel.state.value.model.maskContent
195+
Assert.assertEquals(expected, actual)
196+
}
197+
}
198+
199+
@Test
200+
fun `given received Update MaskMode intent, expected maskMode is updated in UI state`() {
201+
viewModel.processIntent(InPaintIntent.Update.MaskMode(InPaintModel.MaskMode.InPaintNotMasked))
202+
runTest {
203+
val expected = InPaintModel.MaskMode.InPaintNotMasked
204+
val actual = viewModel.state.value.model.maskMode
205+
Assert.assertEquals(expected, actual)
206+
}
207+
}
208+
}

presentation/src/test/java/com/shifthackz/aisdv1/presentation/screen/setup/ServerSetupViewModelTest.kt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.shifthackz.aisdv1.core.validation.common.CommonStringValidator
44
import com.shifthackz.aisdv1.core.validation.url.UrlValidator
55
import com.shifthackz.aisdv1.domain.entity.Configuration
66
import com.shifthackz.aisdv1.domain.entity.DownloadState
7+
import com.shifthackz.aisdv1.domain.entity.ServerSource
78
import com.shifthackz.aisdv1.domain.interactor.settings.SetupConnectionInterActor
89
import com.shifthackz.aisdv1.domain.interactor.wakelock.WakeLockInterActor
910
import com.shifthackz.aisdv1.domain.preference.PreferenceManager
@@ -25,6 +26,7 @@ import io.mockk.verify
2526
import io.reactivex.rxjava3.core.Completable
2627
import io.reactivex.rxjava3.core.Observable
2728
import io.reactivex.rxjava3.core.Single
29+
import kotlinx.coroutines.flow.firstOrNull
2830
import kotlinx.coroutines.test.runTest
2931
import org.junit.Assert
3032
import org.junit.Before
@@ -337,4 +339,104 @@ class ServerSetupViewModelTest : CoreViewModelTest<ServerSetupViewModel>() {
337339
Assert.assertEquals(expected, actual)
338340
}
339341
}
342+
343+
@Test
344+
fun `given received UpdateServerMode intent, expected mode field in UI state is LOCAL`() {
345+
viewModel.processIntent(ServerSetupIntent.UpdateServerMode(ServerSource.LOCAL))
346+
runTest {
347+
val expected = ServerSource.LOCAL
348+
val actual = viewModel.state.value.mode
349+
Assert.assertEquals(expected, actual)
350+
}
351+
}
352+
353+
@Test
354+
fun `given received UpdateServerUrl intent, expected serverUrl field updated, serverUrlValidationError is null in UI state`() {
355+
viewModel.processIntent(ServerSetupIntent.UpdateServerUrl("https://5598.is.my.favorite.com"))
356+
runTest {
357+
val expected = "https://5598.is.my.favorite.com"
358+
val actual = viewModel.state.value.serverUrl
359+
Assert.assertEquals(expected, actual)
360+
}
361+
}
362+
363+
@Test
364+
fun `given received LaunchUrl intent, expected LaunchUrl effect delivered to effect collector`() {
365+
val intent = mockk<ServerSetupIntent.LaunchUrl.A1111Instructions>()
366+
every {
367+
intent::url.get()
368+
} returns "https://5598.is.my.favorite.com"
369+
370+
viewModel.processIntent(intent)
371+
372+
runTest {
373+
val expected = ServerSetupEffect.LaunchUrl("https://5598.is.my.favorite.com")
374+
val actual = viewModel.effect.firstOrNull()
375+
Assert.assertEquals(expected, actual)
376+
}
377+
}
378+
379+
@Test
380+
fun `given received LaunchManageStoragePermission intent, expected LaunchManageStoragePermission effect delivered to effect collector`() {
381+
viewModel.processIntent(ServerSetupIntent.LaunchManageStoragePermission)
382+
runTest {
383+
val expected = ServerSetupEffect.LaunchManageStoragePermission
384+
val actual = viewModel.effect.firstOrNull()
385+
Assert.assertEquals(expected, actual)
386+
}
387+
}
388+
389+
@Test
390+
fun `given received NavigateBack intent, expected router navigateBack() method called`() {
391+
every {
392+
stubMainRouter.navigateBack()
393+
} returns Unit
394+
395+
viewModel.processIntent(ServerSetupIntent.NavigateBack)
396+
397+
verify {
398+
stubMainRouter.navigateBack()
399+
}
400+
}
401+
402+
@Test
403+
fun `given received UpdateStabilityAiApiKey intent, expected stabilityAiApiKey field in UI state is 5598`() {
404+
viewModel.processIntent(ServerSetupIntent.UpdateStabilityAiApiKey("5598"))
405+
runTest {
406+
val expected = "5598"
407+
val actual = viewModel.state.value.stabilityAiApiKey
408+
Assert.assertEquals(expected, actual)
409+
}
410+
}
411+
412+
@Test
413+
fun `given received ConnectToLocalHost intent, expected success, router navigateToHomeScreen() method called, preference forceSetupAfterUpdate is false, dialog is None`() {
414+
every {
415+
stubSetupConnectionInterActor.connectToA1111(any(), any(), any())
416+
} returns Single.just(Result.success(Unit))
417+
418+
every {
419+
stubMainRouter.navigateToHomeScreen()
420+
} returns Unit
421+
422+
every {
423+
stubPreferenceManager::forceSetupAfterUpdate.set(any())
424+
} returns Unit
425+
426+
viewModel.processIntent(ServerSetupIntent.ConnectToLocalHost)
427+
428+
runTest {
429+
Assert.assertEquals(
430+
ServerSetupEffect.HideKeyboard,
431+
viewModel.effect.firstOrNull(),
432+
)
433+
Assert.assertEquals(
434+
Modal.None,
435+
viewModel.state.value.screenModal,
436+
)
437+
}
438+
verify {
439+
stubMainRouter.navigateToHomeScreen()
440+
}
441+
}
340442
}

0 commit comments

Comments
 (0)