From 11fe2145e59df573a416c443637748946c9743db Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Mon, 6 Nov 2023 13:56:33 +0100 Subject: [PATCH] Add payment ui tests to OutOfTimeScreen --- .../compose/screen/OutOfTimeScreenTest.kt | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreenTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreenTest.kt index b0198316e393..3ccbe435c7cf 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreenTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/screen/OutOfTimeScreenTest.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.OutOfTimeUiState +import net.mullvad.mullvadvpn.compose.state.PaymentState +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.model.TunnelState import net.mullvad.mullvadvpn.viewmodel.OutOfTimeViewModel import org.junit.Before @@ -174,4 +179,139 @@ class OutOfTimeScreenTest { // Assert verify(exactly = 1) { mockClickListener.invoke() } } + + @Test + fun testShowPurchaseCompleteDialog() { + // Arrange + composeTestRule.setContent { + OutOfTimeScreen( + showSitePayment = true, + uiState = OutOfTimeUiState(purchaseResult = PurchaseResult.PurchaseCompleted), + uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Assert + composeTestRule.onNodeWithText("Time was successfully added").assertExists() + } + + @Test + fun testShowVerificationErrorDialog() { + // Arrange + composeTestRule.setContent { + OutOfTimeScreen( + showSitePayment = true, + uiState = + OutOfTimeUiState(purchaseResult = PurchaseResult.Error.VerificationError(null)), + uiSideEffect = MutableStateFlow(OutOfTimeViewModel.UiSideEffect.OpenConnectScreen), + onSitePaymentClick = {}, + onRedeemVoucherClick = {}, + onSettingsClick = {}, + onAccountClick = {}, + openConnectScreen = {}, + onPurchaseBillingProductClick = {}, + onTryVerificationAgain = {}, + onTryFetchProductsAgain = {} + ) + } + + // Assert + composeTestRule.onNodeWithText("Payment was unsuccessful").assertExists() + } + + @Test + fun testShowBillingErrorDialog() { + // Arrange + composeTestRule.setContent { + OutOfTimeScreen( + showSitePayment = true, + uiState = OutOfTimeUiState(billingPaymentState = PaymentState.Error.BillingError), + uiSideEffect = MutableStateFlow(OutOfTimeViewModel.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 { + OutOfTimeScreen( + showSitePayment = true, + uiState = + OutOfTimeUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + uiSideEffect = MutableStateFlow(OutOfTimeViewModel.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 { + OutOfTimeScreen( + showSitePayment = true, + uiState = + OutOfTimeUiState( + billingPaymentState = + PaymentState.PaymentAvailable(listOf(mockPaymentProduct)) + ), + uiSideEffect = MutableStateFlow(OutOfTimeViewModel.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") } + } }