Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ClassCastException in PaymentSheetContract #6366

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PaymentSheetContract :
resultCode: Int,
intent: Intent?
): PaymentSheetResult {
val paymentResult = intent?.getParcelableExtra<Result>(EXTRA_RESULT)?.paymentSheetResult
val paymentResult = intent?.getParcelableExtra<PaymentSheetContractV2.Result>(EXTRA_RESULT)?.paymentSheetResult
return paymentResult ?: PaymentSheetResult.Failed(
IllegalArgumentException("Failed to retrieve a PaymentSheetResult.")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
package com.stripe.android.paymentsheet

import android.content.Context
import android.content.Intent
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.Test

@RunWith(RobolectricTestRunner::class)
class PaymentSheetContractTest {

private val context = ApplicationProvider.getApplicationContext<Context>()

@get:Rule
val composeTestRule = createEmptyComposeRule()

@Test
fun `parseResult() with missing data should return failed result`() {
assertThat(PaymentSheetContract().parseResult(0, Intent()))
.isInstanceOf(PaymentSheetResult.Failed::class.java)
}

@Test
fun `Converts PaymentSheetContractV2 result correctly back to PaymentSheetContract result`() {
val contract = PaymentSheetContract()
val args = PaymentSheetContract.Args.createPaymentIntentArgs("pi_123_secret_456")
val intent = contract.createIntent(context, args)

val scenario = ActivityScenario.launchActivityForResult<PaymentSheetActivity>(intent)

scenario.onActivity { activity ->
activity.setActivityResult(PaymentSheetResult.Canceled)
activity.finish()
}

val result = contract.parseResult(scenario.result.resultCode, scenario.result.resultData)
assertThat(result).isEqualTo(PaymentSheetResult.Canceled)
}
}