From ac0e9fd9918d854ce7f7d68428c2c769b907042b Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Thu, 17 Aug 2023 15:58:47 +0200 Subject: [PATCH] Add ui tests for welcome screen --- .../compose/screen/WelcomeScreenTest.kt | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/WelcomeScreenTest.kt diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/WelcomeScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/WelcomeScreenTest.kt new file mode 100644 index 000000000000..051b16b6b1ea --- /dev/null +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/WelcomeScreenTest.kt @@ -0,0 +1,185 @@ +package net.mullvad.mullvadvpn.compose.screen + +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import io.mockk.MockKAnnotations +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.MutableStateFlow +import net.mullvad.mullvadvpn.compose.state.WelcomeUiState +import net.mullvad.mullvadvpn.viewmodel.WelcomeViewModel +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +class WelcomeScreenTest { + @get:Rule val composeTestRule = createComposeRule() + + @Before + fun setup() { + MockKAnnotations.init(this) + } + + @Test + fun testDefaultState() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableSharedFlow(), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Assert + composeTestRule.apply { + onNodeWithText("Congrats!").assertExists() + onNodeWithText("Here’s your account number. Save it!").assertExists() + } + } + + @Test + fun testDisableSitePayment() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = false, + uiState = WelcomeUiState(), + viewActions = MutableSharedFlow(), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Assert + composeTestRule.apply { + onNodeWithText( + "Either buy credit on our website or redeem a voucher.", + substring = true + ) + .assertDoesNotExist() + onNodeWithText("Buy credit").assertDoesNotExist() + } + } + + @Test + fun testShowAccountNumber() { + // Arrange + val rawAccountNumber = "1111222233334444" + val expectedAccountNumber = "1111 2222 3333 4444" + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(accountNumber = rawAccountNumber), + viewActions = MutableSharedFlow(), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Assert + composeTestRule.apply { onNodeWithText(expectedAccountNumber).assertExists() } + } + + @Test + fun testOpenAccountView() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenAccountView("222")), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Assert + composeTestRule.apply { onNodeWithText("Congrats!").assertDoesNotExist() } + } + + @Test + fun testOpenConnectScreen() { + // Arrange + val mockClickListener: () -> Unit = mockk(relaxed = true) + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = mockClickListener + ) + } + + // Assert + verify(exactly = 1) { mockClickListener.invoke() } + } + + @Test + fun testClickSitePaymentButton() { + // Arrange + val mockClickListener: () -> Unit = mockk(relaxed = true) + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen), + onSitePaymentClick = mockClickListener, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Act + composeTestRule.apply { onNodeWithText("Buy credit").performClick() } + + // Assert + verify(exactly = 1) { mockClickListener.invoke() } + } + + @Test + fun testClickRedeemVoucher() { + // Arrange + val mockClickListener: () -> Unit = mockk(relaxed = true) + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = mockClickListener, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {} + ) + } + + // Act + composeTestRule.apply { onNodeWithText("Redeem voucher").performClick() } + + // Assert + verify(exactly = 1) { mockClickListener.invoke() } + } +}