Skip to content

Commit

Permalink
[ExposedDropdownMenu] Update dropdown background when setRawInputType…
Browse files Browse the repository at this point in the history
… is called.

PiperOrigin-RevId: 438829151
  • Loading branch information
leticiarossi authored and drchen committed Apr 5, 2022
1 parent 6fdc114 commit f31414a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Expand Up @@ -161,10 +161,13 @@ public <T extends ListAdapter & Filterable> void setAdapter(@Nullable T adapter)
@Override
public void setInputType(int type) {
super.setInputType(type);
TextInputLayout textInputLayout = findTextInputLayoutAncestor();
if (textInputLayout != null) {
textInputLayout.updateDropdownMenuBackground();
}
updateDropdownMenuBackground();
}

@Override
public void setRawInputType(int type) {
super.setRawInputType(type);
updateDropdownMenuBackground();
}

/**
Expand Down Expand Up @@ -282,6 +285,13 @@ private int measureContentWidth() {
return width;
}

private void updateDropdownMenuBackground() {
TextInputLayout textInputLayout = findTextInputLayoutAncestor();
if (textInputLayout != null) {
textInputLayout.updateDropdownMenuBackground();
}
}

@Nullable
private TextInputLayout findTextInputLayoutAncestor() {
ViewParent parent = getParent();
Expand Down
Expand Up @@ -893,4 +893,26 @@ public void perform(UiController uiController, View view) {
}
};
}

/** Sets the raw input type on an AutoCompleteTextView. */
public static ViewAction setRawInputType(final int inputType) {
return new ViewAction() {

@Override
public Matcher<View> getConstraints() {
return ViewMatchers.isAssignableFrom(AutoCompleteTextView.class);
}

@Override
public String getDescription() {
return "Sets raw input type.";
}

@Override
public void perform(UiController uiController, View view) {
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) view;
autoCompleteTextView.setRawInputType(inputType);
}
};
}
}
Expand Up @@ -29,6 +29,7 @@
import static com.google.android.material.testutils.TestUtilsActions.waitFor;
import static com.google.android.material.testutils.TextInputLayoutActions.clickIcon;
import static com.google.android.material.testutils.TextInputLayoutActions.setInputType;
import static com.google.android.material.testutils.TextInputLayoutActions.setRawInputType;
import static com.google.android.material.testutils.TextInputLayoutActions.skipAnimations;
import static com.google.android.material.testutils.TextInputLayoutMatchers.endIconHasContentDescription;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -105,6 +106,23 @@ public void testSwitchingInputType_updatesBackground() {
assertThat(editText.getBackground(), instanceOf(LayerDrawable.class));
}

@Test
public void testSwitchingRawInputType_updatesBackground() {
final Activity activity = activityTestRule.getActivity();
AutoCompleteTextView editText = activity.findViewById(R.id.edittext_filled);

// Switch to an editable type.
onView(withId(R.id.edittext_filled)).perform(setRawInputType(InputType.TYPE_CLASS_TEXT));
Drawable editableTypeBackground = editText.getBackground();
// Switch back to uneditable.
onView(withId(R.id.edittext_filled)).perform(setRawInputType(InputType.TYPE_NULL));

// Assert background updated.
assertThat(editableTypeBackground, not(instanceOf(LayerDrawable.class)));
// Assert second switch updated the background back to an instance of LayerDrawable.
assertThat(editText.getBackground(), instanceOf(LayerDrawable.class));
}

@Test
public void testEndIconClickShowsDropdownPopup() {
final Activity activity = activityTestRule.getActivity();
Expand Down

0 comments on commit f31414a

Please sign in to comment.