From cfab2198d0d340a5dd90ec2025321e7549a93339 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Wed, 20 Sep 2023 09:08:44 +0200 Subject: [PATCH] Add ui tests for account screen --- .../compose/screen/AccountScreenTest.kt | 105 +++++++++++++++++- 1 file changed, 100 insertions(+), 5 deletions(-) diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/AccountScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/AccountScreenTest.kt index 5d3e229a8ba6..1a8cc21f445c 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/AccountScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/AccountScreenTest.kt @@ -5,16 +5,21 @@ 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.every import io.mockk.mockk import io.mockk.verify import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.asSharedFlow +import net.mullvad.mullvadvpn.compose.state.AccountDialogState import net.mullvad.mullvadvpn.compose.state.AccountUiState +import net.mullvad.mullvadvpn.compose.state.PaymentState +import net.mullvad.mullvadvpn.lib.payment.PaymentProduct import net.mullvad.mullvadvpn.viewmodel.AccountViewModel import org.junit.Before import org.junit.Rule import org.junit.Test +@OptIn(ExperimentalMaterial3Api::class) class AccountScreenTest { @get:Rule val composeTestRule = createComposeRule() @@ -23,7 +28,6 @@ class AccountScreenTest { MockKAnnotations.init(this) } - @OptIn(ExperimentalMaterial3Api::class) @Test fun testDefaultState() { // Arrange @@ -47,7 +51,6 @@ class AccountScreenTest { } } - @OptIn(ExperimentalMaterial3Api::class) @Test fun testManageAccountClick() { // Arrange @@ -58,7 +61,8 @@ class AccountScreenTest { AccountUiState( deviceName = DUMMY_DEVICE_NAME, accountNumber = DUMMY_ACCOUNT_NUMBER, - accountExpiry = null + accountExpiry = null, + webPaymentAvailable = true ), viewActions = MutableSharedFlow().asSharedFlow(), enterTransitionEndAction = MutableSharedFlow().asSharedFlow(), @@ -73,7 +77,6 @@ class AccountScreenTest { verify { mockedClickHandler.invoke() } } - @OptIn(ExperimentalMaterial3Api::class) @Test fun testRedeemVoucherClick() { // Arrange @@ -99,7 +102,6 @@ class AccountScreenTest { verify { mockedClickHandler.invoke() } } - @OptIn(ExperimentalMaterial3Api::class) @Test fun testLogoutClick() { // Arrange @@ -125,6 +127,99 @@ class AccountScreenTest { verify { mockedClickHandler.invoke() } } + @Test + fun testShowPurchaseCompleteDialog() { + // Arrange + composeTestRule.setContent { + AccountScreen( + uiState = AccountUiState(dialogState = AccountDialogState.PurchaseComplete), + viewActions = MutableSharedFlow().asSharedFlow(), + enterTransitionEndAction = MutableSharedFlow().asSharedFlow() + ) + } + + // Assert + composeTestRule.onNodeWithText("Time was successfully added").assertExists() + } + + @Test + fun testShowVerificationErrorDialog() { + // Arrange + composeTestRule.setContent { + AccountScreen( + uiState = AccountUiState(dialogState = AccountDialogState.VerificationError), + viewActions = MutableSharedFlow().asSharedFlow(), + enterTransitionEndAction = MutableSharedFlow().asSharedFlow() + ) + } + + // Assert + composeTestRule.onNodeWithText("Payment was unsuccessful").assertExists() + } + + @Test + fun testShowBillingErrorDialog() { + // Arrange + composeTestRule.setContent { + AccountScreen( + uiState = AccountUiState(dialogState = AccountDialogState.BillingError), + viewActions = MutableSharedFlow().asSharedFlow(), + enterTransitionEndAction = MutableSharedFlow().asSharedFlow() + ) + } + + // Assert + composeTestRule.onNodeWithText("Google Play services not available").assertExists() + } + + @Test + fun testShowBillingPaymentAvailable() { + // Arrange + val mockPaymentProduct: PaymentProduct = mockk() + every { mockPaymentProduct.price } returns "$10" + composeTestRule.setContent { + AccountScreen( + uiState = + AccountUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + viewActions = MutableSharedFlow().asSharedFlow(), + enterTransitionEndAction = MutableSharedFlow().asSharedFlow() + ) + } + + // Assert + composeTestRule.onNodeWithText("Add 30 days time ($10)").assertExists() + } + + @Test + fun testOnPurchaseBillingProductClick() { + // Arrange + val clickHandler: (String) -> Unit = mockk(relaxed = true) + val mockPaymentProduct: PaymentProduct = mockk() + every { mockPaymentProduct.price } returns "$10" + every { mockPaymentProduct.productId } returns "PRODUCT_ID" + composeTestRule.setContent { + AccountScreen( + uiState = + AccountUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + onPurchaseBillingProductClick = clickHandler, + viewActions = MutableSharedFlow().asSharedFlow(), + enterTransitionEndAction = MutableSharedFlow().asSharedFlow() + ) + } + + //Act + composeTestRule.onNodeWithText("Add 30 days time ($10)").performClick() + + // Assert + verify { clickHandler.invoke("PRODUCT_ID") } + } + companion object { private const val DUMMY_DEVICE_NAME = "fake_name" private const val DUMMY_ACCOUNT_NUMBER = "fake_number"