From 75ea1dfb95d061cd3f909c8f15616b9d2606ed66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 23 Dec 2024 18:25:39 +0100 Subject: [PATCH] Fix test --- .../mullvadvpn/compose/screen/ChangelogScreen.kt | 5 ++--- .../main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt | 2 +- .../mullvadvpn/repository/ChangelogRepository.kt | 10 ++++++++-- .../mullvadvpn/repository/ChangelogRepositoryTest.kt | 11 ++++++++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ChangelogScreen.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ChangelogScreen.kt index 73b301d6c06b..c56f9fc28f51 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ChangelogScreen.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/ChangelogScreen.kt @@ -93,10 +93,9 @@ fun ChangelogScreen( .fillMaxWidth() .drawVerticalScrollbar( scrollState, - MaterialTheme.colorScheme.onSurface.copy(alpha = AlphaScrollbar) + MaterialTheme.colorScheme.onSurface.copy(alpha = AlphaScrollbar), ) - .verticalScroll(scrollState) - , + .verticalScroll(scrollState), verticalArrangement = Arrangement.spacedBy(Dimens.mediumPadding), ) { Text( diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt index 49ac1ab70a15..3a0fe945602a 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/di/UiModule.kt @@ -127,7 +127,7 @@ val uiModule = module { single { androidContext().assets } single { androidContext().contentResolver } - single { ChangelogRepository(get(), get(), get(), MainScope()) } + single { ChangelogRepository(get(), get(), get()) } single { UserPreferencesRepository(get(), get()) } single { SettingsRepository(get()) } single { MullvadProblemReport(get()) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepository.kt index 0b27e0cda7bc..0e392e85cf8d 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepository.kt @@ -1,6 +1,8 @@ package net.mullvad.mullvadvpn.repository +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.map @@ -16,7 +18,7 @@ class ChangelogRepository( private val dataProvider: IChangelogDataProvider, private val userPreferencesRepository: UserPreferencesRepository, private val buildVersion: BuildVersion, - scope: CoroutineScope, + dispatcher: CoroutineDispatcher = Dispatchers.IO, ) { val hasUnreadChangelog: StateFlow = userPreferencesRepository.preferencesFlow @@ -24,7 +26,11 @@ class ChangelogRepository( getLastVersionChanges().isNotEmpty() && buildVersion.code > it.versionCodeForLatestShownChangelogNotification } - .stateIn(scope, started = SharingStarted.Eagerly, initialValue = false) + .stateIn( + CoroutineScope(dispatcher), + started = SharingStarted.Eagerly, + initialValue = false, + ) suspend fun setDismissNewChangelogNotification() { userPreferencesRepository.setHasDisplayedChangelogNotification() diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepositoryTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepositoryTest.kt index 1524549e5741..4d608b7231ec 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepositoryTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/repository/ChangelogRepositoryTest.kt @@ -2,15 +2,24 @@ package net.mullvad.mullvadvpn.repository import io.mockk.every import io.mockk.mockk +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.UnconfinedTestDispatcher import net.mullvad.mullvadvpn.lib.common.test.assertLists import net.mullvad.mullvadvpn.util.IChangelogDataProvider import org.junit.jupiter.api.Test +@ExperimentalCoroutinesApi class ChangelogRepositoryTest { private val mockDataProvider: IChangelogDataProvider = mockk() - private val changelogRepository = ChangelogRepository(dataProvider = mockDataProvider) + private val changelogRepository = + ChangelogRepository( + mockDataProvider, + mockk(relaxed = true), + mockk(), + UnconfinedTestDispatcher(), + ) @Test fun `when given a changelog text should return a list of correctly formatted strings`() {