1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/autofill/personal_data_manager_factory.h"
9 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/autofill/core/browser/autofill_metrics.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/personal_data_manager.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
24 class MockAutofillMetrics : public AutofillMetrics {
26 MockAutofillMetrics() {}
27 MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
30 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
33 class TestPersonalDataManager : public PersonalDataManager {
35 TestPersonalDataManager() : PersonalDataManager("en-US") {}
37 using PersonalDataManager::set_database;
38 using PersonalDataManager::SetPrefService;
40 // Overridden to avoid a trip to the database.
41 virtual void LoadProfiles() override {}
42 virtual void LoadCreditCards() override {}
44 MOCK_METHOD1(SaveImportedCreditCard,
45 std::string(const CreditCard& imported_credit_card));
48 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
53 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness {
55 ~AutofillCCInfobarDelegateTest() override;
57 void SetUp() override;
58 void TearDown() override;
61 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
62 MockAutofillMetrics* metric_logger);
64 scoped_ptr<TestPersonalDataManager> personal_data_;
67 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}
69 void AutofillCCInfobarDelegateTest::SetUp() {
70 ChromeRenderViewHostTestHarness::SetUp();
72 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
73 test::DisableSystemServices(profile()->GetPrefs());
75 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
77 ChromeAutofillClient::CreateForWebContents(web_contents());
78 ChromeAutofillClient* autofill_client =
79 ChromeAutofillClient::FromWebContents(web_contents());
81 personal_data_.reset(new TestPersonalDataManager());
82 personal_data_->set_database(autofill_client->GetDatabase());
83 personal_data_->SetPrefService(profile()->GetPrefs());
86 void AutofillCCInfobarDelegateTest::TearDown() {
87 personal_data_.reset();
88 ChromeRenderViewHostTestHarness::TearDown();
91 scoped_ptr<ConfirmInfoBarDelegate>
92 AutofillCCInfobarDelegateTest::CreateDelegate(
93 MockAutofillMetrics* metric_logger) {
94 EXPECT_CALL(*metric_logger,
95 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));
97 CreditCard credit_card;
98 return AutofillCCInfoBarDelegate::Create(
101 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard),
102 base::Unretained(personal_data_.get()),
106 // Test that credit card infobar metrics are logged correctly.
107 TEST_F(AutofillCCInfobarDelegateTest, Metrics) {
108 MockAutofillMetrics metric_logger;
109 ::testing::InSequence dummy;
111 // Accept the infobar.
113 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
114 ASSERT_TRUE(infobar);
115 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
116 EXPECT_CALL(metric_logger,
117 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED));
119 EXPECT_CALL(metric_logger,
120 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
122 EXPECT_TRUE(infobar->Accept());
125 // Cancel the infobar.
127 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
128 ASSERT_TRUE(infobar);
129 EXPECT_CALL(metric_logger,
130 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
132 EXPECT_CALL(metric_logger,
133 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
135 EXPECT_TRUE(infobar->Cancel());
138 // Dismiss the infobar.
140 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
141 ASSERT_TRUE(infobar);
142 EXPECT_CALL(metric_logger,
143 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
145 EXPECT_CALL(metric_logger,
146 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
148 infobar->InfoBarDismissed();
151 // Ignore the infobar.
153 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
154 ASSERT_TRUE(infobar);
155 EXPECT_CALL(metric_logger,
156 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
161 } // namespace autofill