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