diff options
Diffstat (limited to 'android/app')
11 files changed, 238 insertions, 152 deletions
diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ConnectScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ConnectScreenTest.kt index 1ae19c534a..ea0d79e319 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ConnectScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/ConnectScreenTest.kt @@ -29,6 +29,7 @@ import net.mullvad.mullvadvpn.lib.model.TunnelState import net.mullvad.mullvadvpn.repository.InAppNotification import net.mullvad.mullvadvpn.ui.VersionInfo import org.joda.time.DateTime +import org.joda.time.Period import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -570,7 +571,8 @@ class ConnectScreenTest { showLocation = false, deviceName = "", daysLeftUntilExpiry = null, - inAppNotification = InAppNotification.AccountExpiry(expiryDate), + inAppNotification = + InAppNotification.AccountExpiry(Period(DateTime.now(), expiryDate)), isPlayBuild = false, ) ) @@ -630,7 +632,8 @@ class ConnectScreenTest { showLocation = false, deviceName = "", daysLeftUntilExpiry = null, - inAppNotification = InAppNotification.AccountExpiry(expiryDate), + inAppNotification = + InAppNotification.AccountExpiry(Period(DateTime.now(), expiryDate)), isPlayBuild = false, ), ) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/component/notificationbanner/NotificationBanner.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/component/notificationbanner/NotificationBanner.kt index ebed6ea462..d20d9cfcd9 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/component/notificationbanner/NotificationBanner.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/component/notificationbanner/NotificationBanner.kt @@ -36,7 +36,7 @@ import net.mullvad.mullvadvpn.lib.theme.color.warning import net.mullvad.mullvadvpn.repository.InAppNotification import net.mullvad.mullvadvpn.ui.VersionInfo import net.mullvad.mullvadvpn.ui.notification.StatusLevel -import org.joda.time.DateTime +import org.joda.time.Period @Preview @Composable @@ -48,7 +48,7 @@ private fun PreviewNotificationBanner() { InAppNotification.UnsupportedVersion( versionInfo = VersionInfo(currentVersion = "1.0", isSupported = false) ), - InAppNotification.AccountExpiry(expiry = DateTime.now()), + InAppNotification.AccountExpiry(expiry = Period.ZERO), InAppNotification.TunnelStateBlocked, InAppNotification.NewDevice("Courageous Turtle"), InAppNotification.TunnelStateError( diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/extensions/ResourcesExtensions.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/extensions/ResourcesExtensions.kt index 367c1b54af..a130a16f4f 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/extensions/ResourcesExtensions.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/extensions/ResourcesExtensions.kt @@ -2,39 +2,33 @@ package net.mullvad.mullvadvpn.compose.extensions import android.content.res.Resources import net.mullvad.mullvadvpn.R -import org.joda.time.DateTime -import org.joda.time.Duration -import org.joda.time.PeriodType +import org.joda.time.Period -fun Resources.getExpiryQuantityString(accountExpiry: DateTime): String { - val remainingTime = Duration(DateTime.now(), accountExpiry) - - return getExpiryQuantityString(this, accountExpiry, remainingTime) -} - -private fun getExpiryQuantityString( - resources: Resources, - accountExpiry: DateTime, - remainingTime: Duration, -): String { - if (remainingTime.isShorterThan(Duration.ZERO)) { - return resources.getString(R.string.out_of_time) +fun Resources.getExpiryQuantityString(accountExpiry: Period): String { + return if (accountExpiry.isNegative() || accountExpiry == Period.ZERO) { + getString(R.string.out_of_time) + } else if (accountExpiry.years > 0) { + getRemainingText(this, R.plurals.years_left, accountExpiry.years) + } else if (accountExpiry.months >= 3) { + getRemainingText(this, R.plurals.months_left, accountExpiry.months) + } else if (accountExpiry.months > 0 || accountExpiry.days >= 1) { + getRemainingText(this, R.plurals.days_left, accountExpiry.days) } else { - val remainingTimeInfo = - remainingTime.toPeriodTo(accountExpiry, PeriodType.yearMonthDayTime()) - - return if (remainingTimeInfo.years > 0) { - getRemainingText(resources, R.plurals.years_left, remainingTimeInfo.years) - } else if (remainingTimeInfo.months >= 3) { - getRemainingText(resources, R.plurals.months_left, remainingTimeInfo.months) - } else if (remainingTimeInfo.months > 0 || remainingTimeInfo.days >= 1) { - getRemainingText(resources, R.plurals.days_left, remainingTime.standardDays.toInt()) - } else { - resources.getString(R.string.less_than_a_day_left) - } + getString(R.string.less_than_a_day_left) } } private fun getRemainingText(resources: Resources, pluralId: Int, quantity: Int): String { return resources.getQuantityString(pluralId, quantity, quantity) } + +fun Period.isNegative() = + normalizedStandard().let { + it.years < 0 || + it.months < 0 || + it.weeks < 0 || + it.days < 0 || + it.minutes < 0 || + it.seconds < 0 || + it.millis < 0 + } 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 48dada2d61..89c4e1e68d 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 @@ -30,7 +30,7 @@ import net.mullvad.mullvadvpn.repository.SplitTunnelingRepository import net.mullvad.mullvadvpn.ui.MainActivity import net.mullvad.mullvadvpn.ui.serviceconnection.AppVersionInfoRepository import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager -import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase +import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase import net.mullvad.mullvadvpn.usecase.AvailableProvidersUseCase import net.mullvad.mullvadvpn.usecase.EmptyPaymentUseCase import net.mullvad.mullvadvpn.usecase.FilteredRelayListUseCase @@ -141,7 +141,7 @@ val uiModule = module { ) } - single { AccountExpiryNotificationUseCase(get()) } + single { AccountExpiryInAppNotificationUseCase(get()) } single { TunnelStateNotificationUseCase(get()) } single { VersionNotificationUseCase(get(), BuildConfig.ENABLE_IN_APP_VERSION_NOTIFICATIONS) } single { NewDeviceNotificationUseCase(get(), get()) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/InAppNotificationController.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/InAppNotificationController.kt index 821fe769c2..45cfdd2bb1 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/InAppNotificationController.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/InAppNotificationController.kt @@ -7,11 +7,11 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.stateIn import net.mullvad.mullvadvpn.lib.model.ErrorState import net.mullvad.mullvadvpn.ui.VersionInfo -import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase +import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase import net.mullvad.mullvadvpn.usecase.TunnelStateNotificationUseCase import net.mullvad.mullvadvpn.usecase.VersionNotificationUseCase -import org.joda.time.DateTime +import org.joda.time.Period enum class StatusLevel { Error, @@ -38,7 +38,7 @@ sealed class InAppNotification { override val priority: Long = 999 } - data class AccountExpiry(val expiry: DateTime) : InAppNotification() { + data class AccountExpiry(val expiry: Period) : InAppNotification() { override val statusLevel = StatusLevel.Warning override val priority: Long = 1001 } @@ -50,7 +50,7 @@ sealed class InAppNotification { } class InAppNotificationController( - accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase, + accountExpiryInAppNotificationUseCase: AccountExpiryInAppNotificationUseCase, newDeviceNotificationUseCase: NewDeviceNotificationUseCase, versionNotificationUseCase: VersionNotificationUseCase, tunnelStateNotificationUseCase: TunnelStateNotificationUseCase, @@ -61,7 +61,7 @@ class InAppNotificationController( combine( tunnelStateNotificationUseCase(), versionNotificationUseCase(), - accountExpiryNotificationUseCase(), + accountExpiryInAppNotificationUseCase(), newDeviceNotificationUseCase(), ) { a, b, c, d -> a + b + c + d diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt new file mode 100644 index 0000000000..a216847355 --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCase.kt @@ -0,0 +1,30 @@ +package net.mullvad.mullvadvpn.usecase + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map +import net.mullvad.mullvadvpn.lib.shared.AccountRepository +import net.mullvad.mullvadvpn.repository.InAppNotification +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.expiryTickerFlow + +class AccountExpiryInAppNotificationUseCase(private val accountRepository: AccountRepository) { + + operator fun invoke(): Flow<List<InAppNotification>> = + accountRepository.accountData + .flatMapLatest { accountData -> + if (accountData != null) { + expiryTickerFlow( + expiry = accountData.expiryDate, + tickStart = ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD, + updateInterval = { ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL }, + ) + .map { expiresInPeriod -> InAppNotification.AccountExpiry(expiresInPeriod) } + } else { + flowOf<InAppNotification?>(null) + } + } + .map(::listOfNotNull) +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCase.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCase.kt deleted file mode 100644 index d3490692f0..0000000000 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCase.kt +++ /dev/null @@ -1,29 +0,0 @@ -package net.mullvad.mullvadvpn.usecase - -import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.map -import net.mullvad.mullvadvpn.lib.model.AccountData -import net.mullvad.mullvadvpn.lib.shared.AccountRepository -import net.mullvad.mullvadvpn.repository.InAppNotification -import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS -import org.joda.time.DateTime - -class AccountExpiryNotificationUseCase(private val accountRepository: AccountRepository) { - operator fun invoke(): Flow<List<InAppNotification>> = - accountRepository.accountData - .map(::accountExpiryNotification) - .map(::listOfNotNull) - .distinctUntilChanged() - - private fun accountExpiryNotification(accountData: AccountData?) = - if (accountData != null && accountData.expiryDate.isCloseToExpiring()) { - InAppNotification.AccountExpiry(accountData.expiryDate) - } else null - - private fun DateTime.isCloseToExpiring(): Boolean { - val threeDaysFromNow = - DateTime.now().plusDays(ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD_DAYS) - return isBefore(threeDaysFromNow) - } -} diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/InAppNotificationControllerTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/InAppNotificationControllerTest.kt index 84ec047f06..07c6c2ed13 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/InAppNotificationControllerTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/InAppNotificationControllerTest.kt @@ -16,11 +16,11 @@ import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule import net.mullvad.mullvadvpn.lib.model.ErrorState import net.mullvad.mullvadvpn.repository.InAppNotification import net.mullvad.mullvadvpn.repository.InAppNotificationController -import net.mullvad.mullvadvpn.usecase.AccountExpiryNotificationUseCase +import net.mullvad.mullvadvpn.usecase.AccountExpiryInAppNotificationUseCase import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase import net.mullvad.mullvadvpn.usecase.TunnelStateNotificationUseCase import net.mullvad.mullvadvpn.usecase.VersionNotificationUseCase -import org.joda.time.DateTime +import org.joda.time.Period import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -42,11 +42,11 @@ class InAppNotificationControllerTest { fun setup() { MockKAnnotations.init(this) - val accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase = mockk() + val accountExpiryInAppNotificationUseCase: AccountExpiryInAppNotificationUseCase = mockk() val newDeviceNotificationUseCase: NewDeviceNotificationUseCase = mockk() val versionNotificationUseCase: VersionNotificationUseCase = mockk() val tunnelStateNotificationUseCase: TunnelStateNotificationUseCase = mockk() - every { accountExpiryNotificationUseCase.invoke() } returns accountExpiryNotifications + every { accountExpiryInAppNotificationUseCase.invoke() } returns accountExpiryNotifications every { newDeviceNotificationUseCase.invoke() } returns newDeviceNotifications every { versionNotificationUseCase.invoke() } returns versionNotifications every { tunnelStateNotificationUseCase.invoke() } returns tunnelStateNotifications @@ -54,7 +54,7 @@ class InAppNotificationControllerTest { inAppNotificationController = InAppNotificationController( - accountExpiryNotificationUseCase, + accountExpiryInAppNotificationUseCase, newDeviceNotificationUseCase, versionNotificationUseCase, tunnelStateNotificationUseCase, @@ -81,7 +81,7 @@ class InAppNotificationControllerTest { val unsupportedVersion = InAppNotification.UnsupportedVersion(mockk()) versionNotifications.value = listOf(unsupportedVersion) - val accountExpiry = InAppNotification.AccountExpiry(DateTime.now()) + val accountExpiry = InAppNotification.AccountExpiry(Period.ZERO) accountExpiryNotifications.value = listOf(accountExpiry) inAppNotificationController.notifications.test { diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCaseTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCaseTest.kt new file mode 100644 index 0000000000..826c8c4962 --- /dev/null +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryInAppNotificationUseCaseTest.kt @@ -0,0 +1,163 @@ +package net.mullvad.mullvadvpn.usecase + +import app.cash.turbine.test +import io.mockk.MockKAnnotations +import io.mockk.every +import io.mockk.mockk +import io.mockk.unmockkAll +import kotlin.math.roundToInt +import kotlin.test.assertTrue +import kotlin.time.Duration.Companion.seconds +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.test.advanceTimeBy +import kotlinx.coroutines.test.runTest +import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule +import net.mullvad.mullvadvpn.lib.model.AccountData +import net.mullvad.mullvadvpn.lib.shared.AccountRepository +import net.mullvad.mullvadvpn.repository.InAppNotification +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD +import net.mullvad.mullvadvpn.service.notifications.accountexpiry.ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL +import org.joda.time.DateTime +import org.joda.time.Duration +import org.joda.time.Period +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith + +@ExtendWith(TestCoroutineRule::class) +class AccountExpiryInAppNotificationUseCaseTest { + + private val accountExpiry = MutableStateFlow<AccountData?>(null) + private lateinit var accountExpiryInAppNotificationUseCase: + AccountExpiryInAppNotificationUseCase + + private lateinit var notificationThreshold: DateTime + + @BeforeEach + fun setup() { + MockKAnnotations.init(this) + + val accountRepository = mockk<AccountRepository>() + every { accountRepository.accountData } returns accountExpiry + + accountExpiryInAppNotificationUseCase = + AccountExpiryInAppNotificationUseCase(accountRepository) + + notificationThreshold = DateTime.now().plus(ACCOUNT_EXPIRY_CLOSE_TO_EXPIRY_THRESHOLD) + } + + @AfterEach + fun teardown() { + unmockkAll() + } + + @Test + fun `initial state should be empty`() = runTest { + // Arrange, Act, Assert + accountExpiryInAppNotificationUseCase().test { assertTrue { awaitItem().isEmpty() } } + } + + @Test + fun `account that expires within the threshold should emit a notification`() = runTest { + // Arrange, Act, Assert + accountExpiryInAppNotificationUseCase().test { + assertTrue { awaitItem().isEmpty() } + val expiry = setExpiry(notificationThreshold.minusHours(1)) + assertExpiryNotificationAndPeriod(expiry, expectMostRecentItem()) + expectNoEvents() + } + } + + @Test + fun `account that expires after the threshold should not emit a notification`() = runTest { + // Arrange, Act, Assert + accountExpiryInAppNotificationUseCase().test { + assertTrue { awaitItem().isEmpty() } + setExpiry(notificationThreshold.plusDays(1)) + expectNoEvents() + } + } + + @Test + fun `should emit when the threshold is passed`() = runTest { + // Arrange, Act, Assert + accountExpiryInAppNotificationUseCase().test { + assertTrue { awaitItem().isEmpty() } + val expiry = setExpiry(notificationThreshold.plusMinutes(1)) + expectNoEvents() + + // Advance to before threshold + advanceTimeBy(59.seconds) + expectNoEvents() + + // Advance to after threshold + advanceTimeBy(2.seconds) + assertExpiryNotificationAndPeriod(expiry, expectMostRecentItem()) + expectNoEvents() + } + } + + @Test + fun `should emit remaining time divided by update interval time times`() = runTest { + // Arrange, Act, Assert + accountExpiryInAppNotificationUseCase().test { + assertTrue { awaitItem().isEmpty() } + + // Set expiry to to be 1 second before the end of the first update interval + val beforeUpdate = + notificationThreshold + .minus(ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL) + .plusSeconds(1) + val expiry = setExpiry(beforeUpdate) + + // The expiry time is within the notification threshold so we should have an item + // immediately. + assertExpiryNotificationAndPeriod(expiry, expectMostRecentItem()) + expectNoEvents() + + val expectedEmits = + (Duration(DateTime.now(), beforeUpdate).millis.toDouble() / + ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL.millis) + .roundToInt() + + advanceTimeBy(2.seconds) + repeat(expectedEmits) { + expectMostRecentItem() + advanceTimeBy( + ACCOUNT_EXPIRY_IN_APP_NOTIFICATION_UPDATE_INTERVAL.plus( + Duration.standardSeconds(1) + ) + .millis + ) + } + + expectNoEvents() + } + } + + private fun setExpiry(expiryDateTime: DateTime): DateTime { + val expiry = AccountData(mockk(relaxed = true), expiryDateTime) + accountExpiry.value = expiry + return expiryDateTime + } + + // Assert that we go a single AccountExpiry notification and that the period is within + // the expected range (checking exact period values is not possible since we use DateTime.now) + private fun assertExpiryNotificationAndPeriod( + expiry: DateTime, + notifications: List<InAppNotification>, + ) { + assertTrue(notifications.size == 1, "Expected a single notification") + val n = notifications[0] + if (n !is InAppNotification.AccountExpiry) { + error("Expected an AccountExpiry notification") + } + val periodNow = Period(DateTime.now(), expiry) + assertTrue(periodNow.toStandardDuration() <= n.expiry.toStandardDuration()) + assertTrue( + periodNow.toStandardDuration().plus(Duration.standardSeconds(5)) > + n.expiry.toStandardDuration() + ) + } +} diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCaseTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCaseTest.kt deleted file mode 100644 index f8a0e52a3e..0000000000 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/AccountExpiryNotificationUseCaseTest.kt +++ /dev/null @@ -1,73 +0,0 @@ -package net.mullvad.mullvadvpn.usecase - -import app.cash.turbine.test -import io.mockk.MockKAnnotations -import io.mockk.every -import io.mockk.mockk -import io.mockk.unmockkAll -import kotlin.test.assertEquals -import kotlin.test.assertTrue -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.test.runTest -import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule -import net.mullvad.mullvadvpn.lib.model.AccountData -import net.mullvad.mullvadvpn.lib.shared.AccountRepository -import net.mullvad.mullvadvpn.repository.InAppNotification -import org.joda.time.DateTime -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtendWith - -@ExtendWith(TestCoroutineRule::class) -class AccountExpiryNotificationUseCaseTest { - - private val accountExpiry = MutableStateFlow<AccountData?>(null) - private lateinit var accountExpiryNotificationUseCase: AccountExpiryNotificationUseCase - - @BeforeEach - fun setup() { - MockKAnnotations.init(this) - - val accountRepository = mockk<AccountRepository>() - every { accountRepository.accountData } returns accountExpiry - - accountExpiryNotificationUseCase = AccountExpiryNotificationUseCase(accountRepository) - } - - @AfterEach - fun teardown() { - unmockkAll() - } - - @Test - fun `initial state should be empty`() = runTest { - // Arrange, Act, Assert - accountExpiryNotificationUseCase().test { assertTrue { awaitItem().isEmpty() } } - } - - @Test - fun `account that expires within 3 days should emit a notification`() = runTest { - // Arrange, Act, Assert - accountExpiryNotificationUseCase().test { - assertTrue { awaitItem().isEmpty() } - val closeToExpiry = AccountData(mockk(relaxed = true), DateTime.now().plusDays(2)) - accountExpiry.value = closeToExpiry - - assertEquals( - listOf(InAppNotification.AccountExpiry(closeToExpiry.expiryDate)), - awaitItem(), - ) - } - } - - @Test - fun `account that expires in 4 days should not emit a notification`() = runTest { - // Arrange, Act, Assert - accountExpiryNotificationUseCase().test { - assertTrue { awaitItem().isEmpty() } - accountExpiry.value = AccountData(mockk(relaxed = true), DateTime.now().plusDays(4)) - expectNoEvents() - } - } -} diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt index d8438cafe0..e12c2a1d88 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/OutOfTimeUseCaseTest.kt @@ -185,14 +185,12 @@ class OutOfTimeUseCaseTest { advanceTimeBy(90.seconds) expectNoEvents() - // User fills up with more time 10 seconds before expiry + // User fills up with more time 10 seconds before expiry. expiry.emit(updatedExpiry) - advanceTimeBy(1.days) + advanceTimeBy(29.days) expectNoEvents() - // Expect no more emissions while user has time. - advanceTimeBy(29.days + 2.minutes) - println(testScheduler.currentTime) + advanceTimeBy(2.days) assertEquals(true, expectMostRecentItem()) expectNoEvents() } |
