Skip to content

Commit

Permalink
[WIP] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Aug 11, 2023
1 parent 01162c5 commit 45946ae
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.pressBack
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.common.truth.Truth.assertThat
import com.stripe.android.ApiKeyFixtures
import com.stripe.android.PaymentConfiguration
import com.stripe.android.common.ui.BottomSheetContentTestTag
import com.stripe.android.core.Logger
import com.stripe.android.core.injection.WeakMapInjectorRegistry
import com.stripe.android.link.LinkConfigurationCoordinator
Expand All @@ -33,15 +33,13 @@ import com.stripe.android.model.PaymentMethodFixtures
import com.stripe.android.paymentsheet.PaymentSheetFixtures.PAYMENT_OPTIONS_CONTRACT_ARGS
import com.stripe.android.paymentsheet.PaymentSheetFixtures.updateState
import com.stripe.android.paymentsheet.analytics.EventReporter
import com.stripe.android.paymentsheet.databinding.StripeActivityPaymentOptionsBinding
import com.stripe.android.paymentsheet.databinding.StripePrimaryButtonBinding
import com.stripe.android.paymentsheet.model.PaymentSelection
import com.stripe.android.paymentsheet.ui.PrimaryButton
import com.stripe.android.paymentsheet.ui.getLabel
import com.stripe.android.ui.core.forms.resources.LpmRepository
import com.stripe.android.utils.FakeCustomerRepository
import com.stripe.android.utils.InjectableActivityScenario
import com.stripe.android.utils.TestUtils.idleLooper
import com.stripe.android.utils.TestUtils.viewModelFactoryFor
import com.stripe.android.utils.formViewModelSubcomponentBuilder
import com.stripe.android.utils.injectableActivityScenario
Expand Down Expand Up @@ -79,8 +77,8 @@ internal class PaymentOptionsActivityTest {

private val eventReporter = mock<EventReporter>()

private val StripeActivityPaymentOptionsBinding.continueButton: PrimaryButton
get() = root.findViewById(R.id.primary_button)
private val PaymentOptionsActivity.continueButton: PrimaryButton
get() = findViewById(R.id.primary_button)

@BeforeTest
fun setup() {
Expand All @@ -99,11 +97,12 @@ internal class PaymentOptionsActivityTest {
@Test
fun `click outside of bottom sheet before selection should return cancel result without selection`() {
runActivityScenario {
it.onActivity { activity ->
activity.viewBinding.root.performClick()
activity.finish()
it.onActivity {
pressBack()
}

composeTestRule.waitForIdle()

assertThat(
PaymentOptionResult.fromIntent(it.getResult().resultData)
).isEqualTo(
Expand All @@ -127,18 +126,19 @@ internal class PaymentOptionsActivityTest {
)

runActivityScenario(args) {
it.onActivity { activity ->
it.onActivity {
// We use US Bank Account because they don't dismiss PaymentSheet upon selection
// due to their mandate requirement.
val usBankAccountLabel = usBankAccount.getLabel(context.resources)
composeTestRule
.onNodeWithTag("${PAYMENT_OPTION_CARD_TEST_TAG}_$usBankAccountLabel")
.performClick()

activity.viewBinding.root.performClick()
activity.finish()
pressBack()
}

composeTestRule.waitForIdle()

val result = PaymentOptionResult.fromIntent(it.getResult().resultData)
assertThat(result).isEqualTo(
PaymentOptionResult.Canceled(null, initialSelection, paymentMethods)
Expand All @@ -154,7 +154,7 @@ internal class PaymentOptionsActivityTest {

runActivityScenario(args) {
it.onActivity { activity ->
assertThat(activity.viewBinding.continueButton.isVisible).isFalse()
assertThat(activity.continueButton.isVisible).isFalse()
}
}
}
Expand All @@ -167,7 +167,7 @@ internal class PaymentOptionsActivityTest {

runActivityScenario(args) {
it.onActivity { activity ->
assertThat(activity.viewBinding.continueButton.isVisible).isTrue()
assertThat(activity.continueButton.isVisible).isTrue()
}
}
}
Expand All @@ -180,19 +180,19 @@ internal class PaymentOptionsActivityTest {

runActivityScenario(args) {
it.onActivity { activity ->
assertThat(activity.viewBinding.continueButton.isVisible).isFalse()
assertThat(activity.continueButton.isVisible).isFalse()

// Navigate to "Add Payment Method" fragment
composeTestRule
.onNodeWithTag("${PAYMENT_OPTION_CARD_TEST_TAG}_+ Add")
.performClick()

assertThat(activity.viewBinding.continueButton.isVisible).isTrue()
assertThat(activity.continueButton.isVisible).isTrue()

// Navigate back to payment options list
pressBack()

assertThat(activity.viewBinding.continueButton.isVisible).isFalse()
assertThat(activity.continueButton.isVisible).isFalse()
}
}
}
Expand All @@ -201,12 +201,12 @@ internal class PaymentOptionsActivityTest {
fun `Verify Ready state updates the add button label`() {
runActivityScenario {
it.onActivity { activity ->
val addBinding = StripePrimaryButtonBinding.bind(activity.viewBinding.continueButton)
val addBinding = StripePrimaryButtonBinding.bind(activity.continueButton)

assertThat(addBinding.confirmedIcon.isVisible)
.isFalse()

assertThat(activity.viewBinding.continueButton.externalLabel)
assertThat(activity.continueButton.externalLabel)
.isEqualTo("Continue")

activity.finish()
Expand All @@ -217,11 +217,10 @@ internal class PaymentOptionsActivityTest {
@Test
fun `Verify bottom sheet expands on start`() {
runActivityScenario {
it.onActivity { activity ->
assertThat(activity.bottomSheetBehavior.state)
.isEqualTo(BottomSheetBehavior.STATE_EXPANDED)
assertThat(activity.bottomSheetBehavior.isFitToContents)
.isFalse()
it.onActivity {
composeTestRule
.onNodeWithTag(BottomSheetContentTestTag)
.assertIsDisplayed()
}
}
}
Expand All @@ -230,19 +229,22 @@ internal class PaymentOptionsActivityTest {
fun `Verify selecting a payment method closes the sheet`() {
val args = PAYMENT_OPTIONS_CONTRACT_ARGS.updateState(isGooglePayReady = true)

runActivityScenario(args) {
it.onActivity { activity ->
runActivityScenario(args) { scenario ->
scenario.onActivity {
composeTestRule
.onNodeWithTag("${PAYMENT_OPTION_CARD_TEST_TAG}_Google Pay")
.performClick()
}

composeTestRule.waitForIdle()

idleLooper()
composeTestRule.waitForIdle()

assertThat(activity.bottomSheetBehavior.state)
.isEqualTo(BottomSheetBehavior.STATE_HIDDEN)
}
val result = PaymentOptionResult.fromIntent(scenario.getResult().resultData)
assertThat(result).isEqualTo(
PaymentOptionResult.Succeeded(
paymentSelection = PaymentSelection.GooglePay,
paymentMethods = emptyList(),
)
)
}
}

Expand Down Expand Up @@ -300,8 +302,8 @@ internal class PaymentOptionsActivityTest {

runActivityScenario(args) {
it.onActivity { activity ->
assertThat(activity.viewBinding.continueButton.isVisible).isTrue()
assertThat(activity.viewBinding.continueButton.defaultTintList).isEqualTo(
assertThat(activity.continueButton.isVisible).isTrue()
assertThat(activity.continueButton.defaultTintList).isEqualTo(
ColorStateList.valueOf(Color.Magenta.toArgb())
)
}
Expand Down
Loading

0 comments on commit 45946ae

Please sign in to comment.