| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string16.h" | 8 #include "base/string16.h" |
| 10 #include "base/time.h" | 9 #include "base/time.h" |
| 11 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 11 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 13 #include "chrome/browser/autofill/autofill_common_test.h" | 12 #include "chrome/browser/autofill/autofill_common_test.h" |
| 14 #include "chrome/browser/autofill/autofill_manager.h" | 13 #include "chrome/browser/autofill/autofill_manager.h" |
| 15 #include "chrome/browser/autofill/autofill_metrics.h" | 14 #include "chrome/browser/autofill/autofill_metrics.h" |
| 16 #include "chrome/browser/autofill/personal_data_manager.h" | 15 #include "chrome/browser/autofill/personal_data_manager.h" |
| 16 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 17 #include "chrome/browser/webdata/web_data_service.h" | 17 #include "chrome/browser/webdata/web_data_service.h" |
| 18 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 19 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
| 20 #include "content/browser/tab_contents/test_tab_contents.h" | 21 #include "content/browser/tab_contents/test_tab_contents.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "webkit/glue/form_data.h" | 24 #include "webkit/glue/form_data.h" |
| 24 #include "webkit/glue/form_field.h" | 25 #include "webkit/glue/form_field.h" |
| 25 | 26 |
| 26 using ::testing::_; | 27 using ::testing::_; |
| 27 using ::testing::AnyNumber; | 28 using ::testing::AnyNumber; |
| 28 using ::testing::Mock; | 29 using ::testing::Mock; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); | 75 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics); |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 class TestPersonalDataManager : public PersonalDataManager { | 78 class TestPersonalDataManager : public PersonalDataManager { |
| 78 public: | 79 public: |
| 79 TestPersonalDataManager() : autofill_enabled_(true) { | 80 TestPersonalDataManager() : autofill_enabled_(true) { |
| 80 set_metric_logger(new MockAutofillMetrics); | 81 set_metric_logger(new MockAutofillMetrics); |
| 81 CreateTestAutofillProfiles(&web_profiles_); | 82 CreateTestAutofillProfiles(&web_profiles_); |
| 82 } | 83 } |
| 83 | 84 |
| 85 // Factory method for keyed service. PersonalDataManager is NULL for testing. |
| 86 static ProfileKeyedService* Build(Profile* profile) { |
| 87 return NULL; |
| 88 } |
| 89 |
| 84 // Overridden to avoid a trip to the database. This should be a no-op except | 90 // Overridden to avoid a trip to the database. This should be a no-op except |
| 85 // for the side-effect of logging the profile count. | 91 // for the side-effect of logging the profile count. |
| 86 virtual void LoadProfiles() OVERRIDE { | 92 virtual void LoadProfiles() OVERRIDE { |
| 87 std::vector<AutofillProfile*> profiles; | 93 std::vector<AutofillProfile*> profiles; |
| 88 web_profiles_.release(&profiles); | 94 web_profiles_.release(&profiles); |
| 89 WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT, | 95 WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT, |
| 90 profiles); | 96 profiles); |
| 91 ReceiveLoadedProfiles(0, &result); | 97 ReceiveLoadedProfiles(0, &result); |
| 92 } | 98 } |
| 93 | 99 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 AutofillMetricsTest(); | 222 AutofillMetricsTest(); |
| 217 virtual ~AutofillMetricsTest(); | 223 virtual ~AutofillMetricsTest(); |
| 218 | 224 |
| 219 virtual void SetUp(); | 225 virtual void SetUp(); |
| 220 | 226 |
| 221 protected: | 227 protected: |
| 222 AutofillCCInfoBarDelegate* CreateDelegate(MockAutofillMetrics* metric_logger, | 228 AutofillCCInfoBarDelegate* CreateDelegate(MockAutofillMetrics* metric_logger, |
| 223 CreditCard** created_card); | 229 CreditCard** created_card); |
| 224 | 230 |
| 225 scoped_ptr<TestAutofillManager> autofill_manager_; | 231 scoped_ptr<TestAutofillManager> autofill_manager_; |
| 226 scoped_refptr<TestPersonalDataManager> test_personal_data_; | 232 TestPersonalDataManager personal_data_; |
| 227 | 233 |
| 228 private: | 234 private: |
| 229 BrowserThread browser_thread_; | 235 BrowserThread browser_thread_; |
| 230 | 236 |
| 231 DISALLOW_COPY_AND_ASSIGN(AutofillMetricsTest); | 237 DISALLOW_COPY_AND_ASSIGN(AutofillMetricsTest); |
| 232 }; | 238 }; |
| 233 | 239 |
| 234 AutofillMetricsTest::AutofillMetricsTest() | 240 AutofillMetricsTest::AutofillMetricsTest() |
| 235 : TabContentsWrapperTestHarness(), | 241 : TabContentsWrapperTestHarness(), |
| 236 browser_thread_(BrowserThread::UI, &message_loop_) { | 242 browser_thread_(BrowserThread::UI, &message_loop_) { |
| 237 } | 243 } |
| 238 | 244 |
| 239 AutofillMetricsTest::~AutofillMetricsTest() { | 245 AutofillMetricsTest::~AutofillMetricsTest() { |
| 240 // Order of destruction is important as AutofillManager relies on | 246 // Order of destruction is important as AutofillManager relies on |
| 241 // PersonalDataManager to be around when it gets destroyed. | 247 // PersonalDataManager to be around when it gets destroyed. |
| 242 autofill_manager_.reset(NULL); | 248 autofill_manager_.reset(NULL); |
| 243 test_personal_data_ = NULL; | |
| 244 } | 249 } |
| 245 | 250 |
| 246 void AutofillMetricsTest::SetUp() { | 251 void AutofillMetricsTest::SetUp() { |
| 252 Profile* profile = new TestingProfile(); |
| 253 browser_context_.reset(profile); |
| 254 PersonalDataManagerFactory::GetInstance()->SetTestingFactory( |
| 255 profile, TestPersonalDataManager::Build); |
| 256 |
| 247 TabContentsWrapperTestHarness::SetUp(); | 257 TabContentsWrapperTestHarness::SetUp(); |
| 248 test_personal_data_ = new TestPersonalDataManager(); | |
| 249 autofill_manager_.reset(new TestAutofillManager(contents_wrapper(), | 258 autofill_manager_.reset(new TestAutofillManager(contents_wrapper(), |
| 250 test_personal_data_.get())); | 259 &personal_data_)); |
| 251 } | 260 } |
| 252 | 261 |
| 253 AutofillCCInfoBarDelegate* AutofillMetricsTest::CreateDelegate( | 262 AutofillCCInfoBarDelegate* AutofillMetricsTest::CreateDelegate( |
| 254 MockAutofillMetrics* metric_logger, | 263 MockAutofillMetrics* metric_logger, |
| 255 CreditCard** created_card) { | 264 CreditCard** created_card) { |
| 256 EXPECT_CALL(*metric_logger, | 265 EXPECT_CALL(*metric_logger, |
| 257 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN)); | 266 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN)); |
| 258 // The delegate created below will take ownership of this object. | 267 // The delegate created below will take ownership of this object. |
| 259 CreditCard* credit_card = new CreditCard(); | 268 CreditCard* credit_card = new CreditCard(); |
| 260 if (created_card) | 269 if (created_card) |
| 261 *created_card = credit_card; | 270 *created_card = credit_card; |
| 262 return new AutofillCCInfoBarDelegate(contents(), credit_card, | 271 return new AutofillCCInfoBarDelegate(contents(), credit_card, |
| 263 test_personal_data_.get(), metric_logger); | 272 &personal_data_, metric_logger); |
| 264 } | 273 } |
| 265 | 274 |
| 266 // Test that we log quality metrics appropriately. | 275 // Test that we log quality metrics appropriately. |
| 267 TEST_F(AutofillMetricsTest, QualityMetrics) { | 276 TEST_F(AutofillMetricsTest, QualityMetrics) { |
| 268 // Set up our form data. | 277 // Set up our form data. |
| 269 FormData form; | 278 FormData form; |
| 270 form.name = ASCIIToUTF16("TestForm"); | 279 form.name = ASCIIToUTF16("TestForm"); |
| 271 form.method = ASCIIToUTF16("POST"); | 280 form.method = ASCIIToUTF16("POST"); |
| 272 form.origin = GURL("http://example.com/form.html"); | 281 form.origin = GURL("http://example.com/form.html"); |
| 273 form.action = GURL("http://example.com/submit.html"); | 282 form.action = GURL("http://example.com/submit.html"); |
| (...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 850 ADDRESS_HOME_COUNTRY, experiment_id)); | 859 ADDRESS_HOME_COUNTRY, experiment_id)); |
| 851 | 860 |
| 852 // Simulate form submission. | 861 // Simulate form submission. |
| 853 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form, | 862 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form, |
| 854 TimeTicks::Now())); | 863 TimeTicks::Now())); |
| 855 } | 864 } |
| 856 | 865 |
| 857 // Test that the profile count is logged correctly. | 866 // Test that the profile count is logged correctly. |
| 858 TEST_F(AutofillMetricsTest, StoredProfileCount) { | 867 TEST_F(AutofillMetricsTest, StoredProfileCount) { |
| 859 // The metric should be logged when the profiles are first loaded. | 868 // The metric should be logged when the profiles are first loaded. |
| 860 EXPECT_CALL(*test_personal_data_->metric_logger(), | 869 EXPECT_CALL(*personal_data_.metric_logger(), |
| 861 LogStoredProfileCount(2)).Times(1); | 870 LogStoredProfileCount(2)).Times(1); |
| 862 test_personal_data_->LoadProfiles(); | 871 personal_data_.LoadProfiles(); |
| 863 | 872 |
| 864 // The metric should only be logged once. | 873 // The metric should only be logged once. |
| 865 EXPECT_CALL(*test_personal_data_->metric_logger(), | 874 EXPECT_CALL(*personal_data_.metric_logger(), |
| 866 LogStoredProfileCount(::testing::_)).Times(0); | 875 LogStoredProfileCount(::testing::_)).Times(0); |
| 867 test_personal_data_->LoadProfiles(); | 876 personal_data_.LoadProfiles(); |
| 868 } | 877 } |
| 869 | 878 |
| 870 // Test that we correctly log whether Autofill is enabled. | 879 // Test that we correctly log whether Autofill is enabled. |
| 871 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) { | 880 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) { |
| 872 test_personal_data_->set_autofill_enabled(true); | 881 personal_data_.set_autofill_enabled(true); |
| 873 EXPECT_CALL(*test_personal_data_->metric_logger(), | 882 EXPECT_CALL(*personal_data_.metric_logger(), |
| 874 LogIsAutofillEnabledAtStartup(true)).Times(1); | 883 LogIsAutofillEnabledAtStartup(true)).Times(1); |
| 875 test_personal_data_->Init(NULL); | 884 personal_data_.Init(profile()); |
| 876 | 885 |
| 877 test_personal_data_->set_autofill_enabled(false); | 886 personal_data_.set_autofill_enabled(false); |
| 878 EXPECT_CALL(*test_personal_data_->metric_logger(), | 887 EXPECT_CALL(*personal_data_.metric_logger(), |
| 879 LogIsAutofillEnabledAtStartup(false)).Times(1); | 888 LogIsAutofillEnabledAtStartup(false)).Times(1); |
| 880 test_personal_data_->Init(NULL); | 889 personal_data_.Init(profile()); |
| 881 } | 890 } |
| 882 | 891 |
| 883 // Test that we log the number of Autofill suggestions when filling a form. | 892 // Test that we log the number of Autofill suggestions when filling a form. |
| 884 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) { | 893 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) { |
| 885 // Set up our form data. | 894 // Set up our form data. |
| 886 FormData form; | 895 FormData form; |
| 887 form.name = ASCIIToUTF16("TestForm"); | 896 form.name = ASCIIToUTF16("TestForm"); |
| 888 form.method = ASCIIToUTF16("POST"); | 897 form.method = ASCIIToUTF16("POST"); |
| 889 form.origin = GURL("http://example.com/form.html"); | 898 form.origin = GURL("http://example.com/form.html"); |
| 890 form.action = GURL("http://example.com/submit.html"); | 899 form.action = GURL("http://example.com/submit.html"); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 972 // Test that credit card infobar metrics are logged correctly. | 981 // Test that credit card infobar metrics are logged correctly. |
| 973 TEST_F(AutofillMetricsTest, CreditCardInfoBar) { | 982 TEST_F(AutofillMetricsTest, CreditCardInfoBar) { |
| 974 MockAutofillMetrics metric_logger; | 983 MockAutofillMetrics metric_logger; |
| 975 ::testing::InSequence dummy; | 984 ::testing::InSequence dummy; |
| 976 | 985 |
| 977 // Accept the infobar. | 986 // Accept the infobar. |
| 978 { | 987 { |
| 979 CreditCard* credit_card; | 988 CreditCard* credit_card; |
| 980 scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, | 989 scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, |
| 981 &credit_card)); | 990 &credit_card)); |
| 982 EXPECT_CALL(*test_personal_data_.get(), | 991 EXPECT_CALL(personal_data_, SaveImportedCreditCard(*credit_card)); |
| 983 SaveImportedCreditCard(*credit_card)); | |
| 984 EXPECT_CALL(metric_logger, | 992 EXPECT_CALL(metric_logger, |
| 985 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)).Times(1); | 993 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)).Times(1); |
| 986 EXPECT_CALL(metric_logger, | 994 EXPECT_CALL(metric_logger, |
| 987 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0); | 995 LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0); |
| 988 EXPECT_TRUE(static_cast<ConfirmInfoBarDelegate*>(infobar.get())->Accept()); | 996 EXPECT_TRUE(static_cast<ConfirmInfoBarDelegate*>(infobar.get())->Accept()); |
| 989 } | 997 } |
| 990 | 998 |
| 991 // Cancel the infobar. | 999 // Cancel the infobar. |
| 992 { | 1000 { |
| 993 scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, NULL)); | 1001 scoped_ptr<InfoBarDelegate> infobar(CreateDelegate(&metric_logger, NULL)); |
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1399 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), | 1407 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(), |
| 1400 TimeTicks::FromInternalValue(3)); | 1408 TimeTicks::FromInternalValue(3)); |
| 1401 autofill_manager_->OnFormSubmitted(form, TimeTicks::FromInternalValue(17)); | 1409 autofill_manager_->OnFormSubmitted(form, TimeTicks::FromInternalValue(17)); |
| 1402 autofill_manager_->Reset(); | 1410 autofill_manager_->Reset(); |
| 1403 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger()); | 1411 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger()); |
| 1404 } | 1412 } |
| 1405 | 1413 |
| 1406 // Restore the global Gmock verbosity level to its default value. | 1414 // Restore the global Gmock verbosity level to its default value. |
| 1407 ::testing::FLAGS_gmock_verbose = "warning"; | 1415 ::testing::FLAGS_gmock_verbose = "warning"; |
| 1408 } | 1416 } |
| OLD | NEW |