blob: 97e003b3e11ba3032cabe7fdd60f69845023a88d [file] [log] [blame]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill_assistant/browser/actions/highlight_element_action.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h"
#include "components/autofill_assistant/browser/actions/action_test_utils.h"
#include "components/autofill_assistant/browser/actions/mock_action_delegate.h"
#include "components/autofill_assistant/browser/selector.h"
#include "components/autofill_assistant/browser/service.pb.h"
#include "components/autofill_assistant/browser/web/mock_web_controller.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace autofill_assistant {
namespace {
using ::base::test::RunOnceCallback;
using ::testing::_;
using ::testing::InSequence;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::Return;
class HighlightElementActionTest : public testing::Test {
public:
HighlightElementActionTest() {}
void SetUp() override {
ON_CALL(mock_action_delegate_, GetWebController)
.WillByDefault(Return(&mock_web_controller_));
}
protected:
void Run() {
ActionProto action_proto;
*action_proto.mutable_highlight_element() = proto_;
HighlightElementAction action(&mock_action_delegate_, action_proto);
action.ProcessAction(callback_.Get());
}
MockActionDelegate mock_action_delegate_;
MockWebController mock_web_controller_;
base::MockCallback<Action::ProcessActionCallback> callback_;
HighlightElementProto proto_;
};
TEST_F(HighlightElementActionTest, EmptySelectorFails) {
EXPECT_CALL(
callback_,
Run(Pointee(Property(&ProcessedActionProto::status, INVALID_SELECTOR))));
Run();
}
TEST_F(HighlightElementActionTest, ActionFailsForNonExistentElement) {
InSequence sequence;
Selector selector({"#element"});
*proto_.mutable_element() = selector.proto;
Selector expected_selector = selector;
EXPECT_CALL(mock_action_delegate_,
OnShortWaitForElement(expected_selector, _))
.WillOnce(RunOnceCallback<1>(ClientStatus(TIMED_OUT),
base::TimeDelta::FromSeconds(0)));
EXPECT_CALL(callback_,
Run(Pointee(Property(&ProcessedActionProto::status, TIMED_OUT))));
Run();
}
TEST_F(HighlightElementActionTest, CheckExpectedCallChain) {
InSequence sequence;
Selector selector({"#element"});
*proto_.mutable_element() = selector.proto;
Selector expected_selector = selector;
EXPECT_CALL(mock_action_delegate_,
OnShortWaitForElement(expected_selector, _))
.WillOnce(RunOnceCallback<1>(OkClientStatus(),
base::TimeDelta::FromSeconds(0)));
auto expected_element =
test_util::MockFindElement(mock_action_delegate_, expected_selector);
EXPECT_CALL(mock_web_controller_,
HighlightElement(EqualsElement(expected_element), _))
.WillOnce(RunOnceCallback<1>(OkClientStatus()));
EXPECT_CALL(
callback_,
Run(Pointee(Property(&ProcessedActionProto::status, ACTION_APPLIED))));
Run();
}
} // namespace
} // namespace autofill_assistant