-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathAppSyncViewModelTest.kt
More file actions
175 lines (150 loc) · 6.63 KB
/
AppSyncViewModelTest.kt
File metadata and controls
175 lines (150 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.android.ui.home
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.config.TestDispatcherProvider
import com.wire.kalium.logic.feature.client.MLSClientManager
import com.wire.kalium.logic.feature.conversation.keyingmaterials.KeyingMaterialsManager
import com.wire.kalium.logic.feature.e2ei.SyncCertificateRevocationListUseCase
import com.wire.kalium.logic.feature.e2ei.usecase.ObserveCertificateRevocationForSelfClientUseCase
import com.wire.kalium.logic.feature.featureConfig.FeatureFlagsSyncWorker
import com.wire.kalium.logic.feature.mlsmigration.MLSMigrationManager
import com.wire.kalium.logic.feature.server.UpdateApiVersionsUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
@ExtendWith(CoroutineTestExtension::class)
class AppSyncViewModelTest {
private val testDispatcher = TestDispatcherProvider()
@Test
fun `when startSyncingAppConfig is called then it should call the use case`() = runTest(testDispatcher.io()) {
val (arrangement, viewModel) = Arrangement().arrange(testDispatcher) {
withObserveCertificateRevocationForSelfClient()
withFeatureFlagsSyncWorker()
withSyncCertificateRevocationListUseCase()
withUpdateApiVersions()
withMlsClientManager()
withMlsMigrationManager()
withKeyingMaterialsManager()
}
viewModel.startSyncingAppConfig()
advanceUntilIdle()
coVerify { arrangement.observeCertificateRevocationForSelfClient.invoke() }
coVerify { arrangement.syncCertificateRevocationListUseCase.invoke() }
coVerify { arrangement.featureFlagsSyncWorker.execute() }
coVerify { arrangement.updateApiVersions() }
coVerify { arrangement.mlsClientManager() }
coVerify { arrangement.mlsMigrationManager() }
coVerify { arrangement.keyingMaterialsManager() }
}
@Test
fun `when startSyncingAppConfig is called multiple times then it should call the use case with delay`() = runTest(testDispatcher.io()) {
val (arrangement, viewModel) = Arrangement().arrange(testDispatcher) {
withObserveCertificateRevocationForSelfClient(1000)
withFeatureFlagsSyncWorker(1000)
withSyncCertificateRevocationListUseCase(1000)
withUpdateApiVersions(1000)
withMlsClientManager(1000)
withMlsMigrationManager(1000)
withKeyingMaterialsManager(1000)
}
viewModel.startSyncingAppConfig()
viewModel.startSyncingAppConfig()
viewModel.startSyncingAppConfig()
advanceUntilIdle()
coVerify(exactly = 1) { arrangement.observeCertificateRevocationForSelfClient.invoke() }
coVerify(exactly = 1) { arrangement.syncCertificateRevocationListUseCase.invoke() }
coVerify(exactly = 1) { arrangement.featureFlagsSyncWorker.execute() }
coVerify(exactly = 1) { arrangement.updateApiVersions() }
coVerify(exactly = 1) { arrangement.mlsClientManager() }
coVerify(exactly = 1) { arrangement.mlsMigrationManager() }
coVerify(exactly = 1) { arrangement.keyingMaterialsManager() }
}
private class Arrangement {
@MockK
lateinit var syncCertificateRevocationListUseCase: SyncCertificateRevocationListUseCase
@MockK
lateinit var observeCertificateRevocationForSelfClient: ObserveCertificateRevocationForSelfClientUseCase
@MockK
lateinit var featureFlagsSyncWorker: FeatureFlagsSyncWorker
@MockK
lateinit var updateApiVersions: UpdateApiVersionsUseCase
@MockK
lateinit var mlsClientManager: MLSClientManager
@MockK
lateinit var mlsMigrationManager: MLSMigrationManager
@MockK
lateinit var keyingMaterialsManager: KeyingMaterialsManager
init {
MockKAnnotations.init(this)
}
fun withObserveCertificateRevocationForSelfClient(delayMs: Long = 0) {
coEvery { observeCertificateRevocationForSelfClient.invoke() } coAnswers {
delay(delayMs)
}
}
fun withSyncCertificateRevocationListUseCase(delayMs: Long = 0) {
coEvery { syncCertificateRevocationListUseCase.invoke() } coAnswers {
delay(delayMs)
}
}
fun withFeatureFlagsSyncWorker(delayMs: Long = 0) {
coEvery { featureFlagsSyncWorker.execute() } coAnswers {
delay(delayMs)
}
}
fun withUpdateApiVersions(delayMs: Long = 0) {
coEvery { updateApiVersions() } coAnswers {
delay(delayMs)
}
}
fun withMlsClientManager(delayMs: Long = 0) {
coEvery { mlsClientManager() } coAnswers {
delay(delayMs)
}
}
fun withMlsMigrationManager(delayMs: Long = 0) {
coEvery { mlsMigrationManager() } coAnswers {
delay(delayMs)
}
}
fun withKeyingMaterialsManager(delayMs: Long = 0) {
coEvery { keyingMaterialsManager() } coAnswers {
delay(delayMs)
}
}
fun arrange(testDispatcher: TestDispatcherProvider, block: Arrangement.() -> Unit) = apply(block).let {
this to AppSyncViewModel(
syncCertificateRevocationListUseCase,
observeCertificateRevocationForSelfClient,
featureFlagsSyncWorker,
updateApiVersions,
mLSClientManager = mlsClientManager,
mLSMigrationManager = mlsMigrationManager,
keyingMaterialsManager = keyingMaterialsManager,
dispatcher = testDispatcher
)
}
}
}