Commit ac498a16 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Refactor AssistantChip creation to allow reuse.

Before this CL, AssistantChips were always directly bound to methods in
the UiController. Now, their creation is abstracted such that they can
be bound to other callbacks and reused in other contexts.

Bug: b/171776026
Change-Id: I4b18f04f106c563332e3951733b75a279ce9647a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2524526
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarSandro Maggi <sandromaggi@google.com>
Cr-Commit-Position: refs/heads/master@{#826221}
parent 349c2740
...@@ -16,7 +16,6 @@ import org.chromium.chrome.autofill_assistant.R; ...@@ -16,7 +16,6 @@ import org.chromium.chrome.autofill_assistant.R;
import org.chromium.chrome.browser.ActivityTabProvider; import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.app.ChromeActivity; import org.chromium.chrome.browser.app.ChromeActivity;
import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantChip; import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantChip;
import org.chromium.chrome.browser.autofill_assistant.carousel.AssistantChip.Type;
import org.chromium.chrome.browser.autofill_assistant.metrics.DropOutReason; import org.chromium.chrome.browser.autofill_assistant.metrics.DropOutReason;
import org.chromium.chrome.browser.customtabs.CustomTabActivity; import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
...@@ -339,8 +338,10 @@ public class AutofillAssistantUiController { ...@@ -339,8 +338,10 @@ public class AutofillAssistantUiController {
@CalledByNative @CalledByNative
private AssistantChip createActionButton(int icon, String text, int actionIndex, private AssistantChip createActionButton(int icon, String text, int actionIndex,
boolean disabled, boolean sticky, boolean visible) { boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, icon, text, disabled, sticky, AssistantChip chip =
visible, () -> safeNativeOnUserActionSelected(actionIndex)); AssistantChip.createHairlineAssistantChip(icon, text, disabled, sticky, visible);
chip.setSelectedListener(() -> safeNativeOnUserActionSelected(actionIndex));
return chip;
} }
/** /**
...@@ -349,8 +350,10 @@ public class AutofillAssistantUiController { ...@@ -349,8 +350,10 @@ public class AutofillAssistantUiController {
@CalledByNative @CalledByNative
private AssistantChip createHighlightedActionButton(int icon, String text, int actionIndex, private AssistantChip createHighlightedActionButton(int icon, String text, int actionIndex,
boolean disabled, boolean sticky, boolean visible) { boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(Type.BUTTON_FILLED_BLUE, icon, text, disabled, sticky, visible, AssistantChip chip =
() -> safeNativeOnUserActionSelected(actionIndex)); AssistantChip.createHighlightedAssistantChip(icon, text, disabled, sticky, visible);
chip.setSelectedListener(() -> safeNativeOnUserActionSelected(actionIndex));
return chip;
} }
/** /**
...@@ -361,8 +364,10 @@ public class AutofillAssistantUiController { ...@@ -361,8 +364,10 @@ public class AutofillAssistantUiController {
@CalledByNative @CalledByNative
private AssistantChip createCancelButton(int icon, String text, int actionIndex, private AssistantChip createCancelButton(int icon, String text, int actionIndex,
boolean disabled, boolean sticky, boolean visible) { boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, icon, text, disabled, sticky, AssistantChip chip =
visible, () -> safeNativeOnCancelButtonClicked(actionIndex)); AssistantChip.createHairlineAssistantChip(icon, text, disabled, sticky, visible);
chip.setSelectedListener(() -> safeNativeOnCancelButtonClicked(actionIndex));
return chip;
} }
/** /**
...@@ -371,8 +376,10 @@ public class AutofillAssistantUiController { ...@@ -371,8 +376,10 @@ public class AutofillAssistantUiController {
@CalledByNative @CalledByNative
private AssistantChip createCloseButton( private AssistantChip createCloseButton(
int icon, String text, boolean disabled, boolean sticky, boolean visible) { int icon, String text, boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, icon, text, disabled, sticky, AssistantChip chip =
visible, this::safeNativeOnCloseButtonClicked); AssistantChip.createHairlineAssistantChip(icon, text, disabled, sticky, visible);
chip.setSelectedListener(() -> safeNativeOnCloseButtonClicked());
return chip;
} }
@CalledByNative @CalledByNative
......
...@@ -75,7 +75,7 @@ public class AssistantChip { ...@@ -75,7 +75,7 @@ public class AssistantChip {
private final boolean mSticky; private final boolean mSticky;
/** The callback that will be triggered when this chip is clicked. */ /** The callback that will be triggered when this chip is clicked. */
private final Runnable mSelectedListener; private Runnable mSelectedListener;
/** /**
* The list of popup items to show when the chip is tapped. When specified, the regular {@code * The list of popup items to show when the chip is tapped. When specified, the regular {@code
...@@ -87,13 +87,19 @@ public class AssistantChip { ...@@ -87,13 +87,19 @@ public class AssistantChip {
private @Nullable Callback<Integer> mOnPopupItemSelected; private @Nullable Callback<Integer> mOnPopupItemSelected;
public AssistantChip(@Type int type, @Icon int icon, String text, boolean disabled, public AssistantChip(@Type int type, @Icon int icon, String text, boolean disabled,
boolean sticky, boolean visible, Runnable selectedListener) { boolean sticky, boolean visible) {
mType = type; mType = type;
mIcon = icon; mIcon = icon;
mText = text; mText = text;
mDisabled = disabled; mDisabled = disabled;
mSticky = sticky; mSticky = sticky;
mVisible = visible; mVisible = visible;
}
public AssistantChip(@Type int type, @Icon int icon, String text, boolean disabled,
boolean sticky, boolean visible, Runnable selectedListener) {
this(type, icon, text, disabled, sticky, visible);
assert selectedListener != null;
mSelectedListener = selectedListener; mSelectedListener = selectedListener;
} }
...@@ -129,10 +135,14 @@ public class AssistantChip { ...@@ -129,10 +135,14 @@ public class AssistantChip {
return mSticky; return mSticky;
} }
public Runnable getSelectedListener() { public @Nullable Runnable getSelectedListener() {
return mSelectedListener; return mSelectedListener;
} }
public void setSelectedListener(Runnable selectedListener) {
mSelectedListener = selectedListener;
}
public void setPopupItems(List<String> popupItems, Callback<Integer> onSelectedCallback) { public void setPopupItems(List<String> popupItems, Callback<Integer> onSelectedCallback) {
mPopupItems = popupItems; mPopupItems = popupItems;
mOnPopupItemSelected = onSelectedCallback; mOnPopupItemSelected = onSelectedCallback;
...@@ -157,4 +167,23 @@ public class AssistantChip { ...@@ -157,4 +167,23 @@ public class AssistantChip {
&& this.getIcon() == that.getIcon() && this.isSticky() == that.isSticky() && this.getIcon() == that.getIcon() && this.isSticky() == that.isSticky()
&& this.isDisabled() == that.isDisabled() && this.isVisible() == that.isVisible(); && this.isDisabled() == that.isDisabled() && this.isVisible() == that.isVisible();
} }
/**
* Creates a hairline assistant chip with an empty callback. The callback needs to be bound
* before the view is inflated.
*/
public static AssistantChip createHairlineAssistantChip(
int icon, String text, boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(
AssistantChip.Type.BUTTON_HAIRLINE, icon, text, disabled, sticky, visible);
}
/**
* Creates a blue-filled assistant chip with an empty callback. The callback needs to be bound
* before the view is inflated.
*/
public static AssistantChip createHighlightedAssistantChip(
int icon, String text, boolean disabled, boolean sticky, boolean visible) {
return new AssistantChip(Type.BUTTON_FILLED_BLUE, icon, text, disabled, sticky, visible);
}
} }
...@@ -96,7 +96,7 @@ public class AutofillAssistantActionsCarouselUiTest { ...@@ -96,7 +96,7 @@ public class AutofillAssistantActionsCarouselUiTest {
-> model.set(AssistantCarouselModel.CHIPS, -> model.set(AssistantCarouselModel.CHIPS,
Collections.singletonList(new AssistantChip( Collections.singletonList(new AssistantChip(
AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Test", false, true, true, null)))); "Test", false, true, true))));
// Chip was created and is displayed on the screen. // Chip was created and is displayed on the screen.
onView(is(coordinator.getView())) onView(is(coordinator.getView()))
...@@ -117,10 +117,10 @@ public class AutofillAssistantActionsCarouselUiTest { ...@@ -117,10 +117,10 @@ public class AutofillAssistantActionsCarouselUiTest {
List<AssistantChip> chips = new ArrayList<>(); List<AssistantChip> chips = new ArrayList<>();
for (int i = 0; i < numChips; i++) { for (int i = 0; i < numChips; i++) {
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"T" + i, false, false, true, null)); "T" + i, false, false, true));
} }
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"X", false, true, true, null)); "X", false, true, true));
TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips)); TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips));
...@@ -145,10 +145,10 @@ public class AutofillAssistantActionsCarouselUiTest { ...@@ -145,10 +145,10 @@ public class AutofillAssistantActionsCarouselUiTest {
List<AssistantChip> chips = new ArrayList<>(); List<AssistantChip> chips = new ArrayList<>();
for (int i = 0; i < numChips; i++) { for (int i = 0; i < numChips; i++) {
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Test" + i, false, false, true, null)); "Test" + i, false, false, true));
} }
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Cancel", false, true, true, null)); "Cancel", false, true, true));
TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips)); TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips));
// Cancel chip is initially displayed to the user. // Cancel chip is initially displayed to the user.
...@@ -174,9 +174,9 @@ public class AutofillAssistantActionsCarouselUiTest { ...@@ -174,9 +174,9 @@ public class AutofillAssistantActionsCarouselUiTest {
List<AssistantChip> chips = new ArrayList<>(); List<AssistantChip> chips = new ArrayList<>();
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Test 2", false, false, true, null)); "Test 2", false, false, true));
chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, chips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Cancel", false, true, true, null)); "Cancel", false, true, true));
TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips)); TestThreadUtils.runOnUiThreadBlocking(() -> model.set(AssistantCarouselModel.CHIPS, chips));
onView(withText("Cancel")).check(matches(isDisplayed())); onView(withText("Cancel")).check(matches(isDisplayed()));
onView(withText("Test 2")).check(matches(isDisplayed())); onView(withText("Test 2")).check(matches(isDisplayed()));
...@@ -185,7 +185,7 @@ public class AutofillAssistantActionsCarouselUiTest { ...@@ -185,7 +185,7 @@ public class AutofillAssistantActionsCarouselUiTest {
List<AssistantChip> newChips = new ArrayList<>(); List<AssistantChip> newChips = new ArrayList<>();
newChips.add(chips.get(0)); newChips.add(chips.get(0));
newChips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE, newChips.add(new AssistantChip(AssistantChip.Type.BUTTON_HAIRLINE, AssistantChip.Icon.NONE,
"Test 1", false, false, true, null)); "Test 1", false, false, true));
newChips.add(chips.get(1)); newChips.add(chips.get(1));
TestThreadUtils.runOnUiThreadBlocking( TestThreadUtils.runOnUiThreadBlocking(
() -> model.set(AssistantCarouselModel.CHIPS, newChips)); () -> model.set(AssistantCarouselModel.CHIPS, newChips));
......
...@@ -208,7 +208,7 @@ public class AutofillAssistantHeaderUiTest { ...@@ -208,7 +208,7 @@ public class AutofillAssistantHeaderUiTest {
String chipText = "Hello World"; String chipText = "Hello World";
AssistantChip chip = AssistantChip chip =
new AssistantChip(AssistantChip.Type.BUTTON_FILLED_BLUE, Icon.DONE, chipText, new AssistantChip(AssistantChip.Type.BUTTON_FILLED_BLUE, Icon.DONE, chipText,
/* disabled= */ false, /* sticky= */ false, /* visible= */ true, () -> {}); /* disabled= */ false, /* sticky= */ false, /* visible= */ true);
// Set the header chip without displaying it. // Set the header chip without displaying it.
List<AssistantChip> chips = new ArrayList<>(); List<AssistantChip> chips = new ArrayList<>();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment