-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10255 from wordpress-mobile/issue/9720-site-url-v…
…alidation Add realtime site address validation
- Loading branch information
Showing
6 changed files
with
244 additions
and
27 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
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(); | ||
} | ||
}); | ||
} | ||
} |