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 "components/autofill/core/browser/autofill_cc_infobar_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/histogram_tester.h"
9 #include "chrome/browser/autofill/personal_data_manager_factory.h"
10 #include "chrome/browser/ui/autofill/chrome_autofill_client.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.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 TestPersonalDataManager
: public PersonalDataManager
{
26 TestPersonalDataManager() : PersonalDataManager("en-US") {}
28 using PersonalDataManager::set_database
;
29 using PersonalDataManager::SetPrefService
;
31 // Overridden to avoid a trip to the database.
32 void LoadProfiles() override
{}
33 void LoadCreditCards() override
{}
35 MOCK_METHOD1(SaveImportedCreditCard
,
36 std::string(const CreditCard
& imported_credit_card
));
39 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager
);
44 class AutofillCCInfobarDelegateTest
: public ChromeRenderViewHostTestHarness
{
46 AutofillCCInfobarDelegateTest();
47 ~AutofillCCInfobarDelegateTest() override
;
49 void SetUp() override
;
50 void TearDown() override
;
53 scoped_ptr
<ConfirmInfoBarDelegate
> CreateDelegate();
55 scoped_ptr
<TestPersonalDataManager
> personal_data_
;
58 DISALLOW_COPY_AND_ASSIGN(AutofillCCInfobarDelegateTest
);
61 AutofillCCInfobarDelegateTest::AutofillCCInfobarDelegateTest() {
64 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}
66 void AutofillCCInfobarDelegateTest::SetUp() {
67 ChromeRenderViewHostTestHarness::SetUp();
69 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
70 test::DisableSystemServices(profile()->GetPrefs());
72 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL
);
74 ChromeAutofillClient::CreateForWebContents(web_contents());
75 ChromeAutofillClient
* autofill_client
=
76 ChromeAutofillClient::FromWebContents(web_contents());
78 personal_data_
.reset(new TestPersonalDataManager());
79 personal_data_
->set_database(autofill_client
->GetDatabase());
80 personal_data_
->SetPrefService(profile()->GetPrefs());
83 void AutofillCCInfobarDelegateTest::TearDown() {
84 personal_data_
.reset();
85 ChromeRenderViewHostTestHarness::TearDown();
88 scoped_ptr
<ConfirmInfoBarDelegate
>
89 AutofillCCInfobarDelegateTest::CreateDelegate() {
90 base::HistogramTester histogram_tester
;
91 CreditCard credit_card
;
92 scoped_ptr
<ConfirmInfoBarDelegate
> delegate(AutofillCCInfoBarDelegate::Create(
93 ChromeAutofillClient::FromWebContents(web_contents()),
95 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard
),
96 base::Unretained(personal_data_
.get()), credit_card
)));
97 histogram_tester
.ExpectUniqueSample("Autofill.CreditCardInfoBar",
98 AutofillMetrics::INFOBAR_SHOWN
, 1);
99 return delegate
.Pass();
102 // Test that credit card infobar metrics are logged correctly.
103 TEST_F(AutofillCCInfobarDelegateTest
, Metrics
) {
104 ::testing::InSequence dummy
;
106 // Accept the infobar.
108 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate());
109 EXPECT_CALL(*personal_data_
, SaveImportedCreditCard(_
));
111 base::HistogramTester histogram_tester
;
112 EXPECT_TRUE(infobar
->Accept());
113 histogram_tester
.ExpectUniqueSample("Autofill.CreditCardInfoBar",
114 AutofillMetrics::INFOBAR_ACCEPTED
, 1);
117 // Cancel the infobar.
119 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate());
121 base::HistogramTester histogram_tester
;
122 EXPECT_TRUE(infobar
->Cancel());
123 histogram_tester
.ExpectUniqueSample("Autofill.CreditCardInfoBar",
124 AutofillMetrics::INFOBAR_DENIED
, 1);
127 // Dismiss the infobar.
129 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate());
131 base::HistogramTester histogram_tester
;
132 infobar
->InfoBarDismissed();
133 histogram_tester
.ExpectUniqueSample("Autofill.CreditCardInfoBar",
134 AutofillMetrics::INFOBAR_DENIED
, 1);
137 // Ignore the infobar.
139 scoped_ptr
<ConfirmInfoBarDelegate
> infobar(CreateDelegate());
141 base::HistogramTester histogram_tester
;
143 histogram_tester
.ExpectUniqueSample("Autofill.CreditCardInfoBar",
144 AutofillMetrics::INFOBAR_IGNORED
, 1);
148 } // namespace autofill