From ec1768ebb9134f274d34cd1b3e902a091fcaa77c Mon Sep 17 00:00:00 2001 From: "Bruno R. Nunes" <77990083+brnunes-stripe@users.noreply.github.com> Date: Fri, 11 Feb 2022 17:24:22 -0500 Subject: [PATCH] Fix crash when no bank is selected in `AddPaymentMethodActivity` (#4579) --- CHANGELOG.md | 1 + .../activity/AddFpxPaymentMethodTest.kt | 24 +++++++++++++++---- .../AddNetbankingPaymentMethodTest.kt | 24 +++++++++++++++---- .../android/view/AddPaymentMethodFpxView.kt | 12 ++++++---- .../view/AddPaymentMethodNetbankingView.kt | 12 ++++++---- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac14e00bfde..ffffc9f26c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * [CHANGED] [4546](https://github.com/stripe/stripe-android/pull/4546) Update to kotlin 1.6 * [FIXED] [4560](https://github.com/stripe/stripe-android/pull/4560) Fix `cardValidCallback` being added multiple times in `CardInputWidget`. * [FIXED] [4574](https://github.com/stripe/stripe-android/pull/4574) Take `postalCode` into account in `CardMultilineWidget` validation. +* [FIXED] [4579](https://github.com/stripe/stripe-android/pull/4579) Fix crash when no bank is selected in `AddPaymentMethodActivity`. ### PaymentSheet * [FIXED] [4466](https://github.com/stripe/stripe-android/pull/4466) Fix issues when activities are lost on low resource phones. * [FIXED] [4557](https://github.com/stripe/stripe-android/pull/4557) Add missing app info to some Stripe API requests diff --git a/example/src/androidTestDebug/java/com/stripe/example/activity/AddFpxPaymentMethodTest.kt b/example/src/androidTestDebug/java/com/stripe/example/activity/AddFpxPaymentMethodTest.kt index d3aff99611d..87b693ff7c1 100644 --- a/example/src/androidTestDebug/java/com/stripe/example/activity/AddFpxPaymentMethodTest.kt +++ b/example/src/androidTestDebug/java/com/stripe/example/activity/AddFpxPaymentMethodTest.kt @@ -38,6 +38,25 @@ class AddFpxPaymentMethodTest { @Test fun launchFpxAndSelectBank() { + launchBankSelector() + + // click on first bank in the list + onView(withId(R.id.bank_list)).perform( + RecyclerViewActions.actionOnItemAtPosition(0, click()) + ) + } + + @Test + fun launchFpxAndConfirmWithoutSelectingBank() { + launchBankSelector() + + // confirm selection without selecting a bank + onView(withId(R.id.action_save)).perform(click()) + + // Nothing should happen as no bank was selected + } + + private fun launchBankSelector() { // launch FPX selection activity onView(withId(R.id.examples)).perform( RecyclerViewActions.actionOnItemAtPosition(11, click()) @@ -46,10 +65,5 @@ class AddFpxPaymentMethodTest { // click select payment method button onView(withId(R.id.select_payment_method_button)) .perform(click()) - - // click on first bank in the list - onView(withId(R.id.bank_list)).perform( - RecyclerViewActions.actionOnItemAtPosition(0, click()) - ) } } diff --git a/example/src/androidTestDebug/java/com/stripe/example/activity/AddNetbankingPaymentMethodTest.kt b/example/src/androidTestDebug/java/com/stripe/example/activity/AddNetbankingPaymentMethodTest.kt index 8e56085b0c9..f874424d584 100644 --- a/example/src/androidTestDebug/java/com/stripe/example/activity/AddNetbankingPaymentMethodTest.kt +++ b/example/src/androidTestDebug/java/com/stripe/example/activity/AddNetbankingPaymentMethodTest.kt @@ -44,6 +44,25 @@ class AddNetbankingPaymentMethodTest { @Test fun launchNetbankingAndSelectBank() { + launchBankSelector() + + // click on first bank in the list + onView(withId(R.id.bank_list)).perform( + RecyclerViewActions.actionOnItemAtPosition(0, click()) + ) + } + + @Test + fun launchNetbankingAndConfirmWithoutSelectingBank() { + launchBankSelector() + + // confirm selection without selecting a bank + onView(withId(R.id.action_save)).perform(click()) + + // Nothing should happen as no bank was selected + } + + private fun launchBankSelector() { // launch Netbanking selection activity onView(withId(R.id.examples)).perform( RecyclerViewActions.actionOnItemAtPosition(10, click()) @@ -52,10 +71,5 @@ class AddNetbankingPaymentMethodTest { // click select payment method button onView(withId(R.id.select_payment_method_button)) .perform(click()) - - // click on first bank in the list - onView(withId(R.id.bank_list)).perform( - RecyclerViewActions.actionOnItemAtPosition(0, click()) - ) } } diff --git a/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodFpxView.kt b/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodFpxView.kt index 0839fce5af3..39ce5a9d0c8 100644 --- a/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodFpxView.kt +++ b/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodFpxView.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.stripe.android.R import com.stripe.android.databinding.BankListPaymentMethodBinding import com.stripe.android.model.BankStatuses @@ -36,11 +37,12 @@ internal class AddPaymentMethodFpxView @JvmOverloads internal constructor( override val createParams: PaymentMethodCreateParams? get() { - val fpxBank = FpxBank.values()[fpxAdapter.selectedPosition] - - return PaymentMethodCreateParams.create( - PaymentMethodCreateParams.Fpx(bank = fpxBank.code) - ) + return fpxAdapter.selectedPosition.takeIf { it != RecyclerView.NO_POSITION }?.let { + val fpxBank = FpxBank.values()[it] + PaymentMethodCreateParams.create( + PaymentMethodCreateParams.Fpx(bank = fpxBank.code) + ) + } } init { diff --git a/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodNetbankingView.kt b/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodNetbankingView.kt index 02e6586ce2c..93c90b377ec 100644 --- a/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodNetbankingView.kt +++ b/payments-core/src/main/java/com/stripe/android/view/AddPaymentMethodNetbankingView.kt @@ -4,6 +4,7 @@ import android.util.AttributeSet import androidx.fragment.app.FragmentActivity import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import com.stripe.android.R import com.stripe.android.databinding.BankListPaymentMethodBinding import com.stripe.android.model.PaymentMethodCreateParams @@ -25,11 +26,14 @@ internal class AddPaymentMethodNetbankingView @JvmOverloads internal constructor override val createParams: PaymentMethodCreateParams? get() { - val netbankingBank = NetbankingBank.values()[netbankingAdapter.selectedPosition] + return netbankingAdapter.selectedPosition.takeIf { it != RecyclerView.NO_POSITION } + ?.let { + val netbankingBank = NetbankingBank.values()[netbankingAdapter.selectedPosition] - return PaymentMethodCreateParams.create( - PaymentMethodCreateParams.Netbanking(bank = netbankingBank.code) - ) + return PaymentMethodCreateParams.create( + PaymentMethodCreateParams.Netbanking(bank = netbankingBank.code) + ) + } } init {