Commit 908f973c authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Added IntegerSum interaction to ui framework.

Bug: b/145043394
Change-Id: Ib25f6ee1dc6949cb340869a1bc439f55be08743d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2096689
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749712}
parent ff001a53
......@@ -223,6 +223,35 @@ bool Compare(UserModel* user_model,
return true;
}
bool IntegerSum(UserModel* user_model,
const std::string& result_model_identifier,
const IntegerSumProto& proto) {
auto value_a = user_model->GetValue(proto.model_identifier_a());
if (!value_a.has_value()) {
DVLOG(2) << "Error evaluating " << __func__ << ": "
<< proto.model_identifier_a() << " not found in model";
return false;
}
auto value_b = user_model->GetValue(proto.model_identifier_b());
if (!value_b.has_value()) {
DVLOG(2) << "Error evaluating " << __func__ << ": "
<< proto.model_identifier_b() << " not found in model";
return false;
}
if (!AreAllValuesOfSize({*value_a, *value_b}, 1) ||
!AreAllValuesOfType({*value_a, *value_b}, ValueProto::kInts)) {
DVLOG(2) << "Error evaluating " << __func__ << ": "
<< "all input values must be single integers";
return false;
}
user_model->SetValue(
result_model_identifier,
SimpleValue(value_a->ints().values(0) + value_b->ints().values(0)));
return true;
}
} // namespace
base::WeakPtr<BasicInteractions> BasicInteractions::GetWeakPtr() {
......@@ -285,6 +314,15 @@ bool BasicInteractions::ComputeValue(const ComputeValueProto& proto) {
case ComputeValueProto::kComparison:
return Compare(delegate_->GetUserModel(), proto.result_model_identifier(),
proto.comparison());
case ComputeValueProto::kIntegerSum:
if (proto.integer_sum().model_identifier_a().empty() ||
proto.integer_sum().model_identifier_b().empty()) {
DVLOG(2) << "Error computing ComputeValue::IntegerSum: "
"model_identifier_a or model_identifier_b not specified";
return false;
}
return IntegerSum(delegate_->GetUserModel(),
proto.result_model_identifier(), proto.integer_sum());
case ComputeValueProto::KIND_NOT_SET:
DVLOG(2) << "Error computing value: kind not set";
return false;
......
......@@ -196,6 +196,40 @@ TEST_F(BasicInteractionsTest, ComputeValueToString) {
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
}
TEST_F(BasicInteractionsTest, ComputeValueIntegerSum) {
ComputeValueProto proto;
proto.mutable_integer_sum();
user_model_.SetValue("value_a", SimpleValue(1));
user_model_.SetValue("value_b", SimpleValue(2));
// Missing fields.
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
proto.mutable_integer_sum()->set_model_identifier_a("value_a");
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
proto.mutable_integer_sum()->set_model_identifier_b("value_b");
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
proto.set_result_model_identifier("result");
EXPECT_TRUE(basic_interactions_.ComputeValue(proto));
// Size != 1.
ValueProto value;
value.mutable_ints()->add_values(1);
value.mutable_ints()->add_values(2);
user_model_.SetValue("value_a", value);
EXPECT_FALSE(basic_interactions_.ComputeValue(proto));
// Check results.
user_model_.SetValue("value_a", SimpleValue(1));
user_model_.SetValue("value_b", SimpleValue(2));
EXPECT_TRUE(basic_interactions_.ComputeValue(proto));
EXPECT_EQ(user_model_.GetValue("result"), SimpleValue(3));
user_model_.SetValue("value_a", SimpleValue(-1));
user_model_.SetValue("value_b", SimpleValue(5));
EXPECT_TRUE(basic_interactions_.ComputeValue(proto));
EXPECT_EQ(user_model_.GetValue("result"), SimpleValue(4));
}
TEST_F(BasicInteractionsTest, EndActionWithoutCallbackFails) {
EndActionProto proto;
ASSERT_DEATH(basic_interactions_.EndAction(proto),
......
......@@ -86,6 +86,8 @@ message ComputeValueProto {
ToStringProto to_string = 5;
// Compares two values.
ValueComparisonProto comparison = 6;
// Computes the sum of two single integers.
IntegerSumProto integer_sum = 7;
}
// The model identifier to write the result to.
......@@ -148,6 +150,14 @@ message ValueComparisonProto {
optional Mode mode = 3;
}
// Computes the sum of two single integers.
message IntegerSumProto {
// The first model identifier. Must point to an integer value.
optional string model_identifier_a = 1;
// The second model identifier. Must point to an integer value.
optional string model_identifier_b = 2;
}
// Displays a standard info popup.
message ShowInfoPopupProto {
optional InfoPopupProto info_popup = 1;
......
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