Skip to content

Commit fe4a989

Browse files
Add newBuilder to CustomerSheet.Configuration (#7058)
1 parent 827c1a7 commit fe4a989

File tree

3 files changed

+110
-10
lines changed

3 files changed

+110
-10
lines changed

paymentsheet-example/src/main/java/com/stripe/android/paymentsheet/example/samples/ui/customersheet/playground/CustomerSheetPlaygroundViewModel.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,8 @@ class CustomerSheetPlaygroundViewModel(
5757
.googlePayEnabled(viewState.value.isGooglePayEnabled)
5858
.build()
5959
val configuration: StateFlow<CustomerSheet.Configuration> = viewState.map {
60-
CustomerSheet.Configuration.Builder()
61-
.defaultBillingDetails(
62-
PaymentSheet.BillingDetails(
63-
name = "CustomerSheet Testing"
64-
)
65-
).billingDetailsCollectionConfiguration(
66-
PaymentSheet.BillingDetailsCollectionConfiguration(
67-
name = PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
68-
)
69-
)
60+
initialConfiguration
61+
.newBuilder()
7062
.googlePayEnabled(it.isGooglePayEnabled)
7163
.build()
7264
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), initialConfiguration)

paymentsheet/src/main/java/com/stripe/android/customersheet/CustomerSheet.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ class CustomerSheet @Inject internal constructor(
153153
*/
154154
val merchantDisplayName: String? = null,
155155
) {
156+
157+
fun newBuilder(): Builder {
158+
return Builder()
159+
.appearance(appearance)
160+
.googlePayEnabled(googlePayEnabled)
161+
.headerTextForSelectionScreen(headerTextForSelectionScreen)
162+
.defaultBillingDetails(defaultBillingDetails)
163+
.billingDetailsCollectionConfiguration(billingDetailsCollectionConfiguration)
164+
.merchantDisplayName(merchantDisplayName)
165+
}
166+
156167
@ExperimentalCustomerSheetApi
157168
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
158169
class Builder {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.stripe.android.customersheet
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import com.stripe.android.paymentsheet.PaymentSheet
5+
import junit.framework.TestCase.fail
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
import org.robolectric.RobolectricTestRunner
9+
10+
@RunWith(RobolectricTestRunner::class)
11+
@OptIn(ExperimentalCustomerSheetApi::class)
12+
class CustomerSheetConfigurationTest {
13+
@Test
14+
fun `Builder returns a configuration`() {
15+
val googlePayEnabled = true
16+
val merchantDisplayName = "Test"
17+
val appearance = PaymentSheet.Appearance().copy(
18+
typography = PaymentSheet.Typography.default
19+
)
20+
val billingDetailsCollectionConfiguration = PaymentSheet.BillingDetailsCollectionConfiguration(
21+
name = PaymentSheet.BillingDetailsCollectionConfiguration.CollectionMode.Always
22+
)
23+
val defaultBillingDetails = PaymentSheet.BillingDetails(
24+
name = "Test"
25+
)
26+
val headerTextForSelectionScreen = "Test"
27+
28+
val configuration = CustomerSheet.Configuration.Builder()
29+
.googlePayEnabled(googlePayEnabled)
30+
.merchantDisplayName(merchantDisplayName)
31+
.appearance(appearance)
32+
.billingDetailsCollectionConfiguration(billingDetailsCollectionConfiguration)
33+
.defaultBillingDetails(defaultBillingDetails)
34+
.headerTextForSelectionScreen(headerTextForSelectionScreen)
35+
.build()
36+
37+
assertThat(configuration.googlePayEnabled)
38+
.isEqualTo(googlePayEnabled)
39+
assertThat(configuration.merchantDisplayName)
40+
.isEqualTo(merchantDisplayName)
41+
assertThat(configuration.appearance)
42+
.isEqualTo(appearance)
43+
assertThat(configuration.billingDetailsCollectionConfiguration)
44+
.isEqualTo(billingDetailsCollectionConfiguration)
45+
assertThat(configuration.defaultBillingDetails)
46+
.isEqualTo(defaultBillingDetails)
47+
assertThat(configuration.headerTextForSelectionScreen)
48+
.isEqualTo(headerTextForSelectionScreen)
49+
}
50+
51+
@Test
52+
fun `newBuilder returns a new builder with previous configuration`() {
53+
val configuration = CustomerSheet.Configuration.Builder()
54+
.googlePayEnabled(true)
55+
.build()
56+
57+
val configuration1 = configuration.newBuilder()
58+
.build()
59+
60+
assertThat(configuration1.googlePayEnabled)
61+
.isTrue()
62+
63+
val configuration2 = configuration.newBuilder()
64+
.googlePayEnabled(false)
65+
.build()
66+
67+
assertThat(configuration2.googlePayEnabled)
68+
.isFalse()
69+
}
70+
71+
@Test
72+
fun `Builder has the right methods`() {
73+
val knownBuilderMethodNames = setOf(
74+
"googlePayEnabled",
75+
"merchantDisplayName",
76+
"appearance",
77+
"billingDetailsCollectionConfiguration",
78+
"defaultBillingDetails",
79+
"headerTextForSelectionScreen",
80+
"build"
81+
)
82+
83+
// Programmatically check for any new method on the builder using reflection
84+
val configurationMethods = CustomerSheet.Configuration.Builder::class.java.declaredMethods
85+
val newMethods = configurationMethods.map { it.name } - knownBuilderMethodNames
86+
87+
// If there are any new methods, fail the test
88+
if (newMethods.isNotEmpty()) {
89+
fail(
90+
"""
91+
New method added to the Builder class: ${newMethods.joinToString()}, please update
92+
this test and update CustomerSheet.Configuration#newBuilder
93+
""".trimIndent()
94+
)
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)