Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autofill / autofill_cc_infobar_delegate_unittest.cc
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.
4
5 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
6
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"
17
18 using testing::_;
19
20 namespace autofill {
21
22 namespace {
23
24 class MockAutofillMetrics : public AutofillMetrics {
25  public:
26   MockAutofillMetrics() {}
27   MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
28
29  private:
30   DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
31 };
32
33 class TestPersonalDataManager : public PersonalDataManager {
34  public:
35   TestPersonalDataManager() : PersonalDataManager("en-US") {}
36
37   using PersonalDataManager::set_database;
38   using PersonalDataManager::SetPrefService;
39
40   // Overridden to avoid a trip to the database.
41   virtual void LoadProfiles() override {}
42   virtual void LoadCreditCards() override {}
43
44   MOCK_METHOD1(SaveImportedCreditCard,
45                std::string(const CreditCard& imported_credit_card));
46
47  private:
48   DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
49 };
50
51 }  // namespace
52
53 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness {
54  public:
55   ~AutofillCCInfobarDelegateTest() override;
56
57   void SetUp() override;
58   void TearDown() override;
59
60  protected:
61   scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
62       MockAutofillMetrics* metric_logger);
63
64   scoped_ptr<TestPersonalDataManager> personal_data_;
65 };
66
67 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}
68
69 void AutofillCCInfobarDelegateTest::SetUp() {
70   ChromeRenderViewHostTestHarness::SetUp();
71
72   // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
73   test::DisableSystemServices(profile()->GetPrefs());
74
75   PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
76
77   ChromeAutofillClient::CreateForWebContents(web_contents());
78   ChromeAutofillClient* autofill_client =
79       ChromeAutofillClient::FromWebContents(web_contents());
80
81   personal_data_.reset(new TestPersonalDataManager());
82   personal_data_->set_database(autofill_client->GetDatabase());
83   personal_data_->SetPrefService(profile()->GetPrefs());
84 }
85
86 void AutofillCCInfobarDelegateTest::TearDown() {
87   personal_data_.reset();
88   ChromeRenderViewHostTestHarness::TearDown();
89 }
90
91 scoped_ptr<ConfirmInfoBarDelegate>
92 AutofillCCInfobarDelegateTest::CreateDelegate(
93     MockAutofillMetrics* metric_logger) {
94   EXPECT_CALL(*metric_logger,
95               LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));
96
97   CreditCard credit_card;
98   return AutofillCCInfoBarDelegate::Create(
99       metric_logger,
100       base::Bind(
101           base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard),
102           base::Unretained(personal_data_.get()),
103           credit_card));
104 }
105
106 // Test that credit card infobar metrics are logged correctly.
107 TEST_F(AutofillCCInfobarDelegateTest, Metrics) {
108   MockAutofillMetrics metric_logger;
109   ::testing::InSequence dummy;
110
111   // Accept the infobar.
112   {
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));
118
119     EXPECT_CALL(metric_logger,
120                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
121         .Times(0);
122     EXPECT_TRUE(infobar->Accept());
123   }
124
125   // Cancel the infobar.
126   {
127     scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
128     ASSERT_TRUE(infobar);
129     EXPECT_CALL(metric_logger,
130                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
131         .Times(1);
132     EXPECT_CALL(metric_logger,
133                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
134         .Times(0);
135     EXPECT_TRUE(infobar->Cancel());
136   }
137
138   // Dismiss the infobar.
139   {
140     scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
141     ASSERT_TRUE(infobar);
142     EXPECT_CALL(metric_logger,
143                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
144         .Times(1);
145     EXPECT_CALL(metric_logger,
146                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
147         .Times(0);
148     infobar->InfoBarDismissed();
149   }
150
151   // Ignore the infobar.
152   {
153     scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
154     ASSERT_TRUE(infobar);
155     EXPECT_CALL(metric_logger,
156                 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
157         .Times(1);
158   }
159 }
160
161 }  // namespace autofill