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

[Autofill Assistant] Added SetViewEnabled interaction.

Bug: b/145043394
Change-Id: Ife128befc4c910b89accd97e505c3864fff51452
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2145888
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarSandro Maggi <sandromaggi@google.com>
Cr-Commit-Position: refs/heads/master@{#759196}
parent 6f2a925f
......@@ -41,7 +41,7 @@ public class AssistantViewFactory {
public static void setViewAttributes(View view, Context context, int paddingStart,
int paddingTop, int paddingEnd, int paddingBottom,
@Nullable AssistantDrawable background, @Nullable String contentDescription,
boolean visible) {
boolean visible, boolean enabled) {
view.setPaddingRelative(AssistantDimension.getPixelSizeDp(context, paddingStart),
AssistantDimension.getPixelSizeDp(context, paddingTop),
AssistantDimension.getPixelSizeDp(context, paddingEnd),
......@@ -55,6 +55,7 @@ public class AssistantViewFactory {
}
setAccessibility(view, contentDescription);
view.setVisibility(visible ? View.VISIBLE : View.GONE);
view.setEnabled(enabled);
}
/**
......
......@@ -116,6 +116,13 @@ public class AssistantViewInteractions {
}
}
@CalledByNative
private static void setViewEnabled(View view, AssistantValue enabled) {
if (enabled.getBooleans() != null && enabled.getBooleans().length == 1) {
view.setEnabled(enabled.getBooleans()[0]);
}
}
@CalledByNative
private static boolean setToggleButtonChecked(View view, AssistantValue checked) {
if (!(view instanceof AssistantToggleButton)) {
......
......@@ -93,6 +93,7 @@ import org.chromium.chrome.browser.autofill_assistant.proto.ProcessedActionStatu
import org.chromium.chrome.browser.autofill_assistant.proto.PromptProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SetModelValueProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SetUserActionsProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SetViewEnabledProto;
import org.chromium.chrome.browser.autofill_assistant.proto.SetViewVisibilityProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ShapeDrawableProto;
import org.chromium.chrome.browser.autofill_assistant.proto.ShowCalendarPopupProto;
......@@ -2058,4 +2059,95 @@ public class AutofillAssistantGenericUiTest {
BooleanList.newBuilder().addValues(false)))
.build()));
}
}
/**
* Shows two textviews. Repeated clicks on the second one will enable/disable the first one.
*/
@Test
@MediumTest
public void testEnableDisableView() {
List<InteractionProto> interactions = new ArrayList<>();
interactions.add(
(InteractionProto) InteractionProto.newBuilder()
.setTriggerEvent(EventProto.newBuilder().setOnViewClicked(
OnViewClickedEventProto.newBuilder().setViewIdentifier(
"toggle_view")))
.addCallbacks(CallbackProto.newBuilder().setComputeValue(
ComputeValueProto.newBuilder()
.setResultModelIdentifier("enabled")
.setBooleanNot(BooleanNotProto.newBuilder().setValue(
ValueReferenceProto.newBuilder().setModelIdentifier(
"enabled")))))
.build());
interactions.add(
(InteractionProto) InteractionProto.newBuilder()
.setTriggerEvent(EventProto.newBuilder().setOnValueChanged(
OnModelValueChangedEventProto.newBuilder().setModelIdentifier(
"enabled")))
.addCallbacks(CallbackProto.newBuilder().setSetViewEnabled(
SetViewEnabledProto.newBuilder()
.setViewIdentifier("text_view")
.setEnabled(
ValueReferenceProto.newBuilder().setModelIdentifier(
"enabled"))))
.build());
// Disable text_view initially.
List<ModelProto.ModelValue> modelValues = new ArrayList<>();
modelValues.add((ModelProto.ModelValue) ModelProto.ModelValue.newBuilder()
.setIdentifier("enabled")
.setValue(ValueProto.newBuilder().setBooleans(
BooleanList.newBuilder().addValues(false)))
.build());
GenericUserInterfaceProto genericUserInterface =
(GenericUserInterfaceProto) GenericUserInterfaceProto.newBuilder()
.setRootView(ViewProto.newBuilder().setViewContainer(
ViewContainerProto.newBuilder()
.setLinearLayout(
LinearLayoutProto.newBuilder().setOrientation(
LinearLayoutProto.Orientation.VERTICAL))
.addViews(
ViewProto.newBuilder()
.setIdentifier("toggle_view")
.setTextView(
TextViewProto.newBuilder().setText(
"toggle view")))
.addViews(
ViewProto.newBuilder()
.setIdentifier("text_view")
.setTextView(
TextViewProto.newBuilder().setText(
"text view")))))
.setInteractions(
InteractionsProto.newBuilder().addAllInteractions(interactions))
.setModel(ModelProto.newBuilder().addAllValues(modelValues))
.build();
ArrayList<ActionProto> list = new ArrayList<>();
list.add((ActionProto) ActionProto.newBuilder()
.setShowGenericUi(ShowGenericUiProto.newBuilder().setGenericUserInterface(
genericUserInterface))
.build());
AutofillAssistantTestScript script = new AutofillAssistantTestScript(
(SupportedScriptProto) SupportedScriptProto.newBuilder()
.setPath("autofill_assistant_target_website.html")
.setPresentation(PresentationProto.newBuilder().setAutostart(true).setChip(
ChipProto.newBuilder().setText("Autostart")))
.build(),
list);
AutofillAssistantTestService testService =
new AutofillAssistantTestService(Collections.singletonList(script));
startAutofillAssistant(mTestRule.getActivity(), testService);
waitUntilViewMatchesCondition(withText("toggle view"), isCompletelyDisplayed());
onView(withText("text view")).check(matches(not(isEnabled())));
onView(withText("toggle view")).perform(click());
onView(withText("text view")).check(matches(isEnabled()));
onView(withText("toggle view")).perform(click());
onView(withText("text view")).check(matches(not(isEnabled())));
}
}
\ No newline at end of file
......@@ -309,7 +309,7 @@ base::android::ScopedJavaGlobalRef<jobject> CreateJavaView(
? base::android::ConvertUTF8ToJavaString(
env, proto.attributes().content_description())
: nullptr,
proto.attributes().visible());
proto.attributes().visible(), proto.attributes().enabled());
}
if (proto.has_layout_params()) {
Java_AssistantViewFactory_setViewLayoutParams(
......
......@@ -272,6 +272,35 @@ void SetViewVisibility(
ui_controller_android_utils::ToJavaValue(env, *visible_value));
}
void SetViewEnabled(
base::WeakPtr<UserModel> user_model,
const SetViewEnabledProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views) {
if (!user_model) {
return;
}
auto jview = views->find(proto.view_identifier());
if (jview == views->end()) {
DVLOG(2) << "Failed to enable/disable view " << proto.view_identifier()
<< ": view not found";
return;
}
auto enabled_value = user_model->GetValue(proto.enabled());
if (!enabled_value.has_value() ||
enabled_value->booleans().values_size() != 1) {
DVLOG(2) << "Failed to enable/disable view " << proto.view_identifier()
<< ": " << proto.enabled() << " did not contain single boolean";
return;
}
JNIEnv* env = base::android::AttachCurrentThread();
Java_AssistantViewInteractions_setViewEnabled(
env, jview->second,
ui_controller_android_utils::ToJavaValue(env, *enabled_value));
}
void RunConditionalCallback(
base::WeakPtr<BasicInteractions> basic_interactions,
const std::string& condition_identifier,
......
......@@ -67,6 +67,12 @@ void SetViewVisibility(
const SetViewVisibilityProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// Enables or disables a view.
void SetViewEnabled(
base::WeakPtr<UserModel> user_model,
const SetViewEnabledProto& proto,
std::map<std::string, base::android::ScopedJavaGlobalRef<jobject>>* views);
// A simple wrapper around a basic interaction, needed because we can't directly
// bind a repeating callback to a method with non-void return value.
void RunConditionalCallback(
......
......@@ -194,6 +194,21 @@ CreateInteractionCallbackFromProto(
base::BindRepeating(&android_interactions::SetViewVisibility,
user_model->GetWeakPtr(),
proto.set_view_visibility(), views));
case CallbackProto::kSetViewEnabled:
if (proto.set_view_enabled().view_identifier().empty()) {
VLOG(1) << "Error creating SetViewEnabled interaction: "
"view_identifier not set";
return base::nullopt;
}
if (!proto.set_view_enabled().has_enabled()) {
VLOG(1) << "Error creating SetViewEnabled interaction: "
"enabled not set";
return base::nullopt;
}
return base::Optional<InteractionHandlerAndroid::InteractionCallback>(
base::BindRepeating(&android_interactions::SetViewEnabled,
user_model->GetWeakPtr(),
proto.set_view_enabled(), views));
case CallbackProto::KIND_NOT_SET:
VLOG(1) << "Error creating interaction: kind not set";
return base::nullopt;
......
......@@ -38,6 +38,7 @@ message CallbackProto {
SetTextProto set_text = 8;
ToggleUserActionProto toggle_user_action = 9;
SetViewVisibilityProto set_view_visibility = 11;
SetViewEnabledProto set_view_enabled = 12;
}
// Optional model identifier pointing to a single boolean. If set, the
// callback will only be invoked if the condition is true.
......@@ -276,3 +277,13 @@ message SetViewVisibilityProto {
reserved 2;
}
// Enables or disables a view. Note that disabled views won't react to click
// events.
message SetViewEnabledProto {
// The view to modify.
optional string view_identifier = 1;
// Whether the view should be enabled or not. Must point to a single boolean.
optional ValueReferenceProto enabled = 2;
}
......@@ -100,6 +100,8 @@ message ViewAttributesProto {
optional string content_description = 6;
// Whether the view is initially visible or not.
optional bool visible = 7 [default = true];
// Whether the view is initially enabled or not.
optional bool enabled = 8 [default = true];
}
// Parameters configuring how views attach to their parents.
......
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