|
| 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