-
Notifications
You must be signed in to change notification settings - Fork 629
/
TransformAddressToElementTest.kt
180 lines (165 loc) · 6.93 KB
/
TransformAddressToElementTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.stripe.android.ui.core.address
import androidx.compose.ui.text.input.KeyboardCapitalization
import com.google.common.truth.Truth.assertThat
import com.stripe.android.ui.core.R
import com.stripe.android.ui.core.address.AddressRepository.Companion.supportedCountries
import com.stripe.android.ui.core.elements.Capitalization
import com.stripe.android.ui.core.elements.IdentifierSpec
import com.stripe.android.ui.core.elements.KeyboardType
import com.stripe.android.ui.core.elements.RowElement
import com.stripe.android.ui.core.elements.SectionSingleFieldElement
import com.stripe.android.ui.core.elements.SimpleTextSpec
import com.stripe.android.ui.core.elements.TextFieldController
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.Test
import java.io.File
import java.security.InvalidParameterException
class TransformAddressToElementTest {
@Test
fun `Read US Json`() = runBlocking {
val addressSchema = readFile("src/main/assets/addressinfo/US.json")!!
val simpleTextList = addressSchema.transformToElementList("US")
val addressLine1 = SimpleTextSpec(
IdentifierSpec.Line1,
R.string.address_label_address_line1,
Capitalization.Words,
KeyboardType.Text,
showOptionalLabel = false
)
val addressLine2 = SimpleTextSpec(
IdentifierSpec.Line2,
R.string.address_label_address_line2,
Capitalization.Words,
KeyboardType.Text,
showOptionalLabel = true
)
val city = SimpleTextSpec(
IdentifierSpec.City,
R.string.address_label_city,
Capitalization.Words,
KeyboardType.Text,
showOptionalLabel = false
)
val state = SimpleTextSpec(
IdentifierSpec.State,
R.string.address_label_state,
Capitalization.Words,
KeyboardType.Text,
showOptionalLabel = false
)
val zip = SimpleTextSpec(
IdentifierSpec.PostalCode,
R.string.address_label_zip_code,
Capitalization.None,
KeyboardType.NumberPassword,
showOptionalLabel = false
)
assertThat(simpleTextList.size).isEqualTo(4)
verifySimpleTextSpecInTextFieldController(
simpleTextList[0] as SectionSingleFieldElement,
addressLine1
)
verifySimpleTextSpecInTextFieldController(
simpleTextList[1] as SectionSingleFieldElement,
addressLine2
)
val cityZipRow = simpleTextList[2] as RowElement
verifySimpleTextSpecInTextFieldController(
cityZipRow.fields[0],
city
)
verifySimpleTextSpecInTextFieldController(
cityZipRow.fields[1],
zip
)
verifySimpleTextSpecInTextFieldController(
simpleTextList[3] as SectionSingleFieldElement,
state
)
}
private suspend fun verifySimpleTextSpecInTextFieldController(
textElement: SectionSingleFieldElement,
simpleTextSpec: SimpleTextSpec
) {
val actualController = textElement.controller as TextFieldController
assertThat(actualController.capitalization).isEqualTo(
when (simpleTextSpec.capitalization) {
Capitalization.None -> KeyboardCapitalization.None
Capitalization.Characters -> KeyboardCapitalization.Characters
Capitalization.Words -> KeyboardCapitalization.Words
Capitalization.Sentences -> KeyboardCapitalization.Sentences
}
)
assertThat(actualController.keyboardType).isEqualTo(
when (simpleTextSpec.keyboardType) {
KeyboardType.Text -> androidx.compose.ui.text.input.KeyboardType.Text
KeyboardType.Ascii -> androidx.compose.ui.text.input.KeyboardType.Ascii
KeyboardType.Number -> androidx.compose.ui.text.input.KeyboardType.Number
KeyboardType.Phone -> androidx.compose.ui.text.input.KeyboardType.Phone
KeyboardType.Uri -> androidx.compose.ui.text.input.KeyboardType.Uri
KeyboardType.Email -> androidx.compose.ui.text.input.KeyboardType.Email
KeyboardType.Password -> androidx.compose.ui.text.input.KeyboardType.Password
KeyboardType.NumberPassword -> androidx.compose.ui.text.input.KeyboardType.NumberPassword
}
)
assertThat(actualController.label.first()).isEqualTo(
simpleTextSpec.label
)
}
@Test
fun `Make sure name schema is not found on fields not processed`() {
supportedCountries.forEach { countryCode ->
val schemaList = readFile("src/main/assets/addressinfo/$countryCode.json")
val invalidNameType = schemaList?.filter { addressSchema ->
addressSchema.schema?.nameType != null
}
?.filter {
it.type == FieldType.AddressLine1 &&
it.type == FieldType.AddressLine2 &&
it.type == FieldType.Locality
}
invalidNameType?.forEach { println(it.type?.name) }
assertThat(invalidNameType).isEmpty()
}
}
@Test
fun `Make sure sorting code and dependent locality is never required`() {
// Sorting code and dependent locality are not actually sent to the server.
supportedCountries.forEach { countryCode ->
val schemaList = readFile("src/main/assets/addressinfo/$countryCode.json")
val invalidNameType = schemaList?.filter { addressSchema ->
addressSchema.required &&
(
addressSchema.type == FieldType.SortingCode ||
addressSchema.type == FieldType.DependentLocality
)
}
invalidNameType?.forEach { println(it.type?.name) }
assertThat(invalidNameType).isEmpty()
}
}
@Test
fun `Make sure all country code json files are serializable`() {
supportedCountries.forEach { countryCode ->
val schemaList = readFile("src/main/assets/addressinfo/$countryCode.json")
schemaList?.filter { addressSchema ->
addressSchema.schema?.nameType != null
}
?.filter {
it.type == FieldType.AddressLine1 &&
it.type == FieldType.AddressLine2 &&
it.type == FieldType.Locality
}
?.forEach { println(it.type?.name) }
}
}
private fun readFile(filename: String): List<CountryAddressSchema>? {
val file = File(filename)
if (file.exists()) {
return parseAddressesSchema(file.inputStream())
} else {
throw InvalidParameterException("Error could not find the test files.")
}
}
}