1 // Copyright 2013 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.
5 #include "components/autofill/core/browser/autofill_metrics.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "components/autofill/core/browser/autofill_external_delegate.h"
17 #include "components/autofill/core/browser/autofill_manager.h"
18 #include "components/autofill/core/browser/autofill_test_utils.h"
19 #include "components/autofill/core/browser/personal_data_manager.h"
20 #include "components/autofill/core/browser/test_autofill_client.h"
21 #include "components/autofill/core/browser/test_autofill_driver.h"
22 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
23 #include "components/autofill/core/common/form_data.h"
24 #include "components/autofill/core/common/form_field_data.h"
25 #include "components/webdata/common/web_data_results.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gfx/rect.h"
31 using base::ASCIIToUTF16;
32 using base::TimeDelta;
33 using base::TimeTicks;
35 using testing::AnyNumber;
42 class MockAutofillMetrics : public AutofillMetrics {
44 MockAutofillMetrics() {}
45 MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
46 MOCK_CONST_METHOD1(LogDeveloperEngagementMetric,
47 void(DeveloperEngagementMetric metric));
48 MOCK_CONST_METHOD2(LogHeuristicTypePrediction,
49 void(FieldTypeQualityMetric metric,
50 ServerFieldType field_type));
51 MOCK_CONST_METHOD2(LogOverallTypePrediction,
52 void(FieldTypeQualityMetric metric,
53 ServerFieldType field_type));
54 MOCK_CONST_METHOD2(LogServerTypePrediction,
55 void(FieldTypeQualityMetric metric,
56 ServerFieldType field_type));
57 MOCK_CONST_METHOD1(LogServerQueryMetric, void(ServerQueryMetric metric));
58 MOCK_CONST_METHOD1(LogUserHappinessMetric, void(UserHappinessMetric metric));
59 MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithAutofill,
60 void(const TimeDelta& duration));
61 MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithoutAutofill,
62 void(const TimeDelta& duration));
63 MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithAutofill,
64 void(const TimeDelta& duration));
65 MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithoutAutofill,
66 void(const TimeDelta& duration));
67 MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled));
68 MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled));
69 MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles));
70 MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions));
73 DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
76 class TestPersonalDataManager : public PersonalDataManager {
78 TestPersonalDataManager()
79 : PersonalDataManager("en-US"),
80 autofill_enabled_(true) {
81 set_metric_logger(new testing::NiceMock<MockAutofillMetrics>());
82 CreateTestAutofillProfiles(&web_profiles_);
85 using PersonalDataManager::set_database;
86 using PersonalDataManager::SetPrefService;
88 // Overridden to avoid a trip to the database. This should be a no-op except
89 // for the side-effect of logging the profile count.
90 virtual void LoadProfiles() override {
91 std::vector<AutofillProfile*> profiles;
92 web_profiles_.release(&profiles);
93 WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
95 ReceiveLoadedProfiles(0, &result);
98 // Overridden to avoid a trip to the database.
99 virtual void LoadCreditCards() override {}
101 const MockAutofillMetrics* metric_logger() const {
102 return static_cast<const MockAutofillMetrics*>(
103 PersonalDataManager::metric_logger());
106 void set_autofill_enabled(bool autofill_enabled) {
107 autofill_enabled_ = autofill_enabled;
110 virtual bool IsAutofillEnabled() const override {
111 return autofill_enabled_;
114 MOCK_METHOD1(SaveImportedCreditCard,
115 std::string(const CreditCard& imported_credit_card));
118 void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
119 AutofillProfile* profile = new AutofillProfile;
120 test::SetProfileInfo(profile, "Elvis", "Aaron",
121 "Presley", "theking@gmail.com", "RCA",
122 "3734 Elvis Presley Blvd.", "Apt. 10",
123 "Memphis", "Tennessee", "38116", "US",
125 profile->set_guid("00000000-0000-0000-0000-000000000001");
126 profiles->push_back(profile);
127 profile = new AutofillProfile;
128 test::SetProfileInfo(profile, "Charles", "Hardin",
129 "Holley", "buddy@gmail.com", "Decca",
130 "123 Apple St.", "unit 6", "Lubbock",
131 "Texas", "79401", "US", "2345678901");
132 profile->set_guid("00000000-0000-0000-0000-000000000002");
133 profiles->push_back(profile);
136 bool autofill_enabled_;
138 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
141 class TestFormStructure : public FormStructure {
143 explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
144 ~TestFormStructure() override {}
146 void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
147 const std::vector<ServerFieldType>& server_types) {
148 ASSERT_EQ(field_count(), heuristic_types.size());
149 ASSERT_EQ(field_count(), server_types.size());
151 for (size_t i = 0; i < field_count(); ++i) {
152 AutofillField* form_field = field(i);
153 ASSERT_TRUE(form_field);
154 form_field->set_heuristic_type(heuristic_types[i]);
155 form_field->set_server_type(server_types[i]);
158 UpdateAutofillCount();
162 DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
165 class TestAutofillManager : public AutofillManager {
167 TestAutofillManager(AutofillDriver* driver,
168 AutofillClient* autofill_client,
169 TestPersonalDataManager* personal_manager)
170 : AutofillManager(driver, autofill_client, personal_manager),
171 autofill_enabled_(true) {
172 set_metric_logger(new testing::NiceMock<MockAutofillMetrics>);
174 ~TestAutofillManager() override {}
176 bool IsAutofillEnabled() const override { return autofill_enabled_; }
178 void set_autofill_enabled(bool autofill_enabled) {
179 autofill_enabled_ = autofill_enabled;
182 MockAutofillMetrics* metric_logger() {
183 return static_cast<MockAutofillMetrics*>(const_cast<AutofillMetrics*>(
184 AutofillManager::metric_logger()));
187 void AddSeenForm(const FormData& form,
188 const std::vector<ServerFieldType>& heuristic_types,
189 const std::vector<ServerFieldType>& server_types) {
190 FormData empty_form = form;
191 for (size_t i = 0; i < empty_form.fields.size(); ++i) {
192 empty_form.fields[i].value = base::string16();
195 // |form_structure| will be owned by |form_structures()|.
196 TestFormStructure* form_structure = new TestFormStructure(empty_form);
197 form_structure->SetFieldTypes(heuristic_types, server_types);
198 form_structures()->push_back(form_structure);
201 void FormSubmitted(const FormData& form, const TimeTicks& timestamp) {
202 run_loop_.reset(new base::RunLoop());
203 if (!OnFormSubmitted(form, timestamp))
206 // Wait for the asynchronous FormSubmitted() call to complete.
210 void UploadFormDataAsyncCallback(
211 const FormStructure* submitted_form,
212 const base::TimeTicks& load_time,
213 const base::TimeTicks& interaction_time,
214 const base::TimeTicks& submission_time) override {
217 AutofillManager::UploadFormDataAsyncCallback(submitted_form,
224 bool autofill_enabled_;
225 scoped_ptr<base::RunLoop> run_loop_;
227 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
232 class AutofillMetricsTest : public testing::Test {
234 ~AutofillMetricsTest() override;
236 void SetUp() override;
237 void TearDown() override;
240 base::MessageLoop message_loop_;
241 TestAutofillClient autofill_client_;
242 scoped_ptr<TestAutofillDriver> autofill_driver_;
243 scoped_ptr<TestAutofillManager> autofill_manager_;
244 scoped_ptr<TestPersonalDataManager> personal_data_;
245 scoped_ptr<AutofillExternalDelegate> external_delegate_;
248 AutofillMetricsTest::~AutofillMetricsTest() {
249 // Order of destruction is important as AutofillManager relies on
250 // PersonalDataManager to be around when it gets destroyed.
251 autofill_manager_.reset();
254 void AutofillMetricsTest::SetUp() {
255 autofill_client_.SetPrefs(test::PrefServiceForTesting());
257 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
258 test::DisableSystemServices(autofill_client_.GetPrefs());
260 personal_data_.reset(new TestPersonalDataManager());
261 personal_data_->set_database(autofill_client_.GetDatabase());
262 personal_data_->SetPrefService(autofill_client_.GetPrefs());
263 autofill_driver_.reset(new TestAutofillDriver());
264 autofill_manager_.reset(new TestAutofillManager(
265 autofill_driver_.get(), &autofill_client_, personal_data_.get()));
267 external_delegate_.reset(new AutofillExternalDelegate(
268 autofill_manager_.get(),
269 autofill_driver_.get()));
270 autofill_manager_->SetExternalDelegate(external_delegate_.get());
273 void AutofillMetricsTest::TearDown() {
274 // Order of destruction is important as AutofillManager relies on
275 // PersonalDataManager to be around when it gets destroyed.
276 autofill_manager_.reset();
277 autofill_driver_.reset();
278 personal_data_.reset();
281 // Test that we log quality metrics appropriately.
282 TEST_F(AutofillMetricsTest, QualityMetrics) {
283 // Set up our form data.
285 form.name = ASCIIToUTF16("TestForm");
286 form.origin = GURL("http://example.com/form.html");
287 form.action = GURL("http://example.com/submit.html");
288 form.user_submitted = true;
290 std::vector<ServerFieldType> heuristic_types, server_types;
293 test::CreateTestFormField(
294 "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
295 field.is_autofilled = true;
296 form.fields.push_back(field);
297 heuristic_types.push_back(NAME_FULL);
298 server_types.push_back(NAME_FIRST);
300 test::CreateTestFormField(
301 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
302 field.is_autofilled = false;
303 form.fields.push_back(field);
304 heuristic_types.push_back(PHONE_HOME_NUMBER);
305 server_types.push_back(EMAIL_ADDRESS);
307 test::CreateTestFormField("Empty", "empty", "", "text", &field);
308 field.is_autofilled = false;
309 form.fields.push_back(field);
310 heuristic_types.push_back(NAME_FULL);
311 server_types.push_back(NAME_FIRST);
313 test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
314 field.is_autofilled = false;
315 form.fields.push_back(field);
316 heuristic_types.push_back(PHONE_HOME_NUMBER);
317 server_types.push_back(EMAIL_ADDRESS);
319 test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
320 field.is_autofilled = false;
321 form.fields.push_back(field);
322 heuristic_types.push_back(UNKNOWN_TYPE);
323 server_types.push_back(NO_SERVER_DATA);
325 test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field);
326 field.is_autofilled = true;
327 form.fields.push_back(field);
328 heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
329 server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
331 // Simulate having seen this form on page load.
332 autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
334 // Establish our expectations.
335 ::testing::InSequence dummy;
337 EXPECT_CALL(*autofill_manager_->metric_logger(),
338 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
340 EXPECT_CALL(*autofill_manager_->metric_logger(),
341 LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
343 EXPECT_CALL(*autofill_manager_->metric_logger(),
344 LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
346 // Non-autofilled field for which we had data
347 EXPECT_CALL(*autofill_manager_->metric_logger(),
348 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
350 EXPECT_CALL(*autofill_manager_->metric_logger(),
351 LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
353 EXPECT_CALL(*autofill_manager_->metric_logger(),
354 LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
359 EXPECT_CALL(*autofill_manager_->metric_logger(),
360 LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
361 ADDRESS_HOME_COUNTRY));
362 EXPECT_CALL(*autofill_manager_->metric_logger(),
363 LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
364 ADDRESS_HOME_COUNTRY));
365 EXPECT_CALL(*autofill_manager_->metric_logger(),
366 LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
367 ADDRESS_HOME_COUNTRY));
369 EXPECT_CALL(*autofill_manager_->metric_logger(),
370 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
371 PHONE_HOME_WHOLE_NUMBER));
372 EXPECT_CALL(*autofill_manager_->metric_logger(),
373 LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
374 PHONE_HOME_WHOLE_NUMBER));
375 EXPECT_CALL(*autofill_manager_->metric_logger(),
376 LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
377 PHONE_HOME_WHOLE_NUMBER));
378 EXPECT_CALL(*autofill_manager_->metric_logger(),
379 LogUserHappinessMetric(
380 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
382 // Simulate form submission.
383 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
387 // Test that we behave sanely when the cached form differs from the submitted
389 TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
390 // Set up our form data.
392 form.name = ASCIIToUTF16("TestForm");
393 form.origin = GURL("http://example.com/form.html");
394 form.action = GURL("http://example.com/submit.html");
395 form.user_submitted = true;
397 std::vector<ServerFieldType> heuristic_types, server_types;
400 test::CreateTestFormField(
401 "Both match", "match", "Elvis Aaron Presley", "text", &field);
402 field.is_autofilled = true;
403 form.fields.push_back(field);
404 heuristic_types.push_back(NAME_FULL);
405 server_types.push_back(NAME_FULL);
406 test::CreateTestFormField(
407 "Both mismatch", "mismatch", "buddy@gmail.com", "text", &field);
408 field.is_autofilled = false;
409 form.fields.push_back(field);
410 heuristic_types.push_back(PHONE_HOME_NUMBER);
411 server_types.push_back(PHONE_HOME_NUMBER);
412 test::CreateTestFormField(
413 "Only heuristics match", "mixed", "Memphis", "text", &field);
414 field.is_autofilled = false;
415 form.fields.push_back(field);
416 heuristic_types.push_back(ADDRESS_HOME_CITY);
417 server_types.push_back(PHONE_HOME_NUMBER);
418 test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
419 field.is_autofilled = false;
420 form.fields.push_back(field);
421 heuristic_types.push_back(UNKNOWN_TYPE);
422 server_types.push_back(UNKNOWN_TYPE);
424 // Simulate having seen this form with the desired heuristic and server types.
425 // |form_structure| will be owned by |autofill_manager_|.
426 autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
429 // Add a field and re-arrange the remaining form fields before submitting.
430 std::vector<FormFieldData> cached_fields = form.fields;
432 test::CreateTestFormField(
433 "New field", "new field", "Tennessee", "text", &field);
434 form.fields.push_back(field);
435 form.fields.push_back(cached_fields[2]);
436 form.fields.push_back(cached_fields[1]);
437 form.fields.push_back(cached_fields[3]);
438 form.fields.push_back(cached_fields[0]);
440 // Establish our expectations.
441 ::testing::InSequence dummy;
443 EXPECT_CALL(*autofill_manager_->metric_logger(),
444 LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
445 ADDRESS_HOME_STATE));
446 EXPECT_CALL(*autofill_manager_->metric_logger(),
447 LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
448 ADDRESS_HOME_STATE));
449 EXPECT_CALL(*autofill_manager_->metric_logger(),
450 LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
451 ADDRESS_HOME_STATE));
452 // Only heuristics match
453 EXPECT_CALL(*autofill_manager_->metric_logger(),
454 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
456 EXPECT_CALL(*autofill_manager_->metric_logger(),
457 LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
459 EXPECT_CALL(*autofill_manager_->metric_logger(),
460 LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
463 EXPECT_CALL(*autofill_manager_->metric_logger(),
464 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
466 EXPECT_CALL(*autofill_manager_->metric_logger(),
467 LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
469 EXPECT_CALL(*autofill_manager_->metric_logger(),
470 LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
474 EXPECT_CALL(*autofill_manager_->metric_logger(),
475 LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
477 EXPECT_CALL(*autofill_manager_->metric_logger(),
478 LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
480 EXPECT_CALL(*autofill_manager_->metric_logger(),
481 LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
484 // Simulate form submission.
485 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
489 // Verify that we correctly log metrics regarding developer engagement.
490 TEST_F(AutofillMetricsTest, DeveloperEngagement) {
491 // Start with a non-fillable form.
493 form.name = ASCIIToUTF16("TestForm");
494 form.origin = GURL("http://example.com/form.html");
495 form.action = GURL("http://example.com/submit.html");
498 test::CreateTestFormField("Name", "name", "", "text", &field);
499 form.fields.push_back(field);
500 test::CreateTestFormField("Email", "email", "", "text", &field);
501 form.fields.push_back(field);
503 std::vector<FormData> forms(1, form);
505 // Ensure no metrics are logged when loading a non-fillable form.
507 EXPECT_CALL(*autofill_manager_->metric_logger(),
508 LogDeveloperEngagementMetric(_)).Times(0);
509 autofill_manager_->OnFormsSeen(forms, TimeTicks());
510 autofill_manager_->Reset();
511 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
514 // Add another field to the form, so that it becomes fillable.
515 test::CreateTestFormField("Phone", "phone", "", "text", &field);
516 forms.back().fields.push_back(field);
518 // Expect only the "form parsed" metric to be logged; no metrics about
519 // author-specified field type hints.
522 *autofill_manager_->metric_logger(),
523 LogDeveloperEngagementMetric(
524 AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
526 *autofill_manager_->metric_logger(),
527 LogDeveloperEngagementMetric(
528 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(0);
529 autofill_manager_->OnFormsSeen(forms, TimeTicks());
530 autofill_manager_->Reset();
531 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
534 // Add some fields with an author-specified field type to the form.
535 // We need to add at least three fields, because a form must have at least
536 // three fillable fields to be considered to be autofillable; and if at least
537 // one field specifies an explicit type hint, we don't apply any of our usual
538 // local heuristics to detect field types in the rest of the form.
539 test::CreateTestFormField("", "", "", "text", &field);
540 field.autocomplete_attribute = "given-name";
541 forms.back().fields.push_back(field);
542 test::CreateTestFormField("", "", "", "text", &field);
543 field.autocomplete_attribute = "email";
544 forms.back().fields.push_back(field);
545 test::CreateTestFormField("", "", "", "text", &field);
546 field.autocomplete_attribute = "address-line1";
547 forms.back().fields.push_back(field);
549 // Expect both the "form parsed" metric and the author-specified field type
550 // hints metric to be logged.
553 *autofill_manager_->metric_logger(),
554 LogDeveloperEngagementMetric(
555 AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
557 *autofill_manager_->metric_logger(),
558 LogDeveloperEngagementMetric(
559 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(1);
560 autofill_manager_->OnFormsSeen(forms, TimeTicks());
561 autofill_manager_->Reset();
562 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
566 // Test that the profile count is logged correctly.
567 TEST_F(AutofillMetricsTest, StoredProfileCount) {
568 // The metric should be logged when the profiles are first loaded.
569 EXPECT_CALL(*personal_data_->metric_logger(),
570 LogStoredProfileCount(2)).Times(1);
571 personal_data_->LoadProfiles();
573 // The metric should only be logged once.
574 EXPECT_CALL(*personal_data_->metric_logger(),
575 LogStoredProfileCount(::testing::_)).Times(0);
576 personal_data_->LoadProfiles();
579 // Test that we correctly log when Autofill is enabled.
580 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
581 personal_data_->set_autofill_enabled(true);
582 EXPECT_CALL(*personal_data_->metric_logger(),
583 LogIsAutofillEnabledAtStartup(true)).Times(1);
584 personal_data_->Init(
585 autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
588 // Test that we correctly log when Autofill is disabled.
589 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
590 personal_data_->set_autofill_enabled(false);
591 EXPECT_CALL(*personal_data_->metric_logger(),
592 LogIsAutofillEnabledAtStartup(false)).Times(1);
593 personal_data_->Init(
594 autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
597 // Test that we log the number of Autofill suggestions when filling a form.
598 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
599 // Set up our form data.
601 form.name = ASCIIToUTF16("TestForm");
602 form.origin = GURL("http://example.com/form.html");
603 form.action = GURL("http://example.com/submit.html");
604 form.user_submitted = true;
607 std::vector<ServerFieldType> field_types;
608 test::CreateTestFormField("Name", "name", "", "text", &field);
609 form.fields.push_back(field);
610 field_types.push_back(NAME_FULL);
611 test::CreateTestFormField("Email", "email", "", "email", &field);
612 form.fields.push_back(field);
613 field_types.push_back(EMAIL_ADDRESS);
614 test::CreateTestFormField("Phone", "phone", "", "tel", &field);
615 form.fields.push_back(field);
616 field_types.push_back(PHONE_HOME_NUMBER);
618 // Simulate having seen this form on page load.
619 // |form_structure| will be owned by |autofill_manager_|.
620 autofill_manager_->AddSeenForm(form, field_types, field_types);
622 // Establish our expectations.
623 ::testing::InSequence dummy;
624 EXPECT_CALL(*autofill_manager_->metric_logger(),
625 LogAddressSuggestionsCount(2)).Times(1);
627 // Simulate activating the autofill popup for the phone field.
628 autofill_manager_->OnQueryFormFieldAutofill(
629 0, form, field, gfx::Rect(), false);
631 // Simulate activating the autofill popup for the email field after typing.
632 // No new metric should be logged, since we're still on the same page.
633 test::CreateTestFormField("Email", "email", "b", "email", &field);
634 autofill_manager_->OnQueryFormFieldAutofill(
635 0, form, field, gfx::Rect(), false);
637 // Reset the autofill manager state.
638 autofill_manager_->Reset();
639 autofill_manager_->AddSeenForm(form, field_types, field_types);
641 // Establish our expectations.
642 EXPECT_CALL(*autofill_manager_->metric_logger(),
643 LogAddressSuggestionsCount(1)).Times(1);
645 // Simulate activating the autofill popup for the email field after typing.
646 autofill_manager_->OnQueryFormFieldAutofill(
647 0, form, field, gfx::Rect(), false);
649 // Reset the autofill manager state again.
650 autofill_manager_->Reset();
651 autofill_manager_->AddSeenForm(form, field_types, field_types);
653 // Establish our expectations.
654 EXPECT_CALL(*autofill_manager_->metric_logger(),
655 LogAddressSuggestionsCount(::testing::_)).Times(0);
657 // Simulate activating the autofill popup for the email field after typing.
658 form.fields[0].is_autofilled = true;
659 autofill_manager_->OnQueryFormFieldAutofill(
660 0, form, field, gfx::Rect(), false);
663 // Test that we log whether Autofill is enabled when filling a form.
664 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
665 // Establish our expectations.
666 ::testing::InSequence dummy;
667 EXPECT_CALL(*autofill_manager_->metric_logger(),
668 LogIsAutofillEnabledAtPageLoad(true)).Times(1);
670 autofill_manager_->set_autofill_enabled(true);
671 autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
673 // Reset the autofill manager state.
674 autofill_manager_->Reset();
676 // Establish our expectations.
677 EXPECT_CALL(*autofill_manager_->metric_logger(),
678 LogIsAutofillEnabledAtPageLoad(false)).Times(1);
680 autofill_manager_->set_autofill_enabled(false);
681 autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
684 // Verify that we correctly log user happiness metrics dealing with form loading
685 // and form submission.
686 TEST_F(AutofillMetricsTest, UserHappinessFormLoadAndSubmission) {
687 // Start with a form with insufficiently many fields.
689 form.name = ASCIIToUTF16("TestForm");
690 form.origin = GURL("http://example.com/form.html");
691 form.action = GURL("http://example.com/submit.html");
692 form.user_submitted = true;
695 test::CreateTestFormField("Name", "name", "", "text", &field);
696 form.fields.push_back(field);
697 test::CreateTestFormField("Email", "email", "", "text", &field);
698 form.fields.push_back(field);
700 std::vector<FormData> forms(1, form);
702 // Expect no notifications when the form is first seen.
704 EXPECT_CALL(*autofill_manager_->metric_logger(),
705 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED)).Times(0);
706 autofill_manager_->OnFormsSeen(forms, TimeTicks());
710 // Expect no notifications when the form is submitted.
713 *autofill_manager_->metric_logger(),
714 LogUserHappinessMetric(
715 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL)).Times(0);
717 *autofill_manager_->metric_logger(),
718 LogUserHappinessMetric(
719 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME)).Times(0);
721 *autofill_manager_->metric_logger(),
722 LogUserHappinessMetric(
723 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE)).Times(0);
725 *autofill_manager_->metric_logger(),
726 LogUserHappinessMetric(
727 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM)).Times(0);
728 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
731 // Add more fields to the form.
732 test::CreateTestFormField("Phone", "phone", "", "text", &field);
733 form.fields.push_back(field);
734 test::CreateTestFormField("Unknown", "unknown", "", "text", &field);
735 form.fields.push_back(field);
736 forms.front() = form;
738 // Expect a notification when the form is first seen.
740 EXPECT_CALL(*autofill_manager_->metric_logger(),
741 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
742 autofill_manager_->OnFormsSeen(forms, TimeTicks());
745 // Expect a notification when the form is submitted.
747 EXPECT_CALL(*autofill_manager_->metric_logger(),
748 LogUserHappinessMetric(
749 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
750 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
753 // Fill in two of the fields.
754 form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
755 form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
756 forms.front() = form;
758 // Expect a notification when the form is submitted.
760 EXPECT_CALL(*autofill_manager_->metric_logger(),
761 LogUserHappinessMetric(
762 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
763 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
766 // Fill in the third field.
767 form.fields[2].value = ASCIIToUTF16("12345678901");
768 forms.front() = form;
770 // Expect notifications when the form is submitted.
772 EXPECT_CALL(*autofill_manager_->metric_logger(),
773 LogUserHappinessMetric(
774 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE));
775 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
779 // Mark one of the fields as autofilled.
780 form.fields[1].is_autofilled = true;
781 forms.front() = form;
783 // Expect notifications when the form is submitted.
785 EXPECT_CALL(*autofill_manager_->metric_logger(),
786 LogUserHappinessMetric(
787 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
788 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
791 // Mark all of the fillable fields as autofilled.
792 form.fields[0].is_autofilled = true;
793 form.fields[2].is_autofilled = true;
794 forms.front() = form;
796 // Expect notifications when the form is submitted.
798 EXPECT_CALL(*autofill_manager_->metric_logger(),
799 LogUserHappinessMetric(
800 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL));
801 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
804 // Clear out the third field's value.
805 form.fields[2].value = base::string16();
806 forms.front() = form;
808 // Expect notifications when the form is submitted.
810 EXPECT_CALL(*autofill_manager_->metric_logger(),
811 LogUserHappinessMetric(
812 AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
813 autofill_manager_->FormSubmitted(form, TimeTicks::Now());
817 // Verify that we correctly log user happiness metrics dealing with form
819 TEST_F(AutofillMetricsTest, UserHappinessFormInteraction) {
820 // Load a fillable form.
822 form.name = ASCIIToUTF16("TestForm");
823 form.origin = GURL("http://example.com/form.html");
824 form.action = GURL("http://example.com/submit.html");
825 form.user_submitted = true;
828 test::CreateTestFormField("Name", "name", "", "text", &field);
829 form.fields.push_back(field);
830 test::CreateTestFormField("Email", "email", "", "text", &field);
831 form.fields.push_back(field);
832 test::CreateTestFormField("Phone", "phone", "", "text", &field);
833 form.fields.push_back(field);
835 std::vector<FormData> forms(1, form);
837 // Expect a notification when the form is first seen.
839 EXPECT_CALL(*autofill_manager_->metric_logger(),
840 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
841 autofill_manager_->OnFormsSeen(forms, TimeTicks());
846 EXPECT_CALL(*autofill_manager_->metric_logger(),
847 LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE));
848 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
852 // Simulate suggestions shown twice for a single edit (i.e. multiple
853 // keystrokes in a single field).
855 EXPECT_CALL(*autofill_manager_->metric_logger(),
856 LogUserHappinessMetric(
857 AutofillMetrics::SUGGESTIONS_SHOWN)).Times(1);
858 EXPECT_CALL(*autofill_manager_->metric_logger(),
859 LogUserHappinessMetric(
860 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(1);
861 autofill_manager_->DidShowSuggestions(true);
862 autofill_manager_->DidShowSuggestions(false);
865 // Simulate suggestions shown for a different field.
867 EXPECT_CALL(*autofill_manager_->metric_logger(),
868 LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN));
869 EXPECT_CALL(*autofill_manager_->metric_logger(),
870 LogUserHappinessMetric(
871 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(0);
872 autofill_manager_->DidShowSuggestions(true);
875 // Simulate invoking autofill.
877 EXPECT_CALL(*autofill_manager_->metric_logger(),
878 LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
879 EXPECT_CALL(*autofill_manager_->metric_logger(),
880 LogUserHappinessMetric(
881 AutofillMetrics::USER_DID_AUTOFILL_ONCE));
882 autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
885 // Simulate editing an autofilled field.
887 EXPECT_CALL(*autofill_manager_->metric_logger(),
888 LogUserHappinessMetric(
889 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
890 EXPECT_CALL(*autofill_manager_->metric_logger(),
891 LogUserHappinessMetric(
892 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE));
893 PersonalDataManager::GUIDPair guid(
894 "00000000-0000-0000-0000-000000000001", 0);
895 PersonalDataManager::GUIDPair empty(std::string(), 0);
896 autofill_manager_->FillOrPreviewForm(
897 AutofillDriver::FORM_DATA_ACTION_FILL,
898 0, form, form.fields.front(),
899 autofill_manager_->PackGUIDs(empty, guid));
900 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
902 // Simulate a second keystroke; make sure we don't log the metric twice.
903 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
907 // Simulate invoking autofill again.
908 EXPECT_CALL(*autofill_manager_->metric_logger(),
909 LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
910 EXPECT_CALL(*autofill_manager_->metric_logger(),
911 LogUserHappinessMetric(
912 AutofillMetrics::USER_DID_AUTOFILL_ONCE)).Times(0);
913 autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
915 // Simulate editing another autofilled field.
917 EXPECT_CALL(*autofill_manager_->metric_logger(),
918 LogUserHappinessMetric(
919 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
920 autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
924 // Verify that we correctly log metrics tracking the duration of form fill.
925 TEST_F(AutofillMetricsTest, FormFillDuration) {
926 // Load a fillable form.
928 form.name = ASCIIToUTF16("TestForm");
929 form.origin = GURL("http://example.com/form.html");
930 form.action = GURL("http://example.com/submit.html");
931 form.user_submitted = true;
934 test::CreateTestFormField("Name", "name", "", "text", &field);
935 form.fields.push_back(field);
936 test::CreateTestFormField("Email", "email", "", "text", &field);
937 form.fields.push_back(field);
938 test::CreateTestFormField("Phone", "phone", "", "text", &field);
939 form.fields.push_back(field);
941 std::vector<FormData> forms(1, form);
943 // Fill additional form.
944 FormData second_form = form;
945 test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field);
946 second_form.fields.push_back(field);
948 std::vector<FormData> second_forms(1, second_form);
950 // Fill the field values for form submission.
951 form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
952 form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
953 form.fields[2].value = ASCIIToUTF16("12345678901");
955 // Fill the field values for form submission.
956 second_form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
957 second_form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
958 second_form.fields[2].value = ASCIIToUTF16("12345678901");
959 second_form.fields[3].value = ASCIIToUTF16("51512345678");
961 // Expect only form load metrics to be logged if the form is submitted without
964 EXPECT_CALL(*autofill_manager_->metric_logger(),
965 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
966 EXPECT_CALL(*autofill_manager_->metric_logger(),
967 LogFormFillDurationFromLoadWithoutAutofill(
968 TimeDelta::FromInternalValue(16)));
969 EXPECT_CALL(*autofill_manager_->metric_logger(),
970 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
971 EXPECT_CALL(*autofill_manager_->metric_logger(),
972 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
973 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
974 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
975 autofill_manager_->Reset();
976 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
979 // Expect metric to be logged if the user manually edited a form field.
981 EXPECT_CALL(*autofill_manager_->metric_logger(),
982 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
983 EXPECT_CALL(*autofill_manager_->metric_logger(),
984 LogFormFillDurationFromLoadWithoutAutofill(
985 TimeDelta::FromInternalValue(16)));
986 EXPECT_CALL(*autofill_manager_->metric_logger(),
987 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
988 EXPECT_CALL(*autofill_manager_->metric_logger(),
989 LogFormFillDurationFromInteractionWithoutAutofill(
990 TimeDelta::FromInternalValue(14)));
991 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
992 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
993 TimeTicks::FromInternalValue(3));
994 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
995 autofill_manager_->Reset();
996 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
999 // Expect metric to be logged if the user autofilled the form.
1000 form.fields[0].is_autofilled = true;
1002 EXPECT_CALL(*autofill_manager_->metric_logger(),
1003 LogFormFillDurationFromLoadWithAutofill(
1004 TimeDelta::FromInternalValue(16)));
1005 EXPECT_CALL(*autofill_manager_->metric_logger(),
1006 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1007 EXPECT_CALL(*autofill_manager_->metric_logger(),
1008 LogFormFillDurationFromInteractionWithAutofill(
1009 TimeDelta::FromInternalValue(12)));
1010 EXPECT_CALL(*autofill_manager_->metric_logger(),
1011 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1012 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
1013 autofill_manager_->OnDidFillAutofillFormData(
1014 TimeTicks::FromInternalValue(5));
1015 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1016 autofill_manager_->Reset();
1017 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1020 // Expect metric to be logged if the user both manually filled some fields
1021 // and autofilled others. Messages can arrive out of order, so make sure they
1022 // take precedence appropriately.
1024 EXPECT_CALL(*autofill_manager_->metric_logger(),
1025 LogFormFillDurationFromLoadWithAutofill(
1026 TimeDelta::FromInternalValue(16)));
1027 EXPECT_CALL(*autofill_manager_->metric_logger(),
1028 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1029 EXPECT_CALL(*autofill_manager_->metric_logger(),
1030 LogFormFillDurationFromInteractionWithAutofill(
1031 TimeDelta::FromInternalValue(14)));
1032 EXPECT_CALL(*autofill_manager_->metric_logger(),
1033 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1034 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
1035 autofill_manager_->OnDidFillAutofillFormData(
1036 TimeTicks::FromInternalValue(5));
1037 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1038 TimeTicks::FromInternalValue(3));
1039 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1040 autofill_manager_->Reset();
1041 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1044 // Make sure that loading another form doesn't affect metrics from the first
1047 EXPECT_CALL(*autofill_manager_->metric_logger(),
1048 LogFormFillDurationFromLoadWithAutofill(
1049 TimeDelta::FromInternalValue(16)));
1050 EXPECT_CALL(*autofill_manager_->metric_logger(),
1051 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1052 EXPECT_CALL(*autofill_manager_->metric_logger(),
1053 LogFormFillDurationFromInteractionWithAutofill(
1054 TimeDelta::FromInternalValue(14)));
1055 EXPECT_CALL(*autofill_manager_->metric_logger(),
1056 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1057 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
1058 autofill_manager_->OnFormsSeen(second_forms,
1059 TimeTicks::FromInternalValue(3));
1060 autofill_manager_->OnDidFillAutofillFormData(
1061 TimeTicks::FromInternalValue(5));
1062 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1063 TimeTicks::FromInternalValue(3));
1064 autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1065 autofill_manager_->Reset();
1066 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1069 // Make sure that submitting a form that was loaded later will report the
1070 // later loading time.
1072 EXPECT_CALL(*autofill_manager_->metric_logger(),
1073 LogFormFillDurationFromLoadWithoutAutofill(
1074 TimeDelta::FromInternalValue(12)));
1075 EXPECT_CALL(*autofill_manager_->metric_logger(),
1076 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
1077 EXPECT_CALL(*autofill_manager_->metric_logger(),
1078 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
1079 EXPECT_CALL(*autofill_manager_->metric_logger(),
1080 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1081 autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
1082 autofill_manager_->OnFormsSeen(second_forms,
1083 TimeTicks::FromInternalValue(5));
1084 autofill_manager_->FormSubmitted(second_form,
1085 TimeTicks::FromInternalValue(17));
1086 autofill_manager_->Reset();
1087 Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1091 } // namespace autofill