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..0511bc51d408 --- /dev/null +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/WelcomeScreenTest.kt @@ -0,0 +1,177 @@ +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 = {}, + 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 = {}, + 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 mockAccountNumber = "1111 2222 3333 4444" + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(accountNumber = mockAccountNumber), + viewActions = MutableSharedFlow(), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + openConnectScreen = {} + ) + } + + // Assert + composeTestRule.apply { onNodeWithText(mockAccountNumber).assertExists() } + } + + @Test + fun testOpenAccountView() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(), + viewActions = MutableStateFlow(WelcomeViewModel.ViewAction.OpenAccountView("222")), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + 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 = {}, + 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 = {}, + 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 = {}, + openConnectScreen = {} + ) + } + + // Act + composeTestRule.apply { onNodeWithText("Redeem voucher").performClick() } + + // Assert + verify(exactly = 1) { mockClickListener.invoke() } + } +}