Skip to content

Commit 020567f

Browse files
authored
Merge pull request #4722 from owncloud/technical/migrate_tests_to_new_coroutines_api
[TECHNICAL] Migrate tests to the new kotlinx-coroutines-test API
2 parents faca97a + f5a658d commit 020567f

File tree

10 files changed

+680
-612
lines changed

10 files changed

+680
-612
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Table of Contents
22

3+
* [Changelog for unreleased](#changelog-for-owncloud-android-client-unreleased-unreleased)
34
* [Changelog for 4.7.0](#changelog-for-owncloud-android-client-470-2025-11-17)
45
* [Changelog for 4.6.2](#changelog-for-owncloud-android-client-462-2025-08-13)
56
* [Changelog for 4.6.1](#changelog-for-owncloud-android-client-461-2025-08-01)
@@ -30,6 +31,27 @@
3031
* [Changelog for 2.18.1](#changelog-for-owncloud-android-client-2181-2021-07-20)
3132
* [Changelog for 2.18.0](#changelog-for-owncloud-android-client-2180-2021-05-24)
3233
* [Changelog for 2.17 versions and below](#changelog-for-217-versions-and-below)
34+
# Changelog for ownCloud Android Client [unreleased] (UNRELEASED)
35+
36+
The following sections list the changes in ownCloud Android Client unreleased relevant to
37+
ownCloud admins and users.
38+
39+
[unreleased]: https://github.com/owncloud/android/compare/v4.7.0...master
40+
41+
## Summary
42+
43+
* Change - Migrate tests to the new kotlinx-coroutines-test API: [#4710](https://github.com/owncloud/android/issues/4710)
44+
45+
## Details
46+
47+
* Change - Migrate tests to the new kotlinx-coroutines-test API: [#4710](https://github.com/owncloud/android/issues/4710)
48+
49+
Some tests from view model classes have been refactored in order to use the new
50+
kotlinx-coroutines-test API.
51+
52+
https://github.com/owncloud/android/issues/4710
53+
https://github.com/owncloud/android/pull/4722
54+
3355
# Changelog for ownCloud Android Client [4.7.0] (2025-11-17)
3456

3557
The following sections list the changes in ownCloud Android Client 4.7.0 relevant to

changelog/unreleased/4722

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Change: Migrate tests to the new kotlinx-coroutines-test API
2+
3+
Some tests from view model classes have been refactored in order to use the new kotlinx-coroutines-test API.
4+
5+
https://github.com/owncloud/android/issues/4710
6+
https://github.com/owncloud/android/pull/4722

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ glideToVectorYou = "v2.0.0"
3333
junit4 = "4.13.2"
3434
koin = "3.3.3"
3535
kotlin = "1.9.20"
36-
kotlinxCoroutines = "1.6.4"
36+
kotlinxCoroutines = "1.9.0"
3737
ksp = "1.9.20-1.0.14"
3838
ktlint = "11.1.0"
3939
markwon = "4.6.2"

owncloudApp/src/test/java/com/owncloud/android/presentation/viewmodels/DrawerViewModelTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class DrawerViewModelTest : ViewModelTest() {
6969
getUserQuotasUseCase = mockk()
7070
localStorageProvider = mockk()
7171

72-
testCoroutineDispatcher.pauseDispatcher()
7372

7473
drawerViewModel = DrawerViewModel(
7574
getStoredQuotaAsStreamUseCase = getStoredQuotaAsStreamUseCase,

owncloudApp/src/test/java/com/owncloud/android/presentation/viewmodels/ViewModelTest.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import com.owncloud.android.testutil.livedata.getEmittedValues
2828
import io.mockk.unmockkAll
2929
import kotlinx.coroutines.Dispatchers
3030
import kotlinx.coroutines.ExperimentalCoroutinesApi
31-
import kotlinx.coroutines.test.TestCoroutineDispatcher
31+
import kotlinx.coroutines.test.StandardTestDispatcher
3232
import kotlinx.coroutines.test.resetMain
3333
import org.junit.After
3434
import org.junit.Assert.assertEquals
@@ -41,7 +41,7 @@ open class ViewModelTest {
4141
@JvmField
4242
val instantExecutorRule = InstantTaskExecutorRule()
4343

44-
val testCoroutineDispatcher = TestCoroutineDispatcher()
44+
val testCoroutineDispatcher = StandardTestDispatcher()
4545
val coroutineDispatcherProvider: CoroutinesDispatcherProvider = CoroutinesDispatcherProvider(
4646
io = testCoroutineDispatcher,
4747
main = testCoroutineDispatcher,
@@ -51,18 +51,14 @@ open class ViewModelTest {
5151
@After
5252
open fun tearDown() {
5353
Dispatchers.resetMain()
54-
testCoroutineDispatcher.cleanupTestCoroutines()
55-
5654
unmockkAll()
5755
}
5856

5957
fun <DomainModel> assertEmittedValues(
6058
expectedValues: List<Event<UIResult<DomainModel>>>,
6159
liveData: LiveData<Event<UIResult<DomainModel>>>
6260
) {
63-
val emittedValues = liveData.getEmittedValues(expectedValues.size) {
64-
testCoroutineDispatcher.resumeDispatcher()
65-
}
61+
val emittedValues = liveData.getEmittedValues(expectedValues.size)
6662
assertEquals(expectedValues, emittedValues)
6763
}
6864

owncloudApp/src/test/java/com/owncloud/android/presentation/viewmodels/authentication/AuthenticationViewModelTest.kt

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ import io.mockk.mockkConstructor
6262
import io.mockk.verify
6363
import kotlinx.coroutines.Dispatchers
6464
import kotlinx.coroutines.ExperimentalCoroutinesApi
65+
import kotlinx.coroutines.test.advanceUntilIdle
66+
import kotlinx.coroutines.test.runTest
6567
import kotlinx.coroutines.test.setMain
6668
import org.junit.After
6769
import org.junit.Before
@@ -129,7 +131,6 @@ class AuthenticationViewModelTest : ViewModelTest() {
129131
every { contextProvider.getBoolean(R.bool.enforce_secure_connection) } returns false
130132
every { contextProvider.getBoolean(R.bool.enforce_oidc) } returns false
131133

132-
testCoroutineDispatcher.pauseDispatcher()
133134

134135
authenticationViewModel = AuthenticationViewModel(
135136
loginBasicAsyncUseCase = loginBasicAsyncUseCase,
@@ -157,63 +158,55 @@ class AuthenticationViewModelTest : ViewModelTest() {
157158
}
158159

159160
@Test
160-
fun getServerInfoOk() {
161+
fun getServerInfoOk() = runTest {
161162
every { getServerInfoAsyncUseCase(any()) } returns UseCaseResult.Success(OC_SECURE_SERVER_INFO_BASIC_AUTH)
162-
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl)
163163

164+
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl)
165+
advanceUntilIdle()
164166
assertEmittedValues(
165-
expectedValues = listOf(
166-
Event(UIResult.Loading()),
167-
Event(UIResult.Success(OC_SECURE_SERVER_INFO_BASIC_AUTH))
168-
),
167+
expectedValues = listOf(Event(UIResult.Success(OC_SECURE_SERVER_INFO_BASIC_AUTH))),
169168
liveData = authenticationViewModel.serverInfo
170169
)
171170
}
172171

173172
@Test
174-
fun getServerInfoException() {
173+
fun getServerInfoException() = runTest {
175174
every { getServerInfoAsyncUseCase(any()) } returns UseCaseResult.Error(commonException)
176-
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl)
177175

176+
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BASIC_AUTH.baseUrl)
177+
advanceUntilIdle()
178178
assertEmittedValues(
179-
expectedValues = listOf(
180-
Event(UIResult.Loading()),
181-
Event(UIResult.Error(commonException))
182-
),
179+
expectedValues = listOf(Event(UIResult.Error(commonException))),
183180
liveData = authenticationViewModel.serverInfo
184181
)
185182
}
186183

187184
@Test
188-
fun loginBasicOk() {
185+
fun loginBasicOk() = runTest {
189186
every { loginBasicAsyncUseCase(any()) } returns UseCaseResult.Success(OC_BASIC_USERNAME)
190-
authenticationViewModel.loginBasic(OC_BASIC_USERNAME, OC_BASIC_PASSWORD, OC_ACCOUNT_NAME)
191187

188+
authenticationViewModel.loginBasic(OC_BASIC_USERNAME, OC_BASIC_PASSWORD, OC_ACCOUNT_NAME)
189+
advanceUntilIdle()
192190
assertEmittedValues(
193-
expectedValues = listOf(
194-
Event(UIResult.Loading()),
195-
Event(UIResult.Success(OC_BASIC_USERNAME))
196-
),
191+
expectedValues = listOf(Event(UIResult.Success(OC_BASIC_USERNAME))),
197192
liveData = authenticationViewModel.loginResult
198193
)
199194
}
200195

201196
@Test
202-
fun loginBasicException() {
197+
fun loginBasicException() = runTest {
203198
every { loginBasicAsyncUseCase(any()) } returns UseCaseResult.Error(commonException)
204-
authenticationViewModel.loginBasic(OC_BASIC_USERNAME, OC_BASIC_PASSWORD, null)
205199

200+
authenticationViewModel.loginBasic(OC_BASIC_USERNAME, OC_BASIC_PASSWORD, null)
201+
advanceUntilIdle()
206202
assertEmittedValues(
207-
expectedValues = listOf(
208-
Event(UIResult.Loading()),
209-
Event(UIResult.Error(commonException))
210-
),
203+
expectedValues = listOf(Event(UIResult.Error(commonException))),
211204
liveData = authenticationViewModel.loginResult
212205
)
213206
}
214207

215208
@Test
216-
fun loginOAuthWebFingerInstancesOk() {
209+
fun loginOAuthWebFingerInstancesOk() = runTest {
217210
every { getServerInfoAsyncUseCase(any()) } returns UseCaseResult.Success(OC_SECURE_SERVER_INFO_BEARER_AUTH)
218211
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl)
219212

@@ -229,12 +222,9 @@ class AuthenticationViewModelTest : ViewModelTest() {
229222
scope = OC_SCOPE,
230223
clientRegistrationInfo = OC_CLIENT_REGISTRATION
231224
)
232-
225+
advanceUntilIdle()
233226
assertEmittedValues(
234-
expectedValues = listOf(
235-
Event(UIResult.Loading()),
236-
Event(UIResult.Success(OC_BASIC_USERNAME))
237-
),
227+
expectedValues = listOf(Event(UIResult.Success(OC_BASIC_USERNAME))),
238228
liveData = authenticationViewModel.loginResult
239229
)
240230

@@ -255,7 +245,7 @@ class AuthenticationViewModelTest : ViewModelTest() {
255245
}
256246

257247
@Test
258-
fun loginOAuthOk() {
248+
fun loginOAuthOk() = runTest {
259249
every { getServerInfoAsyncUseCase(any()) } returns UseCaseResult.Success(OC_SECURE_SERVER_INFO_BEARER_AUTH)
260250
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl)
261251

@@ -271,12 +261,9 @@ class AuthenticationViewModelTest : ViewModelTest() {
271261
scope = OC_SCOPE,
272262
clientRegistrationInfo = OC_CLIENT_REGISTRATION
273263
)
274-
264+
advanceUntilIdle()
275265
assertEmittedValues(
276-
expectedValues = listOf(
277-
Event(UIResult.Loading()),
278-
Event(UIResult.Success(OC_BASIC_USERNAME))
279-
),
266+
expectedValues = listOf(Event(UIResult.Success(OC_BASIC_USERNAME))),
280267
liveData = authenticationViewModel.loginResult
281268
)
282269

@@ -297,7 +284,7 @@ class AuthenticationViewModelTest : ViewModelTest() {
297284
}
298285

299286
@Test
300-
fun loginOAuthException() {
287+
fun loginOAuthException() = runTest {
301288
every { getServerInfoAsyncUseCase(any()) } returns UseCaseResult.Success(OC_SECURE_SERVER_INFO_BEARER_AUTH)
302289
authenticationViewModel.getServerInfo(OC_SECURE_SERVER_INFO_BEARER_AUTH.baseUrl)
303290

@@ -313,54 +300,55 @@ class AuthenticationViewModelTest : ViewModelTest() {
313300
scope = OC_SCOPE,
314301
clientRegistrationInfo = OC_CLIENT_REGISTRATION
315302
)
316-
303+
advanceUntilIdle()
317304
assertEmittedValues(
318-
expectedValues = listOf(
319-
Event(UIResult.Loading()),
320-
Event(UIResult.Error(commonException))
321-
),
305+
expectedValues = listOf(Event(UIResult.Error(commonException))),
322306
liveData = authenticationViewModel.loginResult
323307
)
324308
}
325309

326310
@Test
327-
fun supportsOAuthOk() {
311+
fun supportsOAuthOk() = runTest {
328312
every { supportsOAuth2UseCase(any()) } returns UseCaseResult.Success(true)
329-
authenticationViewModel.supportsOAuth2(OC_BASIC_USERNAME)
330313

314+
authenticationViewModel.supportsOAuth2(OC_BASIC_USERNAME)
315+
advanceUntilIdle()
331316
assertEmittedValues(
332317
expectedValues = listOf<Event<UIResult<Boolean>>>(Event(UIResult.Success(true))),
333318
liveData = authenticationViewModel.supportsOAuth2
334319
)
335320
}
336321

337322
@Test
338-
fun supportsOAuthException() {
323+
fun supportsOAuthException() = runTest {
339324
every { supportsOAuth2UseCase(any()) } returns UseCaseResult.Error(commonException)
340-
authenticationViewModel.supportsOAuth2(OC_BASIC_USERNAME)
341325

326+
authenticationViewModel.supportsOAuth2(OC_BASIC_USERNAME)
327+
advanceUntilIdle()
342328
assertEmittedValues(
343329
expectedValues = listOf<Event<UIResult<Boolean>>>(Event(UIResult.Error(commonException))),
344330
liveData = authenticationViewModel.supportsOAuth2
345331
)
346332
}
347333

348334
@Test
349-
fun getBaseUrlOk() {
335+
fun getBaseUrlOk() = runTest {
350336
every { getBaseUrlUseCase(any()) } returns UseCaseResult.Success(OC_SECURE_BASE_URL)
351-
authenticationViewModel.getBaseUrl(OC_BASIC_USERNAME)
352337

338+
authenticationViewModel.getBaseUrl(OC_BASIC_USERNAME)
339+
advanceUntilIdle()
353340
assertEmittedValues(
354341
expectedValues = listOf<Event<UIResult<String>>>(Event(UIResult.Success(OC_SECURE_BASE_URL))),
355342
liveData = authenticationViewModel.baseUrl
356343
)
357344
}
358345

359346
@Test
360-
fun getBaseUrlException() {
347+
fun getBaseUrlException() = runTest {
361348
every { getBaseUrlUseCase(any()) } returns UseCaseResult.Error(commonException)
362-
authenticationViewModel.getBaseUrl(OC_BASIC_USERNAME)
363349

350+
authenticationViewModel.getBaseUrl(OC_BASIC_USERNAME)
351+
advanceUntilIdle()
364352
assertEmittedValues(
365353
expectedValues = listOf<Event<UIResult<String>>>(Event(UIResult.Error(commonException))),
366354
liveData = authenticationViewModel.baseUrl

0 commit comments

Comments
 (0)