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

[Autofill Assistant] Added more PR unit tests.

There are still some parts of the code that are un-tested, particularly concerning additional actions and T&C links.

Bug: b/140018932
Change-Id: Iea4a4f92dbb6f118c01e1fab1839721ebac76082
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1769447
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarMathias Carlen <mcarlen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691082}
parent 555e677e
...@@ -6,34 +6,78 @@ ...@@ -6,34 +6,78 @@
#include <utility> #include <utility>
#include "base/guid.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill_assistant/browser/actions/mock_action_delegate.h" #include "components/autofill_assistant/browser/actions/mock_action_delegate.h"
#include "components/autofill_assistant/browser/mock_personal_data_manager.h" #include "components/autofill_assistant/browser/mock_personal_data_manager.h"
#include "components/autofill_assistant/browser/mock_website_login_fetcher.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace {
const char kFakeUrl[] = "https://www.example.com";
const char kFakeUsername[] = "user@example.com";
const char kFakePassword[] = "example_password";
const char kMemoryLocation[] = "billing";
const char kName[] = "John Doe";
const char kEmail[] = "john@doe.com";
const char kPhone[] = "+1 234-567-8901";
} // namespace
namespace autofill_assistant { namespace autofill_assistant {
namespace { namespace {
using ::base::test::RunOnceCallback;
using ::testing::_; using ::testing::_;
using ::testing::Eq;
using ::testing::Invoke; using ::testing::Invoke;
using ::testing::Property;
using ::testing::Return; using ::testing::Return;
class GetPaymentInformationActionTest : public testing::Test { class GetPaymentInformationActionTest
: public content::RenderViewHostTestHarness {
public: public:
void SetUp() override { void SetUp() override {
RenderViewHostTestHarness::SetUp();
ON_CALL(mock_action_delegate_, GetClientMemory)
.WillByDefault(Return(&client_memory_));
ON_CALL(mock_action_delegate_, GetPersonalDataManager) ON_CALL(mock_action_delegate_, GetPersonalDataManager)
.WillByDefault(Return(&mock_personal_data_manager_)); .WillByDefault(Return(&mock_personal_data_manager_));
ON_CALL(mock_action_delegate_, GetWebsiteLoginFetcher)
.WillByDefault(Return(&mock_website_login_fetcher_));
ON_CALL(mock_action_delegate_, GetPaymentInformation(_, _)) ON_CALL(mock_action_delegate_, GetPaymentInformation(_, _))
.WillByDefault( .WillByDefault(
Invoke([](std::unique_ptr<PaymentRequestOptions> options, Invoke([](std::unique_ptr<PaymentRequestOptions> options,
std::unique_ptr<PaymentInformation> information) { std::unique_ptr<PaymentInformation> information) {
std::move(options->confirm_callback).Run(std::move(information)); std::move(options->confirm_callback).Run(std::move(information));
})); }));
ON_CALL(mock_website_login_fetcher_, OnGetLoginsForUrl(_, _))
.WillByDefault(
RunOnceCallback<1>(std::vector<WebsiteLoginFetcher::Login>{
WebsiteLoginFetcher::Login(GURL(kFakeUrl), kFakeUsername)}));
ON_CALL(mock_website_login_fetcher_, OnGetPasswordForLogin(_, _))
.WillByDefault(RunOnceCallback<1>(true, kFakePassword));
content::WebContentsTester::For(web_contents())
->SetLastCommittedURL(GURL(kFakeUrl));
ON_CALL(mock_action_delegate_, GetWebContents())
.WillByDefault(Return(web_contents()));
} }
protected: protected:
base::MockCallback<Action::ProcessActionCallback> callback_; base::MockCallback<Action::ProcessActionCallback> callback_;
MockPersonalDataManager mock_personal_data_manager_; MockPersonalDataManager mock_personal_data_manager_;
MockWebsiteLoginFetcher mock_website_login_fetcher_;
MockActionDelegate mock_action_delegate_; MockActionDelegate mock_action_delegate_;
ClientMemory client_memory_;
}; };
TEST_F(GetPaymentInformationActionTest, PromptIsShown) { TEST_F(GetPaymentInformationActionTest, PromptIsShown) {
...@@ -48,5 +92,149 @@ TEST_F(GetPaymentInformationActionTest, PromptIsShown) { ...@@ -48,5 +92,149 @@ TEST_F(GetPaymentInformationActionTest, PromptIsShown) {
action.ProcessAction(callback_.Get()); action.ProcessAction(callback_.Get());
} }
TEST_F(GetPaymentInformationActionTest, SelectLogin) {
ActionProto action_proto;
auto* login_details =
action_proto.mutable_get_payment_information()->mutable_login_details();
auto* login_option = login_details->add_login_options();
login_option->mutable_password_manager();
login_option->set_payload("payload");
// Action should fetch the logins, but not the passwords.
EXPECT_CALL(mock_website_login_fetcher_, OnGetLoginsForUrl(GURL(kFakeUrl), _))
.Times(1);
EXPECT_CALL(mock_website_login_fetcher_, OnGetPasswordForLogin(_, _))
.Times(0);
ON_CALL(mock_action_delegate_, GetPaymentInformation(_, _))
.WillByDefault(
Invoke([](std::unique_ptr<PaymentRequestOptions> options,
std::unique_ptr<PaymentInformation> information) {
information->succeed = true;
information->login_choice_identifier.assign(
options->login_choices[0].identifier);
std::move(options->confirm_callback).Run(std::move(information));
}));
EXPECT_CALL(
callback_,
Run(Pointee(AllOf(
Property(&ProcessedActionProto::status, ACTION_APPLIED),
Property(&ProcessedActionProto::payment_details,
Property(&PaymentDetails::login_payload, "payload"))))));
GetPaymentInformationAction action(&mock_action_delegate_, action_proto);
action.ProcessAction(callback_.Get());
}
TEST_F(GetPaymentInformationActionTest, LoginChoiceHideIfNoOtherOptions) {
ActionProto action_proto;
auto* payment_information = action_proto.mutable_get_payment_information();
payment_information->set_request_terms_and_conditions(false);
auto* login_details = payment_information->mutable_login_details();
auto* guest_login_option = login_details->add_login_options();
guest_login_option->mutable_custom()->set_label("Guest Checkout");
guest_login_option->set_payload("guest");
guest_login_option->set_hide_if_no_other_options(true);
auto* password_login_option = login_details->add_login_options();
password_login_option->mutable_password_manager();
password_login_option->set_payload("password_manager");
ON_CALL(mock_website_login_fetcher_, OnGetLoginsForUrl(_, _))
.WillByDefault(
RunOnceCallback<1>(std::vector<WebsiteLoginFetcher::Login>{}));
EXPECT_CALL(mock_action_delegate_, GetPaymentInformation(_, _)).Times(0);
EXPECT_CALL(
callback_,
Run(Pointee(
AllOf(Property(&ProcessedActionProto::status, ACTION_APPLIED),
Property(&ProcessedActionProto::payment_details,
Property(&PaymentDetails::login_payload, "guest"))))));
GetPaymentInformationAction action(&mock_action_delegate_, action_proto);
action.ProcessAction(callback_.Get());
}
TEST_F(GetPaymentInformationActionTest, SelectContactDetails) {
ActionProto action_proto;
auto* payment_information_proto =
action_proto.mutable_get_payment_information();
payment_information_proto->set_request_terms_and_conditions(false);
auto* contact_details_proto =
payment_information_proto->mutable_contact_details();
contact_details_proto->set_contact_details_name(kMemoryLocation);
contact_details_proto->set_request_payer_name(true);
contact_details_proto->set_request_payer_email(true);
contact_details_proto->set_request_payer_phone(true);
ON_CALL(mock_action_delegate_, GetPaymentInformation(_, _))
.WillByDefault(
Invoke([](std::unique_ptr<PaymentRequestOptions> options,
std::unique_ptr<PaymentInformation> information) {
information->succeed = true;
information->payer_name.assign(kName);
information->payer_phone.assign(kPhone);
information->payer_email.assign(kEmail);
std::move(options->confirm_callback).Run(std::move(information));
}));
EXPECT_CALL(callback_,
Run(Pointee(AllOf(
Property(&ProcessedActionProto::status, ACTION_APPLIED),
Property(&ProcessedActionProto::payment_details,
Property(&PaymentDetails::payer_email, kEmail))))));
GetPaymentInformationAction action(&mock_action_delegate_, action_proto);
action.ProcessAction(callback_.Get());
EXPECT_EQ(client_memory_.has_selected_address(kMemoryLocation), true);
auto* profile = client_memory_.selected_address(kMemoryLocation);
EXPECT_EQ(profile->GetRawInfo(autofill::NAME_FULL), base::UTF8ToUTF16(kName));
EXPECT_EQ(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER),
base::UTF8ToUTF16(kPhone));
EXPECT_EQ(profile->GetRawInfo(autofill::EMAIL_ADDRESS),
base::UTF8ToUTF16(kEmail));
}
TEST_F(GetPaymentInformationActionTest, SelectPaymentMethod) {
ActionProto action_proto;
action_proto.mutable_get_payment_information()->set_ask_for_payment(true);
action_proto.mutable_get_payment_information()
->set_request_terms_and_conditions(false);
autofill::AutofillProfile billing_profile(base::GenerateGUID(), kFakeUrl);
autofill::test::SetProfileInfo(&billing_profile, "Marion", "Mitchell",
"Morrison", "marion@me.xyz", "Fox",
"123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "16505678910");
ON_CALL(mock_personal_data_manager_, GetProfileByGUID(billing_profile.guid()))
.WillByDefault(Return(&billing_profile));
autofill::CreditCard credit_card(base::GenerateGUID(), kFakeUrl);
autofill::test::SetCreditCardInfo(&credit_card, "Marion Mitchell",
"4111 1111 1111 1111", "01", "2020",
billing_profile.guid());
ON_CALL(mock_action_delegate_, GetPaymentInformation(_, _))
.WillByDefault(
Invoke([=](std::unique_ptr<PaymentRequestOptions> options,
std::unique_ptr<PaymentInformation> information) {
information->card =
std::make_unique<autofill::CreditCard>(credit_card);
information->succeed = true;
std::move(options->confirm_callback).Run(std::move(information));
}));
EXPECT_CALL(
callback_,
Run(Pointee(AllOf(
Property(&ProcessedActionProto::status, ACTION_APPLIED),
Property(&ProcessedActionProto::payment_details,
Property(&PaymentDetails::card_issuer_network, "visa"))))));
GetPaymentInformationAction action(&mock_action_delegate_, action_proto);
action.ProcessAction(callback_.Get());
EXPECT_EQ(client_memory_.has_selected_card(), true);
EXPECT_THAT(client_memory_.selected_card()->Compare(credit_card), Eq(0));
}
} // namespace } // namespace
} // namespace autofill_assistant } // namespace autofill_assistant
...@@ -23,6 +23,8 @@ class MockPersonalDataManager : public autofill::PersonalDataManager { ...@@ -23,6 +23,8 @@ class MockPersonalDataManager : public autofill::PersonalDataManager {
std::string(const autofill::AutofillProfile&)); std::string(const autofill::AutofillProfile&));
MOCK_METHOD1(GetProfileByGUID, MOCK_METHOD1(GetProfileByGUID,
autofill::AutofillProfile*(const std::string&)); autofill::AutofillProfile*(const std::string&));
MOCK_CONST_METHOD0(GetProfiles, std::vector<autofill::AutofillProfile*>());
MOCK_CONST_METHOD0(GetCreditCards, std::vector<autofill::CreditCard*>());
}; };
} // namespace autofill_assistant } // namespace autofill_assistant
......
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