OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "base/ref_counted.h" |
| 6 #include "base/scoped_ptr.h" |
| 7 #include "base/string16.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/autofill/autofill_common_test.h" |
| 10 #include "chrome/browser/autofill/autofill_manager.h" |
| 11 #include "chrome/browser/autofill/autofill_metrics.h" |
| 12 #include "chrome/browser/autofill/personal_data_manager.h" |
| 13 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 14 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "webkit/glue/form_data.h" |
| 18 #include "webkit/glue/form_field.h" |
| 19 |
| 20 using webkit_glue::FormData; |
| 21 using webkit_glue::FormField; |
| 22 |
| 23 namespace { |
| 24 |
| 25 // TODO(isherman): Move this into autofill_common_test.h? |
| 26 class TestPersonalDataManager : public PersonalDataManager { |
| 27 public: |
| 28 TestPersonalDataManager() { |
| 29 CreateTestAutoFillProfiles(&web_profiles_); |
| 30 CreateTestCreditCards(&credit_cards_); |
| 31 } |
| 32 |
| 33 virtual void InitializeIfNeeded() {} |
| 34 virtual void SaveImportedFormData() {} |
| 35 virtual bool IsDataLoaded() const { return true; } |
| 36 |
| 37 private: |
| 38 void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) { |
| 39 AutoFillProfile* profile = new AutoFillProfile; |
| 40 autofill_test::SetProfileInfo(profile, "Home", "Elvis", "Aaron", |
| 41 "Presley", "theking@gmail.com", "RCA", |
| 42 "3734 Elvis Presley Blvd.", "Apt. 10", |
| 43 "Memphis", "Tennessee", "38116", "USA", |
| 44 "12345678901", ""); |
| 45 profile->set_guid("00000000-0000-0000-0000-000000000001"); |
| 46 profiles->push_back(profile); |
| 47 profile = new AutoFillProfile; |
| 48 autofill_test::SetProfileInfo(profile, "Work", "Charles", "Hardin", |
| 49 "Holley", "buddy@gmail.com", "Decca", |
| 50 "123 Apple St.", "unit 6", "Lubbock", |
| 51 "Texas", "79401", "USA", "23456789012", |
| 52 ""); |
| 53 profile->set_guid("00000000-0000-0000-0000-000000000002"); |
| 54 profiles->push_back(profile); |
| 55 profile = new AutoFillProfile; |
| 56 autofill_test::SetProfileInfo(profile, "Empty", "", "", "", "", "", "", "", |
| 57 "", "", "", "", "", ""); |
| 58 profile->set_guid("00000000-0000-0000-0000-000000000003"); |
| 59 profiles->push_back(profile); |
| 60 } |
| 61 |
| 62 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) { |
| 63 CreditCard* credit_card = new CreditCard; |
| 64 autofill_test::SetCreditCardInfo(credit_card, "First", "Elvis Presley", |
| 65 "4234567890123456", // Visa |
| 66 "04", "2012"); |
| 67 credit_card->set_guid("00000000-0000-0000-0000-000000000004"); |
| 68 credit_cards->push_back(credit_card); |
| 69 credit_card = new CreditCard; |
| 70 autofill_test::SetCreditCardInfo(credit_card, "Second", "Buddy Holly", |
| 71 "5187654321098765", // Mastercard |
| 72 "10", "2014"); |
| 73 credit_card->set_guid("00000000-0000-0000-0000-000000000005"); |
| 74 credit_cards->push_back(credit_card); |
| 75 credit_card = new CreditCard; |
| 76 autofill_test::SetCreditCardInfo(credit_card, "Empty", "", "", "", ""); |
| 77 credit_card->set_guid("00000000-0000-0000-0000-000000000006"); |
| 78 credit_cards->push_back(credit_card); |
| 79 } |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); |
| 82 }; |
| 83 |
| 84 class MockAutoFillMetrics : public AutoFillMetrics { |
| 85 public: |
| 86 MockAutoFillMetrics() {} |
| 87 MOCK_CONST_METHOD1(Log, void(ServerQueryMetric metric)); |
| 88 MOCK_CONST_METHOD1(Log, void(QualityMetric metric)); |
| 89 |
| 90 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(MockAutoFillMetrics); |
| 92 }; |
| 93 |
| 94 class TestAutoFillManager : public AutoFillManager { |
| 95 public: |
| 96 TestAutoFillManager(TabContents* tab_contents, |
| 97 TestPersonalDataManager* personal_manager) |
| 98 : AutoFillManager(tab_contents, personal_manager), |
| 99 autofill_enabled_(true) { |
| 100 set_metric_logger(new MockAutoFillMetrics); |
| 101 } |
| 102 virtual ~TestAutoFillManager() {} |
| 103 |
| 104 virtual bool IsAutoFillEnabled() const { return autofill_enabled_; } |
| 105 |
| 106 void set_autofill_enabled(bool autofill_enabled) { |
| 107 autofill_enabled_ = autofill_enabled; |
| 108 } |
| 109 |
| 110 const MockAutoFillMetrics* metric_logger() const { |
| 111 return static_cast<const MockAutoFillMetrics*>( |
| 112 AutoFillManager::metric_logger()); |
| 113 } |
| 114 |
| 115 private: |
| 116 bool autofill_enabled_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); |
| 119 }; |
| 120 |
| 121 } // namespace |
| 122 |
| 123 class AutoFillMetricsTest : public RenderViewHostTestHarness { |
| 124 public: |
| 125 AutoFillMetricsTest() {} |
| 126 virtual ~AutoFillMetricsTest() { |
| 127 // Order of destruction is important as AutoFillManager relies on |
| 128 // PersonalDataManager to be around when it gets destroyed. |
| 129 autofill_manager_.reset(NULL); |
| 130 test_personal_data_ = NULL; |
| 131 } |
| 132 |
| 133 virtual void SetUp() { |
| 134 RenderViewHostTestHarness::SetUp(); |
| 135 test_personal_data_ = new TestPersonalDataManager(); |
| 136 autofill_manager_.reset(new TestAutoFillManager(contents(), |
| 137 test_personal_data_.get())); |
| 138 } |
| 139 |
| 140 protected: |
| 141 scoped_ptr<TestAutoFillManager> autofill_manager_; |
| 142 scoped_refptr<TestPersonalDataManager> test_personal_data_; |
| 143 |
| 144 private: |
| 145 DISALLOW_COPY_AND_ASSIGN(AutoFillMetricsTest); |
| 146 }; |
| 147 |
| 148 // Test that we log quality metrics appropriately. |
| 149 TEST_F(AutoFillMetricsTest, QualityMetrics) { |
| 150 // Set up our form data. |
| 151 FormData form; |
| 152 form.name = ASCIIToUTF16("TestForm"); |
| 153 form.method = ASCIIToUTF16("POST"); |
| 154 form.origin = GURL("http://example.com/form.html"); |
| 155 form.action = GURL("http://example.com/submit.html"); |
| 156 form.user_submitted = true; |
| 157 |
| 158 FormField field; |
| 159 autofill_test::CreateTestFormField( |
| 160 "Autofilled", "autofilled", "Elvis Presley", "text", &field); |
| 161 field.set_autofilled(true); |
| 162 form.fields.push_back(field); |
| 163 autofill_test::CreateTestFormField( |
| 164 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field); |
| 165 form.fields.push_back(field); |
| 166 autofill_test::CreateTestFormField( |
| 167 "Empty", "empty", "", "text", &field); |
| 168 form.fields.push_back(field); |
| 169 autofill_test::CreateTestFormField( |
| 170 "Unknown", "unknown", "garbage", "text", &field); |
| 171 form.fields.push_back(field); |
| 172 autofill_test::CreateTestFormField( |
| 173 "Select", "select", "USA", "select-one", &field); |
| 174 form.fields.push_back(field); |
| 175 |
| 176 // Establish our expectations. |
| 177 ::testing::InSequence dummy; |
| 178 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 179 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 180 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 181 Log(AutoFillMetrics::FIELD_AUTOFILLED)); |
| 182 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 183 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 184 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 185 Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED )); |
| 186 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 187 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 188 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 189 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 190 |
| 191 // Simulate form submission. |
| 192 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form)); |
| 193 } |
| 194 |
| 195 // Test that we don't log quality metrics for non-autofillable forms. |
| 196 TEST_F(AutoFillMetricsTest, NoQualityMetricsForNonAutoFillableForms) { |
| 197 // Forms must include at least three fields to be auto-fillable. |
| 198 FormData form; |
| 199 form.name = ASCIIToUTF16("TestForm"); |
| 200 form.method = ASCIIToUTF16("POST"); |
| 201 form.origin = GURL("http://example.com/form.html"); |
| 202 form.action = GURL("http://example.com/submit.html"); |
| 203 form.user_submitted = true; |
| 204 |
| 205 FormField field; |
| 206 autofill_test::CreateTestFormField( |
| 207 "Autofilled", "autofilled", "Elvis Presley", "text", &field); |
| 208 field.set_autofilled(true); |
| 209 form.fields.push_back(field); |
| 210 autofill_test::CreateTestFormField( |
| 211 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field); |
| 212 form.fields.push_back(field); |
| 213 |
| 214 // Simulate form submission. |
| 215 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 216 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0); |
| 217 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form)); |
| 218 |
| 219 // Search forms are not auto-fillable. |
| 220 form.action = GURL("http://example.com/search?q=Elvis%20Presley"); |
| 221 autofill_test::CreateTestFormField( |
| 222 "Empty", "empty", "", "text", &field); |
| 223 form.fields.push_back(field); |
| 224 |
| 225 // Simulate form submission. |
| 226 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 227 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0); |
| 228 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form)); |
| 229 } |
OLD | NEW |