From fe300b246cf5da07d94c0486b30ff440a6854b3c Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Wed, 27 Sep 2023 13:40:53 +0200 Subject: [PATCH] Add payment ui tests to Welcome Screen --- .../compose/screen/WelcomeScreenTest.kt | 175 +++++++++++++++++- 1 file changed, 168 insertions(+), 7 deletions(-) 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 index 8331794cab0f..5a5b686ec846 100644 --- 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 @@ -4,12 +4,17 @@ 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.MutableStateFlow import net.mullvad.mullvadvpn.compose.setContentWithTheme +import net.mullvad.mullvadvpn.compose.state.PaymentState import net.mullvad.mullvadvpn.compose.state.WelcomeUiState +import net.mullvad.mullvadvpn.lib.payment.model.PaymentProduct +import net.mullvad.mullvadvpn.lib.payment.model.PaymentStatus +import net.mullvad.mullvadvpn.lib.payment.model.PurchaseResult import net.mullvad.mullvadvpn.viewmodel.WelcomeViewModel import org.junit.Before import org.junit.Rule @@ -35,7 +40,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -58,7 +66,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -87,7 +98,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -108,7 +122,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -129,7 +146,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = mockClickListener + openConnectScreen = mockClickListener, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -150,7 +170,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = {}, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -174,7 +197,10 @@ class WelcomeScreenTest { onRedeemVoucherClick = mockClickListener, onSettingsClick = {}, onAccountClick = {}, - openConnectScreen = {} + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} ) } @@ -184,4 +210,139 @@ class WelcomeScreenTest { // Assert verify(exactly = 1) { mockClickListener.invoke() } } + + @Test + fun testShowPurchaseCompleteDialog() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(purchaseResult = PurchaseResult.PurchaseCompleted), + uiSideEffect = MutableStateFlow(WelcomeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Assert + composeTestRule.onNodeWithText("Time was successfully added").assertExists() + } + + @Test + fun testShowVerificationErrorDialog() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = + WelcomeUiState(purchaseResult = PurchaseResult.Error.VerificationError(null)), + uiSideEffect = MutableStateFlow(WelcomeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Assert + composeTestRule.onNodeWithText("Payment was unsuccessful").assertExists() + } + + @Test + fun testShowBillingErrorDialog() { + // Arrange + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = WelcomeUiState(billingPaymentState = PaymentState.Error.BillingError), + uiSideEffect = MutableStateFlow(WelcomeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Assert + composeTestRule.onNodeWithText("Google Play services not available").assertExists() + } + + @Test + fun testShowBillingPaymentAvailable() { + // Arrange + val mockPaymentProduct: PaymentProduct = mockk() + every { mockPaymentProduct.price } returns "$10" + every { mockPaymentProduct.status } returns PaymentStatus.AVAILABLE + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = + WelcomeUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + uiSideEffect = MutableStateFlow(WelcomeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // 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" + every { mockPaymentProduct.status } returns PaymentStatus.AVAILABLE + composeTestRule.setContent { + WelcomeScreen( + showSitePayment = true, + uiState = + WelcomeUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + uiSideEffect = MutableStateFlow(WelcomeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = clickHandler, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Act + composeTestRule.onNodeWithText("Add 30 days time ($10)").performClick() + + // Assert + verify { clickHandler.invoke("PRODUCT_ID") } + } }