-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create EmailEditText to validate email input in BecsDebitWidget (#2294)
Summary `EmailEditText` validates that input is not blank and a valid email address. Testing Add unit tests and manually verify
- Loading branch information
1 parent
c6512a9
commit 0a3a76d
Showing
5 changed files
with
74 additions
and
7 deletions.
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
28 changes: 28 additions & 0 deletions
28
stripe/src/main/java/com/stripe/android/view/EmailEditText.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,28 @@ | ||
package com.stripe.android.view | ||
|
||
import android.content.Context | ||
import android.util.AttributeSet | ||
import android.util.Patterns | ||
import com.stripe.android.R | ||
|
||
internal class EmailEditText @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle | ||
) : StripeEditText(context, attrs, defStyleAttr) { | ||
|
||
val email: String? | ||
get() { | ||
errorMessage = when { | ||
fieldText.isBlank() -> { | ||
resources.getString(R.string.becs_widget_email_required) | ||
} | ||
!Patterns.EMAIL_ADDRESS.matcher(fieldText).matches() -> { | ||
resources.getString(R.string.becs_widget_email_invalid) | ||
} | ||
else -> null | ||
} | ||
|
||
return fieldText.takeIf { errorMessage == null } | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
stripe/src/test/java/com/stripe/android/view/EmailEditTextTest.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,40 @@ | ||
package com.stripe.android.view | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import com.google.common.truth.Truth.assertThat | ||
import kotlin.test.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class EmailEditTextTest { | ||
private val emailEditText = EmailEditText( | ||
ApplicationProvider.getApplicationContext() | ||
) | ||
|
||
@Test | ||
fun email_withEmptyEmail_shouldSetError() { | ||
assertThat(emailEditText.email) | ||
.isNull() | ||
assertThat(emailEditText.errorMessage) | ||
.isEqualTo("Your email address is required.") | ||
} | ||
|
||
@Test | ||
fun email_withIncompleteEmail_shouldSetError() { | ||
emailEditText.setText("jenny@") | ||
assertThat(emailEditText.email) | ||
.isNull() | ||
assertThat(emailEditText.errorMessage) | ||
.isEqualTo("Your email address is invalid.") | ||
} | ||
|
||
@Test | ||
fun email_withValidEmail_shouldNotSetError() { | ||
emailEditText.setText("jrosen@example.com") | ||
assertThat(emailEditText.email) | ||
.isEqualTo("jrosen@example.com") | ||
assertThat(emailEditText.errorMessage) | ||
.isNull() | ||
} | ||
} |