1// Copyright (c) 2011 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 <vector>
6
7#include "base/memory/ref_counted.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/string16.h"
10#include "base/utf_string_conversions.h"
11#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
12#include "chrome/browser/autofill/autofill_common_test.h"
13#include "chrome/browser/autofill/autofill_manager.h"
14#include "chrome/browser/autofill/autofill_metrics.h"
15#include "chrome/browser/autofill/personal_data_manager.h"
16#include "chrome/browser/webdata/web_data_service.h"
17#include "content/browser/tab_contents/test_tab_contents.h"
18#include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
19#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
21#include "webkit/glue/form_data.h"
22#include "webkit/glue/form_field.h"
23
24using webkit_glue::FormData;
25using webkit_glue::FormField;
26
27namespace {
28
29class MockAutofillMetrics : public AutofillMetrics {
30 public:
31  MockAutofillMetrics() {}
32  MOCK_CONST_METHOD1(Log, void(CreditCardInfoBarMetric metric));
33  MOCK_CONST_METHOD3(Log, void(HeuristicTypeQualityMetric metric,
34                               AutofillFieldType field_type,
35                               const std::string& experiment_id));
36  MOCK_CONST_METHOD3(Log, void(PredictedTypeQualityMetric metric,
37                               AutofillFieldType field_type,
38                               const std::string& experiment_id));
39  MOCK_CONST_METHOD2(Log, void(QualityMetric metric,
40                               const std::string& experiment_id));
41  MOCK_CONST_METHOD1(Log, void(ServerQueryMetric metric));
42  MOCK_CONST_METHOD3(Log, void(ServerTypeQualityMetric metric,
43                               AutofillFieldType field_type,
44                               const std::string& experiment_id));
45  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled));
46  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled));
47  MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles));
48  MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions));
49
50 private:
51  DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
52};
53
54class TestPersonalDataManager : public PersonalDataManager {
55 public:
56  TestPersonalDataManager() : autofill_enabled_(true) {
57    set_metric_logger(new MockAutofillMetrics);
58    CreateTestAutofillProfiles(&web_profiles_);
59  }
60
61  // Overridden to avoid a trip to the database. This should be a no-op except
62  // for the side-effect of logging the profile count.
63  virtual void LoadProfiles() OVERRIDE {
64    std::vector<AutofillProfile*> profiles;
65    web_profiles_.release(&profiles);
66    WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
67                                                    profiles);
68    ReceiveLoadedProfiles(0, &result);
69  }
70
71  // Overridden to avoid a trip to the database.
72  virtual void LoadCreditCards() OVERRIDE {}
73
74  // Adds |profile| to |web_profiles_| and takes ownership of the profile's
75  // memory.
76  virtual void AddProfile(AutofillProfile* profile) {
77    web_profiles_.push_back(profile);
78  }
79
80  const MockAutofillMetrics* metric_logger() const {
81    return static_cast<const MockAutofillMetrics*>(
82        PersonalDataManager::metric_logger());
83  }
84
85  void set_autofill_enabled(bool autofill_enabled) {
86    autofill_enabled_ = autofill_enabled;
87  }
88
89  virtual bool IsAutofillEnabled() const OVERRIDE {
90    return autofill_enabled_;
91  }
92
93  MOCK_METHOD1(SaveImportedCreditCard,
94               void(const CreditCard& imported_credit_card));
95
96 private:
97  void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
98    AutofillProfile* profile = new AutofillProfile;
99    autofill_test::SetProfileInfo(profile, "Elvis", "Aaron",
100                                  "Presley", "theking@gmail.com", "RCA",
101                                  "3734 Elvis Presley Blvd.", "Apt. 10",
102                                  "Memphis", "Tennessee", "38116", "USA",
103                                  "12345678901", "");
104    profile->set_guid("00000000-0000-0000-0000-000000000001");
105    profiles->push_back(profile);
106    profile = new AutofillProfile;
107    autofill_test::SetProfileInfo(profile, "Charles", "Hardin",
108                                  "Holley", "buddy@gmail.com", "Decca",
109                                  "123 Apple St.", "unit 6", "Lubbock",
110                                  "Texas", "79401", "USA", "2345678901",
111                                  "");
112    profile->set_guid("00000000-0000-0000-0000-000000000002");
113    profiles->push_back(profile);
114  }
115
116  bool autofill_enabled_;
117
118  DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
119};
120
121class TestAutofillManager : public AutofillManager {
122 public:
123  TestAutofillManager(TabContents* tab_contents,
124                      TestPersonalDataManager* personal_manager)
125      : AutofillManager(tab_contents, personal_manager),
126        autofill_enabled_(true) {
127    set_metric_logger(new MockAutofillMetrics);
128  }
129  virtual ~TestAutofillManager() {}
130
131  virtual bool IsAutofillEnabled() const { return autofill_enabled_; }
132
133  void set_autofill_enabled(bool autofill_enabled) {
134    autofill_enabled_ = autofill_enabled;
135  }
136
137  const MockAutofillMetrics* metric_logger() const {
138    return static_cast<const MockAutofillMetrics*>(
139        AutofillManager::metric_logger());
140  }
141
142  void AddSeenForm(FormStructure* form) {
143    form_structures()->push_back(form);
144  }
145
146 private:
147  bool autofill_enabled_;
148
149  DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
150};
151
152class TestFormStructure : public FormStructure {
153 public:
154  explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
155  virtual ~TestFormStructure() {}
156
157  void SetFieldTypes(const std::vector<AutofillFieldType>& heuristic_types,
158                     const std::vector<AutofillFieldType>& server_types) {
159    ASSERT_EQ(field_count(), heuristic_types.size());
160    ASSERT_EQ(field_count(), server_types.size());
161
162    for (size_t i = 0; i < field_count(); ++i) {
163      AutofillField* field = (*fields())[i];
164      ASSERT_TRUE(field);
165      field->set_heuristic_type(heuristic_types[i]);
166      field->set_server_type(server_types[i]);
167    }
168
169    UpdateAutofillCount();
170  }
171
172  virtual std::string server_experiment_id() const OVERRIDE {
173    return server_experiment_id_;
174  }
175  void set_server_experiment_id(const std::string& server_experiment_id) {
176    server_experiment_id_ = server_experiment_id;
177  }
178
179 private:
180  std::string server_experiment_id_;
181  DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
182};
183
184}  // namespace
185
186class AutofillMetricsTest : public TabContentsWrapperTestHarness {
187 public:
188  AutofillMetricsTest() {}
189  virtual ~AutofillMetricsTest() {
190    // Order of destruction is important as AutofillManager relies on
191    // PersonalDataManager to be around when it gets destroyed.
192    autofill_manager_.reset(NULL);
193    test_personal_data_ = NULL;
194  }
195
196  virtual void SetUp() {
197    TabContentsWrapperTestHarness::SetUp();
198    test_personal_data_ = new TestPersonalDataManager();
199    autofill_manager_.reset(new TestAutofillManager(contents(),
200                                                    test_personal_data_.get()));
201  }
202
203 protected:
204  scoped_ptr<TestAutofillManager> autofill_manager_;
205  scoped_refptr<TestPersonalDataManager> test_personal_data_;
206
207 private:
208  DISALLOW_COPY_AND_ASSIGN(AutofillMetricsTest);
209};
210
211// Test that we log quality metrics appropriately.
212TEST_F(AutofillMetricsTest, QualityMetrics) {
213  // Set up our form data.
214  FormData form;
215  form.name = ASCIIToUTF16("TestForm");
216  form.method = ASCIIToUTF16("POST");
217  form.origin = GURL("http://example.com/form.html");
218  form.action = GURL("http://example.com/submit.html");
219  form.user_submitted = true;
220
221  std::vector<AutofillFieldType> heuristic_types, server_types;
222  FormField field;
223
224  autofill_test::CreateTestFormField(
225      "Autofilled", "autofilled", "Elvis Presley", "text", &field);
226  field.is_autofilled = true;
227  form.fields.push_back(field);
228  heuristic_types.push_back(NAME_FULL);
229  server_types.push_back(NAME_FIRST);
230
231  autofill_test::CreateTestFormField(
232      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
233  form.fields.push_back(field);
234  heuristic_types.push_back(PHONE_HOME_NUMBER);
235  server_types.push_back(EMAIL_ADDRESS);
236
237  autofill_test::CreateTestFormField(
238      "Empty", "empty", "", "text", &field);
239  form.fields.push_back(field);
240  heuristic_types.push_back(NAME_FULL);
241  server_types.push_back(NAME_FIRST);
242
243  autofill_test::CreateTestFormField(
244      "Unknown", "unknown", "garbage", "text", &field);
245  form.fields.push_back(field);
246  heuristic_types.push_back(PHONE_HOME_NUMBER);
247  server_types.push_back(EMAIL_ADDRESS);
248
249  autofill_test::CreateTestFormField(
250      "Select", "select", "USA", "select-one", &field);
251  form.fields.push_back(field);
252  heuristic_types.push_back(UNKNOWN_TYPE);
253  server_types.push_back(NO_SERVER_DATA);
254
255  autofill_test::CreateTestFormField(
256      "Phone", "phone", "2345678901", "tel", &field);
257  field.is_autofilled = true;
258  form.fields.push_back(field);
259  heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
260  server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
261
262  // Simulate having seen this form on page load.
263  // |form_structure| will be owned by |autofill_manager_|.
264  TestFormStructure* form_structure = new TestFormStructure(form);
265  form_structure->SetFieldTypes(heuristic_types, server_types);
266  autofill_manager_->AddSeenForm(form_structure);
267
268  // Establish our expectations.
269  ::testing::InSequence dummy;
270  // Autofilled field
271  EXPECT_CALL(*autofill_manager_->metric_logger(),
272              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
273  EXPECT_CALL(*autofill_manager_->metric_logger(),
274              Log(AutofillMetrics::HEURISTIC_TYPE_MATCH,
275                  UNKNOWN_TYPE, std::string()));
276  EXPECT_CALL(*autofill_manager_->metric_logger(),
277              Log(AutofillMetrics::SERVER_TYPE_MISMATCH,
278                  UNKNOWN_TYPE, std::string()));
279  EXPECT_CALL(*autofill_manager_->metric_logger(),
280              Log(AutofillMetrics::PREDICTED_TYPE_MISMATCH,
281                  UNKNOWN_TYPE, std::string()));
282  EXPECT_CALL(*autofill_manager_->metric_logger(),
283              Log(AutofillMetrics::FIELD_AUTOFILLED, std::string()));
284  // Non-autofilled field for which we had data
285  EXPECT_CALL(*autofill_manager_->metric_logger(),
286              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
287  EXPECT_CALL(*autofill_manager_->metric_logger(),
288              Log(AutofillMetrics::HEURISTIC_TYPE_MISMATCH,
289                  EMAIL_ADDRESS, std::string()));
290  EXPECT_CALL(*autofill_manager_->metric_logger(),
291              Log(AutofillMetrics::SERVER_TYPE_MATCH,
292                  EMAIL_ADDRESS, std::string()));
293  EXPECT_CALL(*autofill_manager_->metric_logger(),
294              Log(AutofillMetrics::PREDICTED_TYPE_MATCH,
295                  EMAIL_ADDRESS, std::string()));
296  EXPECT_CALL(*autofill_manager_->metric_logger(),
297              Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, std::string()));
298  EXPECT_CALL(*autofill_manager_->metric_logger(),
299              Log(AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
300                  std::string()));
301  EXPECT_CALL(*autofill_manager_->metric_logger(),
302              Log(AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH,
303                  std::string()));
304  // Empty field
305  EXPECT_CALL(*autofill_manager_->metric_logger(),
306              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
307  // Unknown field
308  EXPECT_CALL(*autofill_manager_->metric_logger(),
309              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
310  // <select> field
311  EXPECT_CALL(*autofill_manager_->metric_logger(),
312              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
313  EXPECT_CALL(*autofill_manager_->metric_logger(),
314              Log(AutofillMetrics::HEURISTIC_TYPE_UNKNOWN,
315                  ADDRESS_HOME_COUNTRY, std::string()));
316  EXPECT_CALL(*autofill_manager_->metric_logger(),
317              Log(AutofillMetrics::SERVER_TYPE_UNKNOWN,
318                  ADDRESS_HOME_COUNTRY, std::string()));
319  EXPECT_CALL(*autofill_manager_->metric_logger(),
320              Log(AutofillMetrics::PREDICTED_TYPE_UNKNOWN,
321                  ADDRESS_HOME_COUNTRY, std::string()));
322  // Phone field
323  EXPECT_CALL(*autofill_manager_->metric_logger(),
324              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
325  EXPECT_CALL(*autofill_manager_->metric_logger(),
326              Log(AutofillMetrics::HEURISTIC_TYPE_MATCH,
327                  PHONE_HOME_WHOLE_NUMBER, std::string()));
328  EXPECT_CALL(*autofill_manager_->metric_logger(),
329              Log(AutofillMetrics::SERVER_TYPE_MATCH,
330                  PHONE_HOME_WHOLE_NUMBER, std::string()));
331  EXPECT_CALL(*autofill_manager_->metric_logger(),
332              Log(AutofillMetrics::PREDICTED_TYPE_MATCH,
333                  PHONE_HOME_WHOLE_NUMBER, std::string()));
334  EXPECT_CALL(*autofill_manager_->metric_logger(),
335              Log(AutofillMetrics::FIELD_AUTOFILLED, std::string()));
336
337  // Simulate form submission.
338  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
339}
340
341// Test that we log the appropriate additional metrics when Autofill failed.
342TEST_F(AutofillMetricsTest, QualityMetricsForFailure) {
343  // Set up our form data.
344  FormData form;
345  form.name = ASCIIToUTF16("TestForm");
346  form.method = ASCIIToUTF16("POST");
347  form.origin = GURL("http://example.com/form.html");
348  form.action = GURL("http://example.com/submit.html");
349  form.user_submitted = true;
350
351  struct {
352    const char* label;
353    const char* name;
354    const char* value;
355    AutofillFieldType heuristic_type;
356    AutofillFieldType server_type;
357    AutofillMetrics::QualityMetric heuristic_metric;
358    AutofillMetrics::QualityMetric server_metric;
359  } failure_cases[] = {
360    {
361      "Heuristics unknown, server unknown", "0,0", "Elvis",
362      UNKNOWN_TYPE, NO_SERVER_DATA,
363      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
364      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
365    },
366    {
367      "Heuristics match, server unknown", "1,0", "Aaron",
368      NAME_MIDDLE, NO_SERVER_DATA,
369      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
370      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
371    },
372    {
373      "Heuristics mismatch, server unknown", "2,0", "Presley",
374      PHONE_HOME_NUMBER, NO_SERVER_DATA,
375      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
376      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
377    },
378    {
379      "Heuristics unknown, server match", "0,1", "theking@gmail.com",
380      UNKNOWN_TYPE, EMAIL_ADDRESS,
381      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
382      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
383    },
384    {
385      "Heuristics match, server match", "1,1", "3734 Elvis Presley Blvd.",
386      ADDRESS_HOME_LINE1, ADDRESS_HOME_LINE1,
387      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
388      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
389    },
390    {
391      "Heuristics mismatch, server match", "2,1", "Apt. 10",
392      PHONE_HOME_NUMBER, ADDRESS_HOME_LINE2,
393      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
394      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
395    },
396    {
397      "Heuristics unknown, server mismatch", "0,2", "Memphis",
398      UNKNOWN_TYPE, PHONE_HOME_NUMBER,
399      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
400      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
401    },
402    {
403      "Heuristics match, server mismatch", "1,2", "Tennessee",
404      ADDRESS_HOME_STATE, PHONE_HOME_NUMBER,
405      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
406      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
407    },
408    {
409      "Heuristics mismatch, server mismatch", "2,2", "38116",
410      PHONE_HOME_NUMBER, PHONE_HOME_NUMBER,
411      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
412      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
413    }
414  };
415
416  std::vector<AutofillFieldType> heuristic_types, server_types;
417  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) {
418    FormField field;
419    autofill_test::CreateTestFormField(failure_cases[i].label,
420                                       failure_cases[i].name,
421                                       failure_cases[i].value, "text", &field);
422    form.fields.push_back(field);
423    heuristic_types.push_back(failure_cases[i].heuristic_type);
424    server_types.push_back(failure_cases[i].server_type);
425
426  }
427
428  // Simulate having seen this form with the desired heuristic and server types.
429  // |form_structure| will be owned by |autofill_manager_|.
430  TestFormStructure* form_structure = new TestFormStructure(form);
431  form_structure->SetFieldTypes(heuristic_types, server_types);
432  autofill_manager_->AddSeenForm(form_structure);
433
434  // Establish our expectations.
435  ::testing::FLAGS_gmock_verbose = "error";
436  ::testing::InSequence dummy;
437  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) {
438    EXPECT_CALL(*autofill_manager_->metric_logger(),
439                Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
440    EXPECT_CALL(*autofill_manager_->metric_logger(),
441                Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, std::string()));
442    EXPECT_CALL(*autofill_manager_->metric_logger(),
443                Log(failure_cases[i].heuristic_metric, std::string()));
444    EXPECT_CALL(*autofill_manager_->metric_logger(),
445                Log(failure_cases[i].server_metric, std::string()));
446  }
447
448  // Simulate form submission.
449  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
450}
451
452// Test that we behave sanely when the cached form differs from the submitted
453// one.
454TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
455  // Set up our form data.
456  FormData form;
457  form.name = ASCIIToUTF16("TestForm");
458  form.method = ASCIIToUTF16("POST");
459  form.origin = GURL("http://example.com/form.html");
460  form.action = GURL("http://example.com/submit.html");
461  form.user_submitted = true;
462
463  std::vector<AutofillFieldType> heuristic_types, server_types;
464
465  FormField field;
466  autofill_test::CreateTestFormField(
467      "Both match", "match", "Elvis Presley", "text", &field);
468  field.is_autofilled = true;
469  form.fields.push_back(field);
470  heuristic_types.push_back(NAME_FULL);
471  server_types.push_back(NAME_FULL);
472  autofill_test::CreateTestFormField(
473      "Both mismatch", "mismatch", "buddy@gmail.com", "text", &field);
474  form.fields.push_back(field);
475  heuristic_types.push_back(PHONE_HOME_NUMBER);
476  server_types.push_back(PHONE_HOME_NUMBER);
477  autofill_test::CreateTestFormField(
478      "Only heuristics match", "mixed", "Memphis", "text", &field);
479  form.fields.push_back(field);
480  heuristic_types.push_back(ADDRESS_HOME_CITY);
481  server_types.push_back(PHONE_HOME_NUMBER);
482  autofill_test::CreateTestFormField(
483      "Unknown", "unknown", "garbage", "text", &field);
484  form.fields.push_back(field);
485  heuristic_types.push_back(UNKNOWN_TYPE);
486  server_types.push_back(UNKNOWN_TYPE);
487
488  // Simulate having seen this form with the desired heuristic and server types.
489  // |form_structure| will be owned by |autofill_manager_|.
490  TestFormStructure* form_structure = new TestFormStructure(form);
491  form_structure->SetFieldTypes(heuristic_types, server_types);
492  autofill_manager_->AddSeenForm(form_structure);
493
494  // Add a field and re-arrange the remaining form fields before submitting.
495  std::vector<FormField> cached_fields = form.fields;
496  form.fields.clear();
497  autofill_test::CreateTestFormField(
498      "New field", "new field", "Tennessee", "text", &field);
499  form.fields.push_back(field);
500  form.fields.push_back(cached_fields[2]);
501  form.fields.push_back(cached_fields[1]);
502  form.fields.push_back(cached_fields[3]);
503  form.fields.push_back(cached_fields[0]);
504
505  // Establish our expectations.
506  ::testing::InSequence dummy;
507  // New field
508  EXPECT_CALL(*autofill_manager_->metric_logger(),
509              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
510  EXPECT_CALL(*autofill_manager_->metric_logger(),
511              Log(AutofillMetrics::HEURISTIC_TYPE_UNKNOWN,
512                  ADDRESS_HOME_STATE, std::string()));
513  EXPECT_CALL(*autofill_manager_->metric_logger(),
514              Log(AutofillMetrics::SERVER_TYPE_UNKNOWN,
515                  ADDRESS_HOME_STATE, std::string()));
516  EXPECT_CALL(*autofill_manager_->metric_logger(),
517              Log(AutofillMetrics::PREDICTED_TYPE_UNKNOWN,
518                  ADDRESS_HOME_STATE, std::string()));
519  EXPECT_CALL(*autofill_manager_->metric_logger(),
520              Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, std::string()));
521  EXPECT_CALL(*autofill_manager_->metric_logger(),
522              Log(AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
523                  std::string()));
524  EXPECT_CALL(*autofill_manager_->metric_logger(),
525              Log(AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN,
526                  std::string()));
527  // Only heuristics match
528  EXPECT_CALL(*autofill_manager_->metric_logger(),
529              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
530  EXPECT_CALL(*autofill_manager_->metric_logger(),
531              Log(AutofillMetrics::HEURISTIC_TYPE_MATCH,
532                  ADDRESS_HOME_CITY, std::string()));
533  EXPECT_CALL(*autofill_manager_->metric_logger(),
534              Log(AutofillMetrics::SERVER_TYPE_MISMATCH,
535                  ADDRESS_HOME_CITY, std::string()));
536  EXPECT_CALL(*autofill_manager_->metric_logger(),
537              Log(AutofillMetrics::PREDICTED_TYPE_MISMATCH,
538                  ADDRESS_HOME_CITY, std::string()));
539  EXPECT_CALL(*autofill_manager_->metric_logger(),
540              Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, std::string()));
541  EXPECT_CALL(*autofill_manager_->metric_logger(),
542              Log(AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
543                  std::string()));
544  EXPECT_CALL(*autofill_manager_->metric_logger(),
545              Log(AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH,
546                  std::string()));
547  // Both mismatch
548  EXPECT_CALL(*autofill_manager_->metric_logger(),
549              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
550  EXPECT_CALL(*autofill_manager_->metric_logger(),
551              Log(AutofillMetrics::HEURISTIC_TYPE_MISMATCH,
552                  EMAIL_ADDRESS, std::string()));
553  EXPECT_CALL(*autofill_manager_->metric_logger(),
554              Log(AutofillMetrics::SERVER_TYPE_MISMATCH,
555                  EMAIL_ADDRESS, std::string()));
556  EXPECT_CALL(*autofill_manager_->metric_logger(),
557              Log(AutofillMetrics::PREDICTED_TYPE_MISMATCH,
558                  EMAIL_ADDRESS, std::string()));
559  EXPECT_CALL(*autofill_manager_->metric_logger(),
560              Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, std::string()));
561  EXPECT_CALL(*autofill_manager_->metric_logger(),
562              Log(AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
563                  std::string()));
564  EXPECT_CALL(*autofill_manager_->metric_logger(),
565              Log(AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH,
566                  std::string()));
567  // Unknown
568  EXPECT_CALL(*autofill_manager_->metric_logger(),
569              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
570  // Both match
571  EXPECT_CALL(*autofill_manager_->metric_logger(),
572              Log(AutofillMetrics::FIELD_SUBMITTED, std::string()));
573  EXPECT_CALL(*autofill_manager_->metric_logger(),
574              Log(AutofillMetrics::HEURISTIC_TYPE_MATCH,
575                  UNKNOWN_TYPE, std::string()));
576  EXPECT_CALL(*autofill_manager_->metric_logger(),
577              Log(AutofillMetrics::SERVER_TYPE_MATCH,
578                  UNKNOWN_TYPE, std::string()));
579  EXPECT_CALL(*autofill_manager_->metric_logger(),
580              Log(AutofillMetrics::PREDICTED_TYPE_MATCH,
581                  UNKNOWN_TYPE, std::string()));
582  EXPECT_CALL(*autofill_manager_->metric_logger(),
583              Log(AutofillMetrics::FIELD_AUTOFILLED, std::string()));
584
585  // Simulate form submission.
586  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
587}
588
589// Test that we don't log quality metrics for non-autofillable forms.
590TEST_F(AutofillMetricsTest, NoQualityMetricsForNonAutofillableForms) {
591  // Forms must include at least three fields to be auto-fillable.
592  FormData form;
593  form.name = ASCIIToUTF16("TestForm");
594  form.method = ASCIIToUTF16("POST");
595  form.origin = GURL("http://example.com/form.html");
596  form.action = GURL("http://example.com/submit.html");
597  form.user_submitted = true;
598
599  FormField field;
600  autofill_test::CreateTestFormField(
601      "Autofilled", "autofilled", "Elvis Presley", "text", &field);
602  field.is_autofilled = true;
603  form.fields.push_back(field);
604  autofill_test::CreateTestFormField(
605      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
606  form.fields.push_back(field);
607
608  // Simulate form submission.
609  EXPECT_CALL(*autofill_manager_->metric_logger(),
610              Log(AutofillMetrics::FIELD_SUBMITTED, std::string())).Times(0);
611  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
612
613  // Search forms are not auto-fillable.
614  form.action = GURL("http://example.com/search?q=Elvis%20Presley");
615  autofill_test::CreateTestFormField(
616      "Empty", "empty", "", "text", &field);
617  form.fields.push_back(field);
618
619  // Simulate form submission.
620  EXPECT_CALL(*autofill_manager_->metric_logger(),
621              Log(AutofillMetrics::FIELD_SUBMITTED, std::string())).Times(0);
622  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
623}
624
625// Test that we recored the experiment id appropriately.
626TEST_F(AutofillMetricsTest, QualityMetricsWithExperimentId) {
627  // Set up our form data.
628  FormData form;
629  form.name = ASCIIToUTF16("TestForm");
630  form.method = ASCIIToUTF16("POST");
631  form.origin = GURL("http://example.com/form.html");
632  form.action = GURL("http://example.com/submit.html");
633  form.user_submitted = true;
634
635  std::vector<AutofillFieldType> heuristic_types, server_types;
636  FormField field;
637
638  autofill_test::CreateTestFormField(
639      "Autofilled", "autofilled", "Elvis Presley", "text", &field);
640  field.is_autofilled = true;
641  form.fields.push_back(field);
642  heuristic_types.push_back(NAME_FULL);
643  server_types.push_back(NAME_FIRST);
644
645  autofill_test::CreateTestFormField(
646      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
647  form.fields.push_back(field);
648  heuristic_types.push_back(PHONE_HOME_NUMBER);
649  server_types.push_back(EMAIL_ADDRESS);
650
651  autofill_test::CreateTestFormField(
652      "Empty", "empty", "", "text", &field);
653  form.fields.push_back(field);
654  heuristic_types.push_back(NAME_FULL);
655  server_types.push_back(NAME_FIRST);
656
657  autofill_test::CreateTestFormField(
658      "Unknown", "unknown", "garbage", "text", &field);
659  form.fields.push_back(field);
660  heuristic_types.push_back(PHONE_HOME_NUMBER);
661  server_types.push_back(EMAIL_ADDRESS);
662
663  autofill_test::CreateTestFormField(
664      "Select", "select", "USA", "select-one", &field);
665  form.fields.push_back(field);
666  heuristic_types.push_back(UNKNOWN_TYPE);
667  server_types.push_back(NO_SERVER_DATA);
668
669  const std::string experiment_id = "ThatOughtaDoIt";
670
671  // Simulate having seen this form on page load.
672  // |form_structure| will be owned by |autofill_manager_|.
673  TestFormStructure* form_structure = new TestFormStructure(form);
674  form_structure->SetFieldTypes(heuristic_types, server_types);
675  form_structure->set_server_experiment_id(experiment_id);
676  autofill_manager_->AddSeenForm(form_structure);
677
678  // Establish our expectations.
679  ::testing::InSequence dummy;
680  // Autofilled field
681  EXPECT_CALL(*autofill_manager_->metric_logger(),
682              Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id));
683  EXPECT_CALL(*autofill_manager_->metric_logger(),
684              Log(AutofillMetrics::HEURISTIC_TYPE_MATCH,
685                  UNKNOWN_TYPE, experiment_id));
686  EXPECT_CALL(*autofill_manager_->metric_logger(),
687              Log(AutofillMetrics::SERVER_TYPE_MISMATCH,
688                  UNKNOWN_TYPE, experiment_id));
689  EXPECT_CALL(*autofill_manager_->metric_logger(),
690              Log(AutofillMetrics::PREDICTED_TYPE_MISMATCH,
691                  UNKNOWN_TYPE, experiment_id));
692  EXPECT_CALL(*autofill_manager_->metric_logger(),
693              Log(AutofillMetrics::FIELD_AUTOFILLED, experiment_id));
694  // Non-autofilled field for which we had data
695  EXPECT_CALL(*autofill_manager_->metric_logger(),
696              Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id));
697  EXPECT_CALL(*autofill_manager_->metric_logger(),
698              Log(AutofillMetrics::HEURISTIC_TYPE_MISMATCH,
699                  EMAIL_ADDRESS, experiment_id));
700  EXPECT_CALL(*autofill_manager_->metric_logger(),
701              Log(AutofillMetrics::SERVER_TYPE_MATCH,
702                  EMAIL_ADDRESS, experiment_id));
703  EXPECT_CALL(*autofill_manager_->metric_logger(),
704              Log(AutofillMetrics::PREDICTED_TYPE_MATCH,
705                  EMAIL_ADDRESS, experiment_id));
706  EXPECT_CALL(*autofill_manager_->metric_logger(),
707              Log(AutofillMetrics::FIELD_NOT_AUTOFILLED, experiment_id));
708  EXPECT_CALL(*autofill_manager_->metric_logger(),
709              Log(AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
710                  experiment_id));
711  EXPECT_CALL(*autofill_manager_->metric_logger(),
712              Log(AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH,
713                  experiment_id));
714  // Empty field
715  EXPECT_CALL(*autofill_manager_->metric_logger(),
716              Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id));
717  // Unknown field
718  EXPECT_CALL(*autofill_manager_->metric_logger(),
719              Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id));
720  // <select> field
721  EXPECT_CALL(*autofill_manager_->metric_logger(),
722              Log(AutofillMetrics::FIELD_SUBMITTED, experiment_id));
723  EXPECT_CALL(*autofill_manager_->metric_logger(),
724              Log(AutofillMetrics::HEURISTIC_TYPE_UNKNOWN,
725                  ADDRESS_HOME_COUNTRY, experiment_id));
726  EXPECT_CALL(*autofill_manager_->metric_logger(),
727              Log(AutofillMetrics::SERVER_TYPE_UNKNOWN,
728                  ADDRESS_HOME_COUNTRY, experiment_id));
729  EXPECT_CALL(*autofill_manager_->metric_logger(),
730              Log(AutofillMetrics::PREDICTED_TYPE_UNKNOWN,
731                  ADDRESS_HOME_COUNTRY, experiment_id));
732
733  // Simulate form submission.
734  EXPECT_NO_FATAL_FAILURE(autofill_manager_->OnFormSubmitted(form));
735}
736
737// Test that the profile count is logged correctly.
738TEST_F(AutofillMetricsTest, StoredProfileCount) {
739  // The metric should be logged when the profiles are first loaded.
740  EXPECT_CALL(*test_personal_data_->metric_logger(),
741              LogStoredProfileCount(2)).Times(1);
742  test_personal_data_->LoadProfiles();
743
744  // The metric should only be logged once.
745  EXPECT_CALL(*test_personal_data_->metric_logger(),
746              LogStoredProfileCount(::testing::_)).Times(0);
747  test_personal_data_->LoadProfiles();
748}
749
750// Test that we correctly log whether Autofill is enabled.
751TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
752  test_personal_data_->set_autofill_enabled(true);
753  EXPECT_CALL(*test_personal_data_->metric_logger(),
754              LogIsAutofillEnabledAtStartup(true)).Times(1);
755  test_personal_data_->Init(NULL);
756
757  test_personal_data_->set_autofill_enabled(false);
758  EXPECT_CALL(*test_personal_data_->metric_logger(),
759              LogIsAutofillEnabledAtStartup(false)).Times(1);
760  test_personal_data_->Init(NULL);
761}
762
763// Test that we log the number of Autofill suggestions when filling a form.
764TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
765  // Set up our form data.
766  FormData form;
767  form.name = ASCIIToUTF16("TestForm");
768  form.method = ASCIIToUTF16("POST");
769  form.origin = GURL("http://example.com/form.html");
770  form.action = GURL("http://example.com/submit.html");
771  form.user_submitted = true;
772
773  FormField field;
774  std::vector<AutofillFieldType> field_types;
775  autofill_test::CreateTestFormField("Name", "name", "", "text", &field);
776  form.fields.push_back(field);
777  field_types.push_back(NAME_FULL);
778  autofill_test::CreateTestFormField("Email", "email", "", "email", &field);
779  form.fields.push_back(field);
780  field_types.push_back(EMAIL_ADDRESS);
781  autofill_test::CreateTestFormField("Phone", "phone", "", "tel", &field);
782  form.fields.push_back(field);
783  field_types.push_back(PHONE_HOME_NUMBER);
784
785  // Simulate having seen this form on page load.
786  // |form_structure| will be owned by |autofill_manager_|.
787  TestFormStructure* form_structure = new TestFormStructure(form);
788  form_structure->SetFieldTypes(field_types, field_types);
789  autofill_manager_->AddSeenForm(form_structure);
790
791  // Establish our expectations.
792  ::testing::FLAGS_gmock_verbose = "error";
793  ::testing::InSequence dummy;
794  EXPECT_CALL(*autofill_manager_->metric_logger(),
795              LogAddressSuggestionsCount(2)).Times(1);
796
797  // Simulate activating the autofill popup for the phone field.
798  autofill_manager_->OnQueryFormFieldAutofill(0, form, field);
799
800  // Simulate activating the autofill popup for the email field after typing.
801  // No new metric should be logged, since we're still on the same page.
802  autofill_test::CreateTestFormField("Email", "email", "b", "email", &field);
803  autofill_manager_->OnQueryFormFieldAutofill(0, form, field);
804
805  // Reset the autofill manager state.
806  autofill_manager_->Reset();
807  form_structure = new TestFormStructure(form);
808  form_structure->SetFieldTypes(field_types, field_types);
809  autofill_manager_->AddSeenForm(form_structure);
810
811  // Establish our expectations.
812  EXPECT_CALL(*autofill_manager_->metric_logger(),
813              LogAddressSuggestionsCount(1)).Times(1);
814
815  // Simulate activating the autofill popup for the email field after typing.
816  autofill_manager_->OnQueryFormFieldAutofill(0, form, field);
817
818  // Reset the autofill manager state again.
819  autofill_manager_->Reset();
820  form_structure = new TestFormStructure(form);
821  form_structure->SetFieldTypes(field_types, field_types);
822  autofill_manager_->AddSeenForm(form_structure);
823
824  // Establish our expectations.
825  EXPECT_CALL(*autofill_manager_->metric_logger(),
826              LogAddressSuggestionsCount(::testing::_)).Times(0);
827
828  // Simulate activating the autofill popup for the email field after typing.
829  form.fields[0].is_autofilled = true;
830  autofill_manager_->OnQueryFormFieldAutofill(0, form, field);
831}
832
833// Test that we log whether Autofill is enabled when filling a form.
834TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
835  // Establish our expectations.
836  ::testing::FLAGS_gmock_verbose = "error";
837  ::testing::InSequence dummy;
838  EXPECT_CALL(*autofill_manager_->metric_logger(),
839              LogIsAutofillEnabledAtPageLoad(true)).Times(1);
840
841  autofill_manager_->set_autofill_enabled(true);
842  autofill_manager_->OnFormsSeen(std::vector<FormData>());
843
844  // Reset the autofill manager state.
845  autofill_manager_->Reset();
846
847  // Establish our expectations.
848  EXPECT_CALL(*autofill_manager_->metric_logger(),
849              LogIsAutofillEnabledAtPageLoad(false)).Times(1);
850
851  autofill_manager_->set_autofill_enabled(false);
852  autofill_manager_->OnFormsSeen(std::vector<FormData>());
853}
854
855// Test that credit card infobar metrics are logged correctly.
856TEST_F(AutofillMetricsTest, CreditCardInfoBar) {
857  MockAutofillMetrics metric_logger;
858  CreditCard* credit_card;
859  AutofillCCInfoBarDelegate* infobar;
860  ::testing::InSequence dummy;
861
862  // Accept the infobar.
863  EXPECT_CALL(metric_logger, Log(AutofillMetrics::CREDIT_CARD_INFOBAR_SHOWN));
864  credit_card = new CreditCard();
865  infobar = new AutofillCCInfoBarDelegate(contents(),
866                                          credit_card,
867                                          test_personal_data_.get(),
868                                          &metric_logger);
869
870  EXPECT_CALL(*test_personal_data_.get(), SaveImportedCreditCard(*credit_card));
871  EXPECT_CALL(metric_logger,
872              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_ACCEPTED)).Times(1);
873  EXPECT_CALL(metric_logger,
874              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_IGNORED)).Times(0);
875  EXPECT_TRUE(infobar->Accept());
876  infobar->InfoBarClosed();
877
878  // Cancel the infobar.
879  EXPECT_CALL(metric_logger, Log(AutofillMetrics::CREDIT_CARD_INFOBAR_SHOWN));
880  credit_card = new CreditCard();
881  infobar = new AutofillCCInfoBarDelegate(contents(),
882                                          credit_card,
883                                          test_personal_data_.get(),
884                                          &metric_logger);
885
886  EXPECT_CALL(metric_logger,
887              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_DENIED)).Times(1);
888  EXPECT_CALL(metric_logger,
889              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_IGNORED)).Times(0);
890  EXPECT_TRUE(infobar->Cancel());
891  infobar->InfoBarClosed();
892
893  // Dismiss the infobar.
894  EXPECT_CALL(metric_logger, Log(AutofillMetrics::CREDIT_CARD_INFOBAR_SHOWN));
895  credit_card = new CreditCard();
896  infobar = new AutofillCCInfoBarDelegate(contents(),
897                                          credit_card,
898                                          test_personal_data_.get(),
899                                          &metric_logger);
900
901  EXPECT_CALL(metric_logger,
902              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_DENIED)).Times(1);
903  EXPECT_CALL(metric_logger,
904              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_IGNORED)).Times(0);
905  infobar->InfoBarDismissed();
906  infobar->InfoBarClosed();
907
908  // Ignore the infobar.
909  EXPECT_CALL(metric_logger, Log(AutofillMetrics::CREDIT_CARD_INFOBAR_SHOWN));
910  credit_card = new CreditCard();
911  infobar = new AutofillCCInfoBarDelegate(contents(),
912                                          credit_card,
913                                          test_personal_data_.get(),
914                                          &metric_logger);
915
916  EXPECT_CALL(metric_logger,
917              Log(AutofillMetrics::CREDIT_CARD_INFOBAR_IGNORED)).Times(1);
918  infobar->InfoBarClosed();
919}
920