| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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> |
| 6 |
| 5 #include "base/ref_counted.h" | 7 #include "base/ref_counted.h" |
| 6 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 7 #include "base/string16.h" | 9 #include "base/string16.h" |
| 8 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/autofill/autofill_common_test.h" | 11 #include "chrome/browser/autofill/autofill_common_test.h" |
| 10 #include "chrome/browser/autofill/autofill_manager.h" | 12 #include "chrome/browser/autofill/autofill_manager.h" |
| 11 #include "chrome/browser/autofill/autofill_metrics.h" | 13 #include "chrome/browser/autofill/autofill_metrics.h" |
| 12 #include "chrome/browser/autofill/personal_data_manager.h" | 14 #include "chrome/browser/autofill/personal_data_manager.h" |
| 13 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 15 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 14 #include "chrome/browser/tab_contents/test_tab_contents.h" | 16 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 107 |
| 106 void set_autofill_enabled(bool autofill_enabled) { | 108 void set_autofill_enabled(bool autofill_enabled) { |
| 107 autofill_enabled_ = autofill_enabled; | 109 autofill_enabled_ = autofill_enabled; |
| 108 } | 110 } |
| 109 | 111 |
| 110 const MockAutoFillMetrics* metric_logger() const { | 112 const MockAutoFillMetrics* metric_logger() const { |
| 111 return static_cast<const MockAutoFillMetrics*>( | 113 return static_cast<const MockAutoFillMetrics*>( |
| 112 AutoFillManager::metric_logger()); | 114 AutoFillManager::metric_logger()); |
| 113 } | 115 } |
| 114 | 116 |
| 117 void AddSeenForm(FormStructure* form) { |
| 118 form_structures()->push_back(form); |
| 119 } |
| 120 |
| 115 private: | 121 private: |
| 116 bool autofill_enabled_; | 122 bool autofill_enabled_; |
| 117 | 123 |
| 118 DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); | 124 DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); |
| 119 }; | 125 }; |
| 120 | 126 |
| 127 class TestFormStructure : public FormStructure { |
| 128 public: |
| 129 explicit TestFormStructure(const FormData& form) : FormStructure(form) {} |
| 130 virtual ~TestFormStructure() {} |
| 131 |
| 132 void SetFieldTypes(const std::vector<AutoFillFieldType>& heuristic_types, |
| 133 const std::vector<AutoFillFieldType>& server_types) { |
| 134 ASSERT_EQ(field_count(), heuristic_types.size()); |
| 135 ASSERT_EQ(field_count(), server_types.size()); |
| 136 |
| 137 for (size_t i = 0; i < field_count(); ++i) { |
| 138 AutoFillField* field = (*fields())[i]; |
| 139 ASSERT_TRUE(field); |
| 140 field->set_heuristic_type(heuristic_types[i]); |
| 141 field->set_server_type(server_types[i]); |
| 142 } |
| 143 |
| 144 UpdateAutoFillCount(); |
| 145 } |
| 146 |
| 147 private: |
| 148 DISALLOW_COPY_AND_ASSIGN(TestFormStructure); |
| 149 }; |
| 150 |
| 121 } // namespace | 151 } // namespace |
| 122 | 152 |
| 123 class AutoFillMetricsTest : public RenderViewHostTestHarness { | 153 class AutoFillMetricsTest : public RenderViewHostTestHarness { |
| 124 public: | 154 public: |
| 125 AutoFillMetricsTest() {} | 155 AutoFillMetricsTest() {} |
| 126 virtual ~AutoFillMetricsTest() { | 156 virtual ~AutoFillMetricsTest() { |
| 127 // Order of destruction is important as AutoFillManager relies on | 157 // Order of destruction is important as AutoFillManager relies on |
| 128 // PersonalDataManager to be around when it gets destroyed. | 158 // PersonalDataManager to be around when it gets destroyed. |
| 129 autofill_manager_.reset(NULL); | 159 autofill_manager_.reset(NULL); |
| 130 test_personal_data_ = NULL; | 160 test_personal_data_ = NULL; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 205 |
| 176 // Establish our expectations. | 206 // Establish our expectations. |
| 177 ::testing::InSequence dummy; | 207 ::testing::InSequence dummy; |
| 178 EXPECT_CALL(*autofill_manager_->metric_logger(), | 208 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 179 Log(AutoFillMetrics::FIELD_SUBMITTED)); | 209 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 180 EXPECT_CALL(*autofill_manager_->metric_logger(), | 210 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 181 Log(AutoFillMetrics::FIELD_AUTOFILLED)); | 211 Log(AutoFillMetrics::FIELD_AUTOFILLED)); |
| 182 EXPECT_CALL(*autofill_manager_->metric_logger(), | 212 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 183 Log(AutoFillMetrics::FIELD_SUBMITTED)); | 213 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 184 EXPECT_CALL(*autofill_manager_->metric_logger(), | 214 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 185 Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED )); | 215 Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED)); |
| 216 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 217 Log(AutoFillMetrics::FIELD_HEURISTIC_TYPE_UNKNOWN)); |
| 218 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 219 Log(AutoFillMetrics::FIELD_SERVER_TYPE_UNKNOWN)); |
| 186 EXPECT_CALL(*autofill_manager_->metric_logger(), | 220 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 187 Log(AutoFillMetrics::FIELD_SUBMITTED)); | 221 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 188 EXPECT_CALL(*autofill_manager_->metric_logger(), | 222 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 189 Log(AutoFillMetrics::FIELD_SUBMITTED)); | 223 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 190 | 224 |
| 191 // Simulate form submission. | 225 // Simulate form submission. |
| 192 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form)); | 226 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form)); |
| 193 } | 227 } |
| 194 | 228 |
| 229 // Test that we log the appropriate additional metrics when AutoFill failed. |
| 230 TEST_F(AutoFillMetricsTest, QualityMetricsForFailure) { |
| 231 // Set up our form data. |
| 232 FormData form; |
| 233 form.name = ASCIIToUTF16("TestForm"); |
| 234 form.method = ASCIIToUTF16("POST"); |
| 235 form.origin = GURL("http://example.com/form.html"); |
| 236 form.action = GURL("http://example.com/submit.html"); |
| 237 form.user_submitted = true; |
| 238 |
| 239 struct { |
| 240 const char* label; |
| 241 const char* name; |
| 242 const char* value; |
| 243 AutoFillFieldType heuristic_type; |
| 244 AutoFillFieldType server_type; |
| 245 AutoFillMetrics::QualityMetric heuristic_metric; |
| 246 AutoFillMetrics::QualityMetric server_metric; |
| 247 } failure_cases[] = { |
| 248 { |
| 249 "Heuristics unknown, server unknown", "0,0", "Elvis", |
| 250 UNKNOWN_TYPE, NO_SERVER_DATA, |
| 251 AutoFillMetrics::FIELD_HEURISTIC_TYPE_UNKNOWN, |
| 252 AutoFillMetrics::FIELD_SERVER_TYPE_UNKNOWN |
| 253 }, |
| 254 { |
| 255 "Heuristics match, server unknown", "1,0", "Aaron", |
| 256 NAME_MIDDLE, NO_SERVER_DATA, |
| 257 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MATCH, |
| 258 AutoFillMetrics::FIELD_SERVER_TYPE_UNKNOWN |
| 259 }, |
| 260 { |
| 261 "Heuristics mismatch, server unknown", "2,0", "Presley", |
| 262 PHONE_HOME_NUMBER, NO_SERVER_DATA, |
| 263 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MISMATCH, |
| 264 AutoFillMetrics::FIELD_SERVER_TYPE_UNKNOWN |
| 265 }, |
| 266 { |
| 267 "Heuristics unknown, server match", "0,1", "theking@gmail.com", |
| 268 UNKNOWN_TYPE, EMAIL_ADDRESS, |
| 269 AutoFillMetrics::FIELD_HEURISTIC_TYPE_UNKNOWN, |
| 270 AutoFillMetrics::FIELD_SERVER_TYPE_MATCH |
| 271 }, |
| 272 { |
| 273 "Heuristics match, server match", "1,1", "3734 Elvis Presley Blvd.", |
| 274 ADDRESS_HOME_LINE1, ADDRESS_HOME_LINE1, |
| 275 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MATCH, |
| 276 AutoFillMetrics::FIELD_SERVER_TYPE_MATCH |
| 277 }, |
| 278 { |
| 279 "Heuristics mismatch, server match", "2,1", "Apt. 10", |
| 280 PHONE_HOME_NUMBER, ADDRESS_HOME_LINE2, |
| 281 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MISMATCH, |
| 282 AutoFillMetrics::FIELD_SERVER_TYPE_MATCH |
| 283 }, |
| 284 { |
| 285 "Heuristics unknown, server mismatch", "0,2", "Memphis", |
| 286 UNKNOWN_TYPE, PHONE_HOME_NUMBER, |
| 287 AutoFillMetrics::FIELD_HEURISTIC_TYPE_UNKNOWN, |
| 288 AutoFillMetrics::FIELD_SERVER_TYPE_MISMATCH |
| 289 }, |
| 290 { |
| 291 "Heuristics match, server mismatch", "1,2", "Tennessee", |
| 292 ADDRESS_HOME_STATE, PHONE_HOME_NUMBER, |
| 293 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MATCH, |
| 294 AutoFillMetrics::FIELD_SERVER_TYPE_MISMATCH |
| 295 }, |
| 296 { |
| 297 "Heuristics mismatch, server mismatch", "2,2", "38116", |
| 298 PHONE_HOME_NUMBER, PHONE_HOME_NUMBER, |
| 299 AutoFillMetrics::FIELD_HEURISTIC_TYPE_MISMATCH, |
| 300 AutoFillMetrics::FIELD_SERVER_TYPE_MISMATCH |
| 301 } |
| 302 }; |
| 303 |
| 304 std::vector<AutoFillFieldType> heuristic_types, server_types; |
| 305 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) { |
| 306 FormField field; |
| 307 autofill_test::CreateTestFormField(failure_cases[i].label, |
| 308 failure_cases[i].name, |
| 309 failure_cases[i].value, "text", &field); |
| 310 form.fields.push_back(field); |
| 311 heuristic_types.push_back(failure_cases[i].heuristic_type); |
| 312 server_types.push_back(failure_cases[i].server_type); |
| 313 |
| 314 } |
| 315 |
| 316 // Simulate having seen this form with the desired heuristic and server types. |
| 317 // |form_structure| will be owned by |autofill_manager_|. |
| 318 TestFormStructure* form_structure = new TestFormStructure(form); |
| 319 form_structure->SetFieldTypes(heuristic_types, server_types); |
| 320 autofill_manager_->AddSeenForm(form_structure); |
| 321 |
| 322 // Establish our expectations. Only print gmock errors, as the warnings are |
| 323 // too verbose. |
| 324 ::testing::InSequence dummy; |
| 325 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) { |
| 326 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 327 Log(AutoFillMetrics::FIELD_SUBMITTED)); |
| 328 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 329 Log(AutoFillMetrics::FIELD_AUTOFILL_FAILED)); |
| 330 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 331 Log(failure_cases[i].heuristic_metric)); |
| 332 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 333 Log(failure_cases[i].server_metric)); |
| 334 } |
| 335 |
| 336 // Simulate form submission. |
| 337 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form)); |
| 338 } |
| 339 |
| 195 // Test that we don't log quality metrics for non-autofillable forms. | 340 // Test that we don't log quality metrics for non-autofillable forms. |
| 196 TEST_F(AutoFillMetricsTest, NoQualityMetricsForNonAutoFillableForms) { | 341 TEST_F(AutoFillMetricsTest, NoQualityMetricsForNonAutoFillableForms) { |
| 197 // Forms must include at least three fields to be auto-fillable. | 342 // Forms must include at least three fields to be auto-fillable. |
| 198 FormData form; | 343 FormData form; |
| 199 form.name = ASCIIToUTF16("TestForm"); | 344 form.name = ASCIIToUTF16("TestForm"); |
| 200 form.method = ASCIIToUTF16("POST"); | 345 form.method = ASCIIToUTF16("POST"); |
| 201 form.origin = GURL("http://example.com/form.html"); | 346 form.origin = GURL("http://example.com/form.html"); |
| 202 form.action = GURL("http://example.com/submit.html"); | 347 form.action = GURL("http://example.com/submit.html"); |
| 203 form.user_submitted = true; | 348 form.user_submitted = true; |
| 204 | 349 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 220 form.action = GURL("http://example.com/search?q=Elvis%20Presley"); | 365 form.action = GURL("http://example.com/search?q=Elvis%20Presley"); |
| 221 autofill_test::CreateTestFormField( | 366 autofill_test::CreateTestFormField( |
| 222 "Empty", "empty", "", "text", &field); | 367 "Empty", "empty", "", "text", &field); |
| 223 form.fields.push_back(field); | 368 form.fields.push_back(field); |
| 224 | 369 |
| 225 // Simulate form submission. | 370 // Simulate form submission. |
| 226 EXPECT_CALL(*autofill_manager_->metric_logger(), | 371 EXPECT_CALL(*autofill_manager_->metric_logger(), |
| 227 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0); | 372 Log(AutoFillMetrics::FIELD_SUBMITTED)).Times(0); |
| 228 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form)); | 373 EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form)); |
| 229 } | 374 } |
| OLD | NEW |