There's this sample repository, which showcases different Android testing practices. One of the apps is android-testing/unit/BasicSample, which is very simple and, among other things, checks the email field and for that purpose adds a listener like this:

mEmailText.addTextChangedListener(mEmailValidator);

https://github.com/googlesamples/android-testing/blob/923a02c2bee18e6315c5a71c8352a4c252cfa862/unit/BasicSample/app/src/main/java/com/example/android/testing/unittesting/BasicSample/MainActivity.java#L68

And then on save it checks

     if (!mEmailValidator.isValid()) {  
                 mEmailText.setError("Invalid email");
      }

https://github.com/googlesamples/android-testing/blob/923a02c2bee18e6315c5a71c8352a4c252cfa862/unit/BasicSample/app/src/main/java/com/example/android/testing/unittesting/BasicSample/MainActivity.java#L95

Why not just have a simple utility class which will perform this check only when the Save button is pressed and not on each character change?

up vote 3 down vote accepted

Because it can.

It's not overly taxing for the app to do this.

Perhaps there are other places where emails are validated, with UI that responds to the validation status changing. Having just one EmailValidator that listens to TextChange events and produces ValidationChange events is better than duplicating half of that work for a single check.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.