autofill_metrics_unittest.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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/strings/string16.h"
12#include "base/strings/utf_string_conversions.h"
13#include "base/time/time.h"
14#include "chrome/browser/autofill/autofill_cc_infobar_delegate.h"
15#include "chrome/browser/autofill/personal_data_manager_factory.h"
16#include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
17#include "chrome/test/base/chrome_render_view_host_test_harness.h"
18#include "chrome/test/base/testing_profile.h"
19#include "components/autofill/content/browser/autocheckout_page_meta_data.h"
20#include "components/autofill/core/browser/autofill_common_test.h"
21#include "components/autofill/core/browser/autofill_external_delegate.h"
22#include "components/autofill/core/browser/autofill_manager.h"
23#include "components/autofill/core/browser/autofill_manager_delegate.h"
24#include "components/autofill/core/browser/personal_data_manager.h"
25#include "components/autofill/core/browser/test_autofill_driver.h"
26#include "components/autofill/core/common/form_data.h"
27#include "components/autofill/core/common/form_field_data.h"
28#include "components/autofill/core/common/forms_seen_state.h"
29#include "components/webdata/common/web_data_results.h"
30#include "content/public/test/test_utils.h"
31#include "testing/gmock/include/gmock/gmock.h"
32#include "testing/gtest/include/gtest/gtest.h"
33#include "ui/gfx/rect.h"
34#include "url/gurl.h"
35
36using base::TimeDelta;
37using base::TimeTicks;
38using testing::_;
39using testing::AnyNumber;
40using testing::Mock;
41
42namespace autofill {
43
44namespace {
45
46class MockAutofillMetrics : public AutofillMetrics {
47 public:
48  MockAutofillMetrics() {}
49  MOCK_CONST_METHOD1(LogCreditCardInfoBarMetric, void(InfoBarMetric metric));
50  MOCK_CONST_METHOD1(LogDeveloperEngagementMetric,
51                     void(DeveloperEngagementMetric metric));
52  MOCK_CONST_METHOD3(LogHeuristicTypePrediction,
53                     void(FieldTypeQualityMetric metric,
54                          AutofillFieldType field_type,
55                          const std::string& experiment_id));
56  MOCK_CONST_METHOD3(LogOverallTypePrediction,
57                     void(FieldTypeQualityMetric metric,
58                          AutofillFieldType field_type,
59                          const std::string& experiment_id));
60  MOCK_CONST_METHOD3(LogServerTypePrediction,
61                     void(FieldTypeQualityMetric metric,
62                          AutofillFieldType field_type,
63                          const std::string& experiment_id));
64  MOCK_CONST_METHOD2(LogQualityMetric, void(QualityMetric metric,
65                                            const std::string& experiment_id));
66  MOCK_CONST_METHOD1(LogServerQueryMetric, void(ServerQueryMetric metric));
67  MOCK_CONST_METHOD1(LogUserHappinessMetric, void(UserHappinessMetric metric));
68  MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithAutofill,
69                     void(const TimeDelta& duration));
70  MOCK_CONST_METHOD1(LogFormFillDurationFromLoadWithoutAutofill,
71                     void(const TimeDelta& duration));
72  MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithAutofill,
73                     void(const TimeDelta& duration));
74  MOCK_CONST_METHOD1(LogFormFillDurationFromInteractionWithoutAutofill,
75                     void(const TimeDelta& duration));
76  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtPageLoad, void(bool enabled));
77  MOCK_CONST_METHOD1(LogIsAutofillEnabledAtStartup, void(bool enabled));
78  MOCK_CONST_METHOD1(LogStoredProfileCount, void(size_t num_profiles));
79  MOCK_CONST_METHOD1(LogAddressSuggestionsCount, void(size_t num_suggestions));
80  MOCK_CONST_METHOD1(LogServerExperimentIdForQuery,
81                     void(const std::string& experiment_id));
82  MOCK_CONST_METHOD1(LogServerExperimentIdForUpload,
83                     void(const std::string& experiment_id));
84
85 private:
86  DISALLOW_COPY_AND_ASSIGN(MockAutofillMetrics);
87};
88
89class TestPersonalDataManager : public PersonalDataManager {
90 public:
91  TestPersonalDataManager()
92      : PersonalDataManager("en-US"),
93        autofill_enabled_(true) {
94    set_metric_logger(new testing::NiceMock<MockAutofillMetrics>());
95    CreateTestAutofillProfiles(&web_profiles_);
96  }
97
98  void SetBrowserContext(content::BrowserContext* context) {
99    set_browser_context(context);
100  }
101
102  // Overridden to avoid a trip to the database. This should be a no-op except
103  // for the side-effect of logging the profile count.
104  virtual void LoadProfiles() OVERRIDE {
105    std::vector<AutofillProfile*> profiles;
106    web_profiles_.release(&profiles);
107    WDResult<std::vector<AutofillProfile*> > result(AUTOFILL_PROFILES_RESULT,
108                                                    profiles);
109    ReceiveLoadedProfiles(0, &result);
110  }
111
112  // Overridden to avoid a trip to the database.
113  virtual void LoadCreditCards() OVERRIDE {}
114
115  const MockAutofillMetrics* metric_logger() const {
116    return static_cast<const MockAutofillMetrics*>(
117        PersonalDataManager::metric_logger());
118  }
119
120  void set_autofill_enabled(bool autofill_enabled) {
121    autofill_enabled_ = autofill_enabled;
122  }
123
124  virtual bool IsAutofillEnabled() const OVERRIDE {
125    return autofill_enabled_;
126  }
127
128  MOCK_METHOD1(SaveImportedCreditCard,
129               void(const CreditCard& imported_credit_card));
130
131 private:
132  void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
133    AutofillProfile* profile = new AutofillProfile;
134    test::SetProfileInfo(profile, "Elvis", "Aaron",
135                         "Presley", "theking@gmail.com", "RCA",
136                         "3734 Elvis Presley Blvd.", "Apt. 10",
137                         "Memphis", "Tennessee", "38116", "US",
138                         "12345678901");
139    profile->set_guid("00000000-0000-0000-0000-000000000001");
140    profiles->push_back(profile);
141    profile = new AutofillProfile;
142    test::SetProfileInfo(profile, "Charles", "Hardin",
143                         "Holley", "buddy@gmail.com", "Decca",
144                         "123 Apple St.", "unit 6", "Lubbock",
145                         "Texas", "79401", "US", "2345678901");
146    profile->set_guid("00000000-0000-0000-0000-000000000002");
147    profiles->push_back(profile);
148  }
149
150  bool autofill_enabled_;
151
152  DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager);
153};
154
155class TestFormStructure : public FormStructure {
156 public:
157  explicit TestFormStructure(const FormData& form)
158      : FormStructure(form, std::string()) {}
159  virtual ~TestFormStructure() {}
160
161  void SetFieldTypes(const std::vector<AutofillFieldType>& heuristic_types,
162                     const std::vector<AutofillFieldType>& server_types) {
163    ASSERT_EQ(field_count(), heuristic_types.size());
164    ASSERT_EQ(field_count(), server_types.size());
165
166    for (size_t i = 0; i < field_count(); ++i) {
167      AutofillField* form_field = field(i);
168      ASSERT_TRUE(form_field);
169      form_field->set_heuristic_type(heuristic_types[i]);
170      form_field->set_server_type(server_types[i]);
171    }
172
173    UpdateAutofillCount();
174  }
175
176  virtual std::string server_experiment_id() const OVERRIDE {
177    return server_experiment_id_;
178  }
179  void set_server_experiment_id(const std::string& server_experiment_id) {
180    server_experiment_id_ = server_experiment_id;
181  }
182
183 private:
184  std::string server_experiment_id_;
185  DISALLOW_COPY_AND_ASSIGN(TestFormStructure);
186};
187
188class TestAutofillManager : public AutofillManager {
189 public:
190  TestAutofillManager(AutofillDriver* driver,
191                      AutofillManagerDelegate* manager_delegate,
192                      TestPersonalDataManager* personal_manager)
193      : AutofillManager(driver, manager_delegate, personal_manager),
194        autofill_enabled_(true) {
195    set_metric_logger(new testing::NiceMock<MockAutofillMetrics>);
196  }
197  virtual ~TestAutofillManager() {}
198
199  virtual std::string GetAutocheckoutURLPrefix() const OVERRIDE {
200    return std::string();
201  }
202
203  virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
204
205  void set_autofill_enabled(bool autofill_enabled) {
206    autofill_enabled_ = autofill_enabled;
207  }
208
209  MockAutofillMetrics* metric_logger() {
210    return static_cast<MockAutofillMetrics*>(const_cast<AutofillMetrics*>(
211        AutofillManager::metric_logger()));
212  }
213
214  void AddSeenForm(const FormData& form,
215                   const std::vector<AutofillFieldType>& heuristic_types,
216                   const std::vector<AutofillFieldType>& server_types,
217                   const std::string& experiment_id) {
218    FormData empty_form = form;
219    for (size_t i = 0; i < empty_form.fields.size(); ++i) {
220      empty_form.fields[i].value = base::string16();
221    }
222
223    // |form_structure| will be owned by |form_structures()|.
224    TestFormStructure* form_structure = new TestFormStructure(empty_form);
225    form_structure->SetFieldTypes(heuristic_types, server_types);
226    form_structure->set_server_experiment_id(experiment_id);
227    form_structures()->push_back(form_structure);
228  }
229
230  void FormSubmitted(const FormData& form, const TimeTicks& timestamp) {
231    message_loop_runner_ = new content::MessageLoopRunner();
232    if (!OnFormSubmitted(form, timestamp))
233      return;
234
235    // Wait for the asynchronous FormSubmitted() call to complete.
236    message_loop_runner_->Run();
237  }
238
239  virtual 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 {
244    message_loop_runner_->Quit();
245
246    AutofillManager::UploadFormDataAsyncCallback(submitted_form,
247                                                 load_time,
248                                                 interaction_time,
249                                                 submission_time);
250  }
251
252 private:
253  bool autofill_enabled_;
254  scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
255
256  DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
257};
258
259}  // namespace
260
261class AutofillMetricsTest : public ChromeRenderViewHostTestHarness {
262 public:
263  virtual ~AutofillMetricsTest();
264
265  virtual void SetUp() OVERRIDE;
266  virtual void TearDown() OVERRIDE;
267
268 protected:
269  scoped_ptr<ConfirmInfoBarDelegate> CreateDelegate(
270      MockAutofillMetrics* metric_logger);
271
272  scoped_ptr<TestAutofillDriver> autofill_driver_;
273  scoped_ptr<TestAutofillManager> autofill_manager_;
274  scoped_ptr<TestPersonalDataManager> personal_data_;
275  scoped_ptr<AutofillExternalDelegate> external_delegate_;
276};
277
278AutofillMetricsTest::~AutofillMetricsTest() {
279  // Order of destruction is important as AutofillManager relies on
280  // PersonalDataManager to be around when it gets destroyed.
281  autofill_manager_.reset();
282}
283
284void AutofillMetricsTest::SetUp() {
285  ChromeRenderViewHostTestHarness::SetUp();
286
287  // Ensure Mac OS X does not pop up a modal dialog for the Address Book.
288  autofill::test::DisableSystemServices(profile());
289
290  profile()->CreateRequestContext();
291  PersonalDataManagerFactory::GetInstance()->SetTestingFactory(profile(), NULL);
292
293  TabAutofillManagerDelegate::CreateForWebContents(web_contents());
294
295  personal_data_.reset(new TestPersonalDataManager());
296  personal_data_->SetBrowserContext(profile());
297  autofill_driver_.reset(new TestAutofillDriver(web_contents()));
298  autofill_manager_.reset(new TestAutofillManager(
299      autofill_driver_.get(),
300      TabAutofillManagerDelegate::FromWebContents(web_contents()),
301      personal_data_.get()));
302
303  external_delegate_.reset(new AutofillExternalDelegate(
304      web_contents(),
305      autofill_manager_.get(),
306      autofill_driver_.get()));
307  autofill_manager_->SetExternalDelegate(external_delegate_.get());
308}
309
310void AutofillMetricsTest::TearDown() {
311  // Order of destruction is important as AutofillManager relies on
312  // PersonalDataManager to be around when it gets destroyed. Also, a real
313  // AutofillManager is tied to the lifetime of the WebContents, so it must
314  // be destroyed at the destruction of the WebContents.
315  autofill_manager_.reset();
316  autofill_driver_.reset();
317  personal_data_.reset();
318  profile()->ResetRequestContext();
319  ChromeRenderViewHostTestHarness::TearDown();
320}
321
322scoped_ptr<ConfirmInfoBarDelegate> AutofillMetricsTest::CreateDelegate(
323    MockAutofillMetrics* metric_logger) {
324  EXPECT_CALL(*metric_logger,
325              LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_SHOWN));
326
327  CreditCard credit_card;
328  return AutofillCCInfoBarDelegate::Create(
329      metric_logger,
330      base::Bind(&TestPersonalDataManager::SaveImportedCreditCard,
331                 base::Unretained(personal_data_.get()), credit_card));
332}
333
334// Test that we log quality metrics appropriately.
335TEST_F(AutofillMetricsTest, QualityMetrics) {
336  // Set up our form data.
337  FormData form;
338  form.name = ASCIIToUTF16("TestForm");
339  form.method = ASCIIToUTF16("POST");
340  form.origin = GURL("http://example.com/form.html");
341  form.action = GURL("http://example.com/submit.html");
342  form.user_submitted = true;
343
344  std::vector<AutofillFieldType> heuristic_types, server_types;
345  FormFieldData field;
346
347  test::CreateTestFormField(
348      "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
349  field.is_autofilled = true;
350  form.fields.push_back(field);
351  heuristic_types.push_back(NAME_FULL);
352  server_types.push_back(NAME_FIRST);
353
354  test::CreateTestFormField(
355      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
356  field.is_autofilled = false;
357  form.fields.push_back(field);
358  heuristic_types.push_back(PHONE_HOME_NUMBER);
359  server_types.push_back(EMAIL_ADDRESS);
360
361  test::CreateTestFormField("Empty", "empty", "", "text", &field);
362  field.is_autofilled = false;
363  form.fields.push_back(field);
364  heuristic_types.push_back(NAME_FULL);
365  server_types.push_back(NAME_FIRST);
366
367  test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
368  field.is_autofilled = false;
369  form.fields.push_back(field);
370  heuristic_types.push_back(PHONE_HOME_NUMBER);
371  server_types.push_back(EMAIL_ADDRESS);
372
373  test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
374  field.is_autofilled = false;
375  form.fields.push_back(field);
376  heuristic_types.push_back(UNKNOWN_TYPE);
377  server_types.push_back(NO_SERVER_DATA);
378
379  test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field);
380  field.is_autofilled = true;
381  form.fields.push_back(field);
382  heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
383  server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
384
385  // Simulate having seen this form on page load.
386  autofill_manager_->AddSeenForm(form, heuristic_types, server_types,
387                                 std::string());
388
389  // Establish our expectations.
390  ::testing::InSequence dummy;
391  EXPECT_CALL(*autofill_manager_->metric_logger(),
392              LogServerExperimentIdForUpload(std::string()));
393  // Autofilled field
394  EXPECT_CALL(*autofill_manager_->metric_logger(),
395              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
396                               std::string()));
397  EXPECT_CALL(*autofill_manager_->metric_logger(),
398              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
399                  NAME_FULL, std::string()));
400  EXPECT_CALL(*autofill_manager_->metric_logger(),
401              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
402                  NAME_FULL, std::string()));
403  EXPECT_CALL(*autofill_manager_->metric_logger(),
404              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
405                  NAME_FULL, std::string()));
406  EXPECT_CALL(*autofill_manager_->metric_logger(),
407              LogQualityMetric(AutofillMetrics::FIELD_AUTOFILLED,
408                               std::string()));
409  // Non-autofilled field for which we had data
410  EXPECT_CALL(*autofill_manager_->metric_logger(),
411              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
412                               std::string()));
413  EXPECT_CALL(*autofill_manager_->metric_logger(),
414              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
415                  EMAIL_ADDRESS, std::string()));
416  EXPECT_CALL(*autofill_manager_->metric_logger(),
417              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
418                  EMAIL_ADDRESS, std::string()));
419  EXPECT_CALL(*autofill_manager_->metric_logger(),
420              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
421                  EMAIL_ADDRESS, std::string()));
422  EXPECT_CALL(*autofill_manager_->metric_logger(),
423              LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
424                               std::string()));
425  EXPECT_CALL(*autofill_manager_->metric_logger(),
426              LogQualityMetric(
427                  AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
428                  std::string()));
429  EXPECT_CALL(*autofill_manager_->metric_logger(),
430              LogQualityMetric(
431                  AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH,
432                  std::string()));
433  // Empty field
434  EXPECT_CALL(*autofill_manager_->metric_logger(),
435              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
436                               std::string()));
437  // Unknown field
438  EXPECT_CALL(*autofill_manager_->metric_logger(),
439              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
440                               std::string()));
441  // <select> field
442  EXPECT_CALL(*autofill_manager_->metric_logger(),
443              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
444                               std::string()));
445  EXPECT_CALL(*autofill_manager_->metric_logger(),
446              LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
447                  ADDRESS_HOME_COUNTRY, std::string()));
448  EXPECT_CALL(*autofill_manager_->metric_logger(),
449              LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
450                  ADDRESS_HOME_COUNTRY, std::string()));
451  EXPECT_CALL(*autofill_manager_->metric_logger(),
452              LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
453                  ADDRESS_HOME_COUNTRY, std::string()));
454  // Phone field
455  EXPECT_CALL(*autofill_manager_->metric_logger(),
456              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
457                               std::string()));
458  EXPECT_CALL(*autofill_manager_->metric_logger(),
459              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
460                  PHONE_HOME_WHOLE_NUMBER, std::string()));
461  EXPECT_CALL(*autofill_manager_->metric_logger(),
462              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
463                  PHONE_HOME_WHOLE_NUMBER, std::string()));
464  EXPECT_CALL(*autofill_manager_->metric_logger(),
465              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
466                  PHONE_HOME_WHOLE_NUMBER, std::string()));
467  EXPECT_CALL(*autofill_manager_->metric_logger(),
468              LogQualityMetric(AutofillMetrics::FIELD_AUTOFILLED,
469                               std::string()));
470  EXPECT_CALL(*autofill_manager_->metric_logger(),
471              LogUserHappinessMetric(
472                  AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
473
474  // Simulate form submission.
475  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
476                                                           TimeTicks::Now()));
477}
478
479// Test that we log the appropriate additional metrics when Autofill failed.
480TEST_F(AutofillMetricsTest, QualityMetricsForFailure) {
481  // Set up our form data.
482  FormData form;
483  form.name = ASCIIToUTF16("TestForm");
484  form.method = ASCIIToUTF16("POST");
485  form.origin = GURL("http://example.com/form.html");
486  form.action = GURL("http://example.com/submit.html");
487  form.user_submitted = true;
488
489  struct {
490    const char* label;
491    const char* name;
492    const char* value;
493    AutofillFieldType heuristic_type;
494    AutofillFieldType server_type;
495    AutofillMetrics::QualityMetric heuristic_metric;
496    AutofillMetrics::QualityMetric server_metric;
497  } failure_cases[] = {
498    {
499      "Heuristics unknown, server unknown", "0,0", "Elvis",
500      UNKNOWN_TYPE, NO_SERVER_DATA,
501      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
502      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
503    },
504    {
505      "Heuristics match, server unknown", "1,0", "Aaron",
506      NAME_MIDDLE, NO_SERVER_DATA,
507      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
508      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
509    },
510    {
511      "Heuristics mismatch, server unknown", "2,0", "Presley",
512      PHONE_HOME_NUMBER, NO_SERVER_DATA,
513      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
514      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN
515    },
516    {
517      "Heuristics unknown, server match", "0,1", "theking@gmail.com",
518      UNKNOWN_TYPE, EMAIL_ADDRESS,
519      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
520      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
521    },
522    {
523      "Heuristics match, server match", "1,1", "3734 Elvis Presley Blvd.",
524      ADDRESS_HOME_LINE1, ADDRESS_HOME_LINE1,
525      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
526      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
527    },
528    {
529      "Heuristics mismatch, server match", "2,1", "Apt. 10",
530      PHONE_HOME_NUMBER, ADDRESS_HOME_LINE2,
531      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
532      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH
533    },
534    {
535      "Heuristics unknown, server mismatch", "0,2", "Memphis",
536      UNKNOWN_TYPE, PHONE_HOME_NUMBER,
537      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
538      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
539    },
540    {
541      "Heuristics match, server mismatch", "1,2", "Tennessee",
542      ADDRESS_HOME_STATE, PHONE_HOME_NUMBER,
543      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
544      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
545    },
546    {
547      "Heuristics mismatch, server mismatch", "2,2", "38116",
548      PHONE_HOME_NUMBER, PHONE_HOME_NUMBER,
549      AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
550      AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH
551    }
552  };
553
554  std::vector<AutofillFieldType> heuristic_types, server_types;
555  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) {
556    FormFieldData field;
557    test::CreateTestFormField(failure_cases[i].label,
558                              failure_cases[i].name,
559                              failure_cases[i].value, "text", &field);
560    form.fields.push_back(field);
561    heuristic_types.push_back(failure_cases[i].heuristic_type);
562    server_types.push_back(failure_cases[i].server_type);
563
564  }
565
566  // Simulate having seen this form with the desired heuristic and server types.
567  // |form_structure| will be owned by |autofill_manager_|.
568  autofill_manager_->AddSeenForm(form, heuristic_types, server_types,
569                                 std::string());
570
571
572  // Establish our expectations.
573  ::testing::InSequence dummy;
574  EXPECT_CALL(*autofill_manager_->metric_logger(),
575              LogServerExperimentIdForUpload(std::string()));
576  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(failure_cases); ++i) {
577    EXPECT_CALL(*autofill_manager_->metric_logger(),
578                LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
579                                 std::string()));
580    EXPECT_CALL(*autofill_manager_->metric_logger(),
581                LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
582                                 std::string()));
583    EXPECT_CALL(*autofill_manager_->metric_logger(),
584                LogQualityMetric(failure_cases[i].heuristic_metric,
585                                 std::string()));
586    EXPECT_CALL(*autofill_manager_->metric_logger(),
587                LogQualityMetric(failure_cases[i].server_metric,
588                                 std::string()));
589  }
590
591  // Simulate form submission.
592  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
593                                                           TimeTicks::Now()));
594}
595
596// Test that we behave sanely when the cached form differs from the submitted
597// one.
598TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
599  // Set up our form data.
600  FormData form;
601  form.name = ASCIIToUTF16("TestForm");
602  form.method = ASCIIToUTF16("POST");
603  form.origin = GURL("http://example.com/form.html");
604  form.action = GURL("http://example.com/submit.html");
605  form.user_submitted = true;
606
607  std::vector<AutofillFieldType> heuristic_types, server_types;
608
609  FormFieldData field;
610  test::CreateTestFormField(
611      "Both match", "match", "Elvis Aaron Presley", "text", &field);
612  field.is_autofilled = true;
613  form.fields.push_back(field);
614  heuristic_types.push_back(NAME_FULL);
615  server_types.push_back(NAME_FULL);
616  test::CreateTestFormField(
617      "Both mismatch", "mismatch", "buddy@gmail.com", "text", &field);
618  field.is_autofilled = false;
619  form.fields.push_back(field);
620  heuristic_types.push_back(PHONE_HOME_NUMBER);
621  server_types.push_back(PHONE_HOME_NUMBER);
622  test::CreateTestFormField(
623      "Only heuristics match", "mixed", "Memphis", "text", &field);
624  field.is_autofilled = false;
625  form.fields.push_back(field);
626  heuristic_types.push_back(ADDRESS_HOME_CITY);
627  server_types.push_back(PHONE_HOME_NUMBER);
628  test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
629  field.is_autofilled = false;
630  form.fields.push_back(field);
631  heuristic_types.push_back(UNKNOWN_TYPE);
632  server_types.push_back(UNKNOWN_TYPE);
633
634  // Simulate having seen this form with the desired heuristic and server types.
635  // |form_structure| will be owned by |autofill_manager_|.
636  autofill_manager_->AddSeenForm(form, heuristic_types, server_types,
637                                 std::string());
638
639
640  // Add a field and re-arrange the remaining form fields before submitting.
641  std::vector<FormFieldData> cached_fields = form.fields;
642  form.fields.clear();
643  test::CreateTestFormField(
644      "New field", "new field", "Tennessee", "text", &field);
645  form.fields.push_back(field);
646  form.fields.push_back(cached_fields[2]);
647  form.fields.push_back(cached_fields[1]);
648  form.fields.push_back(cached_fields[3]);
649  form.fields.push_back(cached_fields[0]);
650
651  // Establish our expectations.
652  ::testing::InSequence dummy;
653  // New field
654  EXPECT_CALL(*autofill_manager_->metric_logger(),
655              LogServerExperimentIdForUpload(std::string()));
656  EXPECT_CALL(*autofill_manager_->metric_logger(),
657              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
658                               std::string()));
659  EXPECT_CALL(*autofill_manager_->metric_logger(),
660              LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
661                  ADDRESS_HOME_STATE, std::string()));
662  EXPECT_CALL(*autofill_manager_->metric_logger(),
663              LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
664                  ADDRESS_HOME_STATE, std::string()));
665  EXPECT_CALL(*autofill_manager_->metric_logger(),
666              LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
667                  ADDRESS_HOME_STATE, std::string()));
668  EXPECT_CALL(*autofill_manager_->metric_logger(),
669              LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
670                               std::string()));
671  EXPECT_CALL(*autofill_manager_->metric_logger(),
672              LogQualityMetric(
673                  AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_UNKNOWN,
674                  std::string()));
675  EXPECT_CALL(*autofill_manager_->metric_logger(),
676              LogQualityMetric(
677                  AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_UNKNOWN,
678                  std::string()));
679  // Only heuristics match
680  EXPECT_CALL(*autofill_manager_->metric_logger(),
681              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
682                               std::string()));
683  EXPECT_CALL(*autofill_manager_->metric_logger(),
684              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
685                  ADDRESS_HOME_CITY, std::string()));
686  EXPECT_CALL(*autofill_manager_->metric_logger(),
687              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
688                  ADDRESS_HOME_CITY, std::string()));
689  EXPECT_CALL(*autofill_manager_->metric_logger(),
690              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
691                  ADDRESS_HOME_CITY, std::string()));
692  EXPECT_CALL(*autofill_manager_->metric_logger(),
693              LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
694                               std::string()));
695  EXPECT_CALL(*autofill_manager_->metric_logger(),
696              LogQualityMetric(
697                  AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MATCH,
698                  std::string()));
699  EXPECT_CALL(*autofill_manager_->metric_logger(),
700              LogQualityMetric(
701                  AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH,
702                  std::string()));
703  // Both mismatch
704  EXPECT_CALL(*autofill_manager_->metric_logger(),
705              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
706                               std::string()));
707  EXPECT_CALL(*autofill_manager_->metric_logger(),
708              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
709                  EMAIL_ADDRESS, std::string()));
710  EXPECT_CALL(*autofill_manager_->metric_logger(),
711              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
712                  EMAIL_ADDRESS, std::string()));
713  EXPECT_CALL(*autofill_manager_->metric_logger(),
714              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
715                  EMAIL_ADDRESS, std::string()));
716  EXPECT_CALL(*autofill_manager_->metric_logger(),
717              LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
718                               std::string()));
719  EXPECT_CALL(*autofill_manager_->metric_logger(),
720              LogQualityMetric(
721                  AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
722                  std::string()));
723  EXPECT_CALL(*autofill_manager_->metric_logger(),
724              LogQualityMetric(
725                  AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MISMATCH,
726                  std::string()));
727  // Unknown
728  EXPECT_CALL(*autofill_manager_->metric_logger(),
729              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
730                               std::string()));
731  // Both match
732  EXPECT_CALL(*autofill_manager_->metric_logger(),
733              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
734                               std::string()));
735  EXPECT_CALL(*autofill_manager_->metric_logger(),
736              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
737                  NAME_FULL, std::string()));
738  EXPECT_CALL(*autofill_manager_->metric_logger(),
739              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
740                  NAME_FULL, std::string()));
741  EXPECT_CALL(*autofill_manager_->metric_logger(),
742              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
743                  NAME_FULL, std::string()));
744  EXPECT_CALL(*autofill_manager_->metric_logger(),
745              LogQualityMetric(AutofillMetrics::FIELD_AUTOFILLED,
746                               std::string()));
747
748  // Simulate form submission.
749  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
750                                                           TimeTicks::Now()));
751}
752
753// Verify that we correctly log metrics regarding developer engagement.
754TEST_F(AutofillMetricsTest, DeveloperEngagement) {
755  // Start with a non-fillable form.
756  FormData form;
757  form.name = ASCIIToUTF16("TestForm");
758  form.method = ASCIIToUTF16("POST");
759  form.origin = GURL("http://example.com/form.html");
760  form.action = GURL("http://example.com/submit.html");
761
762  FormFieldData field;
763  test::CreateTestFormField("Name", "name", "", "text", &field);
764  form.fields.push_back(field);
765  test::CreateTestFormField("Email", "email", "", "text", &field);
766  form.fields.push_back(field);
767
768  std::vector<FormData> forms(1, form);
769
770  // Ensure no metrics are logged when loading a non-fillable form.
771  {
772    EXPECT_CALL(*autofill_manager_->metric_logger(),
773                LogDeveloperEngagementMetric(_)).Times(0);
774    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
775                                   autofill::NO_SPECIAL_FORMS_SEEN);
776    autofill_manager_->Reset();
777    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
778  }
779
780  // Add another field to the form, so that it becomes fillable.
781  test::CreateTestFormField("Phone", "phone", "", "text", &field);
782  forms.back().fields.push_back(field);
783
784  // Expect only the "form parsed" metric to be logged; no metrics about
785  // author-specified field type hints.
786  {
787    EXPECT_CALL(
788        *autofill_manager_->metric_logger(),
789        LogDeveloperEngagementMetric(
790            AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
791    EXPECT_CALL(
792        *autofill_manager_->metric_logger(),
793        LogDeveloperEngagementMetric(
794            AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(0);
795    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
796                                   autofill::NO_SPECIAL_FORMS_SEEN);
797    autofill_manager_->Reset();
798    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
799  }
800
801  // Add some fields with an author-specified field type to the form.
802  // We need to add at least three fields, because a form must have at least
803  // three fillable fields to be considered to be autofillable; and if at least
804  // one field specifies an explicit type hint, we don't apply any of our usual
805  // local heuristics to detect field types in the rest of the form.
806  test::CreateTestFormField("", "", "", "text", &field);
807  field.autocomplete_attribute = "given-name";
808  forms.back().fields.push_back(field);
809  test::CreateTestFormField("", "", "", "text", &field);
810  field.autocomplete_attribute = "email";
811  forms.back().fields.push_back(field);
812  test::CreateTestFormField("", "", "", "text", &field);
813  field.autocomplete_attribute = "street-address";
814  forms.back().fields.push_back(field);
815
816  // Expect both the "form parsed" metric and the author-specified field type
817  // hints metric to be logged.
818  {
819    EXPECT_CALL(
820        *autofill_manager_->metric_logger(),
821        LogDeveloperEngagementMetric(
822            AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
823    EXPECT_CALL(
824        *autofill_manager_->metric_logger(),
825        LogDeveloperEngagementMetric(
826            AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(1);
827    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
828                                   autofill::NO_SPECIAL_FORMS_SEEN);
829    autofill_manager_->Reset();
830    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
831  }
832}
833
834// Test that we don't log quality metrics for non-autofillable forms.
835TEST_F(AutofillMetricsTest, NoQualityMetricsForNonAutofillableForms) {
836  // Forms must include at least three fields to be auto-fillable.
837  FormData form;
838  form.name = ASCIIToUTF16("TestForm");
839  form.method = ASCIIToUTF16("POST");
840  form.origin = GURL("http://example.com/form.html");
841  form.action = GURL("http://example.com/submit.html");
842  form.user_submitted = true;
843
844  FormFieldData field;
845  test::CreateTestFormField(
846      "Autofilled", "autofilled", "Elvis Presley", "text", &field);
847  field.is_autofilled = true;
848  form.fields.push_back(field);
849  test::CreateTestFormField(
850      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
851  form.fields.push_back(field);
852
853  // Simulate form submission.
854  EXPECT_CALL(*autofill_manager_->metric_logger(),
855              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
856                               std::string())).Times(0);
857  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
858                                                           TimeTicks::Now()));
859
860  // Search forms are not auto-fillable.
861  form.action = GURL("http://example.com/search?q=Elvis%20Presley");
862  test::CreateTestFormField("Empty", "empty", "", "text", &field);
863  form.fields.push_back(field);
864
865  // Simulate form submission.
866  EXPECT_CALL(*autofill_manager_->metric_logger(),
867              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
868                               std::string())).Times(0);
869  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
870                                                           TimeTicks::Now()));
871}
872
873// Test that we recored the experiment id appropriately.
874TEST_F(AutofillMetricsTest, QualityMetricsWithExperimentId) {
875  // Set up our form data.
876  FormData form;
877  form.name = ASCIIToUTF16("TestForm");
878  form.method = ASCIIToUTF16("POST");
879  form.origin = GURL("http://example.com/form.html");
880  form.action = GURL("http://example.com/submit.html");
881  form.user_submitted = true;
882
883  std::vector<AutofillFieldType> heuristic_types, server_types;
884  FormFieldData field;
885
886  test::CreateTestFormField(
887      "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
888  field.is_autofilled = true;
889  form.fields.push_back(field);
890  heuristic_types.push_back(NAME_FULL);
891  server_types.push_back(NAME_FIRST);
892
893  test::CreateTestFormField(
894      "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field);
895  field.is_autofilled = false;
896  form.fields.push_back(field);
897  heuristic_types.push_back(PHONE_HOME_NUMBER);
898  server_types.push_back(EMAIL_ADDRESS);
899
900  test::CreateTestFormField("Empty", "empty", "", "text", &field);
901  field.is_autofilled = false;
902  form.fields.push_back(field);
903  heuristic_types.push_back(NAME_FULL);
904  server_types.push_back(NAME_FIRST);
905
906  test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
907  field.is_autofilled = false;
908  form.fields.push_back(field);
909  heuristic_types.push_back(PHONE_HOME_NUMBER);
910  server_types.push_back(EMAIL_ADDRESS);
911
912  test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
913  field.is_autofilled = false;
914  form.fields.push_back(field);
915  heuristic_types.push_back(UNKNOWN_TYPE);
916  server_types.push_back(NO_SERVER_DATA);
917
918  const std::string experiment_id = "ThatOughtaDoIt";
919
920  // Simulate having seen this form on page load.
921  // |form_structure| will be owned by |autofill_manager_|.
922  autofill_manager_->AddSeenForm(form, heuristic_types, server_types,
923                                 experiment_id);
924
925  // Establish our expectations.
926  ::testing::InSequence dummy;
927  EXPECT_CALL(*autofill_manager_->metric_logger(),
928              LogServerExperimentIdForUpload(experiment_id));
929  // Autofilled field
930  EXPECT_CALL(*autofill_manager_->metric_logger(),
931              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
932                               experiment_id));
933  EXPECT_CALL(*autofill_manager_->metric_logger(),
934              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
935                                         NAME_FULL, experiment_id));
936  EXPECT_CALL(*autofill_manager_->metric_logger(),
937              LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
938                                      NAME_FULL, experiment_id));
939  EXPECT_CALL(*autofill_manager_->metric_logger(),
940              LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
941                                       NAME_FULL, experiment_id));
942  EXPECT_CALL(*autofill_manager_->metric_logger(),
943              LogQualityMetric(AutofillMetrics::FIELD_AUTOFILLED,
944                               experiment_id));
945  // Non-autofilled field for which we had data
946  EXPECT_CALL(*autofill_manager_->metric_logger(),
947              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
948                               experiment_id));
949  EXPECT_CALL(*autofill_manager_->metric_logger(),
950              LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
951                                         EMAIL_ADDRESS, experiment_id));
952  EXPECT_CALL(*autofill_manager_->metric_logger(),
953              LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
954                                      EMAIL_ADDRESS, experiment_id));
955  EXPECT_CALL(*autofill_manager_->metric_logger(),
956              LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
957                                       EMAIL_ADDRESS, experiment_id));
958  EXPECT_CALL(*autofill_manager_->metric_logger(),
959              LogQualityMetric(AutofillMetrics::FIELD_NOT_AUTOFILLED,
960                               experiment_id));
961  EXPECT_CALL(*autofill_manager_->metric_logger(),
962              LogQualityMetric(
963                  AutofillMetrics::NOT_AUTOFILLED_HEURISTIC_TYPE_MISMATCH,
964                  experiment_id));
965  EXPECT_CALL(*autofill_manager_->metric_logger(),
966              LogQualityMetric(
967                  AutofillMetrics::NOT_AUTOFILLED_SERVER_TYPE_MATCH,
968                  experiment_id));
969  // Empty field
970  EXPECT_CALL(*autofill_manager_->metric_logger(),
971              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
972                               experiment_id));
973  // Unknown field
974  EXPECT_CALL(*autofill_manager_->metric_logger(),
975              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
976                               experiment_id));
977  // <select> field
978  EXPECT_CALL(*autofill_manager_->metric_logger(),
979              LogQualityMetric(AutofillMetrics::FIELD_SUBMITTED,
980                               experiment_id));
981  EXPECT_CALL(*autofill_manager_->metric_logger(),
982              LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
983                                         ADDRESS_HOME_COUNTRY, experiment_id));
984  EXPECT_CALL(*autofill_manager_->metric_logger(),
985              LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
986                                      ADDRESS_HOME_COUNTRY, experiment_id));
987  EXPECT_CALL(*autofill_manager_->metric_logger(),
988              LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
989                                       ADDRESS_HOME_COUNTRY, experiment_id));
990
991  // Simulate form submission.
992  EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
993                                                           TimeTicks::Now()));
994}
995
996// Test that the profile count is logged correctly.
997TEST_F(AutofillMetricsTest, StoredProfileCount) {
998  // The metric should be logged when the profiles are first loaded.
999  EXPECT_CALL(*personal_data_->metric_logger(),
1000              LogStoredProfileCount(2)).Times(1);
1001  personal_data_->LoadProfiles();
1002
1003  // The metric should only be logged once.
1004  EXPECT_CALL(*personal_data_->metric_logger(),
1005              LogStoredProfileCount(::testing::_)).Times(0);
1006  personal_data_->LoadProfiles();
1007}
1008
1009// Test that we correctly log when Autofill is enabled.
1010TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
1011  personal_data_->set_autofill_enabled(true);
1012  EXPECT_CALL(*personal_data_->metric_logger(),
1013              LogIsAutofillEnabledAtStartup(true)).Times(1);
1014  personal_data_->Init(profile());
1015}
1016
1017// Test that we correctly log when Autofill is disabled.
1018TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
1019  personal_data_->set_autofill_enabled(false);
1020  EXPECT_CALL(*personal_data_->metric_logger(),
1021              LogIsAutofillEnabledAtStartup(false)).Times(1);
1022  personal_data_->Init(profile());
1023}
1024
1025// Test that we log the number of Autofill suggestions when filling a form.
1026TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
1027  // Set up our form data.
1028  FormData form;
1029  form.name = ASCIIToUTF16("TestForm");
1030  form.method = ASCIIToUTF16("POST");
1031  form.origin = GURL("http://example.com/form.html");
1032  form.action = GURL("http://example.com/submit.html");
1033  form.user_submitted = true;
1034
1035  FormFieldData field;
1036  std::vector<AutofillFieldType> field_types;
1037  test::CreateTestFormField("Name", "name", "", "text", &field);
1038  form.fields.push_back(field);
1039  field_types.push_back(NAME_FULL);
1040  test::CreateTestFormField("Email", "email", "", "email", &field);
1041  form.fields.push_back(field);
1042  field_types.push_back(EMAIL_ADDRESS);
1043  test::CreateTestFormField("Phone", "phone", "", "tel", &field);
1044  form.fields.push_back(field);
1045  field_types.push_back(PHONE_HOME_NUMBER);
1046
1047  // Simulate having seen this form on page load.
1048  // |form_structure| will be owned by |autofill_manager_|.
1049  autofill_manager_->AddSeenForm(form, field_types, field_types,
1050                                 std::string());
1051
1052  // Establish our expectations.
1053  ::testing::InSequence dummy;
1054  EXPECT_CALL(*autofill_manager_->metric_logger(),
1055              LogAddressSuggestionsCount(2)).Times(1);
1056
1057  // Simulate activating the autofill popup for the phone field.
1058  autofill_manager_->OnQueryFormFieldAutofill(
1059      0, form, field, gfx::Rect(), false);
1060
1061  // Simulate activating the autofill popup for the email field after typing.
1062  // No new metric should be logged, since we're still on the same page.
1063  test::CreateTestFormField("Email", "email", "b", "email", &field);
1064  autofill_manager_->OnQueryFormFieldAutofill(
1065      0, form, field, gfx::Rect(), false);
1066
1067  // Reset the autofill manager state.
1068  autofill_manager_->Reset();
1069  autofill_manager_->AddSeenForm(form, field_types, field_types,
1070                                 std::string());
1071
1072  // Establish our expectations.
1073  EXPECT_CALL(*autofill_manager_->metric_logger(),
1074              LogAddressSuggestionsCount(1)).Times(1);
1075
1076  // Simulate activating the autofill popup for the email field after typing.
1077  autofill_manager_->OnQueryFormFieldAutofill(
1078      0, form, field, gfx::Rect(), false);
1079
1080  // Reset the autofill manager state again.
1081  autofill_manager_->Reset();
1082  autofill_manager_->AddSeenForm(form, field_types, field_types,
1083                                 std::string());
1084
1085  // Establish our expectations.
1086  EXPECT_CALL(*autofill_manager_->metric_logger(),
1087              LogAddressSuggestionsCount(::testing::_)).Times(0);
1088
1089  // Simulate activating the autofill popup for the email field after typing.
1090  form.fields[0].is_autofilled = true;
1091  autofill_manager_->OnQueryFormFieldAutofill(
1092      0, form, field, gfx::Rect(), false);
1093}
1094
1095// Test that we log whether Autofill is enabled when filling a form.
1096TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
1097  // Establish our expectations.
1098  ::testing::InSequence dummy;
1099  EXPECT_CALL(*autofill_manager_->metric_logger(),
1100              LogIsAutofillEnabledAtPageLoad(true)).Times(1);
1101
1102  autofill_manager_->set_autofill_enabled(true);
1103  autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks(),
1104                                 autofill::NO_SPECIAL_FORMS_SEEN);
1105
1106  // Reset the autofill manager state.
1107  autofill_manager_->Reset();
1108
1109  // Establish our expectations.
1110  EXPECT_CALL(*autofill_manager_->metric_logger(),
1111              LogIsAutofillEnabledAtPageLoad(false)).Times(1);
1112
1113  autofill_manager_->set_autofill_enabled(false);
1114  autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks(),
1115                                 autofill::NO_SPECIAL_FORMS_SEEN);
1116}
1117
1118// Test that credit card infobar metrics are logged correctly.
1119TEST_F(AutofillMetricsTest, CreditCardInfoBar) {
1120  testing::NiceMock<MockAutofillMetrics> metric_logger;
1121  ::testing::InSequence dummy;
1122
1123  // Accept the infobar.
1124  {
1125    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
1126    ASSERT_TRUE(infobar);
1127    EXPECT_CALL(*personal_data_, SaveImportedCreditCard(_));
1128    EXPECT_CALL(metric_logger,
1129        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_ACCEPTED)).Times(1);
1130    EXPECT_CALL(metric_logger,
1131        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0);
1132    EXPECT_TRUE(infobar->Accept());
1133  }
1134
1135  // Cancel the infobar.
1136  {
1137    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
1138    ASSERT_TRUE(infobar);
1139    EXPECT_CALL(metric_logger,
1140        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)).Times(1);
1141    EXPECT_CALL(metric_logger,
1142        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0);
1143    EXPECT_TRUE(infobar->Cancel());
1144  }
1145
1146  // Dismiss the infobar.
1147  {
1148    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
1149    ASSERT_TRUE(infobar);
1150    EXPECT_CALL(metric_logger,
1151        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_DENIED)).Times(1);
1152    EXPECT_CALL(metric_logger,
1153        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(0);
1154    infobar->InfoBarDismissed();
1155  }
1156
1157  // Ignore the infobar.
1158  {
1159    scoped_ptr<ConfirmInfoBarDelegate> infobar(CreateDelegate(&metric_logger));
1160    ASSERT_TRUE(infobar);
1161    EXPECT_CALL(metric_logger,
1162        LogCreditCardInfoBarMetric(AutofillMetrics::INFOBAR_IGNORED)).Times(1);
1163  }
1164}
1165
1166// Test that server query response experiment id metrics are logged correctly.
1167TEST_F(AutofillMetricsTest, ServerQueryExperimentIdForQuery) {
1168  testing::NiceMock<MockAutofillMetrics> metric_logger;
1169  ::testing::InSequence dummy;
1170
1171  // No experiment specified.
1172  EXPECT_CALL(metric_logger,
1173              LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_RECEIVED));
1174  EXPECT_CALL(metric_logger,
1175              LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_PARSED));
1176  EXPECT_CALL(metric_logger,
1177              LogServerExperimentIdForQuery(std::string()));
1178  EXPECT_CALL(metric_logger,
1179              LogServerQueryMetric(
1180                  AutofillMetrics::QUERY_RESPONSE_MATCHED_LOCAL_HEURISTICS));
1181  AutocheckoutPageMetaData page_meta_data;
1182  FormStructure::ParseQueryResponse(
1183      "<autofillqueryresponse></autofillqueryresponse>",
1184      std::vector<FormStructure*>(),
1185      &page_meta_data,
1186      metric_logger);
1187
1188  // Experiment "ar1" specified.
1189  EXPECT_CALL(metric_logger,
1190              LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_RECEIVED));
1191  EXPECT_CALL(metric_logger,
1192              LogServerQueryMetric(AutofillMetrics::QUERY_RESPONSE_PARSED));
1193  EXPECT_CALL(metric_logger,
1194              LogServerExperimentIdForQuery("ar1"));
1195  EXPECT_CALL(metric_logger,
1196              LogServerQueryMetric(
1197                  AutofillMetrics::QUERY_RESPONSE_MATCHED_LOCAL_HEURISTICS));
1198  FormStructure::ParseQueryResponse(
1199      "<autofillqueryresponse experimentid=\"ar1\"></autofillqueryresponse>",
1200      std::vector<FormStructure*>(),
1201      &page_meta_data,
1202      metric_logger);
1203}
1204
1205// Verify that we correctly log user happiness metrics dealing with form loading
1206// and form submission.
1207TEST_F(AutofillMetricsTest, UserHappinessFormLoadAndSubmission) {
1208  // Start with a form with insufficiently many fields.
1209  FormData form;
1210  form.name = ASCIIToUTF16("TestForm");
1211  form.method = ASCIIToUTF16("POST");
1212  form.origin = GURL("http://example.com/form.html");
1213  form.action = GURL("http://example.com/submit.html");
1214  form.user_submitted = true;
1215
1216  FormFieldData field;
1217  test::CreateTestFormField("Name", "name", "", "text", &field);
1218  form.fields.push_back(field);
1219  test::CreateTestFormField("Email", "email", "", "text", &field);
1220  form.fields.push_back(field);
1221
1222  std::vector<FormData> forms(1, form);
1223
1224  // Expect no notifications when the form is first seen.
1225  {
1226    EXPECT_CALL(*autofill_manager_->metric_logger(),
1227                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED)).Times(0);
1228    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
1229                                   autofill::NO_SPECIAL_FORMS_SEEN);
1230  }
1231
1232
1233  // Expect no notifications when the form is submitted.
1234  {
1235    EXPECT_CALL(
1236        *autofill_manager_->metric_logger(),
1237        LogUserHappinessMetric(
1238            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL)).Times(0);
1239    EXPECT_CALL(
1240        *autofill_manager_->metric_logger(),
1241        LogUserHappinessMetric(
1242            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME)).Times(0);
1243    EXPECT_CALL(
1244        *autofill_manager_->metric_logger(),
1245        LogUserHappinessMetric(
1246            AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE)).Times(0);
1247    EXPECT_CALL(
1248        *autofill_manager_->metric_logger(),
1249        LogUserHappinessMetric(
1250            AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM)).Times(0);
1251    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1252  }
1253
1254  // Add more fields to the form.
1255  test::CreateTestFormField("Phone", "phone", "", "text", &field);
1256  form.fields.push_back(field);
1257  test::CreateTestFormField("Unknown", "unknown", "", "text", &field);
1258  form.fields.push_back(field);
1259  forms.front() = form;
1260
1261  // Expect a notification when the form is first seen.
1262  {
1263    EXPECT_CALL(*autofill_manager_->metric_logger(),
1264                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
1265    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
1266                                   autofill::NO_SPECIAL_FORMS_SEEN);
1267  }
1268
1269  // Expect a notification when the form is submitted.
1270  {
1271    EXPECT_CALL(*autofill_manager_->metric_logger(),
1272                LogUserHappinessMetric(
1273                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
1274    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1275  }
1276
1277  // Fill in two of the fields.
1278  form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
1279  form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
1280  forms.front() = form;
1281
1282  // Expect a notification when the form is submitted.
1283  {
1284    EXPECT_CALL(*autofill_manager_->metric_logger(),
1285                LogUserHappinessMetric(
1286                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
1287    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1288  }
1289
1290  // Fill in the third field.
1291  form.fields[2].value = ASCIIToUTF16("12345678901");
1292  forms.front() = form;
1293
1294  // Expect notifications when the form is submitted.
1295  {
1296    EXPECT_CALL(*autofill_manager_->metric_logger(),
1297                LogUserHappinessMetric(
1298                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE));
1299    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1300  }
1301
1302
1303  // Mark one of the fields as autofilled.
1304  form.fields[1].is_autofilled = true;
1305  forms.front() = form;
1306
1307  // Expect notifications when the form is submitted.
1308  {
1309    EXPECT_CALL(*autofill_manager_->metric_logger(),
1310                LogUserHappinessMetric(
1311                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
1312    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1313  }
1314
1315  // Mark all of the fillable fields as autofilled.
1316  form.fields[0].is_autofilled = true;
1317  form.fields[2].is_autofilled = true;
1318  forms.front() = form;
1319
1320  // Expect notifications when the form is submitted.
1321  {
1322    EXPECT_CALL(*autofill_manager_->metric_logger(),
1323                LogUserHappinessMetric(
1324                    AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL));
1325    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1326  }
1327
1328  // Clear out the third field's value.
1329  form.fields[2].value = base::string16();
1330  forms.front() = form;
1331
1332  // Expect notifications when the form is submitted.
1333  {
1334    EXPECT_CALL(*autofill_manager_->metric_logger(),
1335                LogUserHappinessMetric(
1336                    AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
1337    autofill_manager_->FormSubmitted(form, TimeTicks::Now());
1338  }
1339}
1340
1341// Verify that we correctly log user happiness metrics dealing with form
1342// interaction.
1343TEST_F(AutofillMetricsTest, UserHappinessFormInteraction) {
1344  // Load a fillable form.
1345  FormData form;
1346  form.name = ASCIIToUTF16("TestForm");
1347  form.method = ASCIIToUTF16("POST");
1348  form.origin = GURL("http://example.com/form.html");
1349  form.action = GURL("http://example.com/submit.html");
1350  form.user_submitted = true;
1351
1352  FormFieldData field;
1353  test::CreateTestFormField("Name", "name", "", "text", &field);
1354  form.fields.push_back(field);
1355  test::CreateTestFormField("Email", "email", "", "text", &field);
1356  form.fields.push_back(field);
1357  test::CreateTestFormField("Phone", "phone", "", "text", &field);
1358  form.fields.push_back(field);
1359
1360  std::vector<FormData> forms(1, form);
1361
1362  // Expect a notification when the form is first seen.
1363  {
1364    EXPECT_CALL(*autofill_manager_->metric_logger(),
1365                LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
1366    autofill_manager_->OnFormsSeen(forms, TimeTicks(),
1367                                   autofill::NO_SPECIAL_FORMS_SEEN);
1368  }
1369
1370  // Simulate typing.
1371  {
1372    EXPECT_CALL(*autofill_manager_->metric_logger(),
1373                LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE));
1374    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1375                                            TimeTicks());
1376  }
1377
1378  // Simulate suggestions shown twice for a single edit (i.e. multiple
1379  // keystrokes in a single field).
1380  {
1381    EXPECT_CALL(*autofill_manager_->metric_logger(),
1382                LogUserHappinessMetric(
1383                    AutofillMetrics::SUGGESTIONS_SHOWN)).Times(1);
1384    EXPECT_CALL(*autofill_manager_->metric_logger(),
1385                LogUserHappinessMetric(
1386                    AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(1);
1387    autofill_manager_->OnDidShowAutofillSuggestions(true);
1388    autofill_manager_->OnDidShowAutofillSuggestions(false);
1389  }
1390
1391  // Simulate suggestions shown for a different field.
1392  {
1393    EXPECT_CALL(*autofill_manager_->metric_logger(),
1394                LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN));
1395    EXPECT_CALL(*autofill_manager_->metric_logger(),
1396                LogUserHappinessMetric(
1397                    AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(0);
1398    autofill_manager_->OnDidShowAutofillSuggestions(true);
1399  }
1400
1401  // Simulate invoking autofill.
1402  {
1403    EXPECT_CALL(*autofill_manager_->metric_logger(),
1404                LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
1405    EXPECT_CALL(*autofill_manager_->metric_logger(),
1406                LogUserHappinessMetric(
1407                    AutofillMetrics::USER_DID_AUTOFILL_ONCE));
1408    autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
1409  }
1410
1411  // Simulate editing an autofilled field.
1412  {
1413    EXPECT_CALL(*autofill_manager_->metric_logger(),
1414                LogUserHappinessMetric(
1415                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
1416    EXPECT_CALL(*autofill_manager_->metric_logger(),
1417                LogUserHappinessMetric(
1418                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE));
1419    PersonalDataManager::GUIDPair guid(
1420        "00000000-0000-0000-0000-000000000001", 0);
1421    PersonalDataManager::GUIDPair empty(std::string(), 0);
1422    autofill_manager_->OnFillAutofillFormData(
1423        0, form, form.fields.front(),
1424        autofill_manager_->PackGUIDs(empty, guid));
1425    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1426                                            TimeTicks());
1427    // Simulate a second keystroke; make sure we don't log the metric twice.
1428    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1429                                            TimeTicks());
1430  }
1431
1432  // Simulate invoking autofill again.
1433  EXPECT_CALL(*autofill_manager_->metric_logger(),
1434              LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
1435  EXPECT_CALL(*autofill_manager_->metric_logger(),
1436              LogUserHappinessMetric(
1437                  AutofillMetrics::USER_DID_AUTOFILL_ONCE)).Times(0);
1438  autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
1439
1440  // Simulate editing another autofilled field.
1441  {
1442    EXPECT_CALL(*autofill_manager_->metric_logger(),
1443                LogUserHappinessMetric(
1444                    AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
1445    autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
1446  }
1447}
1448
1449// Verify that we correctly log metrics tracking the duration of form fill.
1450TEST_F(AutofillMetricsTest, FormFillDuration) {
1451  // Load a fillable form.
1452  FormData form;
1453  form.name = ASCIIToUTF16("TestForm");
1454  form.method = ASCIIToUTF16("POST");
1455  form.origin = GURL("http://example.com/form.html");
1456  form.action = GURL("http://example.com/submit.html");
1457  form.user_submitted = true;
1458
1459  FormFieldData field;
1460  test::CreateTestFormField("Name", "name", "", "text", &field);
1461  form.fields.push_back(field);
1462  test::CreateTestFormField("Email", "email", "", "text", &field);
1463  form.fields.push_back(field);
1464  test::CreateTestFormField("Phone", "phone", "", "text", &field);
1465  form.fields.push_back(field);
1466
1467  std::vector<FormData> forms(1, form);
1468
1469  // Fill the field values for form submission.
1470  form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
1471  form.fields[1].value = ASCIIToUTF16("theking@gmail.com");
1472  form.fields[2].value = ASCIIToUTF16("12345678901");
1473
1474  // Expect only form load metrics to be logged if the form is submitted without
1475  // user interaction.
1476  {
1477    EXPECT_CALL(*autofill_manager_->metric_logger(),
1478                LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
1479    EXPECT_CALL(*autofill_manager_->metric_logger(),
1480                LogFormFillDurationFromLoadWithoutAutofill(
1481                    TimeDelta::FromInternalValue(16)));
1482    EXPECT_CALL(*autofill_manager_->metric_logger(),
1483                LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
1484    EXPECT_CALL(*autofill_manager_->metric_logger(),
1485                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1486    autofill_manager_->OnFormsSeen(
1487        forms, TimeTicks::FromInternalValue(1),
1488        autofill::NO_SPECIAL_FORMS_SEEN);
1489    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1490    autofill_manager_->Reset();
1491    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1492  }
1493
1494  // Expect metric to be logged if the user manually edited a form field.
1495  {
1496    EXPECT_CALL(*autofill_manager_->metric_logger(),
1497                LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
1498    EXPECT_CALL(*autofill_manager_->metric_logger(),
1499                LogFormFillDurationFromLoadWithoutAutofill(
1500                    TimeDelta::FromInternalValue(16)));
1501    EXPECT_CALL(*autofill_manager_->metric_logger(),
1502                LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
1503    EXPECT_CALL(*autofill_manager_->metric_logger(),
1504                LogFormFillDurationFromInteractionWithoutAutofill(
1505                    TimeDelta::FromInternalValue(14)));
1506    autofill_manager_->OnFormsSeen(
1507        forms, TimeTicks::FromInternalValue(1),
1508        autofill::NO_SPECIAL_FORMS_SEEN);
1509    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1510                                            TimeTicks::FromInternalValue(3));
1511    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1512    autofill_manager_->Reset();
1513    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1514  }
1515
1516  // Expect metric to be logged if the user autofilled the form.
1517  form.fields[0].is_autofilled = true;
1518  {
1519    EXPECT_CALL(*autofill_manager_->metric_logger(),
1520                LogFormFillDurationFromLoadWithAutofill(
1521                    TimeDelta::FromInternalValue(16)));
1522    EXPECT_CALL(*autofill_manager_->metric_logger(),
1523                LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1524    EXPECT_CALL(*autofill_manager_->metric_logger(),
1525                LogFormFillDurationFromInteractionWithAutofill(
1526                    TimeDelta::FromInternalValue(12)));
1527    EXPECT_CALL(*autofill_manager_->metric_logger(),
1528                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1529    autofill_manager_->OnFormsSeen(
1530        forms, TimeTicks::FromInternalValue(1),
1531        autofill::NO_SPECIAL_FORMS_SEEN);
1532    autofill_manager_->OnDidFillAutofillFormData(
1533        TimeTicks::FromInternalValue(5));
1534    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1535    autofill_manager_->Reset();
1536    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1537  }
1538
1539  // Expect metric to be logged if the user both manually filled some fields
1540  // and autofilled others.  Messages can arrive out of order, so make sure they
1541  // take precedence appropriately.
1542  {
1543    EXPECT_CALL(*autofill_manager_->metric_logger(),
1544                LogFormFillDurationFromLoadWithAutofill(
1545                    TimeDelta::FromInternalValue(16)));
1546    EXPECT_CALL(*autofill_manager_->metric_logger(),
1547                LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
1548    EXPECT_CALL(*autofill_manager_->metric_logger(),
1549                LogFormFillDurationFromInteractionWithAutofill(
1550                    TimeDelta::FromInternalValue(14)));
1551    EXPECT_CALL(*autofill_manager_->metric_logger(),
1552                LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
1553    autofill_manager_->OnFormsSeen(
1554        forms, TimeTicks::FromInternalValue(1),
1555        autofill::NO_SPECIAL_FORMS_SEEN);
1556    autofill_manager_->OnDidFillAutofillFormData(
1557        TimeTicks::FromInternalValue(5));
1558    autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
1559                                            TimeTicks::FromInternalValue(3));
1560    autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
1561    autofill_manager_->Reset();
1562    Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
1563  }
1564}
1565
1566}  // namespace autofill
1567