Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
tillh-stripe committed Oct 26, 2023
1 parent 032f325 commit 26a701c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
1 change: 1 addition & 0 deletions payments-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
testImplementation project(':network-testing')
testImplementation project(':payments-core-testing')
testImplementation testLibs.androidx.archCore
testImplementation testLibs.androidx.composeUi
testImplementation testLibs.androidx.core
testImplementation testLibs.androidx.fragment
testImplementation testLibs.androidx.junit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.stripe.android.R
Expand All @@ -44,6 +45,8 @@ import kotlinx.parcelize.Parcelize
import kotlin.properties.Delegates
import com.stripe.android.uicore.R as StripeUiCoreR

internal val CardBrandDropdownTestTag = "CardBrandDropdownTestTag"

internal class CardBrandView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
Expand Down Expand Up @@ -246,9 +249,9 @@ private fun CardBrand(
Box(modifier) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.clickable(enabled = showDropdown) {
expanded = true
},
modifier = Modifier
.clickable(enabled = showDropdown) { expanded = true }
.testTag(CardBrandDropdownTestTag),
) {
val dropdownIconAlpha by animateFloatAsState(
targetValue = if (showDropdown) ContentAlpha.medium else 0f,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,27 @@ internal class ActivityScenarioFactory(

return requireNotNull(view)
}

fun <ViewType : View> createViewAndScenario(
beforeAttach: (ViewType) -> Unit = {},
viewFactory: (Activity) -> ViewType,
): Pair<ViewType, ActivityScenario<AddPaymentMethodActivity>> {
var view: ViewType? = null
val activityScenario = createAddPaymentMethodActivity()

activityScenario.onActivity { activity ->
activity.setTheme(R.style.StripePaymentSheetDefaultTheme)

activity.findViewById<ViewGroup>(R.id.add_payment_method_card).let { root ->
root.removeAllViews()

view = viewFactory(activity).also {
beforeAttach(it)
root.addView(it)
}
}
}

return requireNotNull(view) to activityScenario
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import android.os.Parcelable
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.core.view.updateLayoutParams
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
Expand Down Expand Up @@ -65,6 +69,9 @@ internal class CardInputWidgetTest {
isEnabled = false,
)

@get:Rule
val composeTestRule = createComposeRule()

private val testDispatcher = StandardTestDispatcher()

private val context: Context = ApplicationProvider.getApplicationContext()
Expand Down Expand Up @@ -1739,8 +1746,36 @@ internal class CardInputWidgetTest {
}
}

@Test
fun `Restores brand state correctly on activity recreation`() {
featureFlagTestRule.setEnabled(true)

runCardInputWidgetTest(
isCbcEligible = true,
block = {
cardNumberEditText.setText("4000 0025 0000 1001")

composeTestRule
.onNodeWithTag(CardBrandDropdownTestTag)
.performClick()

composeTestRule
.onNodeWithText("Cartes Bancaires")
.performClick()

expiryDateEditText.append("12")
expiryDateEditText.append("50")
cvcEditText.append("123")
},
afterRecreation = {
assertThat(cardBrandView.brand).isEqualTo(CardBrand.CartesBancaires)
},
)
}

private fun runCardInputWidgetTest(
isCbcEligible: Boolean = false,
afterRecreation: (CardInputWidget.() -> Unit)? = null,
block: CardInputWidget.() -> Unit,
) {
registerTestActivity()
Expand All @@ -1758,6 +1793,18 @@ internal class CardInputWidgetTest {
widget.setCardInputListener(cardInputListener)
widget.block()
}

if (afterRecreation != null) {
activityScenario.recreate()

activityScenario.onActivity { activity ->
activity.setWorkContext(testDispatcher)

val widget = activity.findViewById<CardInputWidget>(CardInputWidgetTestActivity.VIEW_ID)
widget.setCardInputListener(cardInputListener)
widget.afterRecreation()
}
}
}

private fun registerTestActivity() {
Expand Down Expand Up @@ -1828,7 +1875,6 @@ internal class CardInputWidgetTestActivity : AppCompatActivity() {
}

private val cardInputWidget: CardInputWidget by lazy {

CardInputWidget(this).apply {
id = VIEW_ID

Expand Down

0 comments on commit 26a701c

Please sign in to comment.