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