Chromium Code Reviews| Index: chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc |
| diff --git a/chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc b/chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8c3bd34df18f5e3b2a1b0bfba00f5e9765c2e698 |
| --- /dev/null |
| +++ b/chrome/browser/autofill/autofill_cc_infobar_delegate_unittest.cc |
| @@ -0,0 +1,282 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| + |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/time/time.h" |
| +#include "chrome/browser/autofill/personal_data_manager_factory.h" |
| +#include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" |
| +#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/autofill/core/browser/autofill_metrics.h" |
| +#include "components/autofill/core/browser/autofill_test_utils.h" |
| +#include "components/autofill/core/browser/form_structure.h" |
|
Ilya Sherman
2014/03/06 23:12:02
nit: This include is no longer needed. I bet that
blundell
2014/03/07 14:12:32
Done.
|
| +#include "components/autofill/core/browser/personal_data_manager.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::ASCIIToUTF16; |
| +using base::TimeDelta; |
| +using base::TimeTicks; |
|
Ilya Sherman
2014/03/06 23:12:02
nit: I don't think these three are needed.
blundell
2014/03/07 14:12:32
Done.
|
| +using testing::_; |
| +using testing::AnyNumber; |
| +using testing::Mock; |
|
Ilya Sherman
2014/03/06 23:12:02
nit: Are these two still needed?
blundell
2014/03/07 14:12:32
Done.
|
| + |
| +namespace autofill { |
| + |
| +namespace { |
| + |
| +class MockAutofillMetrics : public AutofillMetrics { |
| + public: |
| + MockAutofillMetrics() {} |
| + MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric)); |
| + MOCK_CONST_METHOD1(LogDeveloperEngagementMetric, |
| + void(DeveloperEngagementMetric metric)); |
| + MOCK_CONST_METHOD2(LogHeuristicTypePrediction, |
| + void(FieldTypeQualityMetric metric, |
| + ServerFieldType field_type)); |
| + MOCK_CONST_METHOD2(LogOverallTypePrediction, |
| + void(FieldTypeQualityMetric metric, |
| + ServerFieldType field_type)); |
| + MOCK_CONST_METHOD2(LogServerTypePrediction, |
| + void(FieldTypeQualityMetric metric, |
| + ServerFieldType field_type)); |
| + MOCK_CONST_METHOD1(LogServerQueryMetric, void(ServerQueryMetric metric)); |
| + MOCK_CONST_METHOD1(LogUserHappinessMetric, void(UserHappinessMetric metric)); |
| + MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithAutofill, |
| + void(const TimeDelta& duration)); |
| + MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithoutAutofill, |
| + void(const TimeDelta& duration)); |
| + MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithAutofill, |
| + void(const TimeDelta& duration)); |
| + MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithoutAutofill, |
| + void(const TimeDelta& duration)); |
| + MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled)); |
| + MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled)); |
| + MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles)); |
| + MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions)); |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); |
| +}; |
|
Ilya Sherman
2014/03/06 23:12:02
I've been meaning to kill this class entirely, act
blundell
2014/03/07 14:12:32
I kept the usage of AutofillMetrics but trimmed it
|
| + |
| +class TestPersonalDataManager : public PersonalDataManager { |
| + public: |
| + TestPersonalDataManager() |
| + : PersonalDataManager("en-US"), autofill_enabled_(true) { |
| + set_metric_logger(new testing::NiceMock<MockAutofillMetrics>()); |
|
Ilya Sherman
2014/03/06 23:12:02
This shouldn't be needed anymore.
blundell
2014/03/07 14:12:32
Done.
|
| + CreateTestAutofillProfiles(&web_profiles_); |
|
Ilya Sherman
2014/03/06 23:12:02
Nor should this.
blundell
2014/03/07 14:12:32
Done.
|
| + } |
| + |
| + using PersonalDataManager::set_database; |
| + using PersonalDataManager::SetPrefService; |
| + |
| + // Overridden to avoid a trip to the database. This should be a no-op except |
| + // for the side-effect of logging the profile count. |
| + virtual void LoadProfiles() OVERRIDE { |
| + std::vector<AutofillProfile*> profiles; |
| + web_profiles_.release(&profiles); |
| + WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT, |
| + profiles); |
| + ReceiveLoadedProfiles(0, &result); |
|
Ilya Sherman
2014/03/06 23:12:02
It should be fine to empty this out.
blundell
2014/03/07 14:12:32
Done.
|
| + } |
| + |
| + // Overridden to avoid a trip to the database. |
| + virtual void LoadCreditCards() OVERRIDE {} |
| + |
| + const MockAutofillMetrics* metric_logger() const { |
| + return static_cast<const MockAutofillMetrics*>( |
| + PersonalDataManager::metric_logger()); |
| + } |
|
Ilya Sherman
2014/03/06 23:12:02
This shouldn't be needed anymore.
blundell
2014/03/07 14:12:32
Done.
|
| + |
| + void set_autofill_enabled(bool autofill_enabled) { |
| + autofill_enabled_ = autofill_enabled; |
| + } |
| + |
| + virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } |
|
Ilya Sherman
2014/03/06 23:12:02
Ditto.
blundell
2014/03/07 14:12:32
Done.
|
| + |
| + MOCK_METHOD1(SaveImportedCreditCard, |
| + std::string(const CreditCard& imported_credit_card)); |
| + |
| + private: |
| + void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) { |
| + AutofillProfile* profile = new AutofillProfile; |
| + test::SetProfileInfo(profile, |
| + "Elvis", |
| + "Aaron", |
| + "Presley", |
| + "theking@gmail.com", |
| + "RCA", |
| + "3734 Elvis Presley Blvd.", |
| + "Apt. 10", |
| + "Memphis", |
| + "Tennessee", |
| + "38116", |
| + "US", |
| + "12345678901"); |
| + profile->set_guid("00000000-0000-0000-0000-000000000001"); |
| + profiles->push_back(profile); |
| + profile = new AutofillProfile; |
| + test::SetProfileInfo(profile, |
| + "Charles", |
| + "Hardin", |
| + "Holley", |
| + "buddy@gmail.com", |
| + "Decca", |
| + "123 Apple St.", |
| + "unit 6", |
| + "Lubbock", |
| + "Texas", |
| + "79401", |
| + "US", |
| + "2345678901"); |
| + profile->set_guid("00000000-0000-0000-0000-000000000002"); |
| + profiles->push_back(profile); |
| + } |
|
Ilya Sherman
2014/03/06 23:12:02
Ditto.
blundell
2014/03/07 14:12:32
Done.
|
| + |
| + bool autofill_enabled_; |
|
Ilya Sherman
2014/03/06 23:12:02
Ditto.
blundell
2014/03/07 14:12:32
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); |
| +}; |
| + |
| +class TestFormStructure : public FormStructure { |
| + public: |
| + explicit TestFormStructure(const FormData& form) : FormStructure(form) {} |
| + virtual ~TestFormStructure() {} |
| + |
| + void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types, |
| + const std::vector<ServerFieldType>& server_types) { |
| + ASSERT_EQ(field_count(), heuristic_types.size()); |
| + ASSERT_EQ(field_count(), server_types.size()); |
| + |
| + for (size_t i = 0; i < field_count(); ++i) { |
| + AutofillField* form_field = field(i); |
| + ASSERT_TRUE(form_field); |
| + form_field->set_heuristic_type(heuristic_types[i]); |
| + form_field->set_server_type(server_types[i]); |
| + } |
| + |
| + UpdateAutofillCount(); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestFormStructure); |
| +}; |
|
Ilya Sherman
2014/03/06 23:12:02
nit: This class doesn't seem to be used anywhere.
blundell
2014/03/07 14:12:32
Done.
|
| + |
| +} // namespace |
| + |
| +class AutofillCCInfobarDelegateTest : public ChromeRenderViewHostTestHarness { |
| + public: |
| + virtual ~AutofillCCInfobarDelegateTest(); |
| + |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + protected: |
| + scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate( |
| + MockAutofillMetrics* metric_logger); |
| + |
| + scoped_ptr<TestPersonalDataManager> personal_data_; |
| +}; |
| + |
| +AutofillCCInfobarDelegateTest::~AutofillCCInfobarDelegateTest() {} |
| + |
| +void AutofillCCInfobarDelegateTest::SetUp() { |
| + ChromeRenderViewHostTestHarness::SetUp(); |
| + |
| + // Ensure Mac OS X does not pop up a modal dialog for the Address Book. |
| + autofill::test::DisableSystemServices(profile()); |
| + |
| + PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL); |
| + |
| + TabAutofillManagerDelegate::CreateForWebContents(web_contents()); |
| + autofill::TabAutofillManagerDelegate* manager_delegate = |
| + autofill::TabAutofillManagerDelegate::FromWebContents(web_contents()); |
| + |
| + personal_data_.reset(new TestPersonalDataManager()); |
| + personal_data_->set_database(manager_delegate->GetDatabase()); |
| + personal_data_->SetPrefService(profile()->GetPrefs()); |
| +} |
| + |
| +void AutofillCCInfobarDelegateTest::TearDown() { |
| + personal_data_.reset(); |
| + ChromeRenderViewHostTestHarness::TearDown(); |
| +} |
| + |
| +scoped_ptr<ConfirmInfoBarDelegate> |
| +AutofillCCInfobarDelegateTest::CreateDelegate( |
| + MockAutofillMetrics* metric_logger) { |
| + EXPECT_CALL(*metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN)); |
| + |
| + CreditCard credit_card; |
| + return AutofillCCInfoBarDelegate::Create( |
| + metric_logger, |
| + base::Bind( |
| + base::IgnoreResult(&TestPersonalDataManager::SaveImportedCreditCard), |
| + base::Unretained(personal_data_.get()), |
| + credit_card)); |
| +} |
| + |
| +// Test that credit card infobar metrics are logged correctly. |
| +TEST_F(AutofillCCInfobarDelegateTest, Metrics) { |
| + testing::NiceMock<MockAutofillMetrics> metric_logger; |
|
Ilya Sherman
2014/03/06 23:12:02
nit: Shouldn't be a need for a NiceMock anymore on
blundell
2014/03/07 14:12:32
Done.
|
| + ::testing::InSequence dummy; |
| + |
| + // Accept the infobar. |
| + { |
| + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| + ASSERT_TRUE(infobar); |
| + EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_)); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)); |
| + |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| + .Times(0); |
| + EXPECT_TRUE(infobar->Accept()); |
| + } |
| + |
| + // Cancel the infobar. |
| + { |
| + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| + ASSERT_TRUE(infobar); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)) |
| + .Times(1); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| + .Times(0); |
| + EXPECT_TRUE(infobar->Cancel()); |
| + } |
| + |
| + // Dismiss the infobar. |
| + { |
| + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| + ASSERT_TRUE(infobar); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)) |
| + .Times(1); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| + .Times(0); |
| + infobar->InfoBarDismissed(); |
| + } |
| + |
| + // Ignore the infobar. |
| + { |
| + scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger)); |
| + ASSERT_TRUE(infobar); |
| + EXPECT_CALL(metric_logger, |
| + LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)) |
| + .Times(1); |
| + } |
| +} |
| + |
| +} // namespace autofill |