|
| 1 | +package com.stripe.android.crypto.onramp.example |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import androidx.compose.ui.test.hasTestTag |
| 5 | +import androidx.compose.ui.test.hasText |
| 6 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 7 | +import androidx.compose.ui.test.onNodeWithTag |
| 8 | +import androidx.compose.ui.test.performTextInput |
| 9 | +import androidx.compose.ui.test.ExperimentalTestApi |
| 10 | +import androidx.compose.ui.test.SemanticsNodeInteraction |
| 11 | +import androidx.compose.ui.test.performClick |
| 12 | +import androidx.compose.ui.test.performScrollTo |
| 13 | +import androidx.compose.ui.test.hasSetTextAction |
| 14 | +import androidx.compose.ui.test.SemanticsMatcher |
| 15 | +import androidx.test.core.app.ApplicationProvider |
| 16 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 17 | +import com.stripe.android.core.utils.FeatureFlags |
| 18 | +import com.stripe.android.testing.FeatureFlagTestRule |
| 19 | +import org.junit.Before |
| 20 | +import org.junit.Rule |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import kotlin.time.Duration |
| 24 | +import kotlin.time.Duration.Companion.seconds |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +class OnrampFlowTest { |
| 28 | + @get:Rule |
| 29 | + internal val composeRule = createAndroidComposeRule<OnrampActivity>() |
| 30 | + |
| 31 | + @get:Rule |
| 32 | + internal val attestationFeatureFlagTestRule = FeatureFlagTestRule( |
| 33 | + featureFlag = FeatureFlags.nativeLinkAttestationEnabled, |
| 34 | + isEnabled = false |
| 35 | + ) |
| 36 | + |
| 37 | + private val defaultTimeout: Duration = 15.seconds |
| 38 | + |
| 39 | + @Before |
| 40 | + fun clearPrefs() { |
| 41 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 42 | + |
| 43 | + context.getSharedPreferences(ONRAMP_PREFS_NAME, Context.MODE_PRIVATE) |
| 44 | + .edit() |
| 45 | + .clear() |
| 46 | + .commit() |
| 47 | + } |
| 48 | + |
| 49 | + @OptIn(ExperimentalTestApi::class) |
| 50 | + @Test |
| 51 | + fun testCheckoutFlow() { |
| 52 | + waitForTag(LOGIN_EMAIL_TAG) |
| 53 | + |
| 54 | + // Enter test login credentials previously registered with the demo backend. |
| 55 | + composeRule.onNodeWithTag(LOGIN_EMAIL_TAG) |
| 56 | + .performTextInput( "[email protected]") |
| 57 | + |
| 58 | + composeRule.onNodeWithTag(LOGIN_PASSWORD_TAG) |
| 59 | + .performTextInput("testing1234") |
| 60 | + |
| 61 | + performClickOnNode(LOGIN_LOGIN_BUTTON_TAG) |
| 62 | + performClickOnNode(AUTHENTICATE_BUTTON_TAG) |
| 63 | + |
| 64 | + waitForTag("OTP-0") |
| 65 | + |
| 66 | + for (i in 0..5) { |
| 67 | + composeRule |
| 68 | + .onNodeWithTag("OTP-$i") |
| 69 | + .slowType("0") |
| 70 | + } |
| 71 | + |
| 72 | + performClickOnNode(REGISTER_WALLET_BUTTON_TAG) |
| 73 | + performClickOnNode(COLLECT_CARD_BUTTON_TAG) |
| 74 | + |
| 75 | + // Optionally wait for a CVV field and input if present |
| 76 | + val cvvMatcher = hasSetTextAction() |
| 77 | + if (waitForOptionalNode(cvvMatcher, timeoutMs = 5.seconds.inWholeMilliseconds)) { |
| 78 | + val cvvNode = try { |
| 79 | + composeRule.onNode(cvvMatcher) |
| 80 | + } catch (_: Throwable) { |
| 81 | + composeRule.onNode(cvvMatcher, useUnmergedTree = true) |
| 82 | + } |
| 83 | + cvvNode.performScrollTo() |
| 84 | + cvvNode.performTextInput("555") |
| 85 | + } |
| 86 | + |
| 87 | + performClickOnNode("PrimaryButtonTag") |
| 88 | + performClickOnNode(CREATE_CRYPTO_TOKEN_BUTTON_TAG) |
| 89 | + performClickOnNode(CREATE_SESSION_BUTTON_TAG) |
| 90 | + performClickOnNode(CHECKOUT_BUTTON_TAG, timeoutMs = 30.seconds.inWholeMilliseconds) |
| 91 | + |
| 92 | + composeRule.waitUntilExactlyOneExists( |
| 93 | + hasText("Checkout completed successfully!"), |
| 94 | + timeoutMillis = 30.seconds.inWholeMilliseconds |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + private fun SemanticsNodeInteraction.slowType( |
| 99 | + text: String, |
| 100 | + delayMs: Long = 200 |
| 101 | + ) { |
| 102 | + text.forEach { char -> |
| 103 | + this.performTextInput(char.toString()) |
| 104 | + Thread.sleep(delayMs) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @OptIn(ExperimentalTestApi::class) |
| 109 | + private fun waitForSnackbarToHide(timeoutMs: Long = defaultTimeout.inWholeMilliseconds) { |
| 110 | + composeRule.waitUntilDoesNotExist(hasTestTag(SNACKBAR_TAG), timeoutMillis = timeoutMs) |
| 111 | + } |
| 112 | + |
| 113 | + @OptIn(ExperimentalTestApi::class) |
| 114 | + private fun waitForTag(tag: String, timeoutMs: Long = defaultTimeout.inWholeMilliseconds) { |
| 115 | + composeRule.waitUntilExactlyOneExists( |
| 116 | + hasTestTag(tag), |
| 117 | + timeoutMillis = timeoutMs |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + private fun waitForOptionalNode( |
| 122 | + matcher: SemanticsMatcher, |
| 123 | + timeoutMs: Long = defaultTimeout.inWholeMilliseconds |
| 124 | + ): Boolean { |
| 125 | + return runCatching { |
| 126 | + composeRule.waitUntil(timeoutMs) { |
| 127 | + composeRule.onAllNodes(matcher).fetchSemanticsNodes().isNotEmpty() |
| 128 | + } |
| 129 | + true |
| 130 | + }.getOrElse { false } |
| 131 | + } |
| 132 | + |
| 133 | + private fun performClickOnNode(tag: String, timeoutMs: Long = defaultTimeout.inWholeMilliseconds) { |
| 134 | + waitForTag(tag = tag, timeoutMs = timeoutMs) |
| 135 | + waitForSnackbarToHide() |
| 136 | + |
| 137 | + val node = composeRule.onNodeWithTag(tag) |
| 138 | + runCatching { node.performScrollTo() } |
| 139 | + node.performClick() |
| 140 | + } |
| 141 | +} |
0 commit comments