Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Oct 11, 2023
1 parent 49f5357 commit 40d98d2
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationBlocked
),
Expand Down Expand Up @@ -118,6 +120,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationBlocked
),
Expand Down Expand Up @@ -151,6 +155,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow()
Expand Down Expand Up @@ -182,6 +188,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow()
Expand Down Expand Up @@ -214,6 +222,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = true,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow()
Expand Down Expand Up @@ -246,6 +256,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = true,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow()
Expand Down Expand Up @@ -280,6 +292,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = true,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationError(
ErrorState(ErrorStateCause.StartTunnelError, true)
Expand Down Expand Up @@ -318,6 +332,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = true,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationError(
ErrorState(ErrorStateCause.StartTunnelError, false)
Expand Down Expand Up @@ -353,6 +369,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationBlocked
),
Expand Down Expand Up @@ -388,6 +406,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = true,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowTunnelStateNotificationBlocked
),
Expand Down Expand Up @@ -423,6 +443,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -454,6 +476,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -485,6 +509,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -515,6 +541,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -545,6 +573,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -576,6 +606,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow(),
Expand Down Expand Up @@ -614,6 +646,8 @@ class ConnectScreenTest {
outAddress = mockOutAddress,
showLocation = false,
isTunnelInfoExpanded = true,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState = ConnectNotificationState.HideNotification
),
uiSideEffect = MutableSharedFlow<ConnectViewModel.UiSideEffect>().asSharedFlow()
Expand Down Expand Up @@ -651,6 +685,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowVersionInfoNotification(versionInfo)
),
Expand Down Expand Up @@ -687,6 +723,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowVersionInfoNotification(versionInfo)
),
Expand Down Expand Up @@ -720,6 +758,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = null,
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowAccountExpiryNotification(expiryDate)
),
Expand Down Expand Up @@ -758,6 +798,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowVersionInfoNotification(versionInfo)
),
Expand Down Expand Up @@ -790,6 +832,8 @@ class ConnectScreenTest {
outAddress = "",
showLocation = false,
isTunnelInfoExpanded = false,
deviceName = "",
daysLeftUntilExpiry = null,
connectNotificationState =
ConnectNotificationState.ShowAccountExpiryNotification(expiryDate)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = false,
uiState = OutOfTimeUiState(),
uiState = OutOfTimeUiState(deviceName = ""),
uiSideEffect = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
Expand Down Expand Up @@ -57,7 +57,7 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(),
uiState = OutOfTimeUiState(deviceName = ""),
uiSideEffect =
MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenAccountView("222")),
onSitePaymentClick = {},
Expand All @@ -80,7 +80,7 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(),
uiState = OutOfTimeUiState(deviceName = ""),
uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
Expand All @@ -102,7 +102,7 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(),
uiState = OutOfTimeUiState(deviceName = ""),
uiSideEffect = MutableSharedFlow(),
onSitePaymentClick = mockClickListener,
onRedeemVoucherClick = {},
Expand All @@ -127,7 +127,7 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(),
uiState = OutOfTimeUiState(deviceName = ""),
uiSideEffect = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = mockClickListener,
Expand All @@ -152,7 +152,11 @@ class OutOfTimeScreenTest {
composeTestRule.setContent {
OutOfTimeScreen(
showSitePayment = true,
uiState = OutOfTimeUiState(tunnelState = TunnelState.Connecting(null, null)),
uiState =
OutOfTimeUiState(
tunnelState = TunnelState.Connecting(null, null),
deviceName = ""
),
uiSideEffect = MutableSharedFlow(),
onSitePaymentClick = {},
onRedeemVoucherClick = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fun MullvadMediumTopBar(

@Preview
@Composable
fun PreviewMullvadTopBarWithLongDeviceName() {
private fun PreviewMullvadTopBarWithLongDeviceName() {
AppTheme {
Surface {
MullvadTopBarWithDeviceName(
Expand All @@ -237,7 +237,7 @@ fun PreviewMullvadTopBarWithLongDeviceName() {

@Preview
@Composable
fun PreviewMullvadTopBarWithShortDeviceName() {
private fun PreviewMullvadTopBarWithShortDeviceName() {
AppTheme {
Surface {
MullvadTopBarWithDeviceName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import net.mullvad.mullvadvpn.compose.state.ConnectNotificationState
import net.mullvad.mullvadvpn.compose.state.ConnectUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.AccountExpiry
import net.mullvad.mullvadvpn.model.DeviceState
import net.mullvad.mullvadvpn.model.GeoIpLocation
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.relaylist.RelayCountry
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.DeviceRepository
import net.mullvad.mullvadvpn.ui.VersionInfo
import net.mullvad.mullvadvpn.ui.serviceconnection.AppVersionInfoCache
import net.mullvad.mullvadvpn.ui.serviceconnection.AuthTokenCache
Expand Down Expand Up @@ -65,6 +67,7 @@ class ConnectViewModelTest {
)
)
private val accountExpiryState = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing)
private val deviceState = MutableStateFlow<DeviceState>(DeviceState.Initial)

// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
Expand All @@ -77,6 +80,9 @@ class ConnectViewModelTest {
// Account Repository
private val mockAccountRepository: AccountRepository = mockk()

// Device Repository
private val mockDeviceRepository: DeviceRepository = mockk()

// Captures
private val locationSlot = slot<((GeoIpLocation?) -> Unit)>()
private val relaySlot = slot<(List<RelayCountry>, RelayItem?) -> Unit>()
Expand All @@ -103,6 +109,8 @@ class ConnectViewModelTest {

every { mockAccountRepository.accountExpiryState } returns accountExpiryState

every { mockDeviceRepository.deviceState } returns deviceState

every { mockConnectionProxy.onUiStateChange } returns eventNotifierTunnelUiState
every { mockConnectionProxy.onStateChange } returns eventNotifierTunnelRealState

Expand All @@ -117,6 +125,7 @@ class ConnectViewModelTest {
ConnectViewModel(
serviceConnectionManager = mockServiceConnectionManager,
accountRepository = mockAccountRepository,
deviceRepository = mockDeviceRepository,
isVersionInfoNotificationEnabled = true
)
}
Expand Down Expand Up @@ -351,6 +360,7 @@ class ConnectViewModelTest {
val expectedConnectNotificationState =
ConnectNotificationState.ShowAccountExpiryNotification(mockDateTime)
every { mockDateTime.isBefore(any<ReadableInstant>()) } returns true
every { mockDateTime.toInstant().millis } returns 0

// Act, Assert
viewModel.uiState.test {
Expand All @@ -360,6 +370,7 @@ class ConnectViewModelTest {
locationSlot.captured.invoke(mockLocation)
relaySlot.captured.invoke(mockk(), mockk())
accountExpiryState.value = AccountExpiry.Available(mockDateTime)

val result = awaitItem()
assertEquals(expectedConnectNotificationState, result.connectNotificationState)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.state.OutOfTimeUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.model.AccountExpiry
import net.mullvad.mullvadvpn.model.DeviceState
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.repository.AccountRepository
import net.mullvad.mullvadvpn.repository.DeviceRepository
import net.mullvad.mullvadvpn.ui.serviceconnection.AuthTokenCache
import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer
Expand All @@ -39,6 +41,7 @@ class OutOfTimeViewModelTest {
private val serviceConnectionState =
MutableStateFlow<ServiceConnectionState>(ServiceConnectionState.Disconnected)
private val accountExpiryState = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing)
private val deviceState = MutableStateFlow<DeviceState>(DeviceState.Initial)

// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
Expand All @@ -48,6 +51,7 @@ class OutOfTimeViewModelTest {
private val eventNotifierTunnelRealState = EventNotifier<TunnelState>(TunnelState.Disconnected)

private val mockAccountRepository: AccountRepository = mockk()
private val mockDeviceRepository: DeviceRepository = mockk()
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()

private lateinit var viewModel: OutOfTimeViewModel
Expand All @@ -64,10 +68,13 @@ class OutOfTimeViewModelTest {

every { mockAccountRepository.accountExpiryState } returns accountExpiryState

every { mockDeviceRepository.deviceState } returns deviceState

viewModel =
OutOfTimeViewModel(
accountRepository = mockAccountRepository,
serviceConnectionManager = mockServiceConnectionManager,
deviceRepository = mockDeviceRepository,
pollAccountExpiry = false
)
}
Expand Down Expand Up @@ -104,7 +111,7 @@ class OutOfTimeViewModelTest {

// Act, Assert
viewModel.uiState.test {
assertEquals(OutOfTimeUiState(), awaitItem())
assertEquals(OutOfTimeUiState(deviceName = ""), awaitItem())
serviceConnectionState.value =
ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer)
eventNotifierTunnelRealState.notify(tunnelRealStateTestItem)
Expand Down

0 comments on commit 40d98d2

Please sign in to comment.