Skip to content

Commit

Permalink
Fix crash on special characters input in expiry date (#283)
Browse files Browse the repository at this point in the history
* Fix crash on special characters input

* Extract logic to private function & format

* Fixed failing unit tests

* Ignore flaky test

* Test adding delay to UI tests

* Revert "Test adding delay to UI tests"

This reverts commit 2e15d2d.

* Add test to cover new cases

* Remove nullability for sequence in onTextChanged

* Restrict change to keyboard type
  • Loading branch information
AnasNaouchi authored Nov 7, 2023
1 parent cdcbb0e commit 356a79e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
5 changes: 2 additions & 3 deletions sdk/src/main/java/co/omise/android/ui/ExpiryDateEditText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ExpiryDateEditText : OmiseEditText {
addTextChangedListener(textWatcher)
disableOptions()
filters = arrayOf(InputFilter.LengthFilter(MAX_CHARS))
inputType = InputType.TYPE_CLASS_PHONE
inputType = InputType.TYPE_CLASS_NUMBER
}

override fun onSelectionChanged(selStart: Int, selEnd: Int) {
Expand Down Expand Up @@ -70,8 +70,7 @@ class ExpiryDateEditText : OmiseEditText {
beforeChangedText = s.toString()
}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s == null || s.length > MAX_CHARS) return
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

// On deleting
if (s.length < beforeChangedText.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class CreditCardActivityTest {
fun form_validForm() {
onView(withId(R.id.edit_card_number)).perform(typeText("4242424242424242"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"), pressImeActionButton())

onView(withId(R.id.edit_card_number)).check(matches(withText("4242 4242 4242 4242")))
Expand All @@ -118,7 +118,7 @@ class CreditCardActivityTest {
fun form_validBillingAddressForm() {
onView(withId(R.id.edit_card_number)).perform(typeText("4242424242424242"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"))
onView(withId(R.id.edit_country)).perform(scrollTo(), click())

Expand Down Expand Up @@ -148,7 +148,7 @@ class CreditCardActivityTest {
fun form_invalidForm() {
onView(withId(R.id.edit_card_number)).perform(typeText("1234567890"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"), pressImeActionButton())

onView(withId(R.id.edit_card_number)).check(matches(withText("1234 5678 90")))
Expand Down Expand Up @@ -298,7 +298,7 @@ class CreditCardActivityTest {
whenever(mockClient.send<Token>(any(), any())).doAnswer {}
onView(withId(R.id.edit_card_number)).perform(typeText("4242424242424242"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"), closeSoftKeyboard())
onView(withId(R.id.button_submit)).perform(scrollTo(), click())

Expand All @@ -319,7 +319,7 @@ class CreditCardActivityTest {

onView(withId(R.id.edit_card_number)).perform(typeText("4242424242424242"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"), closeSoftKeyboard())
onView(withId(R.id.button_submit)).perform(scrollTo(), click())

Expand All @@ -346,7 +346,7 @@ class CreditCardActivityTest {

onView(withId(R.id.edit_card_number)).perform(typeText("4242424242424242"))
onView(withId(R.id.edit_card_name)).perform(typeText("John Doe"))
onView(withId(R.id.edit_expiry_date)).perform(typeText("1234"))
onView(withId(R.id.edit_expiry_date)).perform(typeNumberText("1234"))
onView(withId(R.id.edit_security_code)).perform(typeNumberText("123"), closeSoftKeyboard())
onView(withId(R.id.edit_country)).perform(scrollTo(), click())
onView(withId(R.id.country_list))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ class ExpiryDateEditTextTest {
assertEquals(Unit, editText.validate())
}

@Test
fun validate_ignoreSpecialCharactersInput() {
"*,.".forEach { editText.append(it.toString()) }

assertEquals("", editText.text.toString())
"1234".forEach { editText.append(it.toString()) } // 12/34
assertEquals("12/34", editText.text.toString())
}

@Test(expected = InputValidationException.EmptyInputException::class)
fun validate_emptyValue() {
"".forEach { editText.append(it.toString()) }
Expand Down

0 comments on commit 356a79e

Please sign in to comment.