Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc

Issue 780423002: Don't deref stale AutofillMetrics pointer in AutofillCCInfoBarDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: static-ify Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
8 #include "chrome/browser/autofill/personal_data_manager_factory.h" 9 #include "chrome/browser/autofill/personal_data_manager_factory.h"
9 #include "chrome/browser/ui/autofill/chrome_autofill_client.h" 10 #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/chrome_render_view_host_test_harness.h"
11 #include "chrome/test/base/testing_profile.h" 12 #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
33 class TestPersonalDataManager : public PersonalDataManager { 24 class TestPersonalDataManager : public PersonalDataManager {
34 public: 25 public:
35 TestPersonalDataManager() : PersonalDataManager("en-US") {} 26 TestPersonalDataManager() : PersonalDataManager("en-US") {}
36 27
37 using PersonalDataManager::set_database; 28 using PersonalDataManager::set_database;
38 using PersonalDataManager::SetPrefService; 29 using PersonalDataManager::SetPrefService;
39 30
40 // Overridden to avoid a trip to the database. 31 // Overridden to avoid a trip to the database.
41 virtual void LoadProfiles() override {} 32 virtual void LoadProfiles() override {}
42 virtual void LoadCreditCards() override {} 33 virtual void LoadCreditCards() override {}
43 34
44 MOCK_METHOD1(SaveImportedCreditCard, 35 MOCK_METHOD1(SaveImportedCreditCard,
45 std::string(const CreditCard& imported_credit_card)); 36 std::string(const CreditCard& imported_credit_card));
46 37
47 private: 38 private:
48 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); 39 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
49 }; 40 };
50 41
51 } // namespace 42 } // namespace
52 43
53 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness { 44 class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness {
54 public: 45 public:
55 ~AutofillCCInfobarDelegateTest() override; 46 ~AutofillCCInfobarDelegateTest() override;
56 47
57 void SetUp() override; 48 void SetUp() override;
58 void TearDown() override; 49 void TearDown() override;
59 50
60 protected: 51 protected:
61 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate( 52 scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate();
62 MockAutofillMetrics* metric_logger);
63 53
64 scoped_ptr<TestPersonalDataManager> personal_data_; 54 scoped_ptr<TestPersonalDataManager> personal_data_;
65 }; 55 };
66 56
67 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {} 57 AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {}
68 58
69 void AutofillCCInfobarDelegateTest::SetUp() { 59 void AutofillCCInfobarDelegateTest::SetUp() {
70 ChromeRenderViewHostTestHarness::SetUp(); 60 ChromeRenderViewHostTestHarness::SetUp();
71 61
72 // Ensure Mac OS X does not pop up a modal dialog for the Address Book. 62 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
73 test::DisableSystemServices(profile()->GetPrefs()); 63 test::DisableSystemServices(profile()->GetPrefs());
74 64
75 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); 65 PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
76 66
77 ChromeAutofillClient::CreateForWebContents(web_contents()); 67 ChromeAutofillClient::CreateForWebContents(web_contents());
78 ChromeAutofillClient* autofill_client = 68 ChromeAutofillClient* autofill_client =
79 ChromeAutofillClient::FromWebContents(web_contents()); 69 ChromeAutofillClient::FromWebContents(web_contents());
80 70
81 personal_data_.reset(new TestPersonalDataManager()); 71 personal_data_.reset(new TestPersonalDataManager());
82 personal_data_->set_database(autofill_client->GetDatabase()); 72 personal_data_->set_database(autofill_client->GetDatabase());
83 personal_data_->SetPrefService(profile()->GetPrefs()); 73 personal_data_->SetPrefService(profile()->GetPrefs());
84 } 74 }
85 75
86 void AutofillCCInfobarDelegateTest::TearDown() { 76 void AutofillCCInfobarDelegateTest::TearDown() {
87 personal_data_.reset(); 77 personal_data_.reset();
88 ChromeRenderViewHostTestHarness::TearDown(); 78 ChromeRenderViewHostTestHarness::TearDown();
89 } 79 }
90 80
91 scoped_ptr<ConfirmInfoBarDelegate> 81 scoped_ptr<ConfirmInfoBarDelegate>
92 AutofillCCInfobarDelegateTest::CreateDelegate( 82 AutofillCCInfobarDelegateTest::CreateDelegate() {
93 MockAutofillMetrics* metric_logger) { 83 base::HistogramTester histogram_tester;
94 EXPECT_CALL(*metric_logger,
95 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));
96
97 CreditCard credit_card; 84 CreditCard credit_card;
98 return AutofillCCInfoBarDelegate::Create( 85 scoped_ptr<ConfirmInfoBarDelegate> delegate(
99 metric_logger, 86 AutofillCCInfoBarDelegate::Create(base::Bind(
100 base::Bind(
101 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard), 87 base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard),
102 base::Unretained(personal_data_.get()), 88 base::Unretained(personal_data_.get()), credit_card)));
103 credit_card)); 89 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar",
90 AutofillMetrics::INFOBAR_SHOWN, 1);
91 return delegate.Pass();
104 } 92 }
105 93
106 // Test that credit card infobar metrics are logged correctly. 94 // Test that credit card infobar metrics are logged correctly.
107 TEST_F(AutofillCCInfobarDelegateTest, Metrics) { 95 TEST_F(AutofillCCInfobarDelegateTest, Metrics) {
108 MockAutofillMetrics metric_logger;
109 ::testing::InSequence dummy; 96 ::testing::InSequence dummy;
110 97
111 // Accept the infobar. 98 // Accept the infobar.
112 { 99 {
113 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); 100 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate());
114 ASSERT_TRUE(infobar);
115 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); 101 EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
116 EXPECT_CALL(metric_logger,
117 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED));
118 102
119 EXPECT_CALL(metric_logger, 103 base::HistogramTester histogram_tester;
120 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
121 .Times(0);
122 EXPECT_TRUE(infobar->Accept()); 104 EXPECT_TRUE(infobar->Accept());
105 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar",
106 AutofillMetrics::INFOBAR_ACCEPTED, 1);
123 } 107 }
124 108
125 // Cancel the infobar. 109 // Cancel the infobar.
126 { 110 {
127 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); 111 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate());
128 ASSERT_TRUE(infobar); 112
129 EXPECT_CALL(metric_logger, 113 base::HistogramTester histogram_tester;
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()); 114 EXPECT_TRUE(infobar->Cancel());
115 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar",
116 AutofillMetrics::INFOBAR_DENIED, 1);
136 } 117 }
137 118
138 // Dismiss the infobar. 119 // Dismiss the infobar.
139 { 120 {
140 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); 121 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate());
141 ASSERT_TRUE(infobar); 122
142 EXPECT_CALL(metric_logger, 123 base::HistogramTester histogram_tester;
143 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED))
144 .Times(1);
145 EXPECT_CALL(metric_logger,
146 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED))
147 .Times(0);
148 infobar->InfoBarDismissed(); 124 infobar->InfoBarDismissed();
125 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar",
126 AutofillMetrics::INFOBAR_DENIED, 1);
149 } 127 }
150 128
151 // Ignore the infobar. 129 // Ignore the infobar.
152 { 130 {
153 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); 131 scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate());
154 ASSERT_TRUE(infobar); 132
155 EXPECT_CALL(metric_logger, 133 base::HistogramTester histogram_tester;
156 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) 134 infobar.reset();
157 .Times(1); 135 histogram_tester.ExpectUniqueSample("Autofill.CreditCardInfoBar",
136 AutofillMetrics::INFOBAR_IGNORED, 1);
158 } 137 }
159 } 138 }
160 139
161 } // namespace autofill 140 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/browser/autofill/autofill_cc_infobar_delegate.cc ('k') | chrome/browser/ui/autofill/chrome_autofill_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698