Commit 40395f3e authored by sandromaggi's avatar sandromaggi Committed by Commit Bot

[Autofill Assistant] Add ComputeProto for StringEmpty

This adds a new option for computing a value by checking
whether or not a string value is empty.

Bug: None
Change-Id: I2f1a18cde4dd9add1e300621d70c27ebe61369b2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2537353
Commit-Queue: Sandro Maggi <sandromaggi@google.com>
Reviewed-by: default avatarClemens Arbesser <arbesser@google.com>
Cr-Commit-Position: refs/heads/master@{#828223}
parent ab6115c9
......@@ -385,6 +385,27 @@ bool CreateLoginOptionResponse(UserModel* user_model,
return true;
}
bool StringEmpty(UserModel* user_model,
const std::string& result_model_identifier,
const StringEmptyProto& proto) {
auto value = user_model->GetValue(proto.value());
if (!value.has_value()) {
DVLOG(2) << "Failed to find value in user model";
return false;
}
if (value->strings().values().size() != 1) {
DVLOG(2) << "Error evaluating " << __func__
<< ": expected single string, but got " << *value;
return false;
}
user_model->SetValue(result_model_identifier,
SimpleValue(value->strings().values(0).empty(),
ContainsClientOnlyValue({*value})));
return true;
}
} // namespace
base::WeakPtr<BasicInteractions> BasicInteractions::GetWeakPtr() {
......@@ -479,6 +500,14 @@ bool BasicInteractions::ComputeValue(const ComputeValueProto& proto) {
proto.result_model_identifier(),
proto.create_login_option_response());
break;
case ComputeValueProto::kStringEmpty:
if (!proto.string_empty().has_value()) {
DVLOG(2)
<< "Error computing ComputeValue::StringEmpty: no value specified";
return false;
}
return StringEmpty(delegate_->GetUserModel(),
proto.result_model_identifier(), proto.string_empty());
case ComputeValueProto::KIND_NOT_SET:
DVLOG(2) << "Error computing value: kind not set";
return false;
......
......@@ -633,6 +633,32 @@ TEST_F(BasicInteractionsTest, ComputeValueCreateLoginOptionResponse) {
EXPECT_EQ(user_model_.GetValue("result"), expected_response_value);
}
TEST_F(BasicInteractionsTest, ComputeStringEmpty) {
ComputeValueProto proto;
proto.set_result_model_identifier("result");
proto.mutable_string_empty()->mutable_value()->set_model_identifier("value");
// Missing value.
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
// Multiple strings.
ValueProto multiple_strings;
multiple_strings.mutable_strings()->add_values("Hello");
multiple_strings.mutable_strings()->add_values("World");
user_model_.SetValue("value", multiple_strings);
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
// Single empty string.
user_model_.SetValue("value", SimpleValue(std::string()));
EXPECT_TRUE(basic_interactions_.ComputeValue(proto));
EXPECT_EQ(user_model_.GetValue("result"), SimpleValue(true));
// Single non-empty string.
user_model_.SetValue("value", SimpleValue(std::string("Hello")));
EXPECT_TRUE(basic_interactions_.ComputeValue(proto));
EXPECT_EQ(user_model_.GetValue("result"), SimpleValue(false));
}
TEST_F(BasicInteractionsTest, ToggleUserAction) {
ToggleUserActionProto proto;
ValueProto user_actions_value;
......
......@@ -136,6 +136,8 @@ message ComputeValueProto {
CreateCreditCardResponseProto create_credit_card_response = 8;
// Writes safe-to-transmit parts of the input login option to the result.
CreateLoginOptionResponseProto create_login_option_response = 9;
// Check if a string value is empty.
StringEmptyProto string_empty = 10;
}
// The model identifier to write the result to.
......@@ -254,6 +256,12 @@ message CreateLoginOptionResponseProto {
optional ValueReferenceProto value = 1;
}
// Checks whether or not a string value is empty.
message StringEmptyProto {
// The input string.
optional ValueReferenceProto value = 1;
}
// Displays a standard info popup.
message ShowInfoPopupProto {
optional InfoPopupProto info_popup = 1;
......
......@@ -323,6 +323,14 @@ void ReplacePlaceholdersInCallback(
placeholders);
}
return;
case ComputeValueProto::kStringEmpty:
if (in_out_proto->compute_value().string_empty().has_value()) {
ReplacePlaceholdersInValue(in_out_proto->mutable_compute_value()
->mutable_string_empty()
->mutable_value(),
placeholders);
}
return;
case ComputeValueProto::KIND_NOT_SET:
return;
}
......
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