|
| 1 | +package com.braintreepayments.api.googlepay |
| 2 | + |
| 3 | +import androidx.activity.result.ActivityResultLauncher |
| 4 | +import androidx.activity.result.ActivityResultRegistry |
| 5 | +import androidx.activity.result.contract.ActivityResultContract |
| 6 | +import androidx.fragment.app.FragmentActivity |
| 7 | +import com.google.android.gms.wallet.PaymentDataRequest |
| 8 | +import io.mockk.every |
| 9 | +import io.mockk.mockk |
| 10 | +import io.mockk.verify |
| 11 | +import org.junit.Before |
| 12 | +import org.junit.Test |
| 13 | +import org.junit.runner.RunWith |
| 14 | +import org.robolectric.RobolectricTestRunner |
| 15 | + |
| 16 | +@RunWith(RobolectricTestRunner::class) |
| 17 | +class GooglePayLauncherUnitTest { |
| 18 | + |
| 19 | + private val activityResultLauncher = |
| 20 | + mockk<ActivityResultLauncher<GooglePayPaymentAuthRequestParams?>>(relaxed = true) |
| 21 | + private val callback = mockk<GooglePayLauncherCallback>(relaxed = true) |
| 22 | + private val activityResultRegistry = mockk<ActivityResultRegistry>(relaxed = true) |
| 23 | + |
| 24 | + @Before |
| 25 | + fun beforeEach() { |
| 26 | + every { |
| 27 | + activityResultRegistry.register( |
| 28 | + any(), |
| 29 | + any(), |
| 30 | + any<ActivityResultContract<GooglePayPaymentAuthRequestParams, Any>>(), |
| 31 | + any() |
| 32 | + ) |
| 33 | + } returns activityResultLauncher |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun constructor_createsActivityLauncher() { |
| 38 | + val expectedKey = "com.braintreepayments.api.GooglePay.RESULT" |
| 39 | + val lifecycleOwner = FragmentActivity() |
| 40 | + |
| 41 | + val registry = mockk<ActivityResultRegistry>(relaxed = true) |
| 42 | + GooglePayLauncher(registry, lifecycleOwner, callback) |
| 43 | + |
| 44 | + verify { |
| 45 | + registry.register( |
| 46 | + eq(expectedKey), eq(lifecycleOwner), |
| 47 | + any<ActivityResultContract<GooglePayPaymentAuthRequestParams, GooglePayPaymentAuthResult>>(), |
| 48 | + any() |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun launch_launchesActivity() { |
| 55 | + val lifecycleOwner = FragmentActivity() |
| 56 | + val sut = GooglePayLauncher( |
| 57 | + activityResultRegistry, lifecycleOwner, |
| 58 | + callback |
| 59 | + ) |
| 60 | + |
| 61 | + val googlePayRequest = GooglePayRequest("USD", "1.00", GooglePayTotalPriceStatus.TOTAL_PRICE_STATUS_FINAL) |
| 62 | + val paymentDataRequest = PaymentDataRequest.fromJson(googlePayRequest.toJson()) |
| 63 | + val intentData = GooglePayPaymentAuthRequestParams(1, paymentDataRequest) |
| 64 | + |
| 65 | + sut.launch(GooglePayPaymentAuthRequest.ReadyToLaunch(intentData)) |
| 66 | + verify { activityResultLauncher.launch(intentData) } |
| 67 | + } |
| 68 | +} |
0 commit comments