summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJonatan Rhodin <jonatan.rhodin@mullvad.net>2023-11-16 00:57:41 +0100
committerJonatan Rhodin <jonatan.rhodin@mullvad.net>2023-11-16 09:34:26 +0100
commit868e35cb9a814fea6c313eb93849ab426326be53 (patch)
treee80f9bc2ffb13d880e852218b71c413c3a2c2f7d
parent2a0c1b1e84d28fa8f3db3dda0c115059c678eb35 (diff)
downloadmullvadvpn-868e35cb9a814fea6c313eb93849ab426326be53.tar.xz
mullvadvpn-868e35cb9a814fea6c313eb93849ab426326be53.zip
Add unit tests for PlayPaymentUseCase
-rw-r--r--android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/PlayPaymentUseCaseTest.kt104
1 files changed, 104 insertions, 0 deletions
diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/PlayPaymentUseCaseTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/PlayPaymentUseCaseTest.kt
new file mode 100644
index 0000000000..a1d8bee37a
--- /dev/null
+++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/usecase/PlayPaymentUseCaseTest.kt
@@ -0,0 +1,104 @@
+package net.mullvad.mullvadvpn.usecase
+
+import app.cash.turbine.test
+import io.mockk.coVerify
+import io.mockk.every
+import io.mockk.mockk
+import kotlin.test.assertEquals
+import kotlin.test.assertNull
+import kotlinx.coroutines.flow.flow
+import kotlinx.coroutines.test.runTest
+import net.mullvad.mullvadvpn.lib.payment.PaymentRepository
+import net.mullvad.mullvadvpn.lib.payment.model.PaymentAvailability
+import net.mullvad.mullvadvpn.lib.payment.model.ProductId
+import net.mullvad.mullvadvpn.lib.payment.model.PurchaseResult
+import org.junit.Test
+
+class PlayPaymentUseCaseTest {
+
+ private val mockPaymentRepository: PaymentRepository = mockk(relaxed = true)
+
+ private val playPaymentUseCase = PlayPaymentUseCase(mockPaymentRepository)
+
+ @Test
+ fun testUpdatePaymentAvailability() = runTest {
+ // Arrange
+ val productsUnavailable = PaymentAvailability.ProductsUnavailable
+ val paymentRepositoryQueryPaymentAvailabilityFlow = flow { emit(productsUnavailable) }
+ every { mockPaymentRepository.queryPaymentAvailability() } returns
+ paymentRepositoryQueryPaymentAvailabilityFlow
+
+ // Act, Assert
+ playPaymentUseCase.paymentAvailability.test {
+ assertNull(awaitItem())
+ playPaymentUseCase.queryPaymentAvailability()
+ assertEquals(productsUnavailable, awaitItem())
+ }
+ }
+
+ @Test
+ fun testUpdatePurchaseResult() = runTest {
+ // Arrange
+ val fetchingProducts = PurchaseResult.FetchingProducts
+ val productId = ProductId("productId")
+ val paymentRepositoryPurchaseResultFlow = flow { emit(fetchingProducts) }
+ every { mockPaymentRepository.purchaseProduct(any(), any()) } returns
+ paymentRepositoryPurchaseResultFlow
+
+ // Act, Assert
+ playPaymentUseCase.purchaseResult.test {
+ assertNull(awaitItem())
+ playPaymentUseCase.purchaseProduct(productId, mockk())
+ assertEquals(fetchingProducts, awaitItem())
+ }
+ }
+
+ @Test
+ fun testPurchaseProduct() = runTest {
+ // Arrange
+ val productId = ProductId("productId")
+
+ // Act
+ playPaymentUseCase.purchaseProduct(productId, mockk())
+
+ // Assert
+ coVerify { mockPaymentRepository.purchaseProduct(productId, any()) }
+ }
+
+ @Test
+ fun testQueryPaymentAvailability() = runTest {
+ // Act
+ playPaymentUseCase.queryPaymentAvailability()
+
+ // Assert
+ coVerify { mockPaymentRepository.queryPaymentAvailability() }
+ }
+
+ @Test
+ fun testResetPurchaseResult() = runTest {
+ // Arrange
+ val completedSuccess = PurchaseResult.Completed.Success
+ val productId = ProductId("productId")
+ val paymentRepositoryPurchaseResultFlow = flow { emit(completedSuccess) }
+ every { mockPaymentRepository.purchaseProduct(any(), any()) } returns
+ paymentRepositoryPurchaseResultFlow
+
+ // Act, Assert
+ playPaymentUseCase.purchaseResult.test {
+ assertNull(awaitItem())
+ playPaymentUseCase.purchaseProduct(productId, mockk())
+ assertEquals(completedSuccess, awaitItem())
+ playPaymentUseCase.resetPurchaseResult()
+ assertNull(awaitItem())
+ }
+ }
+
+ @Test
+ fun testVerifyPurchases() = runTest {
+ // Act
+ playPaymentUseCase.verifyPurchases()
+
+ // Assert
+ coVerify { mockPaymentRepository.verifyPurchases() }
+ }
+}