Skip to content

Commit

Permalink
Scroll to elements that are off-screen in PasswordSettingsTest
Browse files Browse the repository at this point in the history
A few test cases require access to list elements that can be off-screen.
Those tests should first attempt to scroll to the required element, so
that its corresponding view actually exists in the hierarchy.

Bug: 1129884
Change-Id: Ifa5df8bdd26823c244339197d70806843763cd13
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2418672
Commit-Queue: Ioana Pandele <ioanap@chromium.org>
Reviewed-by: Friedrich [CET] <fhorschig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808376}
  • Loading branch information
Ioana Pandele authored and Commit Bot committed Sep 18, 2020
1 parent 40a9b51 commit 091af51
Showing 1 changed file with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.RecyclerViewActions.scrollToHolder;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.Intents.intending;
import static androidx.test.espresso.intent.matcher.BundleMatchers.hasEntry;
Expand Down Expand Up @@ -67,6 +68,8 @@
import androidx.annotation.StringRes;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceViewHolder;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import androidx.test.espresso.Espresso;
import androidx.test.espresso.intent.Intents;
import androidx.test.espresso.intent.rule.IntentsTestRule;
Expand Down Expand Up @@ -155,6 +158,27 @@ public class PasswordSettingsTest {
@Mock
private PasswordCheck mPasswordCheck;

/**
* @param text The text that the view holder has in its view hierarchy.
* @return A Matcher to find a particular {@link ViewHolder} that contains certain text.
*/
private static Matcher<ViewHolder> hasTextInViewHolder(String text) {
return new BoundedMatcher<ViewHolder, PreferenceViewHolder>(PreferenceViewHolder.class) {
@Override
public void describeTo(Description description) {
description.appendText("has text: " + text);
}

@Override
protected boolean matchesSafely(PreferenceViewHolder preferenceViewHolder) {
ArrayList<View> outViews = new ArrayList<>();
preferenceViewHolder.itemView.findViewsWithText(
outViews, text, View.FIND_VIEWS_WITH_TEXT);
return !outViews.isEmpty();
}
};
}

private static final class FakePasswordManagerHandler implements PasswordManagerHandler {
// This class has exactly one observer, set on construction and expected to last at least as
// long as this object (a good candidate is the owner of this object).
Expand Down Expand Up @@ -847,7 +871,7 @@ public void testCheckPasswordsDisabled() {
@Test
@SmallTest
@Feature({"Preferences"})
@EnableFeatures(ChromeFeatureList.EDIT_PASSWORDS_IN_SETTINGS)
@EnableFeatures({ChromeFeatureList.EDIT_PASSWORDS_IN_SETTINGS})
public void testSelectedStoredPasswordIndexIsSameAsInShowPasswordEntryEditingView() {
PasswordEditingDelegateProvider.getInstance().setPasswordEditingDelegate(
mMockPasswordEditingDelegate);
Expand All @@ -857,7 +881,8 @@ public void testSelectedStoredPasswordIndexIsSameAsInShowPasswordEntryEditingVie
new SavedPasswordEntry("https://test.com", "test user", "test password")});

startPasswordSettingsFromMainSettings();

Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());

Espresso.onView(withEditMenuIdOrText()).perform(click());
Expand Down Expand Up @@ -902,6 +927,8 @@ public void testPasswordEditingMethodWasCalled() throws Exception {

startPasswordSettingsFromMainSettings();

Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());

Espresso.onView(withEditMenuIdOrText()).perform(click());
Expand Down Expand Up @@ -931,6 +958,8 @@ public void testChangeOfStoredPasswordDataIsPropagated() throws Exception {

startPasswordSettingsFromMainSettings();

Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());

Espresso.onView(withEditMenuIdOrText()).perform(click());
Expand All @@ -946,6 +975,8 @@ public void testChangeOfStoredPasswordDataIsPropagated() throws Exception {

Espresso.pressBack();
// Check if the password preferences activity has the updated data in the list of passwords.
Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user new")));
Espresso.onView(withText("test user new")).check(matches(isDisplayed()));
}

Expand Down Expand Up @@ -1754,6 +1785,8 @@ public void testViewPasswordNoLock() {
final SettingsActivity settingsActivity = startPasswordSettingsFromMainSettings();

View mainDecorView = settingsActivity.getWindow().getDecorView();
Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());
Espresso.onView(withContentDescription(R.string.password_entry_viewer_copy_stored_password))
.perform(click());
Expand All @@ -1777,7 +1810,8 @@ public void testViewPassword() {
ReauthenticationManager.OverrideState.AVAILABLE);

startPasswordSettingsFromMainSettings();

Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());

// Before tapping the view button, pretend that the last successful reauthentication just
Expand Down Expand Up @@ -1823,7 +1857,8 @@ public void testEditSavedPasswordIconVisibleInActionBarWithFeature() {
new SavedPasswordEntry("https://example.com", "test user", "test password"));

startPasswordSettingsFromMainSettings();

Espresso.onView(withId(R.id.recycler_view))
.perform(scrollToHolder(hasTextInViewHolder("test user")));
Espresso.onView(withText(containsString("test user"))).perform(click());

Espresso.onView(withEditMenuIdOrText()).check(matches(isDisplayed()));
Expand Down

0 comments on commit 091af51

Please sign in to comment.