Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Aug 14, 2023
1 parent 852636b commit 4510480
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.stripe.android.common.ui

import android.os.Build
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -31,6 +30,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.stripe.android.paymentsheet.utils.isRunningTest
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.first

Expand Down Expand Up @@ -59,8 +59,7 @@ internal class BottomSheetState(
}

suspend fun hide() {
val skip = runCatching { Build.FINGERPRINT.lowercase() == "robolectric" }.getOrDefault(false)
if (skip) {
if (isRunningTest) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.stripe.android.common.ui.rememberBottomSheetState
import com.stripe.android.paymentsheet.ui.BaseSheetActivity
import com.stripe.android.paymentsheet.ui.PaymentOptionsScreen
import com.stripe.android.uicore.StripeTheme
import kotlinx.coroutines.flow.filterNotNull

/**
* An `Activity` for selecting a payment option.
Expand Down Expand Up @@ -45,14 +46,13 @@ internal class PaymentOptionsActivity : BaseSheetActivity<PaymentOptionResult>()
setContent {
StripeTheme {
val isProcessing by viewModel.processing.collectAsState()
val result by viewModel.paymentOptionResult.collectAsState(initial = null)

val bottomSheetState = rememberBottomSheetState(
confirmValueChange = { !isProcessing },
)

result?.let { sheetResult ->
LaunchedEffect(sheetResult) {
LaunchedEffect(Unit) {
viewModel.paymentOptionResult.filterNotNull().collect { sheetResult ->
setActivityResult(sheetResult)
bottomSheetState.hide()
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.stripe.android.googlepaylauncher.GooglePayPaymentMethodLauncherContra
import com.stripe.android.paymentsheet.ui.BaseSheetActivity
import com.stripe.android.paymentsheet.ui.PaymentSheetScreen
import com.stripe.android.uicore.StripeTheme
import kotlinx.coroutines.flow.filterNotNull
import java.security.InvalidParameterException

internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
Expand Down Expand Up @@ -60,14 +61,13 @@ internal class PaymentSheetActivity : BaseSheetActivity<PaymentSheetResult>() {
setContent {
StripeTheme {
val isProcessing by viewModel.processing.collectAsState()
val result by viewModel.paymentSheetResult.collectAsState(initial = null)

val bottomSheetState = rememberBottomSheetState(
confirmValueChange = { !isProcessing },
)

result?.let { sheetResult ->
LaunchedEffect(sheetResult) {
LaunchedEffect(Unit) {
viewModel.paymentSheetResult.filterNotNull().collect { sheetResult ->
setActivityResult(sheetResult)
bottomSheetState.hide()
finish()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.stripe.android.paymentsheet.utils

import android.os.Build

internal val isRunningTest: Boolean
get() {
val isUnitTest = runCatching {
Build.FINGERPRINT.lowercase() == "robolectric"
}.getOrDefault(false)

val isUiTest = runCatching {
Class.forName("androidx.test.InstrumentationRegistry")
}.isSuccess

return isUnitTest || isUiTest
}
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,7 +33,6 @@ 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
Expand Down Expand Up @@ -79,8 +78,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 @@ -97,13 +96,15 @@ internal class PaymentOptionsActivityTest {
}

@Test
fun `click outside of bottom sheet before selection should return cancel result without selection`() {
fun `dismissing 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()
idleLooper()

assertThat(
PaymentOptionResult.fromIntent(it.getResult().resultData)
).isEqualTo(
Expand All @@ -113,7 +114,7 @@ internal class PaymentOptionsActivityTest {
}

@Test
fun `click outside of bottom sheet should return cancel result even if there is a selection`() {
fun `dismissing bottom sheet should return cancel result even if there is a selection`() {
val initialSelection = PaymentSelection.Saved(
paymentMethod = PaymentMethodFixtures.createCard(),
)
Expand All @@ -127,18 +128,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 +156,7 @@ internal class PaymentOptionsActivityTest {

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

runActivityScenario(args) {
it.onActivity { activity ->
assertThat(activity.viewBinding.continueButton.isVisible).isTrue()
assertThat(activity.continueButton.isVisible).isTrue()
}
}
}
Expand All @@ -180,19 +182,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 +203,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 +219,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 +231,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 +304,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 4510480

Please sign in to comment.