Commit 94e4fc0f authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] Adds parsing of trigger scripts.

Bug: b/171776026
Change-Id: Iae2f60159b3ee6e1d912aa2a37aad238ffe8597b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2517721
Auto-Submit: Clemens Arbesser <arbesser@google.com>
Commit-Queue: Marian Fechete <marianfe@google.com>
Reviewed-by: default avatarMarian Fechete <marianfe@google.com>
Cr-Commit-Position: refs/heads/master@{#823609}
parent 4781ddfd
...@@ -286,4 +286,23 @@ bool ProtocolUtils::ParseActions(ActionDelegate* delegate, ...@@ -286,4 +286,23 @@ bool ProtocolUtils::ParseActions(ActionDelegate* delegate,
return true; return true;
} }
// static
bool ProtocolUtils::ParseTriggerScripts(
const std::string& response,
std::vector<std::unique_ptr<TriggerScript>>* trigger_scripts) {
DCHECK(trigger_scripts);
GetTriggerScriptsResponseProto response_proto;
if (!response_proto.ParseFromString(response)) {
LOG(ERROR) << "Failed to parse trigger scripts response";
return false;
}
for (const auto& trigger_script_proto : response_proto.trigger_scripts()) {
trigger_scripts->emplace_back(
std::make_unique<TriggerScript>(trigger_script_proto));
}
return true;
}
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "components/autofill_assistant/browser/actions/action.h" #include "components/autofill_assistant/browser/actions/action.h"
#include "components/autofill_assistant/browser/script.h" #include "components/autofill_assistant/browser/script.h"
#include "components/autofill_assistant/browser/service.pb.h" #include "components/autofill_assistant/browser/service.pb.h"
#include "components/autofill_assistant/browser/trigger_scripts/trigger_script.h"
class GURL; class GURL;
...@@ -74,6 +75,12 @@ class ProtocolUtils { ...@@ -74,6 +75,12 @@ class ProtocolUtils {
std::vector<std::unique_ptr<Script>>* scripts, std::vector<std::unique_ptr<Script>>* scripts,
bool* should_update_scripts); bool* should_update_scripts);
// Parse trigger scripts from the given |response| and insert them into
// |trigger_scripts|. Returns false if parsing failed, else true.
static bool ParseTriggerScripts(
const std::string& response,
std::vector<std::unique_ptr<TriggerScript>>* trigger_scripts);
private: private:
// To avoid instantiate this class by accident. // To avoid instantiate this class by accident.
ProtocolUtils() = delete; ProtocolUtils() = delete;
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#include "components/autofill_assistant/browser/protocol_utils.h" #include "components/autofill_assistant/browser/protocol_utils.h"
#include "base/macros.h" #include "base/macros.h"
#include "components/autofill_assistant/browser/selector.h"
#include "components/autofill_assistant/browser/service.pb.h" #include "components/autofill_assistant/browser/service.pb.h"
#include "components/autofill_assistant/browser/test_util.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -17,6 +19,8 @@ using ::testing::Eq; ...@@ -17,6 +19,8 @@ using ::testing::Eq;
using ::testing::IsEmpty; using ::testing::IsEmpty;
using ::testing::Not; using ::testing::Not;
using ::testing::Pair; using ::testing::Pair;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::SizeIs; using ::testing::SizeIs;
using ::testing::UnorderedElementsAreArray; using ::testing::UnorderedElementsAreArray;
...@@ -321,5 +325,36 @@ TEST_F(ProtocolUtilsTest, ParseActionsUpdateScriptListFullFeatured) { ...@@ -321,5 +325,36 @@ TEST_F(ProtocolUtilsTest, ParseActionsUpdateScriptListFullFeatured) {
EXPECT_THAT("name", Eq(scripts[0]->handle.chip.text)); EXPECT_THAT("name", Eq(scripts[0]->handle.chip.text));
} }
TEST_F(ProtocolUtilsTest, ParseTriggerScriptsParseError) {
std::vector<std::unique_ptr<TriggerScript>> trigger_scripts;
EXPECT_FALSE(ProtocolUtils::ParseTriggerScripts("invalid", &trigger_scripts));
EXPECT_TRUE(trigger_scripts.empty());
}
TEST_F(ProtocolUtilsTest, ParseTriggerScriptsValid) {
GetTriggerScriptsResponseProto proto;
TriggerScriptProto trigger_script_1;
*trigger_script_1.mutable_trigger_condition()->mutable_selector() =
ToSelectorProto("fake_element_1");
TriggerScriptProto trigger_script_2;
trigger_script_2.set_on_trigger_condition_no_longer_true(
TriggerScriptProto::CANCEL_SESSION);
*proto.add_trigger_scripts() = trigger_script_1;
*proto.add_trigger_scripts() = trigger_script_2;
std::string proto_str;
proto.SerializeToString(&proto_str);
std::vector<std::unique_ptr<TriggerScript>> trigger_scripts;
EXPECT_TRUE(ProtocolUtils::ParseTriggerScripts(proto_str, &trigger_scripts));
EXPECT_THAT(
trigger_scripts,
ElementsAre(
Pointee(Property(&TriggerScript::AsProto, Eq(trigger_script_1))),
Pointee(Property(&TriggerScript::AsProto, Eq(trigger_script_2)))));
}
} // namespace } // namespace
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -73,4 +73,8 @@ bool TriggerScript::EvaluateTriggerConditions( ...@@ -73,4 +73,8 @@ bool TriggerScript::EvaluateTriggerConditions(
dynamic_trigger_conditions); dynamic_trigger_conditions);
} }
TriggerScriptProto TriggerScript::AsProto() const {
return proto_;
}
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -26,6 +26,8 @@ class TriggerScript { ...@@ -26,6 +26,8 @@ class TriggerScript {
const StaticTriggerConditions& static_trigger_conditions, const StaticTriggerConditions& static_trigger_conditions,
const DynamicTriggerConditions& dynamic_trigger_conditions) const; const DynamicTriggerConditions& dynamic_trigger_conditions) const;
TriggerScriptProto AsProto() const;
private: private:
friend class TriggerScriptTest; friend class TriggerScriptTest;
......
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