autofill_metrics_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include "components/autofill/core/browser/autofill_metrics.h"
6
7#include <vector>
8
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/prefs/pref_service.h"
12#include "base/run_loop.h"
13#include "base/strings/string16.h"
14#include "base/strings/utf_string_conversions.h"
15#include "base/time/time.h"
16#include "components/autofill/core/browser/autofill_external_delegate.h"
17#include "components/autofill/core/browser/autofill_manager.h"
18#include "components/autofill/core/browser/autofill_test_utils.h"
19#include "components/autofill/core/browser/personal_data_manager.h"
20#include "components/autofill/core/browser/test_autofill_driver.h"
21#include "components/autofill/core/browser/test_autofill_manager_delegate.h"
22#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
23#include "components/autofill/core/common/form_data.h"
24#include "components/autofill/core/common/form_field_data.h"
25#include "components/autofill/core/common/forms_seen_state.h"
26#include "components/webdata/common/web_data_results.h"
27#include "testing/gmock/include/gmock/gmock.h"
28#include "testing/gtest/include/gtest/gtest.h"
29#include "ui/gfx/rect.h"
30#include "url/gurl.h"
31
32using base::ASCIIToUTF16;
33using base::TimeDelta;
34using base::TimeTicks;
35using testing::_;
36using testing::AnyNumber;
37using testing::Mock;
38
39namespace autofill {
40
41namespace {
42
43class MockAutofillMetrics : public AutofillMetrics {
44 public:
45  MockAutofillMetrics() {}
46  MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
47  MOCK_CONST_METHOD1(LogDeveloperEngagementMetric,
48                     void(DeveloperEngagementMetric metric));
49  MOCK_CONST_METHOD2(LogHeuristicTypePrediction,
50                     void(FieldTypeQualityMetric metric,
51                          ServerFieldType field_type));
52  MOCK_CONST_METHOD2(LogOverallTypePrediction,
53                     void(FieldTypeQualityMetric metric,
54                          ServerFieldType field_type));
55  MOCK_CONST_METHOD2(LogServerTypePrediction,
56                     void(FieldTypeQualityMetric metric,
57                          ServerFieldType field_type));
58  MOCK_CONST_METHOD1(LogServerQueryMetric, void(ServerQueryMetric metric));
59  MOCK_CONST_METHOD1(LogUserHappinessMetric, void(UserHappinessMetric metric));
60  MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithAutofill,
61                     void(const TimeDelta& duration));
62  MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithoutAutofill,
63                     void(const TimeDelta& duration));
64  MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithAutofill,
65                     void(const TimeDelta& duration));
66  MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithoutAutofill,
67                     void(const TimeDelta& duration));
68  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled));
69  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled));
70  MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles));
71  MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions));
72
73 private:
74  DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
75};
76
77class TestPersonalDataManager : public PersonalDataManager {
78 public:
79  TestPersonalDataManager()
80      : PersonalDataManager("en-US"),
81        autofill_enabled_(true) {
82    set_metric_logger(new testing::NiceMock<MockAutofillMetrics>());
83    CreateTestAutofillProfiles(&web_profiles_);
84  }
85
86  using PersonalDataManager::set_database;
87  using PersonalDataManager::SetPrefService;
88
89  // Overridden to avoid a trip to the database. This should be a no-op except
90  // for the side-effect of logging the profile count.
91  virtual void LoadProfiles() OVERRIDE {
92    std::vector<AutofillProfile*> profiles;
93    web_profiles_.release(&profiles);
94    WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
95                                                    profiles);
96    ReceiveLoadedProfiles(0, &result);
97  }
98
99  // Overridden to avoid a trip to the database.
100  virtual void LoadCreditCards() OVERRIDE {}
101
102  const MockAutofillMetrics* metric_logger() const {
103    return static_cast<const MockAutofillMetrics*>(
104        PersonalDataManager::metric_logger());
105  }
106
107  void set_autofill_enabled(bool autofill_enabled) {
108    autofill_enabled_ = autofill_enabled;
109  }
110
111  virtual bool IsAutofillEnabled() const OVERRIDE {
112    return autofill_enabled_;
113  }
114
115  MOCK_METHOD1(SaveImportedCreditCard,
116               std::string(const CreditCard& imported_credit_card));
117
118 private:
119  void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
120    AutofillProfile* profile = new AutofillProfile;
121    test::SetProfileInfo(profile, "Elvis", "Aaron",
122                         "Presley", "theking@gmail.com", "RCA",
123                         "3734 Elvis Presley Blvd.", "Apt. 10",
124                         "Memphis", "Tennessee", "38116", "US",
125                         "12345678901");
126    profile->set_guid("00000000-0000-0000-0000-000000000001");
127    profiles->push_back(profile);
128    profile = new AutofillProfile;
129    test::SetProfileInfo(profile, "Charles", "Hardin",
130                         "Holley", "buddy@gmail.com", "Decca",
131                         "123 Apple St.", "unit 6", "Lubbock",
132                         "Texas", "79401", "US", "2345678901");
133    profile->set_guid("00000000-0000-0000-0000-000000000002");
134    profiles->push_back(profile);
135  }
136
137  bool autofill_enabled_;
138
139  DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
140};
141
142class TestFormStructure : public FormStructure {
143 public:
144  explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
145  virtual ~TestFormStructure() {}
146
147  void SetFieldTypes(const std::vector<ServerFieldType>& heuristic_types,
148                     const std::vector<ServerFieldType>& server_types) {
149    ASSERT_EQ(field_count(), heuristic_types.size());
150    ASSERT_EQ(field_count(), server_types.size());
151
152    for (size_t i = 0; i < field_count(); ++i) {
153      AutofillField* form_field = field(i);
154      ASSERT_TRUE(form_field);
155      form_field->set_heuristic_type(heuristic_types[i]);
156      form_field->set_server_type(server_types[i]);
157    }
158
159    UpdateAutofillCount();
160  }
161
162 private:
163  DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
164};
165
166class TestAutofillManager : public AutofillManager {
167 public:
168  TestAutofillManager(AutofillDriver* driver,
169                      AutofillManagerDelegate* manager_delegate,
170                      TestPersonalDataManager* personal_manager)
171      : AutofillManager(driver, manager_delegate, personal_manager),
172        autofill_enabled_(true) {
173    set_metric_logger(new testing::NiceMock<MockAutofillMetrics>);
174  }
175  virtual ~TestAutofillManager() {}
176
177  virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
178
179  void set_autofill_enabled(bool autofill_enabled) {
180    autofill_enabled_ = autofill_enabled;
181  }
182
183  MockAutofillMetrics* metric_logger() {
184    return static_cast<MockAutofillMetrics*>(const_cast<AutofillMetrics*>(
185        AutofillManager::metric_logger()));
186  }
187
188  void AddSeenForm(const FormData& form,
189                   const std::vector<ServerFieldType>& heuristic_types,
190                   const std::vector<ServerFieldType>& server_types) {
191    FormData empty_form = form;
192    for (size_t i = 0; i < empty_form.fields.size(); ++i) {
193      empty_form.fields[i].value = base::string16();
194    }
195
196    // |form_structure| will be owned by |form_structures()|.
197    TestFormStructure* form_structure = new TestFormStructure(empty_form);
198    form_structure->SetFieldTypes(heuristic_types, server_types);
199    form_structures()->push_back(form_structure);
200  }
201
202  void FormSubmitted(const FormData& form, const TimeTicks& timestamp) {
203    run_loop_.reset(new base::RunLoop());
204    if (!OnFormSubmitted(form, timestamp))
205      return;
206
207    // Wait for the asynchronous FormSubmitted() call to complete.
208    run_loop_->Run();
209  }
210
211  virtual void UploadFormDataAsyncCallback(
212      const FormStructure* submitted_form,
213      const base::TimeTicks& load_time,
214      const base::TimeTicks& interaction_time,
215      const base::TimeTicks& submission_time) OVERRIDE {
216    run_loop_->Quit();
217
218    AutofillManager::UploadFormDataAsyncCallback(submitted_form,
219                                                 load_time,
220                                                 interaction_time,
221                                                 submission_time);
222  }
223
224 private:
225  bool autofill_enabled_;
226  scoped_ptr<base::RunLoop> run_loop_;
227
228  DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
229};
230
231}  // namespace
232
233class AutofillMetricsTest : public testing::Test {
234 public:
235  virtual ~AutofillMetricsTest();
236
237  virtual void SetUp() OVERRIDE;
238  virtual void TearDown() OVERRIDE;
239
240 protected:
241  base::MessageLoop message_loop_;
242  TestAutofillManagerDelegate manager_delegate_;
243  scoped_ptr<TestAutofillDriver> autofill_driver_;
244  scoped_ptr<TestAutofillManager> autofill_manager_;
245  scoped_ptr<TestPersonalDataManager> personal_data_;
246  scoped_ptr<AutofillExternalDelegate> external_delegate_;
247};
248
249AutofillMetricsTest::~AutofillMetricsTest() {
250  // Order of destruction is important as AutofillManager relies on
251  // PersonalDataManager to be around when it gets destroyed.
252  autofill_manager_.reset();
253}
254
255void AutofillMetricsTest::SetUp() {
256  manager_delegate_.SetPrefs(test::PrefServiceForTesting());
257
258  // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
259  test::DisableSystemServices(manager_delegate_.GetPrefs());
260
261  personal_data_.reset(new TestPersonalDataManager());
262  personal_data_->set_database(manager_delegate_.GetDatabase());
263  personal_data_->SetPrefService(manager_delegate_.GetPrefs());
264  autofill_driver_.reset(new TestAutofillDriver());
265  autofill_manager_.reset(new TestAutofillManager(
266      autofill_driver_.get(), &manager_delegate_, personal_data_.get()));
267
268  external_delegate_.reset(new AutofillExternalDelegate(
269      autofill_manager_.get(),
270      autofill_driver_.get()));
271  autofill_manager_->SetExternalDelegate(external_delegate_.get());
272}
273
274void AutofillMetricsTest::TearDown() {
275  // Order of destruction is important as AutofillManager relies on
276  // PersonalDataManager to be around when it gets destroyed.
277  autofill_manager_.reset();
278  autofill_driver_.reset();
279  personal_data_.reset();
280}
281
282// Test that we log quality metrics appropriately.
283TEST_F(AutofillMetricsTest, QualityMetrics) {
284  // Set up our form data.
285  FormData form;
286  form.name = ASCIIToUTF16("TestForm");
287  form.method = ASCIIToUTF16("POST");
288  form.origin = GURL("http://example.com/form.html");
289  form.action = GURL("http://example.com/submit.html");
290  form.user_submitted = true;
291
292  std::vector<ServerFieldType> heuristic_types, server_types;
293  FormFieldData field;
294
295  test::CreateTestFormField(
296      "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
297  field.is_autofilled = true;
298  form.fields.push_back(field);
299  heuristic_types.push_back(NAME_FULL);
300  server_types.push_back(NAME_FIRST);
301
302  test::CreateTestFormField(
303      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
304  field.is_autofilled = false;
305  form.fields.push_back(field);
306  heuristic_types.push_back(PHONE_HOME_NUMBER);
307  server_types.push_back(EMAIL_ADDRESS);
308
309  test::CreateTestFormField("Empty", "empty", "", "text", &field);
310  field.is_autofilled = false;
311  form.fields.push_back(field);
312  heuristic_types.push_back(NAME_FULL);
313  server_types.push_back(NAME_FIRST);
314
315  test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
316  field.is_autofilled = false;
317  form.fields.push_back(field);
318  heuristic_types.push_back(PHONE_HOME_NUMBER);
319  server_types.push_back(EMAIL_ADDRESS);
320
321  test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
322  field.is_autofilled = false;
323  form.fields.push_back(field);
324  heuristic_types.push_back(UNKNOWN_TYPE);
325  server_types.push_back(NO_SERVER_DATA);
326
327  test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field);
328  field.is_autofilled = true;
329  form.fields.push_back(field);
330  heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
331  server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
332
333  // Simulate having seen this form on page load.
334  autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
335
336  // Establish our expectations.
337  ::testing::InSequence dummy;
338  // Autofilled field
339  EXPECT_CALL(*autofill_manager_->metric_logger(),
340              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
341                  NAME_FULL));
342  EXPECT_CALL(*autofill_manager_->metric_logger(),
343              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
344                  NAME_FULL));
345  EXPECT_CALL(*autofill_manager_->metric_logger(),
346              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
347                  NAME_FULL));
348  // Non-autofilled field for which we had data
349  EXPECT_CALL(*autofill_manager_->metric_logger(),
350              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
351                  EMAIL_ADDRESS));
352  EXPECT_CALL(*autofill_manager_->metric_logger(),
353              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
354                  EMAIL_ADDRESS));
355  EXPECT_CALL(*autofill_manager_->metric_logger(),
356              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
357                  EMAIL_ADDRESS));
358  // Empty field
359  // Unknown field
360  // <select> field
361  EXPECT_CALL(*autofill_manager_->metric_logger(),
362              LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
363                  ADDRESS_HOME_COUNTRY));
364  EXPECT_CALL(*autofill_manager_->metric_logger(),
365              LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
366                  ADDRESS_HOME_COUNTRY));
367  EXPECT_CALL(*autofill_manager_->metric_logger(),
368              LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
369                  ADDRESS_HOME_COUNTRY));
370  // Phone field
371  EXPECT_CALL(*autofill_manager_->metric_logger(),
372              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
373                  PHONE_HOME_WHOLE_NUMBER));
374  EXPECT_CALL(*autofill_manager_->metric_logger(),
375              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
376                  PHONE_HOME_WHOLE_NUMBER));
377  EXPECT_CALL(*autofill_manager_->metric_logger(),
378              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
379                  PHONE_HOME_WHOLE_NUMBER));
380  EXPECT_CALL(*autofill_manager_->metric_logger(),
381              LogUserHappinessMetric(
382                  AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
383
384  // Simulate form submission.
385  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
386                                                           TimeTicks::Now()));
387}
388
389// Test that we behave sanely when the cached form differs from the submitted
390// one.
391TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
392  // Set up our form data.
393  FormData form;
394  form.name = ASCIIToUTF16("TestForm");
395  form.method = ASCIIToUTF16("POST");
396  form.origin = GURL("http://example.com/form.html");
397  form.action = GURL("http://example.com/submit.html");
398  form.user_submitted = true;
399
400  std::vector<ServerFieldType> heuristic_types, server_types;
401
402  FormFieldData field;
403  test::CreateTestFormField(
404      "Both match", "match", "Elvis Aaron Presley", "text", &field);
405  field.is_autofilled = true;
406  form.fields.push_back(field);
407  heuristic_types.push_back(NAME_FULL);
408  server_types.push_back(NAME_FULL);
409  test::CreateTestFormField(
410      "Both mismatch", "mismatch", "buddy@gmail.com", "text", &field);
411  field.is_autofilled = false;
412  form.fields.push_back(field);
413  heuristic_types.push_back(PHONE_HOME_NUMBER);
414  server_types.push_back(PHONE_HOME_NUMBER);
415  test::CreateTestFormField(
416      "Only heuristics match", "mixed", "Memphis", "text", &field);
417  field.is_autofilled = false;
418  form.fields.push_back(field);
419  heuristic_types.push_back(ADDRESS_HOME_CITY);
420  server_types.push_back(PHONE_HOME_NUMBER);
421  test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
422  field.is_autofilled = false;
423  form.fields.push_back(field);
424  heuristic_types.push_back(UNKNOWN_TYPE);
425  server_types.push_back(UNKNOWN_TYPE);
426
427  // Simulate having seen this form with the desired heuristic and server types.
428  // |form_structure| will be owned by |autofill_manager_|.
429  autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
430
431
432  // Add a field and re-arrange the remaining form fields before submitting.
433  std::vector<FormFieldData> cached_fields = form.fields;
434  form.fields.clear();
435  test::CreateTestFormField(
436      "New field", "new field", "Tennessee", "text", &field);
437  form.fields.push_back(field);
438  form.fields.push_back(cached_fields[2]);
439  form.fields.push_back(cached_fields[1]);
440  form.fields.push_back(cached_fields[3]);
441  form.fields.push_back(cached_fields[0]);
442
443  // Establish our expectations.
444  ::testing::InSequence dummy;
445  // New field
446  EXPECT_CALL(*autofill_manager_->metric_logger(),
447              LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
448                  ADDRESS_HOME_STATE));
449  EXPECT_CALL(*autofill_manager_->metric_logger(),
450              LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
451                  ADDRESS_HOME_STATE));
452  EXPECT_CALL(*autofill_manager_->metric_logger(),
453              LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
454                  ADDRESS_HOME_STATE));
455  // Only heuristics match
456  EXPECT_CALL(*autofill_manager_->metric_logger(),
457              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
458                  ADDRESS_HOME_CITY));
459  EXPECT_CALL(*autofill_manager_->metric_logger(),
460              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
461                  ADDRESS_HOME_CITY));
462  EXPECT_CALL(*autofill_manager_->metric_logger(),
463              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
464                  ADDRESS_HOME_CITY));
465  // Both mismatch
466  EXPECT_CALL(*autofill_manager_->metric_logger(),
467              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
468                  EMAIL_ADDRESS));
469  EXPECT_CALL(*autofill_manager_->metric_logger(),
470              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
471                  EMAIL_ADDRESS));
472  EXPECT_CALL(*autofill_manager_->metric_logger(),
473              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
474                  EMAIL_ADDRESS));
475  // Unknown
476  // Both match
477  EXPECT_CALL(*autofill_manager_->metric_logger(),
478              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
479                  NAME_FULL));
480  EXPECT_CALL(*autofill_manager_->metric_logger(),
481              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
482                  NAME_FULL));
483  EXPECT_CALL(*autofill_manager_->metric_logger(),
484              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
485                  NAME_FULL));
486
487  // Simulate form submission.
488  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
489                                                           TimeTicks::Now()));
490}
491
492// Verify that we correctly log metrics regarding developer engagement.
493TEST_F(AutofillMetricsTest, DeveloperEngagement) {
494  // Start with a non-fillable form.
495  FormData form;
496  form.name = ASCIIToUTF16("TestForm");
497  form.method = ASCIIToUTF16("POST");
498  form.origin = GURL("http://example.com/form.html");
499  form.action = GURL("http://example.com/submit.html");
500
501  FormFieldData field;
502  test::CreateTestFormField("Name", "name", "", "text", &field);
503  form.fields.push_back(field);
504  test::CreateTestFormField("Email", "email", "", "text", &field);
505  form.fields.push_back(field);
506
507  std::vector<FormData> forms(1, form);
508
509  // Ensure no metrics are logged when loading a non-fillable form.
510  {
511    EXPECT_CALL(*autofill_manager_->metric_logger(),
512                LogDeveloperEngagementMetric(_)).Times(0);
513    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
514                                   autofill::NO_SPECIAL_FORMS_SEEN);
515    autofill_manager_->Reset();
516    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
517  }
518
519  // Add another field to the form, so that it becomes fillable.
520  test::CreateTestFormField("Phone", "phone", "", "text", &field);
521  forms.back().fields.push_back(field);
522
523  // Expect only the "form parsed" metric to be logged; no metrics about
524  // author-specified field type hints.
525  {
526    EXPECT_CALL(
527        *autofill_manager_->metric_logger(),
528        LogDeveloperEngagementMetric(
529            AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
530    EXPECT_CALL(
531        *autofill_manager_->metric_logger(),
532        LogDeveloperEngagementMetric(
533            AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(0);
534    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
535                                   autofill::NO_SPECIAL_FORMS_SEEN);
536    autofill_manager_->Reset();
537    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
538  }
539
540  // Add some fields with an author-specified field type to the form.
541  // We need to add at least three fields, because a form must have at least
542  // three fillable fields to be considered to be autofillable; and if at least
543  // one field specifies an explicit type hint, we don't apply any of our usual
544  // local heuristics to detect field types in the rest of the form.
545  test::CreateTestFormField("", "", "", "text", &field);
546  field.autocomplete_attribute = "given-name";
547  forms.back().fields.push_back(field);
548  test::CreateTestFormField("", "", "", "text", &field);
549  field.autocomplete_attribute = "email";
550  forms.back().fields.push_back(field);
551  test::CreateTestFormField("", "", "", "text", &field);
552  field.autocomplete_attribute = "address-line1";
553  forms.back().fields.push_back(field);
554
555  // Expect both the "form parsed" metric and the author-specified field type
556  // hints metric to be logged.
557  {
558    EXPECT_CALL(
559        *autofill_manager_->metric_logger(),
560        LogDeveloperEngagementMetric(
561            AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
562    EXPECT_CALL(
563        *autofill_manager_->metric_logger(),
564        LogDeveloperEngagementMetric(
565            AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(1);
566    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
567                                   autofill::NO_SPECIAL_FORMS_SEEN);
568    autofill_manager_->Reset();
569    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
570  }
571}
572
573// Test that the profile count is logged correctly.
574TEST_F(AutofillMetricsTest, StoredProfileCount) {
575  // The metric should be logged when the profiles are first loaded.
576  EXPECT_CALL(*personal_data_->metric_logger(),
577              LogStoredProfileCount(2)).Times(1);
578  personal_data_->LoadProfiles();
579
580  // The metric should only be logged once.
581  EXPECT_CALL(*personal_data_->metric_logger(),
582              LogStoredProfileCount(::testing::_)).Times(0);
583  personal_data_->LoadProfiles();
584}
585
586// Test that we correctly log when Autofill is enabled.
587TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
588  personal_data_->set_autofill_enabled(true);
589  EXPECT_CALL(*personal_data_->metric_logger(),
590              LogIsAutofillEnabledAtStartup(true)).Times(1);
591  personal_data_->Init(
592      manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false);
593}
594
595// Test that we correctly log when Autofill is disabled.
596TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
597  personal_data_->set_autofill_enabled(false);
598  EXPECT_CALL(*personal_data_->metric_logger(),
599              LogIsAutofillEnabledAtStartup(false)).Times(1);
600  personal_data_->Init(
601      manager_delegate_.GetDatabase(), manager_delegate_.GetPrefs(), false);
602}
603
604// Test that we log the number of Autofill suggestions when filling a form.
605TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
606  // Set up our form data.
607  FormData form;
608  form.name = ASCIIToUTF16("TestForm");
609  form.method = ASCIIToUTF16("POST");
610  form.origin = GURL("http://example.com/form.html");
611  form.action = GURL("http://example.com/submit.html");
612  form.user_submitted = true;
613
614  FormFieldData field;
615  std::vector<ServerFieldType> field_types;
616  test::CreateTestFormField("Name", "name", "", "text", &field);
617  form.fields.push_back(field);
618  field_types.push_back(NAME_FULL);
619  test::CreateTestFormField("Email", "email", "", "email", &field);
620  form.fields.push_back(field);
621  field_types.push_back(EMAIL_ADDRESS);
622  test::CreateTestFormField("Phone", "phone", "", "tel", &field);
623  form.fields.push_back(field);
624  field_types.push_back(PHONE_HOME_NUMBER);
625
626  // Simulate having seen this form on page load.
627  // |form_structure| will be owned by |autofill_manager_|.
628  autofill_manager_->AddSeenForm(form, field_types, field_types);
629
630  // Establish our expectations.
631  ::testing::InSequence dummy;
632  EXPECT_CALL(*autofill_manager_->metric_logger(),
633              LogAddressSuggestionsCount(2)).Times(1);
634
635  // Simulate activating the autofill popup for the phone field.
636  autofill_manager_->OnQueryFormFieldAutofill(
637      0, form, field, gfx::Rect(), false);
638
639  // Simulate activating the autofill popup for the email field after typing.
640  // No new metric should be logged, since we're still on the same page.
641  test::CreateTestFormField("Email", "email", "b", "email", &field);
642  autofill_manager_->OnQueryFormFieldAutofill(
643      0, form, field, gfx::Rect(), false);
644
645  // Reset the autofill manager state.
646  autofill_manager_->Reset();
647  autofill_manager_->AddSeenForm(form, field_types, field_types);
648
649  // Establish our expectations.
650  EXPECT_CALL(*autofill_manager_->metric_logger(),
651              LogAddressSuggestionsCount(1)).Times(1);
652
653  // Simulate activating the autofill popup for the email field after typing.
654  autofill_manager_->OnQueryFormFieldAutofill(
655      0, form, field, gfx::Rect(), false);
656
657  // Reset the autofill manager state again.
658  autofill_manager_->Reset();
659  autofill_manager_->AddSeenForm(form, field_types, field_types);
660
661  // Establish our expectations.
662  EXPECT_CALL(*autofill_manager_->metric_logger(),
663              LogAddressSuggestionsCount(::testing::_)).Times(0);
664
665  // Simulate activating the autofill popup for the email field after typing.
666  form.fields[0].is_autofilled = true;
667  autofill_manager_->OnQueryFormFieldAutofill(
668      0, form, field, gfx::Rect(), false);
669}
670
671// Test that we log whether Autofill is enabled when filling a form.
672TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
673  // Establish our expectations.
674  ::testing::InSequence dummy;
675  EXPECT_CALL(*autofill_manager_->metric_logger(),
676              LogIsAutofillEnabledAtPageLoad(true)).Times(1);
677
678  autofill_manager_->set_autofill_enabled(true);
679  autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks(),
680                                 autofill::NO_SPECIAL_FORMS_SEEN);
681
682  // Reset the autofill manager state.
683  autofill_manager_->Reset();
684
685  // Establish our expectations.
686  EXPECT_CALL(*autofill_manager_->metric_logger(),
687              LogIsAutofillEnabledAtPageLoad(false)).Times(1);
688
689  autofill_manager_->set_autofill_enabled(false);
690  autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks(),
691                                 autofill::NO_SPECIAL_FORMS_SEEN);
692}
693
694// Verify that we correctly log user happiness metrics dealing with form loading
695// and form submission.
696TEST_F(AutofillMetricsTest, UserHappinessFormLoadAndSubmission) {
697  // Start with a form with insufficiently many fields.
698  FormData form;
699  form.name = ASCIIToUTF16("TestForm");
700  form.method = ASCIIToUTF16("POST");
701  form.origin = GURL("http://example.com/form.html");
702  form.action = GURL("http://example.com/submit.html");
703  form.user_submitted = true;
704
705  FormFieldData field;
706  test::CreateTestFormField("Name", "name", "", "text", &field);
707  form.fields.push_back(field);
708  test::CreateTestFormField("Email", "email", "", "text", &field);
709  form.fields.push_back(field);
710
711  std::vector<FormData> forms(1, form);
712
713  // Expect no notifications when the form is first seen.
714  {
715    EXPECT_CALL(*autofill_manager_->metric_logger(),
716                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED)).Times(0);
717    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
718                                   autofill::NO_SPECIAL_FORMS_SEEN);
719  }
720
721
722  // Expect no notifications when the form is submitted.
723  {
724    EXPECT_CALL(
725        *autofill_manager_->metric_logger(),
726        LogUserHappinessMetric(
727            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL)).Times(0);
728    EXPECT_CALL(
729        *autofill_manager_->metric_logger(),
730        LogUserHappinessMetric(
731            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME)).Times(0);
732    EXPECT_CALL(
733        *autofill_manager_->metric_logger(),
734        LogUserHappinessMetric(
735            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE)).Times(0);
736    EXPECT_CALL(
737        *autofill_manager_->metric_logger(),
738        LogUserHappinessMetric(
739            AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM)).Times(0);
740    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
741  }
742
743  // Add more fields to the form.
744  test::CreateTestFormField("Phone", "phone", "", "text", &field);
745  form.fields.push_back(field);
746  test::CreateTestFormField("Unknown", "unknown", "", "text", &field);
747  form.fields.push_back(field);
748  forms.front() = form;
749
750  // Expect a notification when the form is first seen.
751  {
752    EXPECT_CALL(*autofill_manager_->metric_logger(),
753                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
754    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
755                                   autofill::NO_SPECIAL_FORMS_SEEN);
756  }
757
758  // Expect a notification when the form is submitted.
759  {
760    EXPECT_CALL(*autofill_manager_->metric_logger(),
761                LogUserHappinessMetric(
762                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
763    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
764  }
765
766  // Fill in two of the fields.
767  form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
768  form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
769  forms.front() = form;
770
771  // Expect a notification when the form is submitted.
772  {
773    EXPECT_CALL(*autofill_manager_->metric_logger(),
774                LogUserHappinessMetric(
775                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
776    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
777  }
778
779  // Fill in the third field.
780  form.fields[2].value = ASCIIToUTF16("12345678901");
781  forms.front() = form;
782
783  // Expect notifications when the form is submitted.
784  {
785    EXPECT_CALL(*autofill_manager_->metric_logger(),
786                LogUserHappinessMetric(
787                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE));
788    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
789  }
790
791
792  // Mark one of the fields as autofilled.
793  form.fields[1].is_autofilled = true;
794  forms.front() = form;
795
796  // Expect notifications when the form is submitted.
797  {
798    EXPECT_CALL(*autofill_manager_->metric_logger(),
799                LogUserHappinessMetric(
800                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
801    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
802  }
803
804  // Mark all of the fillable fields as autofilled.
805  form.fields[0].is_autofilled = true;
806  form.fields[2].is_autofilled = true;
807  forms.front() = form;
808
809  // Expect notifications when the form is submitted.
810  {
811    EXPECT_CALL(*autofill_manager_->metric_logger(),
812                LogUserHappinessMetric(
813                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL));
814    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
815  }
816
817  // Clear out the third field's value.
818  form.fields[2].value = base::string16();
819  forms.front() = form;
820
821  // Expect notifications when the form is submitted.
822  {
823    EXPECT_CALL(*autofill_manager_->metric_logger(),
824                LogUserHappinessMetric(
825                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
826    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
827  }
828}
829
830// Verify that we correctly log user happiness metrics dealing with form
831// interaction.
832TEST_F(AutofillMetricsTest, UserHappinessFormInteraction) {
833  // Load a fillable form.
834  FormData form;
835  form.name = ASCIIToUTF16("TestForm");
836  form.method = ASCIIToUTF16("POST");
837  form.origin = GURL("http://example.com/form.html");
838  form.action = GURL("http://example.com/submit.html");
839  form.user_submitted = true;
840
841  FormFieldData field;
842  test::CreateTestFormField("Name", "name", "", "text", &field);
843  form.fields.push_back(field);
844  test::CreateTestFormField("Email", "email", "", "text", &field);
845  form.fields.push_back(field);
846  test::CreateTestFormField("Phone", "phone", "", "text", &field);
847  form.fields.push_back(field);
848
849  std::vector<FormData> forms(1, form);
850
851  // Expect a notification when the form is first seen.
852  {
853    EXPECT_CALL(*autofill_manager_->metric_logger(),
854                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
855    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
856                                   autofill::NO_SPECIAL_FORMS_SEEN);
857  }
858
859  // Simulate typing.
860  {
861    EXPECT_CALL(*autofill_manager_->metric_logger(),
862                LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE));
863    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
864                                            TimeTicks());
865  }
866
867  // Simulate suggestions shown twice for a single edit (i.e. multiple
868  // keystrokes in a single field).
869  {
870    EXPECT_CALL(*autofill_manager_->metric_logger(),
871                LogUserHappinessMetric(
872                    AutofillMetrics::SUGGESTIONS_SHOWN)).Times(1);
873    EXPECT_CALL(*autofill_manager_->metric_logger(),
874                LogUserHappinessMetric(
875                    AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(1);
876    autofill_manager_->DidShowSuggestions(true);
877    autofill_manager_->DidShowSuggestions(false);
878  }
879
880  // Simulate suggestions shown for a different field.
881  {
882    EXPECT_CALL(*autofill_manager_->metric_logger(),
883                LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN));
884    EXPECT_CALL(*autofill_manager_->metric_logger(),
885                LogUserHappinessMetric(
886                    AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(0);
887    autofill_manager_->DidShowSuggestions(true);
888  }
889
890  // Simulate invoking autofill.
891  {
892    EXPECT_CALL(*autofill_manager_->metric_logger(),
893                LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
894    EXPECT_CALL(*autofill_manager_->metric_logger(),
895                LogUserHappinessMetric(
896                    AutofillMetrics::USER_DID_AUTOFILL_ONCE));
897    autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
898  }
899
900  // Simulate editing an autofilled field.
901  {
902    EXPECT_CALL(*autofill_manager_->metric_logger(),
903                LogUserHappinessMetric(
904                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
905    EXPECT_CALL(*autofill_manager_->metric_logger(),
906                LogUserHappinessMetric(
907                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE));
908    PersonalDataManager::GUIDPair guid(
909        "00000000-0000-0000-0000-000000000001", 0);
910    PersonalDataManager::GUIDPair empty(std::string(), 0);
911    autofill_manager_->FillOrPreviewForm(
912        AutofillDriver::FORM_DATA_ACTION_FILL,
913        0, form, form.fields.front(),
914        autofill_manager_->PackGUIDs(empty, guid));
915    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
916                                            TimeTicks());
917    // Simulate a second keystroke; make sure we don't log the metric twice.
918    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
919                                            TimeTicks());
920  }
921
922  // Simulate invoking autofill again.
923  EXPECT_CALL(*autofill_manager_->metric_logger(),
924              LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
925  EXPECT_CALL(*autofill_manager_->metric_logger(),
926              LogUserHappinessMetric(
927                  AutofillMetrics::USER_DID_AUTOFILL_ONCE)).Times(0);
928  autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
929
930  // Simulate editing another autofilled field.
931  {
932    EXPECT_CALL(*autofill_manager_->metric_logger(),
933                LogUserHappinessMetric(
934                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
935    autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
936  }
937}
938
939// Verify that we correctly log metrics tracking the duration of form fill.
940TEST_F(AutofillMetricsTest, FormFillDuration) {
941  // Load a fillable form.
942  FormData form;
943  form.name = ASCIIToUTF16("TestForm");
944  form.method = ASCIIToUTF16("POST");
945  form.origin = GURL("http://example.com/form.html");
946  form.action = GURL("http://example.com/submit.html");
947  form.user_submitted = true;
948
949  FormFieldData field;
950  test::CreateTestFormField("Name", "name", "", "text", &field);
951  form.fields.push_back(field);
952  test::CreateTestFormField("Email", "email", "", "text", &field);
953  form.fields.push_back(field);
954  test::CreateTestFormField("Phone", "phone", "", "text", &field);
955  form.fields.push_back(field);
956
957  std::vector<FormData> forms(1, form);
958
959  // Fill the field values for form submission.
960  form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
961  form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
962  form.fields[2].value = ASCIIToUTF16("12345678901");
963
964  // Expect only form load metrics to be logged if the form is submitted without
965  // user interaction.
966  {
967    EXPECT_CALL(*autofill_manager_->metric_logger(),
968                LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
969    EXPECT_CALL(*autofill_manager_->metric_logger(),
970                LogFormFillDurationFromLoadWithoutAutofill(
971                    TimeDelta::FromInternalValue(16)));
972    EXPECT_CALL(*autofill_manager_->metric_logger(),
973                LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
974    EXPECT_CALL(*autofill_manager_->metric_logger(),
975                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
976    autofill_manager_->OnFormsSeen(
977        forms, TimeTicks::FromInternalValue(1),
978        autofill::NO_SPECIAL_FORMS_SEEN);
979    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
980    autofill_manager_->Reset();
981    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
982  }
983
984  // Expect metric to be logged if the user manually edited a form field.
985  {
986    EXPECT_CALL(*autofill_manager_->metric_logger(),
987                LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
988    EXPECT_CALL(*autofill_manager_->metric_logger(),
989                LogFormFillDurationFromLoadWithoutAutofill(
990                    TimeDelta::FromInternalValue(16)));
991    EXPECT_CALL(*autofill_manager_->metric_logger(),
992                LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
993    EXPECT_CALL(*autofill_manager_->metric_logger(),
994                LogFormFillDurationFromInteractionWithoutAutofill(
995                    TimeDelta::FromInternalValue(14)));
996    autofill_manager_->OnFormsSeen(
997        forms, TimeTicks::FromInternalValue(1),
998        autofill::NO_SPECIAL_FORMS_SEEN);
999    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1000                                            TimeTicks::FromInternalValue(3));
1001    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1002    autofill_manager_->Reset();
1003    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1004  }
1005
1006  // Expect metric to be logged if the user autofilled the form.
1007  form.fields[0].is_autofilled = true;
1008  {
1009    EXPECT_CALL(*autofill_manager_->metric_logger(),
1010                LogFormFillDurationFromLoadWithAutofill(
1011                    TimeDelta::FromInternalValue(16)));
1012    EXPECT_CALL(*autofill_manager_->metric_logger(),
1013                LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1014    EXPECT_CALL(*autofill_manager_->metric_logger(),
1015                LogFormFillDurationFromInteractionWithAutofill(
1016                    TimeDelta::FromInternalValue(12)));
1017    EXPECT_CALL(*autofill_manager_->metric_logger(),
1018                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1019    autofill_manager_->OnFormsSeen(
1020        forms, TimeTicks::FromInternalValue(1),
1021        autofill::NO_SPECIAL_FORMS_SEEN);
1022    autofill_manager_->OnDidFillAutofillFormData(
1023        TimeTicks::FromInternalValue(5));
1024    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1025    autofill_manager_->Reset();
1026    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1027  }
1028
1029  // Expect metric to be logged if the user both manually filled some fields
1030  // and autofilled others.  Messages can arrive out of order, so make sure they
1031  // take precedence appropriately.
1032  {
1033    EXPECT_CALL(*autofill_manager_->metric_logger(),
1034                LogFormFillDurationFromLoadWithAutofill(
1035                    TimeDelta::FromInternalValue(16)));
1036    EXPECT_CALL(*autofill_manager_->metric_logger(),
1037                LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1038    EXPECT_CALL(*autofill_manager_->metric_logger(),
1039                LogFormFillDurationFromInteractionWithAutofill(
1040                    TimeDelta::FromInternalValue(14)));
1041    EXPECT_CALL(*autofill_manager_->metric_logger(),
1042                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1043    autofill_manager_->OnFormsSeen(
1044        forms, TimeTicks::FromInternalValue(1),
1045        autofill::NO_SPECIAL_FORMS_SEEN);
1046    autofill_manager_->OnDidFillAutofillFormData(
1047        TimeTicks::FromInternalValue(5));
1048    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1049                                            TimeTicks::FromInternalValue(3));
1050    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1051    autofill_manager_->Reset();
1052    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1053  }
1054}
1055
1056}  // namespace autofill
1057