-
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.
Squashed 'libs/login/' changes from 43ac7e9468..2289f9707b
2289f9707b Merge commit '43ac7e9468ada491ccd1fcd63cfa1ef0616bd791' into issue/9720-login-subtree-update b5ac63f082 Fix redundancy in R.string.enter_site_address bc3903bd29 Update login_invalid_site_url 5d1b930343 Add tests for LoginSiteAddressValidator ae203df2ec Delete unused R.string.login_empty_site_url 763eeb9490 Replace login_invalid_site_url 1032574f3f Do not report an error if the text is empty 1fa189853a Site Address: Show an error message after 2 sec e869735555 Do not react to keyboard enter if site is invalid 8d50ab92c6 Move LoginSiteAddress validation to a class a8f234de25 Merge pull request #10150 from wordpress-mobile/amanda/woo-login-test-wp-2 6dbf72672a Updated login and editor libraries to remove right/left attributes when a start/end attribute already exists git-subtree-dir: libs/login git-subtree-split: 2289f9707bcac4e7138f5014de81292196d20e84
- Loading branch information
Showing
5 changed files
with
244 additions
and
26 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
74 changes: 74 additions & 0 deletions
74
WordPressLoginFlow/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(); | ||
} | ||
}); | ||
} | ||
} |