| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 5 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.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" | 8 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 10 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" | 9 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" |
| 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "chrome/test/base/testing_profile.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" | 13 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 14 #include "components/autofill/core/browser/personal_data_manager.h" | 14 #include "components/autofill/core/browser/personal_data_manager.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 using testing::_; | 18 using testing::_; |
| 19 | 19 |
| 20 namespace autofill { | 20 namespace autofill { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 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 |
| 24 class TestPersonalDataManager : public PersonalDataManager { | 33 class TestPersonalDataManager : public PersonalDataManager { |
| 25 public: | 34 public: |
| 26 TestPersonalDataManager() : PersonalDataManager("en-US") {} | 35 TestPersonalDataManager() : PersonalDataManager("en-US") {} |
| 27 | 36 |
| 28 using PersonalDataManager::set_database; | 37 using PersonalDataManager::set_database; |
| 29 using PersonalDataManager::SetPrefService; | 38 using PersonalDataManager::SetPrefService; |
| 30 | 39 |
| 31 // Overridden to avoid a trip to the database. | 40 // Overridden to avoid a trip to the database. |
| 32 virtual void LoadProfiles() override {} | 41 virtual void LoadProfiles() override {} |
| 33 virtual void LoadCreditCards() override {} | 42 virtual void LoadCreditCards() override {} |
| 34 | 43 |
| 35 MOCK_METHOD1(SaveImportedCreditCard, | 44 MOCK_METHOD1(SaveImportedCreditCard, |
| 36 std::string(const CreditCard& imported_credit_card)); | 45 std::string(const CreditCard& imported_credit_card)); |
| 37 | 46 |
| 38 private: | 47 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); | 48 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); |
| 40 }; | 49 }; |
| 41 | 50 |
| 42 } // namespace | 51 } // namespace |
| 43 | 52 |
| 44 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness { | 53 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness { |
| 45 public: | 54 public: |
| 46 ~AutofillCCInfobarDelegateTest() override; | 55 ~AutofillCCInfobarDelegateTest() override; |
| 47 | 56 |
| 48 void SetUp() override; | 57 void SetUp() override; |
| 49 void TearDown() override; | 58 void TearDown() override; |
| 50 | 59 |
| 51 protected: | 60 protected: |
| 52 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(); | 61 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate( |
| 62 MockAutofillMetrics* metric_logger); |
| 53 | 63 |
| 54 scoped_ptr<TestPersonalDataManager> personal_data_; | 64 scoped_ptr<TestPersonalDataManager> personal_data_; |
| 55 }; | 65 }; |
| 56 | 66 |
| 57 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {} | 67 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {} |
| 58 | 68 |
| 59 void AutofillCCInfobarDelegateTest::SetUp() { | 69 void AutofillCCInfobarDelegateTest::SetUp() { |
| 60 ChromeRenderViewHostTestHarness::SetUp(); | 70 ChromeRenderViewHostTestHarness::SetUp(); |
| 61 | 71 |
| 62 // Ensure Mac OS X does not pop up a modal dialog for the Address Book. | 72 // Ensure Mac OS X does not pop up a modal dialog for the Address Book. |
| 63 test::DisableSystemServices(profile()->GetPrefs()); | 73 test::DisableSystemServices(profile()->GetPrefs()); |
| 64 | 74 |
| 65 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); | 75 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); |
| 66 | 76 |
| 67 ChromeAutofillClient::CreateForWebContents(web_contents()); | 77 ChromeAutofillClient::CreateForWebContents(web_contents()); |
| 68 ChromeAutofillClient* autofill_client = | 78 ChromeAutofillClient* autofill_client = |
| 69 ChromeAutofillClient::FromWebContents(web_contents()); | 79 ChromeAutofillClient::FromWebContents(web_contents()); |
| 70 | 80 |
| 71 personal_data_.reset(new TestPersonalDataManager()); | 81 personal_data_.reset(new TestPersonalDataManager()); |
| 72 personal_data_->set_database(autofill_client->GetDatabase()); | 82 personal_data_->set_database(autofill_client->GetDatabase()); |
| 73 personal_data_->SetPrefService(profile()->GetPrefs()); | 83 personal_data_->SetPrefService(profile()->GetPrefs()); |
| 74 } | 84 } |
| 75 | 85 |
| 76 void AutofillCCInfobarDelegateTest::TearDown() { | 86 void AutofillCCInfobarDelegateTest::TearDown() { |
| 77 personal_data_.reset(); | 87 personal_data_.reset(); |
| 78 ChromeRenderViewHostTestHarness::TearDown(); | 88 ChromeRenderViewHostTestHarness::TearDown(); |
| 79 } | 89 } |
| 80 | 90 |
| 81 scoped_ptr<ConfirmInfoBarDelegate> | 91 scoped_ptr<ConfirmInfoBarDelegate> |
| 82 AutofillCCInfobarDelegateTest::CreateDelegate() { | 92 AutofillCCInfobarDelegateTest::CreateDelegate( |
| 83 base::HistogramTester histogram_tester; | 93 MockAutofillMetrics* metric_logger) { |
| 94 EXPECT_CALL(*metric_logger, |
| 95 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN)); |
| 96 |
| 84 CreditCard credit_card; | 97 CreditCard credit_card; |
| 85 scoped_ptr<ConfirmInfoBarDelegate> delegate( | 98 return AutofillCCInfoBarDelegate::Create( |
| 86 AutofillCCInfoBarDelegate::Create(base::Bind( | 99 metric_logger, |
| 100 base::Bind( |
| 87 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard), | 101 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard), |
| 88 base::Unretained(personal_data_.get()), credit_card))); | 102 base::Unretained(personal_data_.get()), |
| 89 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar", | 103 credit_card)); |
| 90 AutofillMetrics::INFOBAR_SHOWN, 1); | |
| 91 return delegate.Pass(); | |
| 92 } | 104 } |
| 93 | 105 |
| 94 // Test that credit card infobar metrics are logged correctly. | 106 // Test that credit card infobar metrics are logged correctly. |
| 95 TEST_F(AutofillCCInfobarDelegateTest, Metrics) { | 107 TEST_F(AutofillCCInfobarDelegateTest, Metrics) { |
| 108 MockAutofillMetrics metric_logger; |
| 96 ::testing::InSequence dummy; | 109 ::testing::InSequence dummy; |
| 97 | 110 |
| 98 // Accept the infobar. | 111 // Accept the infobar. |
| 99 { | 112 { |
| 100 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate()); | 113 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| 114 ASSERT_TRUE(infobar); |
| 101 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); | 115 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); |
| 116 EXPECT_CALL(metric_logger, |
| 117 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)); |
| 102 | 118 |
| 103 base::HistogramTester histogram_tester; | 119 EXPECT_CALL(metric_logger, |
| 120 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| 121 .Times(0); |
| 104 EXPECT_TRUE(infobar->Accept()); | 122 EXPECT_TRUE(infobar->Accept()); |
| 105 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar", | |
| 106 AutofillMetrics::INFOBAR_ACCEPTED, 1); | |
| 107 } | 123 } |
| 108 | 124 |
| 109 // Cancel the infobar. | 125 // Cancel the infobar. |
| 110 { | 126 { |
| 111 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate()); | 127 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| 112 | 128 ASSERT_TRUE(infobar); |
| 113 base::HistogramTester histogram_tester; | 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); |
| 114 EXPECT_TRUE(infobar->Cancel()); | 135 EXPECT_TRUE(infobar->Cancel()); |
| 115 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar", | |
| 116 AutofillMetrics::INFOBAR_DENIED, 1); | |
| 117 } | 136 } |
| 118 | 137 |
| 119 // Dismiss the infobar. | 138 // Dismiss the infobar. |
| 120 { | 139 { |
| 121 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate()); | 140 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| 122 | 141 ASSERT_TRUE(infobar); |
| 123 base::HistogramTester histogram_tester; | 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); |
| 124 infobar->InfoBarDismissed(); | 148 infobar->InfoBarDismissed(); |
| 125 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar", | |
| 126 AutofillMetrics::INFOBAR_DENIED, 1); | |
| 127 } | 149 } |
| 128 | 150 |
| 129 // Ignore the infobar. | 151 // Ignore the infobar. |
| 130 { | 152 { |
| 131 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate()); | 153 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| 132 | 154 ASSERT_TRUE(infobar); |
| 133 base::HistogramTester histogram_tester; | 155 EXPECT_CALL(metric_logger, |
| 134 infobar.reset(); | 156 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| 135 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar", | 157 .Times(1); |
| 136 AutofillMetrics::INFOBAR_IGNORED, 1); | |
| 137 } | 158 } |
| 138 } | 159 } |
| 139 | 160 |
| 140 } // namespace autofill | 161 } // namespace autofill |
| OLD | NEW |