-
Notifications
You must be signed in to change notification settings - Fork 664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add formatted content description for Expiration Date text field #10185
Merged
Merged
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8476964
Add formatted content description for Expiration Date text field
tjclawson-stripe fe0766b
Refactor
tjclawson-stripe 21a35fb
Add tests
tjclawson-stripe 33aa46e
Refactor
tjclawson-stripe dc70bd8
Update string descriptions
tjclawson-stripe bd36000
Import java.util.Locale
tjclawson-stripe 15cf4dd
Add test for month localization
tjclawson-stripe d15c653
Update exception in formatExpirationDateForAccessibility
tjclawson-stripe d6fa25b
Update CHANGELOG.md
tjclawson-stripe 6eeaa07
Merge branch 'master' into tyler/mobilesdk-2530
tjclawson-stripe 1e2feb4
API dump
tjclawson-stripe 7cef837
Update FormPage.fillOutCardDetails to match on expiration date conten…
tjclawson-stripe 41c38f5
Fix tests
tjclawson-stripe 9b80729
Fix CustomerSheetTest
tjclawson-stripe e06d56e
Fix tests
tjclawson-stripe 82552f4
Merge branch 'master' into tyler/mobilesdk-2530
tjclawson-stripe 9648ebc
Detekt
tjclawson-stripe 61415e3
Fix TestCard
tjclawson-stripe bffcf8e
Merge branch 'master' into tyler/mobilesdk-2530
tjclawson-stripe 2ca82b2
Merge branch 'master' into tyler/mobilesdk-2530
tjclawson-stripe 237ce8f
Lint
tjclawson-stripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...rc/main/java/com/stripe/android/ui/core/elements/ExpiryDateContentDescriptionFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.stripe.android.ui.core.elements | ||
|
||
import androidx.appcompat.app.AppCompatDelegate | ||
import com.stripe.android.core.strings.ResolvableString | ||
import com.stripe.android.core.strings.resolvableString | ||
import com.stripe.android.uicore.R | ||
import java.text.SimpleDateFormat | ||
import java.util.Locale | ||
|
||
internal fun formatExpirationDateForAccessibility(input: String): ResolvableString { | ||
if (input.isEmpty()) { | ||
return resolvableString(R.string.stripe_expiration_date_empty_content_description) | ||
} | ||
|
||
val locale = AppCompatDelegate.getApplicationLocales()[0] ?: Locale.getDefault() | ||
|
||
val canOnlyBeSingleDigitMonth = input.isNotBlank() && !(input[0] == '0' || input[0] == '1') | ||
val canOnlyBeJanuary = input.length > 1 && input.take(2).toInt() > 12 | ||
val isSingleDigitMonth = canOnlyBeSingleDigitMonth || canOnlyBeJanuary | ||
|
||
val lastIndexOfMonth = if (isSingleDigitMonth) 0 else 1 | ||
val month = input.take(lastIndexOfMonth + 1).toIntOrNull() | ||
val year = input.slice(lastIndexOfMonth + 1..input.lastIndex).toIntOrNull() | ||
|
||
try { | ||
if (month != null) { | ||
val monthName = SimpleDateFormat("MM", locale).parse("$month")?.let { | ||
SimpleDateFormat("MMMM", locale).format(it) | ||
} | ||
|
||
return when (year) { | ||
null -> return resolvableString( | ||
R.string.stripe_expiration_date_month_complete_content_description, | ||
monthName | ||
) | ||
in 0..9 -> resolvableString( | ||
R.string.stripe_expiration_date_year_incomplete_content_description, | ||
monthName | ||
) | ||
else -> resolvableString( | ||
R.string.stripe_expiration_date_content_description, | ||
monthName, | ||
2000 + year | ||
) | ||
} | ||
} | ||
|
||
return input.resolvableString | ||
} catch (e: Exception) { | ||
return input.resolvableString | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...-core/src/test/java/com/stripe/android/utils/ExpiryDateContentDescriptionFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.stripe.android.utils | ||
|
||
import androidx.appcompat.app.AppCompatDelegate | ||
import androidx.core.os.LocaleListCompat | ||
import com.google.common.truth.Truth.assertThat | ||
import com.stripe.android.core.strings.resolvableString | ||
import com.stripe.android.ui.core.elements.formatExpirationDateForAccessibility | ||
import com.stripe.android.uicore.R | ||
import org.junit.Test | ||
|
||
class ExpiryDateContentDescriptionFormatterTest { | ||
|
||
@Test | ||
fun `formats correctly for empty input`() { | ||
amk-stripe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
val result = formatExpirationDateForAccessibility("4") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_month_complete_content_description, | ||
"April" | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats correctly for month only`() { | ||
val result = formatExpirationDateForAccessibility("4") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_month_complete_content_description, | ||
"April" | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats correctly for month and incomplete year`() { | ||
val result = formatExpirationDateForAccessibility("55") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_year_incomplete_content_description, | ||
"May" | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats correctly for month and year`() { | ||
val result = formatExpirationDateForAccessibility("555") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_content_description, | ||
"May", | ||
2055 | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats correctly for double digit month`() { | ||
val result = formatExpirationDateForAccessibility("1255") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_content_description, | ||
"December", | ||
2055 | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats correctly for single digit month with leading 0`() { | ||
val result = formatExpirationDateForAccessibility("0155") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_content_description, | ||
"January", | ||
2055 | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats first two numbers as month if less than 13`() { | ||
val result = formatExpirationDateForAccessibility("126") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_year_incomplete_content_description, | ||
"December" | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `formats month correctly based on locale`() { | ||
AppCompatDelegate.setApplicationLocales(LocaleListCompat.forLanguageTags("FR")) | ||
val result = formatExpirationDateForAccessibility("126") | ||
val expected = resolvableString( | ||
R.string.stripe_expiration_date_year_incomplete_content_description, | ||
"décembre" | ||
) | ||
|
||
assertThat(result).isEqualTo(expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which exceptions are we catching here and why is it ok to ignore them?
Can you consider:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to
ParseException
and left a comment on why it's fine to ignore.ParseException
should never be thrown but since the text passed in here is coming directly from the TextField input we don't want to crash the app if somehow a non numeric string is entered