-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 realtime site address validation #10255
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe396ca
Move LoginSiteAddress validation to a class
shiki 00fdc98
Do not react to keyboard enter if site is invalid
shiki 3020aec
Site Address: Show an error message after 2 sec
shiki 244ffe2
Do not report an error if the text is empty
shiki 495db16
Replace login_invalid_site_url
shiki d3d4752
Delete unused R.string.login_empty_site_url
shiki e350913
Add tests for LoginSiteAddressValidator
shiki 69661d1
Update login_invalid_site_url
shiki 2401e41
Fix redundancy in R.string.enter_site_address
shiki 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
74 changes: 74 additions & 0 deletions
74
...rdPressLoginFlow/src/main/java/org/wordpress/android/login/LoginSiteAddressValidator.java
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,74 @@ | ||
package org.wordpress.android.login; | ||
|
||
import android.util.Patterns; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
|
||
import org.wordpress.android.util.helpers.Debouncer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Encapsulates the site address validation, cleaning, and error reporting of {@link LoginSiteAddressFragment}. | ||
*/ | ||
class LoginSiteAddressValidator { | ||
private static final int SECONDS_DELAY_BEFORE_SHOWING_ERROR_MESSAGE = 2; | ||
|
||
private MutableLiveData<Boolean> mIsValid = new MutableLiveData<>(); | ||
private MutableLiveData<Integer> mErrorMessageResId = new MutableLiveData<>(); | ||
|
||
private String mCleanedSiteAddress = ""; | ||
private final Debouncer mDebouncer; | ||
|
||
@NonNull LiveData<Boolean> getIsValid() { | ||
return mIsValid; | ||
} | ||
|
||
@NonNull LiveData<Integer> getErrorMessageResId() { | ||
return mErrorMessageResId; | ||
} | ||
|
||
@NonNull String getCleanedSiteAddress() { | ||
return mCleanedSiteAddress; | ||
} | ||
|
||
LoginSiteAddressValidator() { | ||
this(new Debouncer()); | ||
} | ||
|
||
LoginSiteAddressValidator(@NonNull Debouncer debouncer) { | ||
mIsValid.setValue(false); | ||
mDebouncer = debouncer; | ||
} | ||
|
||
void dispose() { | ||
mDebouncer.shutdown(); | ||
} | ||
|
||
void setAddress(@NonNull String siteAddress) { | ||
mCleanedSiteAddress = cleanSiteAddress(siteAddress); | ||
final boolean isValid = siteAddressIsValid(mCleanedSiteAddress); | ||
|
||
mIsValid.setValue(isValid); | ||
mErrorMessageResId.setValue(null); | ||
|
||
// Call debounce regardless if there was an error so that the previous Runnable will be cancelled. | ||
mDebouncer.debounce(Void.class, new Runnable() { | ||
@Override public void run() { | ||
if (!isValid && !mCleanedSiteAddress.isEmpty()) { | ||
mErrorMessageResId.postValue(R.string.login_invalid_site_url); | ||
} | ||
} | ||
}, SECONDS_DELAY_BEFORE_SHOWING_ERROR_MESSAGE, TimeUnit.SECONDS); | ||
} | ||
|
||
private static String cleanSiteAddress(@NonNull String siteAddress) { | ||
return siteAddress.trim().replaceAll("[\r\n]", ""); | ||
} | ||
|
||
private static boolean siteAddressIsValid(@NonNull String cleanedSiteAddress) { | ||
return Patterns.WEB_URL.matcher(cleanedSiteAddress).matches(); | ||
} | ||
} |
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
129 changes: 129 additions & 0 deletions
129
...essLoginFlow/src/test/java/org/wordpress/android/login/LoginSiteAddressValidatorTest.java
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,129 @@ | ||
package org.wordpress.android.login; | ||
|
||
import androidx.arch.core.executor.testing.InstantTaskExecutorRule; | ||
import androidx.lifecycle.Observer; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.wordpress.android.util.helpers.Debouncer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.doAnswer; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class LoginSiteAddressValidatorTest { | ||
@Rule | ||
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule(); | ||
|
||
private Debouncer mDebouncer; | ||
private LoginSiteAddressValidator mValidator; | ||
|
||
@Before | ||
public void setUp() { | ||
mDebouncer = mock(Debouncer.class); | ||
doAnswer(new Answer<Void>() { | ||
@Override public Void answer(InvocationOnMock invocation) { | ||
final Runnable runnable = invocation.getArgument(1); | ||
runnable.run(); | ||
return null; | ||
} | ||
}).when(mDebouncer).debounce(any(), any(Runnable.class), anyLong(), any(TimeUnit.class)); | ||
|
||
mValidator = new LoginSiteAddressValidator(mDebouncer); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
mValidator = null; | ||
mDebouncer = null; | ||
} | ||
|
||
@Test | ||
public void testAnErrorIsReturnedWhenGivenAnInvalidAddress() { | ||
// Arrange | ||
assertThat(mValidator.getErrorMessageResId().getValue()).isNull(); | ||
|
||
// Act | ||
mValidator.setAddress("invalid"); | ||
|
||
// Assert | ||
assertThat(mValidator.getErrorMessageResId().getValue()).isNotNull(); | ||
assertThat(mValidator.getCleanedSiteAddress()).isEqualTo("invalid"); | ||
assertThat(mValidator.getIsValid().getValue()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testNoErrorIsReturnedButIsInvalidWhenGivenAnEmptyAddress() { | ||
// Act | ||
mValidator.setAddress(""); | ||
|
||
// Assert | ||
assertThat(mValidator.getErrorMessageResId().getValue()).isNull(); | ||
assertThat(mValidator.getIsValid().getValue()).isFalse(); | ||
assertThat(mValidator.getCleanedSiteAddress()).isEqualTo(""); | ||
} | ||
|
||
@Test | ||
public void testTheErrorIsImmediatelyClearedWhenANewAddressIsGiven() { | ||
// Arrange | ||
final ArrayList<Optional<Integer>> resIdValues = new ArrayList<>(); | ||
mValidator.getErrorMessageResId().observeForever(new Observer<Integer>() { | ||
@Override public void onChanged(Integer resId) { | ||
resIdValues.add(Optional.ofNullable(resId)); | ||
} | ||
}); | ||
|
||
// Act | ||
mValidator.setAddress("invalid"); | ||
mValidator.setAddress("another-invalid"); | ||
|
||
// Assert | ||
assertThat(resIdValues).hasSize(4); | ||
assertThat(resIdValues.get(0)).isEmpty(); | ||
assertThat(resIdValues.get(1)).isNotEmpty(); | ||
assertThat(resIdValues.get(2)).isEmpty(); | ||
assertThat(resIdValues.get(3)).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
public void testItReturnsValidWhenGivenValidURLs() { | ||
// Arrange | ||
final List<String> validUrls = Arrays.asList( | ||
"http://subdomain.example.com", | ||
"http://example.ca", | ||
"example.ca", | ||
"subdomain.example.com", | ||
" space-with-subdomain.example.net", | ||
"https://subdomain.example.com/folder", | ||
"http://subdomain.example.com/folder/over/there ", | ||
"7.7.7.7", | ||
"http://7.7.13.45", | ||
"http://47.147.43.45/folder "); | ||
|
||
// Act and Assert | ||
assertThat(validUrls).allSatisfy(new Consumer<String>() { | ||
@Override public void accept(String url) { | ||
mValidator.setAddress(url); | ||
|
||
assertThat(mValidator.getErrorMessageResId().getValue()).isNull(); | ||
assertThat(mValidator.getIsValid().getValue()).isTrue(); | ||
} | ||
}); | ||
} | ||
} |
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.
Auto-formatted. ¯\_(ツ)_/¯