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/scoped_ptr.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string16.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/test/histogram_tester.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/wallet/real_pan_wallet_client.h"
23 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
24 #include "components/autofill/core/common/autofill_pref_names.h"
25 #include "components/autofill/core/common/form_data.h"
26 #include "components/autofill/core/common/form_field_data.h"
27 #include "components/webdata/common/web_data_results.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/gfx/geometry/rect.h"
32 using base::ASCIIToUTF16
;
33 using base::TimeTicks
;
38 class TestPersonalDataManager
: public PersonalDataManager
{
40 TestPersonalDataManager()
41 : PersonalDataManager("en-US"),
42 autofill_enabled_(true) {
43 CreateTestAutofillProfiles(&web_profiles_
);
46 using PersonalDataManager::set_database
;
47 using PersonalDataManager::SetPrefService
;
49 // Overridden to avoid a trip to the database. This should be a no-op except
50 // for the side-effect of logging the profile count.
51 void LoadProfiles() override
{
53 std::vector
<AutofillProfile
*> profiles
;
54 web_profiles_
.release(&profiles
);
55 WDResult
<std::vector
<AutofillProfile
*> > result(AUTOFILL_PROFILES_RESULT
,
57 pending_profiles_query_
= 123;
58 OnWebDataServiceRequestDone(pending_profiles_query_
, &result
);
61 std::vector
<AutofillProfile
*> profiles
;
62 server_profiles_
.release(&profiles
);
63 WDResult
<std::vector
<AutofillProfile
*> > result(AUTOFILL_PROFILES_RESULT
,
65 pending_server_profiles_query_
= 124;
66 OnWebDataServiceRequestDone(pending_server_profiles_query_
, &result
);
70 // Overridden to avoid a trip to the database.
71 void LoadCreditCards() override
{
73 std::vector
<CreditCard
*> credit_cards
;
74 local_credit_cards_
.release(&credit_cards
);
75 WDResult
<std::vector
<CreditCard
*> > result(
76 AUTOFILL_CREDITCARDS_RESULT
, credit_cards
);
77 pending_creditcards_query_
= 125;
78 OnWebDataServiceRequestDone(pending_creditcards_query_
, &result
);
81 std::vector
<CreditCard
*> credit_cards
;
82 server_credit_cards_
.release(&credit_cards
);
83 WDResult
<std::vector
<CreditCard
*> > result(
84 AUTOFILL_CREDITCARDS_RESULT
, credit_cards
);
85 pending_server_creditcards_query_
= 126;
86 OnWebDataServiceRequestDone(pending_server_creditcards_query_
, &result
);
90 void set_autofill_enabled(bool autofill_enabled
) {
91 autofill_enabled_
= autofill_enabled
;
94 // Removes all existing profiles and creates 0 or 1 local profiles and 0 or 1
95 // server profile according to the paramters.
96 void RecreateProfiles(bool include_local_profile
,
97 bool include_server_profile
) {
98 web_profiles_
.clear();
99 server_profiles_
.clear();
100 if (include_local_profile
) {
101 AutofillProfile
* profile
= new AutofillProfile
;
102 test::SetProfileInfo(profile
, "Elvis", "Aaron",
103 "Presley", "theking@gmail.com", "RCA",
104 "3734 Elvis Presley Blvd.", "Apt. 10",
105 "Memphis", "Tennessee", "38116", "US",
107 profile
->set_guid("00000000-0000-0000-0000-000000000001");
108 web_profiles_
.push_back(profile
);
110 if (include_server_profile
) {
111 AutofillProfile
* profile
= new AutofillProfile(
112 AutofillProfile::SERVER_PROFILE
, "server_id");
113 test::SetProfileInfo(profile
, "Charles", "Hardin",
114 "Holley", "buddy@gmail.com", "Decca",
115 "123 Apple St.", "unit 6", "Lubbock",
116 "Texas", "79401", "US", "2345678901");
117 profile
->set_guid("00000000-0000-0000-0000-000000000002");
118 server_profiles_
.push_back(profile
);
123 // Removes all existing credit cards and creates 0 or 1 local profiles and
124 // 0 or 1 server profile according to the paramters.
125 void RecreateCreditCards(bool include_local_credit_card
,
126 bool include_masked_server_credit_card
,
127 bool include_full_server_credit_card
) {
128 local_credit_cards_
.clear();
129 server_credit_cards_
.clear();
130 if (include_local_credit_card
) {
131 CreditCard
* credit_card
= new CreditCard
;
132 credit_card
->set_guid("10000000-0000-0000-0000-000000000001");
133 local_credit_cards_
.push_back(credit_card
);
135 if (include_masked_server_credit_card
) {
136 CreditCard
* credit_card
= new CreditCard(
137 CreditCard::MASKED_SERVER_CARD
, "server_id");
138 credit_card
->set_guid("10000000-0000-0000-0000-000000000002");
139 credit_card
->SetTypeForMaskedCard(kDiscoverCard
);
140 server_credit_cards_
.push_back(credit_card
);
142 if (include_full_server_credit_card
) {
143 CreditCard
* credit_card
= new CreditCard(
144 CreditCard::FULL_SERVER_CARD
, "server_id");
145 credit_card
->set_guid("10000000-0000-0000-0000-000000000003");
146 server_credit_cards_
.push_back(credit_card
);
151 bool IsAutofillEnabled() const override
{ return autofill_enabled_
; }
154 void CreateTestAutofillProfiles(ScopedVector
<AutofillProfile
>* profiles
) {
155 AutofillProfile
* profile
= new AutofillProfile
;
156 test::SetProfileInfo(profile
, "Elvis", "Aaron",
157 "Presley", "theking@gmail.com", "RCA",
158 "3734 Elvis Presley Blvd.", "Apt. 10",
159 "Memphis", "Tennessee", "38116", "US",
161 profile
->set_guid("00000000-0000-0000-0000-000000000001");
162 profiles
->push_back(profile
);
163 profile
= new AutofillProfile
;
164 test::SetProfileInfo(profile
, "Charles", "Hardin",
165 "Holley", "buddy@gmail.com", "Decca",
166 "123 Apple St.", "unit 6", "Lubbock",
167 "Texas", "79401", "US", "2345678901");
168 profile
->set_guid("00000000-0000-0000-0000-000000000002");
169 profiles
->push_back(profile
);
172 bool autofill_enabled_
;
174 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager
);
177 class TestFormStructure
: public FormStructure
{
179 explicit TestFormStructure(const FormData
& form
) : FormStructure(form
) {}
180 ~TestFormStructure() override
{}
182 void SetFieldTypes(const std::vector
<ServerFieldType
>& heuristic_types
,
183 const std::vector
<ServerFieldType
>& server_types
) {
184 ASSERT_EQ(field_count(), heuristic_types
.size());
185 ASSERT_EQ(field_count(), server_types
.size());
187 for (size_t i
= 0; i
< field_count(); ++i
) {
188 AutofillField
* form_field
= field(i
);
189 ASSERT_TRUE(form_field
);
190 form_field
->set_heuristic_type(heuristic_types
[i
]);
191 form_field
->set_server_type(server_types
[i
]);
194 UpdateAutofillCount();
198 DISALLOW_COPY_AND_ASSIGN(TestFormStructure
);
201 class TestAutofillManager
: public AutofillManager
{
203 TestAutofillManager(AutofillDriver
* driver
,
204 AutofillClient
* autofill_client
,
205 TestPersonalDataManager
* personal_manager
)
206 : AutofillManager(driver
, autofill_client
, personal_manager
),
207 autofill_enabled_(true) {}
208 ~TestAutofillManager() override
{}
210 bool IsAutofillEnabled() const override
{ return autofill_enabled_
; }
212 void set_autofill_enabled(bool autofill_enabled
) {
213 autofill_enabled_
= autofill_enabled
;
216 void AddSeenForm(const FormData
& form
,
217 const std::vector
<ServerFieldType
>& heuristic_types
,
218 const std::vector
<ServerFieldType
>& server_types
) {
219 FormData empty_form
= form
;
220 for (size_t i
= 0; i
< empty_form
.fields
.size(); ++i
) {
221 empty_form
.fields
[i
].value
= base::string16();
224 // |form_structure| will be owned by |form_structures()|.
225 TestFormStructure
* form_structure
= new TestFormStructure(empty_form
);
226 form_structure
->SetFieldTypes(heuristic_types
, server_types
);
227 form_structures()->push_back(form_structure
);
230 void FormSubmitted(const FormData
& form
, const TimeTicks
& timestamp
) {
231 run_loop_
.reset(new base::RunLoop());
232 if (!OnFormSubmitted(form
, timestamp
))
235 // Wait for the asynchronous FormSubmitted() call to complete.
239 void UploadFormDataAsyncCallback(
240 const FormStructure
* submitted_form
,
241 const base::TimeTicks
& load_time
,
242 const base::TimeTicks
& interaction_time
,
243 const base::TimeTicks
& submission_time
) override
{
246 AutofillManager::UploadFormDataAsyncCallback(submitted_form
,
253 bool autofill_enabled_
;
254 scoped_ptr
<base::RunLoop
> run_loop_
;
256 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager
);
261 // This is defined in the autofill_metrics.cc implementation file.
262 int GetFieldTypeGroupMetric(ServerFieldType field_type
,
263 AutofillMetrics::FieldTypeQualityMetric metric
);
265 class AutofillMetricsTest
: public testing::Test
{
267 ~AutofillMetricsTest() override
;
269 void SetUp() override
;
270 void TearDown() override
;
273 base::MessageLoop message_loop_
;
274 TestAutofillClient autofill_client_
;
275 scoped_ptr
<TestAutofillDriver
> autofill_driver_
;
276 scoped_ptr
<TestAutofillManager
> autofill_manager_
;
277 scoped_ptr
<TestPersonalDataManager
> personal_data_
;
278 scoped_ptr
<AutofillExternalDelegate
> external_delegate_
;
281 AutofillMetricsTest::~AutofillMetricsTest() {
282 // Order of destruction is important as AutofillManager relies on
283 // PersonalDataManager to be around when it gets destroyed.
284 autofill_manager_
.reset();
287 void AutofillMetricsTest::SetUp() {
288 autofill_client_
.SetPrefs(test::PrefServiceForTesting());
290 // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
291 test::DisableSystemServices(autofill_client_
.GetPrefs());
293 personal_data_
.reset(new TestPersonalDataManager());
294 personal_data_
->set_database(autofill_client_
.GetDatabase());
295 personal_data_
->SetPrefService(autofill_client_
.GetPrefs());
296 autofill_driver_
.reset(new TestAutofillDriver());
297 autofill_manager_
.reset(new TestAutofillManager(
298 autofill_driver_
.get(), &autofill_client_
, personal_data_
.get()));
300 external_delegate_
.reset(new AutofillExternalDelegate(
301 autofill_manager_
.get(),
302 autofill_driver_
.get()));
303 autofill_manager_
->SetExternalDelegate(external_delegate_
.get());
306 void AutofillMetricsTest::TearDown() {
307 // Order of destruction is important as AutofillManager relies on
308 // PersonalDataManager to be around when it gets destroyed.
309 autofill_manager_
.reset();
310 autofill_driver_
.reset();
311 personal_data_
.reset();
314 // Test that we log quality metrics appropriately.
315 TEST_F(AutofillMetricsTest
, QualityMetrics
) {
316 // Set up our form data.
318 form
.name
= ASCIIToUTF16("TestForm");
319 form
.origin
= GURL("http://example.com/form.html");
320 form
.action
= GURL("http://example.com/submit.html");
321 form
.user_submitted
= true;
323 std::vector
<ServerFieldType
> heuristic_types
, server_types
;
326 test::CreateTestFormField(
327 "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field
);
328 field
.is_autofilled
= true;
329 form
.fields
.push_back(field
);
330 heuristic_types
.push_back(NAME_FULL
);
331 server_types
.push_back(NAME_FIRST
);
333 test::CreateTestFormField(
334 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field
);
335 field
.is_autofilled
= false;
336 form
.fields
.push_back(field
);
337 heuristic_types
.push_back(PHONE_HOME_NUMBER
);
338 server_types
.push_back(EMAIL_ADDRESS
);
340 test::CreateTestFormField("Empty", "empty", "", "text", &field
);
341 field
.is_autofilled
= false;
342 form
.fields
.push_back(field
);
343 heuristic_types
.push_back(NAME_FULL
);
344 server_types
.push_back(NAME_FIRST
);
346 test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field
);
347 field
.is_autofilled
= false;
348 form
.fields
.push_back(field
);
349 heuristic_types
.push_back(PHONE_HOME_NUMBER
);
350 server_types
.push_back(EMAIL_ADDRESS
);
352 test::CreateTestFormField("Select", "select", "USA", "select-one", &field
);
353 field
.is_autofilled
= false;
354 form
.fields
.push_back(field
);
355 heuristic_types
.push_back(UNKNOWN_TYPE
);
356 server_types
.push_back(NO_SERVER_DATA
);
358 test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field
);
359 field
.is_autofilled
= true;
360 form
.fields
.push_back(field
);
361 heuristic_types
.push_back(PHONE_HOME_CITY_AND_NUMBER
);
362 server_types
.push_back(PHONE_HOME_WHOLE_NUMBER
);
364 // Simulate having seen this form on page load.
365 autofill_manager_
->AddSeenForm(form
, heuristic_types
, server_types
);
367 // Simulate form submission.
368 base::HistogramTester histogram_tester
;
369 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
371 // Heuristic predictions.
373 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
374 AutofillMetrics::TYPE_UNKNOWN
, 1);
375 histogram_tester
.ExpectBucketCount(
376 "Autofill.Quality.HeuristicType.ByFieldType",
377 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
378 AutofillMetrics::TYPE_UNKNOWN
),
381 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
382 AutofillMetrics::TYPE_MATCH
, 2);
383 histogram_tester
.ExpectBucketCount(
384 "Autofill.Quality.HeuristicType.ByFieldType",
385 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MATCH
), 1);
386 histogram_tester
.ExpectBucketCount(
387 "Autofill.Quality.HeuristicType.ByFieldType",
388 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
389 AutofillMetrics::TYPE_MATCH
),
392 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
393 AutofillMetrics::TYPE_MISMATCH
, 1);
394 histogram_tester
.ExpectBucketCount(
395 "Autofill.Quality.HeuristicType.ByFieldType",
396 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MISMATCH
),
399 // Server predictions:
401 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
402 AutofillMetrics::TYPE_UNKNOWN
, 1);
403 histogram_tester
.ExpectBucketCount(
404 "Autofill.Quality.ServerType.ByFieldType",
405 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
406 AutofillMetrics::TYPE_UNKNOWN
),
409 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
410 AutofillMetrics::TYPE_MATCH
, 2);
411 histogram_tester
.ExpectBucketCount(
412 "Autofill.Quality.ServerType.ByFieldType",
413 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MATCH
), 1);
414 histogram_tester
.ExpectBucketCount(
415 "Autofill.Quality.ServerType.ByFieldType",
416 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
417 AutofillMetrics::TYPE_MATCH
),
420 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
421 AutofillMetrics::TYPE_MISMATCH
, 1);
422 histogram_tester
.ExpectBucketCount(
423 "Autofill.Quality.ServerType.ByFieldType",
424 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MISMATCH
), 1);
426 // Overall predictions:
428 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
429 AutofillMetrics::TYPE_UNKNOWN
, 1);
430 histogram_tester
.ExpectBucketCount(
431 "Autofill.Quality.PredictedType.ByFieldType",
432 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
433 AutofillMetrics::TYPE_UNKNOWN
),
436 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
437 AutofillMetrics::TYPE_MATCH
, 2);
438 histogram_tester
.ExpectBucketCount(
439 "Autofill.Quality.PredictedType.ByFieldType",
440 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MATCH
), 1);
441 histogram_tester
.ExpectBucketCount(
442 "Autofill.Quality.PredictedType.ByFieldType",
443 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
444 AutofillMetrics::TYPE_MATCH
),
447 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
448 AutofillMetrics::TYPE_MISMATCH
, 1);
449 histogram_tester
.ExpectBucketCount(
450 "Autofill.Quality.PredictedType.ByFieldType",
451 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MISMATCH
), 1);
454 // Verify that when a field is annotated with the autocomplete attribute, its
455 // predicted type is remembered when quality metrics are logged.
456 TEST_F(AutofillMetricsTest
, PredictedMetricsWithAutocomplete
) {
457 // Set up our form data.
459 form
.name
= ASCIIToUTF16("TestForm");
460 form
.origin
= GURL("http://example.com/form.html");
461 form
.action
= GURL("http://example.com/submit.html");
462 form
.user_submitted
= true;
464 FormFieldData field1
;
465 test::CreateTestFormField("Select", "select", "USA", "select-one", &field1
);
466 field1
.autocomplete_attribute
= "country";
467 form
.fields
.push_back(field1
);
469 // Two other fields to have the minimum of 3 to be parsed by autofill. Note
470 // that they have default values not found in the user profiles. They will be
471 // changed between the time the form is seen/parsed, and the time it is
473 FormFieldData field2
;
474 test::CreateTestFormField("Unknown", "Unknown", "", "text", &field2
);
475 form
.fields
.push_back(field2
);
476 FormFieldData field3
;
477 test::CreateTestFormField("Phone", "phone", "", "tel", &field3
);
478 form
.fields
.push_back(field3
);
480 std::vector
<FormData
> forms(1, form
);
483 base::HistogramTester histogram_tester
;
484 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
485 // We change the value of the text fields to change the default/seen values
486 // (hence the values are not cleared in UpdateFromCache). The new values
487 // match what is in the test profile.
488 form
.fields
[1].value
= base::ASCIIToUTF16("79401");
489 form
.fields
[2].value
= base::ASCIIToUTF16("2345678901");
490 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
492 // First verify that country was not predicted by client or server.
493 histogram_tester
.ExpectBucketCount(
494 "Autofill.Quality.ServerType.ByFieldType",
495 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
496 AutofillMetrics::TYPE_UNKNOWN
),
498 histogram_tester
.ExpectBucketCount(
499 "Autofill.Quality.HeuristicType.ByFieldType",
500 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
501 AutofillMetrics::TYPE_UNKNOWN
),
503 // We expect a match for country because it had |autocomplete_attribute|.
504 histogram_tester
.ExpectBucketCount(
505 "Autofill.Quality.PredictedType.ByFieldType",
506 GetFieldTypeGroupMetric(ADDRESS_HOME_COUNTRY
,
507 AutofillMetrics::TYPE_MATCH
),
510 // We did not predict zip code or phone number, because they did not have
511 // |autocomplete_attribute|, nor client or server predictions.
512 histogram_tester
.ExpectBucketCount(
513 "Autofill.Quality.ServerType.ByFieldType",
514 GetFieldTypeGroupMetric(ADDRESS_HOME_ZIP
,
515 AutofillMetrics::TYPE_UNKNOWN
),
517 histogram_tester
.ExpectBucketCount(
518 "Autofill.Quality.HeuristicType.ByFieldType",
519 GetFieldTypeGroupMetric(ADDRESS_HOME_ZIP
,
520 AutofillMetrics::TYPE_UNKNOWN
),
522 histogram_tester
.ExpectBucketCount(
523 "Autofill.Quality.PredictedType.ByFieldType",
524 GetFieldTypeGroupMetric(ADDRESS_HOME_ZIP
,
525 AutofillMetrics::TYPE_UNKNOWN
),
527 histogram_tester
.ExpectBucketCount(
528 "Autofill.Quality.ServerType.ByFieldType",
529 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
530 AutofillMetrics::TYPE_UNKNOWN
),
532 histogram_tester
.ExpectBucketCount(
533 "Autofill.Quality.HeuristicType.ByFieldType",
534 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
535 AutofillMetrics::TYPE_UNKNOWN
),
537 histogram_tester
.ExpectBucketCount(
538 "Autofill.Quality.PredictedType.ByFieldType",
539 GetFieldTypeGroupMetric(PHONE_HOME_WHOLE_NUMBER
,
540 AutofillMetrics::TYPE_UNKNOWN
),
544 histogram_tester
.ExpectTotalCount("Autofill.Quality.PredictedType", 3);
548 // Test that we behave sanely when the cached form differs from the submitted
550 TEST_F(AutofillMetricsTest
, SaneMetricsWithCacheMismatch
) {
551 // Set up our form data.
553 form
.name
= ASCIIToUTF16("TestForm");
554 form
.origin
= GURL("http://example.com/form.html");
555 form
.action
= GURL("http://example.com/submit.html");
556 form
.user_submitted
= true;
558 std::vector
<ServerFieldType
> heuristic_types
, server_types
;
561 test::CreateTestFormField(
562 "Both match", "match", "Elvis Aaron Presley", "text", &field
);
563 field
.is_autofilled
= true;
564 form
.fields
.push_back(field
);
565 heuristic_types
.push_back(NAME_FULL
);
566 server_types
.push_back(NAME_FULL
);
567 test::CreateTestFormField(
568 "Both mismatch", "mismatch", "buddy@gmail.com", "text", &field
);
569 field
.is_autofilled
= false;
570 form
.fields
.push_back(field
);
571 heuristic_types
.push_back(PHONE_HOME_NUMBER
);
572 server_types
.push_back(PHONE_HOME_NUMBER
);
573 test::CreateTestFormField(
574 "Only heuristics match", "mixed", "Memphis", "text", &field
);
575 field
.is_autofilled
= false;
576 form
.fields
.push_back(field
);
577 heuristic_types
.push_back(ADDRESS_HOME_CITY
);
578 server_types
.push_back(PHONE_HOME_NUMBER
);
579 test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field
);
580 field
.is_autofilled
= false;
581 form
.fields
.push_back(field
);
582 heuristic_types
.push_back(UNKNOWN_TYPE
);
583 server_types
.push_back(UNKNOWN_TYPE
);
585 // Simulate having seen this form with the desired heuristic and server types.
586 // |form_structure| will be owned by |autofill_manager_|.
587 autofill_manager_
->AddSeenForm(form
, heuristic_types
, server_types
);
590 // Add a field and re-arrange the remaining form fields before submitting.
591 std::vector
<FormFieldData
> cached_fields
= form
.fields
;
593 test::CreateTestFormField(
594 "New field", "new field", "Tennessee", "text", &field
);
595 form
.fields
.push_back(field
);
596 form
.fields
.push_back(cached_fields
[2]);
597 form
.fields
.push_back(cached_fields
[1]);
598 form
.fields
.push_back(cached_fields
[3]);
599 form
.fields
.push_back(cached_fields
[0]);
601 // Simulate form submission.
602 base::HistogramTester histogram_tester
;
603 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
605 // Heuristic predictions.
607 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
608 AutofillMetrics::TYPE_UNKNOWN
, 1);
609 histogram_tester
.ExpectBucketCount(
610 "Autofill.Quality.HeuristicType.ByFieldType",
611 GetFieldTypeGroupMetric(ADDRESS_HOME_STATE
,
612 AutofillMetrics::TYPE_UNKNOWN
),
615 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
616 AutofillMetrics::TYPE_MATCH
, 2);
617 histogram_tester
.ExpectBucketCount(
618 "Autofill.Quality.HeuristicType.ByFieldType",
619 GetFieldTypeGroupMetric(ADDRESS_HOME_CITY
, AutofillMetrics::TYPE_MATCH
),
621 histogram_tester
.ExpectBucketCount(
622 "Autofill.Quality.HeuristicType.ByFieldType",
623 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MATCH
), 1);
625 histogram_tester
.ExpectBucketCount("Autofill.Quality.HeuristicType",
626 AutofillMetrics::TYPE_MISMATCH
, 1);
627 histogram_tester
.ExpectBucketCount(
628 "Autofill.Quality.HeuristicType.ByFieldType",
629 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MISMATCH
),
632 // Server predictions:
634 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
635 AutofillMetrics::TYPE_UNKNOWN
, 1);
636 histogram_tester
.ExpectBucketCount(
637 "Autofill.Quality.ServerType.ByFieldType",
638 GetFieldTypeGroupMetric(ADDRESS_HOME_STATE
,
639 AutofillMetrics::TYPE_UNKNOWN
),
642 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
643 AutofillMetrics::TYPE_MATCH
, 1);
644 histogram_tester
.ExpectBucketCount(
645 "Autofill.Quality.ServerType.ByFieldType",
646 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MATCH
), 1);
648 histogram_tester
.ExpectBucketCount("Autofill.Quality.ServerType",
649 AutofillMetrics::TYPE_MISMATCH
, 2);
650 histogram_tester
.ExpectBucketCount(
651 "Autofill.Quality.ServerType.ByFieldType",
652 GetFieldTypeGroupMetric(ADDRESS_HOME_CITY
,
653 AutofillMetrics::TYPE_MISMATCH
),
655 histogram_tester
.ExpectBucketCount(
656 "Autofill.Quality.ServerType.ByFieldType",
657 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MISMATCH
),
660 // Overall predictions:
662 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
663 AutofillMetrics::TYPE_UNKNOWN
, 1);
664 histogram_tester
.ExpectBucketCount(
665 "Autofill.Quality.PredictedType.ByFieldType",
666 GetFieldTypeGroupMetric(ADDRESS_HOME_STATE
,
667 AutofillMetrics::TYPE_UNKNOWN
),
670 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
671 AutofillMetrics::TYPE_MATCH
, 1);
672 histogram_tester
.ExpectBucketCount(
673 "Autofill.Quality.PredictedType.ByFieldType",
674 GetFieldTypeGroupMetric(NAME_FULL
, AutofillMetrics::TYPE_MATCH
), 1);
676 histogram_tester
.ExpectBucketCount("Autofill.Quality.PredictedType",
677 AutofillMetrics::TYPE_MISMATCH
, 2);
678 histogram_tester
.ExpectBucketCount(
679 "Autofill.Quality.PredictedType.ByFieldType",
680 GetFieldTypeGroupMetric(ADDRESS_HOME_CITY
,
681 AutofillMetrics::TYPE_MISMATCH
),
683 histogram_tester
.ExpectBucketCount(
684 "Autofill.Quality.PredictedType.ByFieldType",
685 GetFieldTypeGroupMetric(EMAIL_ADDRESS
, AutofillMetrics::TYPE_MISMATCH
),
689 // Verify that when submitting an autofillable form, the stored profile metric
691 TEST_F(AutofillMetricsTest
, StoredProfileCountAutofillableFormSubmission
) {
692 // Construct a fillable form.
694 form
.name
= ASCIIToUTF16("TestForm");
695 form
.origin
= GURL("http://example.com/form.html");
696 form
.action
= GURL("http://example.com/submit.html");
697 form
.user_submitted
= true;
699 // Three fields is enough to make it an autofillable form.
701 test::CreateTestFormField("Name", "name", "", "text", &field
);
702 form
.fields
.push_back(field
);
703 test::CreateTestFormField("Email", "email", "", "text", &field
);
704 form
.fields
.push_back(field
);
705 test::CreateTestFormField("Phone", "phone", "", "text", &field
);
706 form
.fields
.push_back(field
);
708 std::vector
<FormData
> forms(1, form
);
710 // Simulate form submission.
711 base::HistogramTester histogram_tester
;
712 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
713 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
715 // An autofillable form was submitted, and the number of stored profiles is
717 histogram_tester
.ExpectUniqueSample(
718 "Autofill.StoredProfileCountAtAutofillableFormSubmission", 2, 1);
721 // Verify that when submitting a non-autofillable form, the stored profile
722 // metric is not logged.
723 TEST_F(AutofillMetricsTest
, StoredProfileCountNonAutofillableFormSubmission
) {
724 // Construct a non-fillable form.
726 form
.name
= ASCIIToUTF16("TestForm");
727 form
.origin
= GURL("http://example.com/form.html");
728 form
.action
= GURL("http://example.com/submit.html");
729 form
.user_submitted
= true;
731 // Two fields is not enough to make it an autofillable form.
733 test::CreateTestFormField("Name", "name", "", "text", &field
);
734 form
.fields
.push_back(field
);
735 test::CreateTestFormField("Email", "email", "", "text", &field
);
736 form
.fields
.push_back(field
);
738 std::vector
<FormData
> forms(1, form
);
740 // Simulate form submission.
741 base::HistogramTester histogram_tester
;
742 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
743 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
745 // A non-autofillable form was submitted, and number of stored profiles is NOT
747 histogram_tester
.ExpectTotalCount(
748 "Autofill.StoredProfileCountAtAutofillableFormSubmission", 0);
751 // Verify that we correctly log metrics regarding developer engagement.
752 TEST_F(AutofillMetricsTest
, DeveloperEngagement
) {
753 // Start with a non-fillable form.
755 form
.name
= ASCIIToUTF16("TestForm");
756 form
.origin
= GURL("http://example.com/form.html");
757 form
.action
= GURL("http://example.com/submit.html");
760 test::CreateTestFormField("Name", "name", "", "text", &field
);
761 form
.fields
.push_back(field
);
762 test::CreateTestFormField("Email", "email", "", "text", &field
);
763 form
.fields
.push_back(field
);
765 std::vector
<FormData
> forms(1, form
);
767 // Ensure no metrics are logged when loading a non-fillable form.
769 base::HistogramTester histogram_tester
;
770 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
771 autofill_manager_
->Reset();
772 histogram_tester
.ExpectTotalCount("Autofill.DeveloperEngagement", 0);
775 // Add another field to the form, so that it becomes fillable.
776 test::CreateTestFormField("Phone", "phone", "", "text", &field
);
777 forms
.back().fields
.push_back(field
);
779 // Expect only the "form parsed" metric to be logged; no metrics about
780 // author-specified field type hints.
782 base::HistogramTester histogram_tester
;
783 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
784 autofill_manager_
->Reset();
785 histogram_tester
.ExpectUniqueSample("Autofill.DeveloperEngagement",
786 AutofillMetrics::FILLABLE_FORM_PARSED
,
790 // Add some fields with an author-specified field type to the form.
791 // We need to add at least three fields, because a form must have at least
792 // three fillable fields to be considered to be autofillable; and if at least
793 // one field specifies an explicit type hint, we don't apply any of our usual
794 // local heuristics to detect field types in the rest of the form.
795 test::CreateTestFormField("", "", "", "text", &field
);
796 field
.autocomplete_attribute
= "given-name";
797 forms
.back().fields
.push_back(field
);
798 test::CreateTestFormField("", "", "", "text", &field
);
799 field
.autocomplete_attribute
= "email";
800 forms
.back().fields
.push_back(field
);
801 test::CreateTestFormField("", "", "", "text", &field
);
802 field
.autocomplete_attribute
= "address-line1";
803 forms
.back().fields
.push_back(field
);
805 // Expect both the "form parsed" metric and the author-specified field type
806 // hints metric to be logged.
808 base::HistogramTester histogram_tester
;
809 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
810 autofill_manager_
->Reset();
811 histogram_tester
.ExpectBucketCount("Autofill.DeveloperEngagement",
812 AutofillMetrics::FILLABLE_FORM_PARSED
,
814 histogram_tester
.ExpectBucketCount(
815 "Autofill.DeveloperEngagement",
816 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS
, 1);
820 // Test that the profile count is logged correctly.
821 TEST_F(AutofillMetricsTest
, StoredProfileCount
) {
822 // The metric should be logged when the profiles are first loaded.
824 base::HistogramTester histogram_tester
;
825 personal_data_
->LoadProfiles();
826 histogram_tester
.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1);
829 // The metric should only be logged once.
831 base::HistogramTester histogram_tester
;
832 personal_data_
->LoadProfiles();
833 histogram_tester
.ExpectTotalCount("Autofill.StoredProfileCount", 0);
837 // Test that we correctly log when Autofill is enabled.
838 TEST_F(AutofillMetricsTest
, AutofillIsEnabledAtStartup
) {
839 base::HistogramTester histogram_tester
;
840 personal_data_
->set_autofill_enabled(true);
841 personal_data_
->Init(
842 autofill_client_
.GetDatabase(), autofill_client_
.GetPrefs(), false);
843 histogram_tester
.ExpectUniqueSample("Autofill.IsEnabled.Startup", true, 1);
846 // Test that we correctly log when Autofill is disabled.
847 TEST_F(AutofillMetricsTest
, AutofillIsDisabledAtStartup
) {
848 base::HistogramTester histogram_tester
;
849 personal_data_
->set_autofill_enabled(false);
850 personal_data_
->Init(
851 autofill_client_
.GetDatabase(), autofill_client_
.GetPrefs(), false);
852 histogram_tester
.ExpectUniqueSample("Autofill.IsEnabled.Startup", false, 1);
855 // Test that we log the number of Autofill suggestions when filling a form.
856 TEST_F(AutofillMetricsTest
, AddressSuggestionsCount
) {
857 // Set up our form data.
859 form
.name
= ASCIIToUTF16("TestForm");
860 form
.origin
= GURL("http://example.com/form.html");
861 form
.action
= GURL("http://example.com/submit.html");
862 form
.user_submitted
= true;
865 std::vector
<ServerFieldType
> field_types
;
866 test::CreateTestFormField("Name", "name", "", "text", &field
);
867 form
.fields
.push_back(field
);
868 field_types
.push_back(NAME_FULL
);
869 test::CreateTestFormField("Email", "email", "", "email", &field
);
870 form
.fields
.push_back(field
);
871 field_types
.push_back(EMAIL_ADDRESS
);
872 test::CreateTestFormField("Phone", "phone", "", "tel", &field
);
873 form
.fields
.push_back(field
);
874 field_types
.push_back(PHONE_HOME_NUMBER
);
876 // Simulate having seen this form on page load.
877 // |form_structure| will be owned by |autofill_manager_|.
878 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
881 // Simulate activating the autofill popup for the phone field.
882 base::HistogramTester histogram_tester
;
883 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
885 histogram_tester
.ExpectUniqueSample("Autofill.AddressSuggestionsCount", 2,
890 // Simulate activating the autofill popup for the email field after typing.
891 // No new metric should be logged, since we're still on the same page.
892 test::CreateTestFormField("Email", "email", "b", "email", &field
);
893 base::HistogramTester histogram_tester
;
894 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
896 histogram_tester
.ExpectTotalCount("Autofill.AddressSuggestionsCount", 0);
899 // Reset the autofill manager state.
900 autofill_manager_
->Reset();
901 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
904 // Simulate activating the autofill popup for the email field after typing.
905 base::HistogramTester histogram_tester
;
906 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
908 histogram_tester
.ExpectUniqueSample("Autofill.AddressSuggestionsCount", 1,
912 // Reset the autofill manager state again.
913 autofill_manager_
->Reset();
914 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
917 // Simulate activating the autofill popup for the email field after typing.
918 form
.fields
[0].is_autofilled
= true;
919 base::HistogramTester histogram_tester
;
920 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
922 histogram_tester
.ExpectTotalCount("Autofill.AddressSuggestionsCount", 0);
926 // Test that we log interacted form event for credit cards related.
927 TEST_F(AutofillMetricsTest
, CreditCardInteractedFormEvents
) {
928 // Set up our form data.
930 form
.name
= ASCIIToUTF16("TestForm");
931 form
.origin
= GURL("http://example.com/form.html");
932 form
.action
= GURL("http://example.com/submit.html");
933 form
.user_submitted
= true;
936 std::vector
<ServerFieldType
> field_types
;
937 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
938 form
.fields
.push_back(field
);
939 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
940 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
941 form
.fields
.push_back(field
);
942 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
943 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
944 form
.fields
.push_back(field
);
945 field_types
.push_back(CREDIT_CARD_NUMBER
);
947 // Simulate having seen this form on page load.
948 // |form_structure| will be owned by |autofill_manager_|.
949 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
952 // Simulate activating the autofill popup for the credit card field.
953 base::HistogramTester histogram_tester
;
954 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
956 histogram_tester
.ExpectUniqueSample(
957 "Autofill.FormEvents.CreditCard",
958 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
961 // Reset the autofill manager state.
962 autofill_manager_
->Reset();
963 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
966 // Simulate activating the autofill popup for the credit card field twice.
967 base::HistogramTester histogram_tester
;
968 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
970 autofill_manager_
->OnQueryFormFieldAutofill(1, form
, field
, gfx::Rect(),
972 histogram_tester
.ExpectUniqueSample(
973 "Autofill.FormEvents.CreditCard",
974 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
978 // Test that we log suggestion shown form events for credit cards.
979 TEST_F(AutofillMetricsTest
, CreditCardShownFormEvents
) {
980 // Set up our form data.
982 form
.name
= ASCIIToUTF16("TestForm");
983 form
.origin
= GURL("http://example.com/form.html");
984 form
.action
= GURL("http://example.com/submit.html");
985 form
.user_submitted
= true;
988 std::vector
<ServerFieldType
> field_types
;
989 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
990 form
.fields
.push_back(field
);
991 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
992 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
993 form
.fields
.push_back(field
);
994 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
995 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
996 form
.fields
.push_back(field
);
997 field_types
.push_back(CREDIT_CARD_NUMBER
);
999 // Simulate having seen this form on page load.
1000 // |form_structure| will be owned by |autofill_manager_|.
1001 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1004 // Simulating new popup being shown.
1005 base::HistogramTester histogram_tester
;
1006 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1007 histogram_tester
.ExpectBucketCount(
1008 "Autofill.FormEvents.CreditCard",
1009 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 1);
1010 histogram_tester
.ExpectBucketCount(
1011 "Autofill.FormEvents.CreditCard",
1012 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 1);
1015 // Reset the autofill manager state.
1016 autofill_manager_
->Reset();
1017 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1020 // Simulating two popups in the same page load.
1021 base::HistogramTester histogram_tester
;
1022 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1023 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1024 histogram_tester
.ExpectBucketCount(
1025 "Autofill.FormEvents.CreditCard",
1026 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 2);
1027 histogram_tester
.ExpectBucketCount(
1028 "Autofill.FormEvents.CreditCard",
1029 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 1);
1032 // Reset the autofill manager state.
1033 autofill_manager_
->Reset();
1034 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1037 // Simulating same popup being refreshed.
1038 base::HistogramTester histogram_tester
;
1039 autofill_manager_
->DidShowSuggestions(false /* is_new_popup */, form
,
1041 histogram_tester
.ExpectBucketCount(
1042 "Autofill.FormEvents.CreditCard",
1043 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 0);
1044 histogram_tester
.ExpectBucketCount(
1045 "Autofill.FormEvents.CreditCard",
1046 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 0);
1050 // Test that we log selected form event for credit cards.
1051 TEST_F(AutofillMetricsTest
, CreditCardSelectedFormEvents
) {
1052 autofill_client_
.GetPrefs()->SetBoolean(
1053 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1054 // Creating all kinds of cards.
1055 personal_data_
->RecreateCreditCards(
1056 true /* include_local_credit_card */,
1057 true /* include_masked_server_credit_card */,
1058 true /* include_full_server_credit_card */);
1059 // Set up our form data.
1061 form
.name
= ASCIIToUTF16("TestForm");
1062 form
.origin
= GURL("http://example.com/form.html");
1063 form
.action
= GURL("http://example.com/submit.html");
1064 form
.user_submitted
= true;
1066 FormFieldData field
;
1067 std::vector
<ServerFieldType
> field_types
;
1068 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
1069 form
.fields
.push_back(field
);
1070 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
1071 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
1072 form
.fields
.push_back(field
);
1073 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
1074 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
1075 form
.fields
.push_back(field
);
1076 field_types
.push_back(CREDIT_CARD_NUMBER
);
1078 // Simulate having seen this form on page load.
1079 // |form_structure| will be owned by |autofill_manager_|.
1080 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1083 // Simulating selecting a masked card server suggestion.
1084 base::HistogramTester histogram_tester
;
1085 SuggestionBackendID
guid(
1086 "10000000-0000-0000-0000-000000000002", 0); // masked server card
1087 autofill_manager_
->FillOrPreviewForm(
1088 AutofillDriver::FORM_DATA_ACTION_FILL
,
1089 0, form
, form
.fields
.front(),
1090 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1091 histogram_tester
.ExpectBucketCount(
1092 "Autofill.FormEvents.CreditCard",
1093 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_SELECTED
, 1);
1094 histogram_tester
.ExpectBucketCount(
1095 "Autofill.FormEvents.CreditCard",
1096 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_SELECTED_ONCE
,
1100 // Reset the autofill manager state.
1101 autofill_manager_
->Reset();
1102 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1105 // Simulating selecting multiple times a masked card server.
1106 base::HistogramTester histogram_tester
;
1107 SuggestionBackendID
guid(
1108 "10000000-0000-0000-0000-000000000002", 0); // masked server card
1109 autofill_manager_
->FillOrPreviewForm(
1110 AutofillDriver::FORM_DATA_ACTION_FILL
,
1111 0, form
, form
.fields
.front(),
1112 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1113 autofill_manager_
->FillOrPreviewForm(
1114 AutofillDriver::FORM_DATA_ACTION_FILL
,
1115 0, form
, form
.fields
.front(),
1116 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1117 histogram_tester
.ExpectBucketCount(
1118 "Autofill.FormEvents.CreditCard",
1119 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_SELECTED
, 2);
1120 histogram_tester
.ExpectBucketCount(
1121 "Autofill.FormEvents.CreditCard",
1122 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_SELECTED_ONCE
,
1127 // Test that we log filled form events for credit cards.
1128 TEST_F(AutofillMetricsTest
, CreditCardFilledFormEvents
) {
1129 autofill_client_
.GetPrefs()->SetBoolean(
1130 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1131 // Creating all kinds of cards.
1132 personal_data_
->RecreateCreditCards(
1133 true /* include_local_credit_card */,
1134 true /* include_masked_server_credit_card */,
1135 true /* include_full_server_credit_card */);
1136 // Set up our form data.
1138 form
.name
= ASCIIToUTF16("TestForm");
1139 form
.origin
= GURL("http://example.com/form.html");
1140 form
.action
= GURL("http://example.com/submit.html");
1141 form
.user_submitted
= true;
1143 FormFieldData field
;
1144 std::vector
<ServerFieldType
> field_types
;
1145 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
1146 form
.fields
.push_back(field
);
1147 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
1148 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
1149 form
.fields
.push_back(field
);
1150 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
1151 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
1152 form
.fields
.push_back(field
);
1153 field_types
.push_back(CREDIT_CARD_NUMBER
);
1155 // Simulate having seen this form on page load.
1156 // |form_structure| will be owned by |autofill_manager_|.
1157 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1160 // Simulating filling a local card suggestion.
1161 base::HistogramTester histogram_tester
;
1162 SuggestionBackendID
guid(
1163 "10000000-0000-0000-0000-000000000001", 0); // local card
1164 autofill_manager_
->FillOrPreviewForm(
1165 AutofillDriver::FORM_DATA_ACTION_FILL
,
1166 0, form
, form
.fields
.front(),
1167 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1168 histogram_tester
.ExpectBucketCount(
1169 "Autofill.FormEvents.CreditCard",
1170 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED
, 1);
1171 histogram_tester
.ExpectBucketCount(
1172 "Autofill.FormEvents.CreditCard",
1173 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED_ONCE
, 1);
1176 // Reset the autofill manager state.
1177 autofill_manager_
->Reset();
1178 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1181 // Simulating filling a masked card server suggestion.
1182 base::HistogramTester histogram_tester
;
1183 SuggestionBackendID
guid(
1184 "10000000-0000-0000-0000-000000000002", 0); // masked server card
1185 autofill_manager_
->FillOrPreviewForm(
1186 AutofillDriver::FORM_DATA_ACTION_FILL
,
1187 0, form
, form
.fields
.front(),
1188 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1189 autofill_manager_
->OnDidGetRealPan(AutofillClient::SUCCESS
,
1190 "6011000990139424");
1191 histogram_tester
.ExpectBucketCount(
1192 "Autofill.FormEvents.CreditCard",
1193 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_FILLED
, 1);
1194 histogram_tester
.ExpectBucketCount(
1195 "Autofill.FormEvents.CreditCard",
1196 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_FILLED_ONCE
,
1200 // Recreating cards as the previous test should have upgraded the masked
1201 // card to a full card.
1202 personal_data_
->RecreateCreditCards(
1203 true /* include_local_credit_card */,
1204 true /* include_masked_server_credit_card */,
1205 true /* include_full_server_credit_card */);
1207 // Reset the autofill manager state.
1208 autofill_manager_
->Reset();
1209 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1212 // Simulating filling a full card server suggestion.
1213 base::HistogramTester histogram_tester
;
1214 SuggestionBackendID
guid(
1215 "10000000-0000-0000-0000-000000000003", 0); // full server card
1216 autofill_manager_
->FillOrPreviewForm(
1217 AutofillDriver::FORM_DATA_ACTION_FILL
,
1218 0, form
, form
.fields
.front(),
1219 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1220 histogram_tester
.ExpectBucketCount(
1221 "Autofill.FormEvents.CreditCard",
1222 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_FILLED
, 1);
1223 histogram_tester
.ExpectBucketCount(
1224 "Autofill.FormEvents.CreditCard",
1225 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_FILLED_ONCE
, 1);
1228 // Reset the autofill manager state.
1229 autofill_manager_
->Reset();
1230 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1233 // Simulating filling multiple times.
1234 base::HistogramTester histogram_tester
;
1235 SuggestionBackendID
guid(
1236 "10000000-0000-0000-0000-000000000001", 0); // local card
1237 autofill_manager_
->FillOrPreviewForm(
1238 AutofillDriver::FORM_DATA_ACTION_FILL
,
1239 0, form
, form
.fields
.front(),
1240 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1241 autofill_manager_
->FillOrPreviewForm(
1242 AutofillDriver::FORM_DATA_ACTION_FILL
,
1243 0, form
, form
.fields
.front(),
1244 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1245 histogram_tester
.ExpectBucketCount(
1246 "Autofill.FormEvents.CreditCard",
1247 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED
, 2);
1248 histogram_tester
.ExpectBucketCount(
1249 "Autofill.FormEvents.CreditCard",
1250 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED_ONCE
, 1);
1254 // Test that we log submitted form events for credit cards.
1255 TEST_F(AutofillMetricsTest
, CreditCardSubmittedFormEvents
) {
1256 autofill_client_
.GetPrefs()->SetBoolean(
1257 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1258 // Creating all kinds of cards.
1259 personal_data_
->RecreateCreditCards(
1260 true /* include_local_credit_card */,
1261 true /* include_masked_server_credit_card */,
1262 true /* include_full_server_credit_card */);
1263 // Set up our form data.
1265 form
.name
= ASCIIToUTF16("TestForm");
1266 form
.origin
= GURL("http://example.com/form.html");
1267 form
.action
= GURL("http://example.com/submit.html");
1268 form
.user_submitted
= true;
1270 FormFieldData field
;
1271 std::vector
<ServerFieldType
> field_types
;
1272 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
1273 form
.fields
.push_back(field
);
1274 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
1275 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
1276 form
.fields
.push_back(field
);
1277 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
1278 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
1279 form
.fields
.push_back(field
);
1280 field_types
.push_back(CREDIT_CARD_NUMBER
);
1282 // Simulate having seen this form on page load.
1283 // |form_structure| will be owned by |autofill_manager_|.
1284 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1287 // Simulating submission with no filled data.
1288 base::HistogramTester histogram_tester
;
1289 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1291 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1292 histogram_tester
.ExpectBucketCount(
1293 "Autofill.FormEvents.CreditCard",
1294 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 1);
1297 // Reset the autofill manager state.
1298 autofill_manager_
->Reset();
1299 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1302 // Simulating submission with filled local data.
1303 base::HistogramTester histogram_tester
;
1304 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1306 SuggestionBackendID
guid(
1307 "10000000-0000-0000-0000-000000000001", 0); // local card
1308 autofill_manager_
->FillOrPreviewForm(
1309 AutofillDriver::FORM_DATA_ACTION_FILL
,
1310 0, form
, form
.fields
.front(),
1311 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1312 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1313 histogram_tester
.ExpectBucketCount(
1314 "Autofill.FormEvents.CreditCard",
1315 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_SUBMITTED_ONCE
, 1);
1318 // Reset the autofill manager state.
1319 autofill_manager_
->Reset();
1320 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1323 // Simulating submission with filled server data.
1324 base::HistogramTester histogram_tester
;
1325 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1327 SuggestionBackendID
guid(
1328 "10000000-0000-0000-0000-000000000003", 0); // full server card
1329 autofill_manager_
->FillOrPreviewForm(
1330 AutofillDriver::FORM_DATA_ACTION_FILL
,
1331 0, form
, form
.fields
.front(),
1332 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1333 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1334 histogram_tester
.ExpectBucketCount(
1335 "Autofill.FormEvents.CreditCard",
1336 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 1);
1339 // Reset the autofill manager state.
1340 autofill_manager_
->Reset();
1341 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1344 // Simulating submission with a masked card server suggestion.
1345 base::HistogramTester histogram_tester
;
1346 SuggestionBackendID
guid(
1347 "10000000-0000-0000-0000-000000000002", 0); // masked server card
1348 autofill_manager_
->FillOrPreviewForm(
1349 AutofillDriver::FORM_DATA_ACTION_FILL
,
1350 0, form
, form
.fields
.front(),
1351 autofill_manager_
->MakeFrontendID(guid
, SuggestionBackendID()));
1352 autofill_manager_
->OnDidGetRealPan(AutofillClient::SUCCESS
,
1353 "6011000990139424");
1354 histogram_tester
.ExpectBucketCount(
1355 "Autofill.FormEvents.CreditCard",
1356 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_FILLED
, 1);
1357 histogram_tester
.ExpectBucketCount(
1358 "Autofill.FormEvents.CreditCard",
1359 AutofillMetrics::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_FILLED_ONCE
,
1363 // Recreating cards as the previous test should have upgraded the masked
1364 // card to a full card.
1365 personal_data_
->RecreateCreditCards(
1366 true /* include_local_credit_card */,
1367 true /* include_masked_server_credit_card */,
1368 true /* include_full_server_credit_card */);
1370 // Reset the autofill manager state.
1371 autofill_manager_
->Reset();
1372 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1375 // Simulating multiple submissions.
1376 base::HistogramTester histogram_tester
;
1377 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1379 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1380 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1381 histogram_tester
.ExpectBucketCount(
1382 "Autofill.FormEvents.CreditCard",
1383 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 1);
1384 histogram_tester
.ExpectBucketCount(
1385 "Autofill.FormEvents.CreditCard",
1386 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_SUBMITTED_ONCE
, 0);
1387 histogram_tester
.ExpectBucketCount(
1388 "Autofill.FormEvents.CreditCard",
1389 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 0);
1390 histogram_tester
.ExpectBucketCount(
1391 "Autofill.FormEvents.CreditCard",
1393 ::FORM_EVENT_MASKED_SERVER_CARD_SUGGESTION_SUBMITTED_ONCE
,
1397 // Reset the autofill manager state.
1398 autofill_manager_
->Reset();
1399 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1402 // Simulating submission without previous interaction.
1403 base::HistogramTester histogram_tester
;
1404 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1405 histogram_tester
.ExpectBucketCount(
1406 "Autofill.FormEvents.CreditCard",
1407 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 0);
1408 histogram_tester
.ExpectBucketCount(
1409 "Autofill.FormEvents.CreditCard",
1410 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 0);
1411 histogram_tester
.ExpectBucketCount(
1412 "Autofill.FormEvents.CreditCard",
1413 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 0);
1417 // Test that we log interacted form events for address.
1418 TEST_F(AutofillMetricsTest
, AddressInteractedFormEvents
) {
1419 // Set up our form data.
1421 form
.name
= ASCIIToUTF16("TestForm");
1422 form
.origin
= GURL("http://example.com/form.html");
1423 form
.action
= GURL("http://example.com/submit.html");
1424 form
.user_submitted
= true;
1426 FormFieldData field
;
1427 std::vector
<ServerFieldType
> field_types
;
1428 test::CreateTestFormField("State", "state", "", "text", &field
);
1429 form
.fields
.push_back(field
);
1430 field_types
.push_back(ADDRESS_HOME_STATE
);
1431 test::CreateTestFormField("City", "city", "", "text", &field
);
1432 form
.fields
.push_back(field
);
1433 field_types
.push_back(ADDRESS_HOME_CITY
);
1434 test::CreateTestFormField("Street", "street", "", "text", &field
);
1435 form
.fields
.push_back(field
);
1436 field_types
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
1438 // Simulate having seen this form on page load.
1439 // |form_structure| will be owned by |autofill_manager_|.
1440 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1443 // Simulate activating the autofill popup for the street field.
1444 base::HistogramTester histogram_tester
;
1445 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1447 histogram_tester
.ExpectUniqueSample(
1448 "Autofill.FormEvents.Address",
1449 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1452 // Reset the autofill manager state.
1453 autofill_manager_
->Reset();
1454 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1457 // Simulate activating the autofill popup for the street field twice.
1458 base::HistogramTester histogram_tester
;
1459 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1461 autofill_manager_
->OnQueryFormFieldAutofill(1, form
, field
, gfx::Rect(),
1463 histogram_tester
.ExpectUniqueSample(
1464 "Autofill.FormEvents.Address",
1465 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1469 // Test that we log suggestion shown form events for address.
1470 TEST_F(AutofillMetricsTest
, AddressShownFormEvents
) {
1471 // Creating all kinds of profiles.
1472 personal_data_
->RecreateProfiles(true /* include_local_profile */,
1473 true /* include_server_profile */);
1474 // Set up our form data.
1476 form
.name
= ASCIIToUTF16("TestForm");
1477 form
.origin
= GURL("http://example.com/form.html");
1478 form
.action
= GURL("http://example.com/submit.html");
1479 form
.user_submitted
= true;
1481 FormFieldData field
;
1482 std::vector
<ServerFieldType
> field_types
;
1483 test::CreateTestFormField("State", "state", "", "text", &field
);
1484 form
.fields
.push_back(field
);
1485 field_types
.push_back(ADDRESS_HOME_STATE
);
1486 test::CreateTestFormField("City", "city", "", "text", &field
);
1487 form
.fields
.push_back(field
);
1488 field_types
.push_back(ADDRESS_HOME_CITY
);
1489 test::CreateTestFormField("Street", "street", "", "text", &field
);
1490 form
.fields
.push_back(field
);
1491 field_types
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
1493 // Simulate having seen this form on page load.
1494 // |form_structure| will be owned by |autofill_manager_|.
1495 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1498 // Simulating new popup being shown.
1499 base::HistogramTester histogram_tester
;
1500 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1501 histogram_tester
.ExpectBucketCount(
1502 "Autofill.FormEvents.Address",
1503 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 1);
1504 histogram_tester
.ExpectBucketCount(
1505 "Autofill.FormEvents.Address",
1506 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 1);
1509 // Reset the autofill manager state.
1510 autofill_manager_
->Reset();
1511 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1514 // Simulating two popups in the same page load.
1515 base::HistogramTester histogram_tester
;
1516 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1517 autofill_manager_
->DidShowSuggestions(true /* is_new_popup */, form
, field
);
1518 histogram_tester
.ExpectBucketCount(
1519 "Autofill.FormEvents.Address",
1520 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 2);
1521 histogram_tester
.ExpectBucketCount(
1522 "Autofill.FormEvents.Address",
1523 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 1);
1526 // Reset the autofill manager state.
1527 autofill_manager_
->Reset();
1528 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1531 // Simulating same popup being refreshed.
1532 base::HistogramTester histogram_tester
;
1533 autofill_manager_
->DidShowSuggestions(false /* is_new_popup */, form
,
1535 histogram_tester
.ExpectBucketCount(
1536 "Autofill.FormEvents.Address",
1537 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN
, 0);
1538 histogram_tester
.ExpectBucketCount(
1539 "Autofill.FormEvents.Address",
1540 AutofillMetrics::FORM_EVENT_SUGGESTIONS_SHOWN_ONCE
, 0);
1544 // Test that we log filled form events for address.
1545 TEST_F(AutofillMetricsTest
, AddressFilledFormEvents
) {
1546 autofill_client_
.GetPrefs()->SetBoolean(
1547 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1548 // Creating all kinds of profiles.
1549 personal_data_
->RecreateProfiles(true /* include_local_profile */,
1550 true /* include_server_profile */);
1551 // Set up our form data.
1553 form
.name
= ASCIIToUTF16("TestForm");
1554 form
.origin
= GURL("http://example.com/form.html");
1555 form
.action
= GURL("http://example.com/submit.html");
1556 form
.user_submitted
= true;
1558 FormFieldData field
;
1559 std::vector
<ServerFieldType
> field_types
;
1560 test::CreateTestFormField("State", "state", "", "text", &field
);
1561 form
.fields
.push_back(field
);
1562 field_types
.push_back(ADDRESS_HOME_STATE
);
1563 test::CreateTestFormField("City", "city", "", "text", &field
);
1564 form
.fields
.push_back(field
);
1565 field_types
.push_back(ADDRESS_HOME_CITY
);
1566 test::CreateTestFormField("Street", "street", "", "text", &field
);
1567 form
.fields
.push_back(field
);
1568 field_types
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
1570 // Simulate having seen this form on page load.
1571 // |form_structure| will be owned by |autofill_manager_|.
1572 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1575 // Simulating selecting/filling a local profile suggestion.
1576 base::HistogramTester histogram_tester
;
1577 SuggestionBackendID
guid(
1578 "00000000-0000-0000-0000-000000000001", 0); // local profile
1579 autofill_manager_
->FillOrPreviewForm(
1580 AutofillDriver::FORM_DATA_ACTION_FILL
,
1581 0, form
, form
.fields
.front(),
1582 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1583 histogram_tester
.ExpectBucketCount(
1584 "Autofill.FormEvents.Address",
1585 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED
, 1);
1586 histogram_tester
.ExpectBucketCount(
1587 "Autofill.FormEvents.Address",
1588 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED_ONCE
, 1);
1591 // Reset the autofill manager state.
1592 autofill_manager_
->Reset();
1593 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1596 // Simulating selecting/filling a server profile suggestion.
1597 base::HistogramTester histogram_tester
;
1598 SuggestionBackendID
guid(
1599 "00000000-0000-0000-0000-000000000002", 0); // server profile
1600 autofill_manager_
->FillOrPreviewForm(
1601 AutofillDriver::FORM_DATA_ACTION_FILL
,
1602 0, form
, form
.fields
.front(),
1603 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1604 histogram_tester
.ExpectBucketCount(
1605 "Autofill.FormEvents.Address",
1606 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_FILLED
, 1);
1607 histogram_tester
.ExpectBucketCount(
1608 "Autofill.FormEvents.Address",
1609 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_FILLED_ONCE
, 1);
1612 // Reset the autofill manager state.
1613 autofill_manager_
->Reset();
1614 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1617 // Simulating selecting/filling a local profile suggestion.
1618 base::HistogramTester histogram_tester
;
1619 SuggestionBackendID
guid(
1620 "00000000-0000-0000-0000-000000000001", 0); // local profile
1621 autofill_manager_
->FillOrPreviewForm(
1622 AutofillDriver::FORM_DATA_ACTION_FILL
,
1623 0, form
, form
.fields
.front(),
1624 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1625 autofill_manager_
->FillOrPreviewForm(
1626 AutofillDriver::FORM_DATA_ACTION_FILL
,
1627 0, form
, form
.fields
.front(),
1628 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1629 histogram_tester
.ExpectBucketCount(
1630 "Autofill.FormEvents.Address",
1631 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED
, 2);
1632 histogram_tester
.ExpectBucketCount(
1633 "Autofill.FormEvents.Address",
1634 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_FILLED_ONCE
, 1);
1638 // Test that we log submitted form events for address.
1639 TEST_F(AutofillMetricsTest
, AddressSubmittedFormEvents
) {
1640 autofill_client_
.GetPrefs()->SetBoolean(
1641 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1642 // Creating all kinds of profiles.
1643 personal_data_
->RecreateProfiles(true /* include_local_profile */,
1644 true /* include_server_profile */);
1645 // Set up our form data.
1647 form
.name
= ASCIIToUTF16("TestForm");
1648 form
.origin
= GURL("http://example.com/form.html");
1649 form
.action
= GURL("http://example.com/submit.html");
1650 form
.user_submitted
= true;
1652 FormFieldData field
;
1653 std::vector
<ServerFieldType
> field_types
;
1654 test::CreateTestFormField("State", "state", "", "text", &field
);
1655 form
.fields
.push_back(field
);
1656 field_types
.push_back(ADDRESS_HOME_STATE
);
1657 test::CreateTestFormField("City", "city", "", "text", &field
);
1658 form
.fields
.push_back(field
);
1659 field_types
.push_back(ADDRESS_HOME_CITY
);
1660 test::CreateTestFormField("Street", "street", "", "text", &field
);
1661 form
.fields
.push_back(field
);
1662 field_types
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
1664 // Simulate having seen this form on page load.
1665 // |form_structure| will be owned by |autofill_manager_|.
1666 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1669 // Simulating submission with no filled data.
1670 base::HistogramTester histogram_tester
;
1671 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1673 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1674 histogram_tester
.ExpectBucketCount(
1675 "Autofill.FormEvents.Address",
1676 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 1);
1679 // Reset the autofill manager state.
1680 autofill_manager_
->Reset();
1681 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1684 // Simulating submission with filled local data.
1685 base::HistogramTester histogram_tester
;
1686 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1688 SuggestionBackendID
guid(
1689 "00000000-0000-0000-0000-000000000001", 0); // local profile
1690 autofill_manager_
->FillOrPreviewForm(
1691 AutofillDriver::FORM_DATA_ACTION_FILL
,
1692 0, form
, form
.fields
.front(),
1693 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1694 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1695 histogram_tester
.ExpectBucketCount(
1696 "Autofill.FormEvents.Address",
1697 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_SUBMITTED_ONCE
, 1);
1700 // Reset the autofill manager state.
1701 autofill_manager_
->Reset();
1702 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1705 // Simulating submission with filled server data.
1706 base::HistogramTester histogram_tester
;
1707 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1709 SuggestionBackendID
guid(
1710 "00000000-0000-0000-0000-000000000002", 0); // server profile
1711 autofill_manager_
->FillOrPreviewForm(
1712 AutofillDriver::FORM_DATA_ACTION_FILL
,
1713 0, form
, form
.fields
.front(),
1714 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
1715 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1716 histogram_tester
.ExpectBucketCount(
1717 "Autofill.FormEvents.Address",
1718 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 1);
1721 // Reset the autofill manager state.
1722 autofill_manager_
->Reset();
1723 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1726 // Simulating multiple submissions.
1727 base::HistogramTester histogram_tester
;
1728 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1730 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1731 histogram_tester
.ExpectBucketCount(
1732 "Autofill.FormEvents.Address",
1733 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 1);
1734 histogram_tester
.ExpectBucketCount(
1735 "Autofill.FormEvents.Address",
1736 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_SUBMITTED_ONCE
, 0);
1737 histogram_tester
.ExpectBucketCount(
1738 "Autofill.FormEvents.Address",
1739 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 0);
1742 // Reset the autofill manager state.
1743 autofill_manager_
->Reset();
1744 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1747 // Simulating submission without previous interaction.
1748 base::HistogramTester histogram_tester
;
1749 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
1750 histogram_tester
.ExpectBucketCount(
1751 "Autofill.FormEvents.Address",
1752 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE
, 0);
1753 histogram_tester
.ExpectBucketCount(
1754 "Autofill.FormEvents.Address",
1755 AutofillMetrics::FORM_EVENT_LOCAL_SUGGESTION_SUBMITTED_ONCE
, 0);
1756 histogram_tester
.ExpectBucketCount(
1757 "Autofill.FormEvents.Address",
1758 AutofillMetrics::FORM_EVENT_SERVER_SUGGESTION_SUBMITTED_ONCE
, 0);
1762 // Test that we log interacted form event for credit cards only once.
1763 TEST_F(AutofillMetricsTest
, CreditCardFormEventsAreSegmented
) {
1764 autofill_client_
.GetPrefs()->SetBoolean(
1765 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1767 // Set up our form data.
1769 form
.name
= ASCIIToUTF16("TestForm");
1770 form
.origin
= GURL("http://example.com/form.html");
1771 form
.action
= GURL("http://example.com/submit.html");
1772 form
.user_submitted
= true;
1774 FormFieldData field
;
1775 std::vector
<ServerFieldType
> field_types
;
1776 test::CreateTestFormField("Month", "card_month", "", "text", &field
);
1777 form
.fields
.push_back(field
);
1778 field_types
.push_back(CREDIT_CARD_EXP_MONTH
);
1779 test::CreateTestFormField("Year", "card_year", "", "text", &field
);
1780 form
.fields
.push_back(field
);
1781 field_types
.push_back(CREDIT_CARD_EXP_2_DIGIT_YEAR
);
1782 test::CreateTestFormField("Credit card", "card", "", "text", &field
);
1783 form
.fields
.push_back(field
);
1784 field_types
.push_back(CREDIT_CARD_NUMBER
);
1786 // Simulate having seen this form on page load.
1787 // |form_structure| will be owned by |autofill_manager_|.
1788 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1789 personal_data_
->RecreateCreditCards(
1790 false /* include_local_credit_card */,
1791 false /* include_masked_server_credit_card */,
1792 false /* include_full_server_credit_card */);
1795 // Simulate activating the autofill popup for the credit card field.
1796 base::HistogramTester histogram_tester
;
1797 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1799 histogram_tester
.ExpectUniqueSample(
1800 "Autofill.FormEvents.CreditCard.WithNoData",
1801 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1804 // Reset the autofill manager state.
1805 autofill_manager_
->Reset();
1806 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1807 personal_data_
->RecreateCreditCards(
1808 true /* include_local_credit_card */,
1809 false /* include_masked_server_credit_card */,
1810 false /* include_full_server_credit_card */);
1813 // Simulate activating the autofill popup for the credit card field.
1814 base::HistogramTester histogram_tester
;
1815 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1817 histogram_tester
.ExpectUniqueSample(
1818 "Autofill.FormEvents.CreditCard.WithOnlyLocalData",
1819 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1822 // Reset the autofill manager state.
1823 autofill_manager_
->Reset();
1824 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1825 personal_data_
->RecreateCreditCards(
1826 false /* include_local_credit_card */,
1827 true /* include_masked_server_credit_card */,
1828 false /* include_full_server_credit_card */);
1831 // Simulate activating the autofill popup for the credit card field.
1832 base::HistogramTester histogram_tester
;
1833 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1835 histogram_tester
.ExpectUniqueSample(
1836 "Autofill.FormEvents.CreditCard.WithOnlyServerData",
1837 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1840 // Reset the autofill manager state.
1841 autofill_manager_
->Reset();
1842 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1843 personal_data_
->RecreateCreditCards(
1844 false /* include_local_credit_card */,
1845 false /* include_masked_server_credit_card */,
1846 true /* include_full_server_credit_card */);
1849 // Simulate activating the autofill popup for the credit card field.
1850 base::HistogramTester histogram_tester
;
1851 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1853 histogram_tester
.ExpectUniqueSample(
1854 "Autofill.FormEvents.CreditCard.WithOnlyServerData",
1855 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1858 // Reset the autofill manager state.
1859 autofill_manager_
->Reset();
1860 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1861 personal_data_
->RecreateCreditCards(
1862 true /* include_local_credit_card */,
1863 false /* include_masked_server_credit_card */,
1864 true /* include_full_server_credit_card */);
1867 // Simulate activating the autofill popup for the credit card field.
1868 base::HistogramTester histogram_tester
;
1869 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1871 histogram_tester
.ExpectUniqueSample(
1872 "Autofill.FormEvents.CreditCard.WithBothServerAndLocalData",
1873 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1877 // Test that we log interacted form event for address only once.
1878 TEST_F(AutofillMetricsTest
, AddressFormEventsAreSegmented
) {
1879 autofill_client_
.GetPrefs()->SetBoolean(
1880 prefs::kAutofillWalletSyncExperimentEnabled
, true);
1882 // Set up our form data.
1884 form
.name
= ASCIIToUTF16("TestForm");
1885 form
.origin
= GURL("http://example.com/form.html");
1886 form
.action
= GURL("http://example.com/submit.html");
1887 form
.user_submitted
= true;
1889 FormFieldData field
;
1890 std::vector
<ServerFieldType
> field_types
;
1891 test::CreateTestFormField("State", "state", "", "text", &field
);
1892 form
.fields
.push_back(field
);
1893 field_types
.push_back(ADDRESS_HOME_STATE
);
1894 test::CreateTestFormField("City", "city", "", "text", &field
);
1895 form
.fields
.push_back(field
);
1896 field_types
.push_back(ADDRESS_HOME_CITY
);
1897 test::CreateTestFormField("Street", "street", "", "text", &field
);
1898 form
.fields
.push_back(field
);
1899 field_types
.push_back(ADDRESS_HOME_STREET_ADDRESS
);
1901 // Simulate having seen this form on page load.
1902 // |form_structure| will be owned by |autofill_manager_|.
1903 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1904 personal_data_
->RecreateProfiles(false /* include_local_profile */,
1905 false /* include_server_profile */);
1908 // Simulate activating the autofill popup for the street field.
1909 base::HistogramTester histogram_tester
;
1910 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1912 histogram_tester
.ExpectUniqueSample(
1913 "Autofill.FormEvents.Address.WithNoData",
1914 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1917 // Reset the autofill manager state.
1918 autofill_manager_
->Reset();
1919 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1920 personal_data_
->RecreateProfiles(true /* include_local_profile */,
1921 false /* include_server_profile */);
1924 // Simulate activating the autofill popup for the street field.
1925 base::HistogramTester histogram_tester
;
1926 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1928 histogram_tester
.ExpectUniqueSample(
1929 "Autofill.FormEvents.Address.WithOnlyLocalData",
1930 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1933 // Reset the autofill manager state.
1934 autofill_manager_
->Reset();
1935 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1936 personal_data_
->RecreateProfiles(false /* include_local_profile */,
1937 true /* include_server_profile */);
1940 // Simulate activating the autofill popup for the street field.
1941 base::HistogramTester histogram_tester
;
1942 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1944 histogram_tester
.ExpectUniqueSample(
1945 "Autofill.FormEvents.Address.WithOnlyServerData",
1946 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1949 // Reset the autofill manager state.
1950 autofill_manager_
->Reset();
1951 autofill_manager_
->AddSeenForm(form
, field_types
, field_types
);
1952 personal_data_
->RecreateProfiles(true /* include_local_profile */,
1953 true /* include_server_profile */);
1956 // Simulate activating the autofill popup for the street field.
1957 base::HistogramTester histogram_tester
;
1958 autofill_manager_
->OnQueryFormFieldAutofill(0, form
, field
, gfx::Rect(),
1960 histogram_tester
.ExpectUniqueSample(
1961 "Autofill.FormEvents.Address.WithBothServerAndLocalData",
1962 AutofillMetrics::FORM_EVENT_INTERACTED_ONCE
, 1);
1967 // Test that we log that Autofill is enabled when filling a form.
1968 TEST_F(AutofillMetricsTest
, AutofillIsEnabledAtPageLoad
) {
1969 base::HistogramTester histogram_tester
;
1970 autofill_manager_
->set_autofill_enabled(true);
1971 autofill_manager_
->OnFormsSeen(std::vector
<FormData
>(), TimeTicks());
1972 histogram_tester
.ExpectUniqueSample("Autofill.IsEnabled.PageLoad", true, 1);
1975 // Test that we log that Autofill is disabled when filling a form.
1976 TEST_F(AutofillMetricsTest
, AutofillIsDisabledAtPageLoad
) {
1977 base::HistogramTester histogram_tester
;
1978 autofill_manager_
->set_autofill_enabled(false);
1979 autofill_manager_
->OnFormsSeen(std::vector
<FormData
>(), TimeTicks());
1980 histogram_tester
.ExpectUniqueSample("Autofill.IsEnabled.PageLoad", false, 1);
1983 // Verify that we correctly log user happiness metrics dealing with form loading
1984 // and form submission.
1985 TEST_F(AutofillMetricsTest
, UserHappinessFormLoadAndSubmission
) {
1986 // Start with a form with insufficiently many fields.
1988 form
.name
= ASCIIToUTF16("TestForm");
1989 form
.origin
= GURL("http://example.com/form.html");
1990 form
.action
= GURL("http://example.com/submit.html");
1991 form
.user_submitted
= true;
1993 FormFieldData field
;
1994 test::CreateTestFormField("Name", "name", "", "text", &field
);
1995 form
.fields
.push_back(field
);
1996 test::CreateTestFormField("Email", "email", "", "text", &field
);
1997 form
.fields
.push_back(field
);
1999 std::vector
<FormData
> forms(1, form
);
2001 // Expect no notifications when the form is first seen.
2003 base::HistogramTester histogram_tester
;
2004 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
2005 histogram_tester
.ExpectTotalCount("Autofill.UserHappiness", 0);
2009 // Expect no notifications when the form is submitted.
2011 base::HistogramTester histogram_tester
;
2012 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2013 histogram_tester
.ExpectTotalCount("Autofill.UserHappiness", 0);
2016 // Add more fields to the form.
2017 test::CreateTestFormField("Phone", "phone", "", "text", &field
);
2018 form
.fields
.push_back(field
);
2019 test::CreateTestFormField("Unknown", "unknown", "", "text", &field
);
2020 form
.fields
.push_back(field
);
2021 forms
.front() = form
;
2023 // Expect a notification when the form is first seen.
2025 base::HistogramTester histogram_tester
;
2026 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
2027 histogram_tester
.ExpectUniqueSample("Autofill.UserHappiness",
2028 AutofillMetrics::FORMS_LOADED
, 1);
2031 // Expect a notification when the form is submitted.
2033 base::HistogramTester histogram_tester
;
2034 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2035 histogram_tester
.ExpectUniqueSample(
2036 "Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM
,
2040 // Fill in two of the fields.
2041 form
.fields
[0].value
= ASCIIToUTF16("Elvis Aaron Presley");
2042 form
.fields
[1].value
= ASCIIToUTF16("theking@gmail.com");
2043 forms
.front() = form
;
2045 // Expect a notification when the form is submitted.
2047 base::HistogramTester histogram_tester
;
2048 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2049 histogram_tester
.ExpectUniqueSample(
2050 "Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM
,
2054 // Fill in the third field.
2055 form
.fields
[2].value
= ASCIIToUTF16("12345678901");
2056 forms
.front() = form
;
2058 // Expect notifications when the form is submitted.
2060 base::HistogramTester histogram_tester
;
2061 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2062 histogram_tester
.ExpectUniqueSample(
2063 "Autofill.UserHappiness",
2064 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE
, 1);
2068 // Mark one of the fields as autofilled.
2069 form
.fields
[1].is_autofilled
= true;
2070 forms
.front() = form
;
2072 // Expect notifications when the form is submitted.
2074 base::HistogramTester histogram_tester
;
2075 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2076 histogram_tester
.ExpectUniqueSample(
2077 "Autofill.UserHappiness",
2078 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME
, 1);
2081 // Mark all of the fillable fields as autofilled.
2082 form
.fields
[0].is_autofilled
= true;
2083 form
.fields
[2].is_autofilled
= true;
2084 forms
.front() = form
;
2086 // Expect notifications when the form is submitted.
2088 base::HistogramTester histogram_tester
;
2089 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2090 histogram_tester
.ExpectUniqueSample(
2091 "Autofill.UserHappiness",
2092 AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL
, 1);
2095 // Clear out the third field's value.
2096 form
.fields
[2].value
= base::string16();
2097 forms
.front() = form
;
2099 // Expect notifications when the form is submitted.
2101 base::HistogramTester histogram_tester
;
2102 autofill_manager_
->FormSubmitted(form
, TimeTicks::Now());
2103 histogram_tester
.ExpectUniqueSample(
2104 "Autofill.UserHappiness", AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM
,
2109 // Verify that we correctly log user happiness metrics dealing with form
2111 TEST_F(AutofillMetricsTest
, UserHappinessFormInteraction
) {
2112 // Load a fillable form.
2114 form
.name
= ASCIIToUTF16("TestForm");
2115 form
.origin
= GURL("http://example.com/form.html");
2116 form
.action
= GURL("http://example.com/submit.html");
2117 form
.user_submitted
= true;
2119 FormFieldData field
;
2120 test::CreateTestFormField("Name", "name", "", "text", &field
);
2121 form
.fields
.push_back(field
);
2122 test::CreateTestFormField("Email", "email", "", "text", &field
);
2123 form
.fields
.push_back(field
);
2124 test::CreateTestFormField("Phone", "phone", "", "text", &field
);
2125 form
.fields
.push_back(field
);
2127 std::vector
<FormData
> forms(1, form
);
2129 // Expect a notification when the form is first seen.
2131 base::HistogramTester histogram_tester
;
2132 autofill_manager_
->OnFormsSeen(forms
, TimeTicks());
2133 histogram_tester
.ExpectUniqueSample("Autofill.UserHappiness",
2134 AutofillMetrics::FORMS_LOADED
, 1);
2139 base::HistogramTester histogram_tester
;
2140 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2142 histogram_tester
.ExpectUniqueSample("Autofill.UserHappiness",
2143 AutofillMetrics::USER_DID_TYPE
, 1);
2146 // Simulate suggestions shown twice for a single edit (i.e. multiple
2147 // keystrokes in a single field).
2149 base::HistogramTester histogram_tester
;
2150 autofill_manager_
->DidShowSuggestions(true, form
, field
);
2151 autofill_manager_
->DidShowSuggestions(false, form
, field
);
2152 histogram_tester
.ExpectBucketCount("Autofill.UserHappiness",
2153 AutofillMetrics::SUGGESTIONS_SHOWN
, 1);
2154 histogram_tester
.ExpectBucketCount(
2155 "Autofill.UserHappiness", AutofillMetrics::SUGGESTIONS_SHOWN_ONCE
, 1);
2158 // Simulate suggestions shown for a different field.
2160 base::HistogramTester histogram_tester
;
2161 autofill_manager_
->DidShowSuggestions(true, form
, form
.fields
[1]);
2162 histogram_tester
.ExpectUniqueSample("Autofill.UserHappiness",
2163 AutofillMetrics::SUGGESTIONS_SHOWN
, 1);
2166 // Simulate invoking autofill.
2168 base::HistogramTester histogram_tester
;
2169 autofill_manager_
->OnDidFillAutofillFormData(TimeTicks());
2170 histogram_tester
.ExpectBucketCount("Autofill.UserHappiness",
2171 AutofillMetrics::USER_DID_AUTOFILL
, 1);
2172 histogram_tester
.ExpectBucketCount(
2173 "Autofill.UserHappiness", AutofillMetrics::USER_DID_AUTOFILL_ONCE
, 1);
2176 // Simulate editing an autofilled field.
2178 base::HistogramTester histogram_tester
;
2179 SuggestionBackendID
guid(
2180 "00000000-0000-0000-0000-000000000001", 0);
2181 autofill_manager_
->FillOrPreviewForm(
2182 AutofillDriver::FORM_DATA_ACTION_FILL
,
2183 0, form
, form
.fields
.front(),
2184 autofill_manager_
->MakeFrontendID(SuggestionBackendID(), guid
));
2185 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2187 // Simulate a second keystroke; make sure we don't log the metric twice.
2188 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2190 histogram_tester
.ExpectBucketCount(
2191 "Autofill.UserHappiness",
2192 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD
, 1);
2193 histogram_tester
.ExpectBucketCount(
2194 "Autofill.UserHappiness",
2195 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE
, 1);
2198 // Simulate invoking autofill again.
2200 base::HistogramTester histogram_tester
;
2201 autofill_manager_
->OnDidFillAutofillFormData(TimeTicks());
2202 histogram_tester
.ExpectUniqueSample("Autofill.UserHappiness",
2203 AutofillMetrics::USER_DID_AUTOFILL
, 1);
2206 // Simulate editing another autofilled field.
2208 base::HistogramTester histogram_tester
;
2209 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
[1], TimeTicks());
2210 histogram_tester
.ExpectUniqueSample(
2211 "Autofill.UserHappiness",
2212 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD
, 1);
2216 // Verify that we correctly log metrics tracking the duration of form fill.
2217 TEST_F(AutofillMetricsTest
, FormFillDuration
) {
2218 // Load a fillable form.
2220 form
.name
= ASCIIToUTF16("TestForm");
2221 form
.origin
= GURL("http://example.com/form.html");
2222 form
.action
= GURL("http://example.com/submit.html");
2223 form
.user_submitted
= true;
2225 FormFieldData field
;
2226 test::CreateTestFormField("Name", "name", "", "text", &field
);
2227 form
.fields
.push_back(field
);
2228 test::CreateTestFormField("Email", "email", "", "text", &field
);
2229 form
.fields
.push_back(field
);
2230 test::CreateTestFormField("Phone", "phone", "", "text", &field
);
2231 form
.fields
.push_back(field
);
2233 std::vector
<FormData
> forms(1, form
);
2235 // Fill additional form.
2236 FormData second_form
= form
;
2237 test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field
);
2238 second_form
.fields
.push_back(field
);
2240 std::vector
<FormData
> second_forms(1, second_form
);
2242 // Fill the field values for form submission.
2243 form
.fields
[0].value
= ASCIIToUTF16("Elvis Aaron Presley");
2244 form
.fields
[1].value
= ASCIIToUTF16("theking@gmail.com");
2245 form
.fields
[2].value
= ASCIIToUTF16("12345678901");
2247 // Fill the field values for form submission.
2248 second_form
.fields
[0].value
= ASCIIToUTF16("Elvis Aaron Presley");
2249 second_form
.fields
[1].value
= ASCIIToUTF16("theking@gmail.com");
2250 second_form
.fields
[2].value
= ASCIIToUTF16("12345678901");
2251 second_form
.fields
[3].value
= ASCIIToUTF16("51512345678");
2253 // Expect only form load metrics to be logged if the form is submitted without
2254 // user interaction.
2256 base::HistogramTester histogram_tester
;
2257 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2258 autofill_manager_
->FormSubmitted(form
, TimeTicks::FromInternalValue(17));
2260 histogram_tester
.ExpectTotalCount(
2261 "Autofill.FillDuration.FromLoad.WithAutofill", 0);
2262 histogram_tester
.ExpectUniqueSample(
2263 "Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1);
2264 histogram_tester
.ExpectTotalCount(
2265 "Autofill.FillDuration.FromInteraction.WithAutofill", 0);
2266 histogram_tester
.ExpectTotalCount(
2267 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2269 autofill_manager_
->Reset();
2272 // Expect metric to be logged if the user manually edited a form field.
2274 base::HistogramTester histogram_tester
;
2275 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2276 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2277 TimeTicks::FromInternalValue(3));
2278 autofill_manager_
->FormSubmitted(form
, TimeTicks::FromInternalValue(17));
2280 histogram_tester
.ExpectTotalCount(
2281 "Autofill.FillDuration.FromLoad.WithAutofill", 0);
2282 histogram_tester
.ExpectUniqueSample(
2283 "Autofill.FillDuration.FromLoad.WithoutAutofill", 16, 1);
2284 histogram_tester
.ExpectTotalCount(
2285 "Autofill.FillDuration.FromInteraction.WithAutofill", 0);
2286 histogram_tester
.ExpectUniqueSample(
2287 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 14, 1);
2289 autofill_manager_
->Reset();
2292 // Expect metric to be logged if the user autofilled the form.
2293 form
.fields
[0].is_autofilled
= true;
2295 base::HistogramTester histogram_tester
;
2296 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2297 autofill_manager_
->OnDidFillAutofillFormData(
2298 TimeTicks::FromInternalValue(5));
2299 autofill_manager_
->FormSubmitted(form
, TimeTicks::FromInternalValue(17));
2301 histogram_tester
.ExpectUniqueSample(
2302 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
2303 histogram_tester
.ExpectTotalCount(
2304 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
2305 histogram_tester
.ExpectUniqueSample(
2306 "Autofill.FillDuration.FromInteraction.WithAutofill", 12, 1);
2307 histogram_tester
.ExpectTotalCount(
2308 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2310 autofill_manager_
->Reset();
2313 // Expect metric to be logged if the user both manually filled some fields
2314 // and autofilled others. Messages can arrive out of order, so make sure they
2315 // take precedence appropriately.
2317 base::HistogramTester histogram_tester
;
2319 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2320 autofill_manager_
->OnDidFillAutofillFormData(
2321 TimeTicks::FromInternalValue(5));
2322 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2323 TimeTicks::FromInternalValue(3));
2324 autofill_manager_
->FormSubmitted(form
, TimeTicks::FromInternalValue(17));
2326 histogram_tester
.ExpectUniqueSample(
2327 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
2328 histogram_tester
.ExpectTotalCount(
2329 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
2330 histogram_tester
.ExpectUniqueSample(
2331 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
2332 histogram_tester
.ExpectTotalCount(
2333 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2335 autofill_manager_
->Reset();
2338 // Make sure that loading another form doesn't affect metrics from the first
2341 base::HistogramTester histogram_tester
;
2342 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2343 autofill_manager_
->OnFormsSeen(second_forms
,
2344 TimeTicks::FromInternalValue(3));
2345 autofill_manager_
->OnDidFillAutofillFormData(
2346 TimeTicks::FromInternalValue(5));
2347 autofill_manager_
->OnTextFieldDidChange(form
, form
.fields
.front(),
2348 TimeTicks::FromInternalValue(3));
2349 autofill_manager_
->FormSubmitted(form
, TimeTicks::FromInternalValue(17));
2351 histogram_tester
.ExpectUniqueSample(
2352 "Autofill.FillDuration.FromLoad.WithAutofill", 16, 1);
2353 histogram_tester
.ExpectTotalCount(
2354 "Autofill.FillDuration.FromLoad.WithoutAutofill", 0);
2355 histogram_tester
.ExpectUniqueSample(
2356 "Autofill.FillDuration.FromInteraction.WithAutofill", 14, 1);
2357 histogram_tester
.ExpectTotalCount(
2358 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2360 autofill_manager_
->Reset();
2363 // Make sure that submitting a form that was loaded later will report the
2364 // later loading time.
2366 base::HistogramTester histogram_tester
;
2367 autofill_manager_
->OnFormsSeen(forms
, TimeTicks::FromInternalValue(1));
2368 autofill_manager_
->OnFormsSeen(second_forms
,
2369 TimeTicks::FromInternalValue(5));
2370 autofill_manager_
->FormSubmitted(second_form
,
2371 TimeTicks::FromInternalValue(17));
2373 histogram_tester
.ExpectTotalCount(
2374 "Autofill.FillDuration.FromLoad.WithAutofill", 0);
2375 histogram_tester
.ExpectUniqueSample(
2376 "Autofill.FillDuration.FromLoad.WithoutAutofill", 12, 1);
2377 histogram_tester
.ExpectTotalCount(
2378 "Autofill.FillDuration.FromInteraction.WithAutofill", 0);
2379 histogram_tester
.ExpectTotalCount(
2380 "Autofill.FillDuration.FromInteraction.WithoutAutofill", 0);
2382 autofill_manager_
->Reset();
2386 } // namespace autofill