Home | History | Annotate | Download | only in browser
      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:
     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:
     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.
     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.
     99   virtual void LoadCreditCards() OVERRIDE {}
    100 
    101   const MockAutofillMetrics* metric_logger() const {
    102     return static_cast<const MockAutofillMetrics*>(
    103         PersonalDataManager::metric_logger());
    104   }
    105 
    106   void set_autofill_enabled(bool autofill_enabled) {
    107     autofill_enabled_ = autofill_enabled;
    108   }
    109 
    110   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:
    118   void CreateTestAutofillProfiles(ScopedVector<AutofillProfile>* profiles) {
    119     AutofillProfile* profile = new AutofillProfile;
    120     test::SetProfileInfo(profile, "Elvis", "Aaron",
    121                          "Presley", "theking (at) 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 (at) 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:
    143   explicit TestFormStructure(const FormData& form) : FormStructure(form) {}
    144   virtual ~TestFormStructure() {}
    145 
    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:
    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   }
    174   virtual ~TestAutofillManager() {}
    175 
    176   virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; }
    177 
    178   void set_autofill_enabled(bool autofill_enabled) {
    179     autofill_enabled_ = autofill_enabled;
    180   }
    181 
    182   MockAutofillMetrics* metric_logger() {
    183     return static_cast<MockAutofillMetrics*>(const_cast<AutofillMetrics*>(
    184         AutofillManager::metric_logger()));
    185   }
    186 
    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 
    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 
    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 
    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 
    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 
    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.
    282 TEST_F(AutofillMetricsTest, QualityMetrics) {
    283   // Set up our form data.
    284   FormData form;
    285   form.name = ASCIIToUTF16("TestForm");
    286   form.origin = GURL("http://example.com/form.html");
    287   form.action = GURL("http://example.com/submit.html");
    288   form.user_submitted = true;
    289 
    290   std::vector<ServerFieldType> heuristic_types, server_types;
    291   FormFieldData field;
    292 
    293   test::CreateTestFormField(
    294       "Autofilled", "autofilled", "Elvis Aaron Presley", "text", &field);
    295   field.is_autofilled = true;
    296   form.fields.push_back(field);
    297   heuristic_types.push_back(NAME_FULL);
    298   server_types.push_back(NAME_FIRST);
    299 
    300   test::CreateTestFormField(
    301       "Autofill Failed", "autofillfailed", "buddy (at) gmail.com", "text", &field);
    302   field.is_autofilled = false;
    303   form.fields.push_back(field);
    304   heuristic_types.push_back(PHONE_HOME_NUMBER);
    305   server_types.push_back(EMAIL_ADDRESS);
    306 
    307   test::CreateTestFormField("Empty", "empty", "", "text", &field);
    308   field.is_autofilled = false;
    309   form.fields.push_back(field);
    310   heuristic_types.push_back(NAME_FULL);
    311   server_types.push_back(NAME_FIRST);
    312 
    313   test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
    314   field.is_autofilled = false;
    315   form.fields.push_back(field);
    316   heuristic_types.push_back(PHONE_HOME_NUMBER);
    317   server_types.push_back(EMAIL_ADDRESS);
    318 
    319   test::CreateTestFormField("Select", "select", "USA", "select-one", &field);
    320   field.is_autofilled = false;
    321   form.fields.push_back(field);
    322   heuristic_types.push_back(UNKNOWN_TYPE);
    323   server_types.push_back(NO_SERVER_DATA);
    324 
    325   test::CreateTestFormField("Phone", "phone", "2345678901", "tel", &field);
    326   field.is_autofilled = true;
    327   form.fields.push_back(field);
    328   heuristic_types.push_back(PHONE_HOME_CITY_AND_NUMBER);
    329   server_types.push_back(PHONE_HOME_WHOLE_NUMBER);
    330 
    331   // Simulate having seen this form on page load.
    332   autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
    333 
    334   // Establish our expectations.
    335   ::testing::InSequence dummy;
    336   // Autofilled field
    337   EXPECT_CALL(*autofill_manager_->metric_logger(),
    338               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
    339                   NAME_FULL));
    340   EXPECT_CALL(*autofill_manager_->metric_logger(),
    341               LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    342                   NAME_FULL));
    343   EXPECT_CALL(*autofill_manager_->metric_logger(),
    344               LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    345                   NAME_FULL));
    346   // Non-autofilled field for which we had data
    347   EXPECT_CALL(*autofill_manager_->metric_logger(),
    348               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    349                   EMAIL_ADDRESS));
    350   EXPECT_CALL(*autofill_manager_->metric_logger(),
    351               LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
    352                   EMAIL_ADDRESS));
    353   EXPECT_CALL(*autofill_manager_->metric_logger(),
    354               LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
    355                   EMAIL_ADDRESS));
    356   // Empty field
    357   // Unknown field
    358   // <select> field
    359   EXPECT_CALL(*autofill_manager_->metric_logger(),
    360               LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    361                   ADDRESS_HOME_COUNTRY));
    362   EXPECT_CALL(*autofill_manager_->metric_logger(),
    363               LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    364                   ADDRESS_HOME_COUNTRY));
    365   EXPECT_CALL(*autofill_manager_->metric_logger(),
    366               LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    367                   ADDRESS_HOME_COUNTRY));
    368   // Phone field
    369   EXPECT_CALL(*autofill_manager_->metric_logger(),
    370               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
    371                   PHONE_HOME_WHOLE_NUMBER));
    372   EXPECT_CALL(*autofill_manager_->metric_logger(),
    373               LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
    374                   PHONE_HOME_WHOLE_NUMBER));
    375   EXPECT_CALL(*autofill_manager_->metric_logger(),
    376               LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
    377                   PHONE_HOME_WHOLE_NUMBER));
    378   EXPECT_CALL(*autofill_manager_->metric_logger(),
    379               LogUserHappinessMetric(
    380                   AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
    381 
    382   // Simulate form submission.
    383   EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
    384                                                            TimeTicks::Now()));
    385 }
    386 
    387 // Test that we behave sanely when the cached form differs from the submitted
    388 // one.
    389 TEST_F(AutofillMetricsTest, SaneMetricsWithCacheMismatch) {
    390   // Set up our form data.
    391   FormData form;
    392   form.name = ASCIIToUTF16("TestForm");
    393   form.origin = GURL("http://example.com/form.html");
    394   form.action = GURL("http://example.com/submit.html");
    395   form.user_submitted = true;
    396 
    397   std::vector<ServerFieldType> heuristic_types, server_types;
    398 
    399   FormFieldData field;
    400   test::CreateTestFormField(
    401       "Both match", "match", "Elvis Aaron Presley", "text", &field);
    402   field.is_autofilled = true;
    403   form.fields.push_back(field);
    404   heuristic_types.push_back(NAME_FULL);
    405   server_types.push_back(NAME_FULL);
    406   test::CreateTestFormField(
    407       "Both mismatch", "mismatch", "buddy (at) gmail.com", "text", &field);
    408   field.is_autofilled = false;
    409   form.fields.push_back(field);
    410   heuristic_types.push_back(PHONE_HOME_NUMBER);
    411   server_types.push_back(PHONE_HOME_NUMBER);
    412   test::CreateTestFormField(
    413       "Only heuristics match", "mixed", "Memphis", "text", &field);
    414   field.is_autofilled = false;
    415   form.fields.push_back(field);
    416   heuristic_types.push_back(ADDRESS_HOME_CITY);
    417   server_types.push_back(PHONE_HOME_NUMBER);
    418   test::CreateTestFormField("Unknown", "unknown", "garbage", "text", &field);
    419   field.is_autofilled = false;
    420   form.fields.push_back(field);
    421   heuristic_types.push_back(UNKNOWN_TYPE);
    422   server_types.push_back(UNKNOWN_TYPE);
    423 
    424   // Simulate having seen this form with the desired heuristic and server types.
    425   // |form_structure| will be owned by |autofill_manager_|.
    426   autofill_manager_->AddSeenForm(form, heuristic_types, server_types);
    427 
    428 
    429   // Add a field and re-arrange the remaining form fields before submitting.
    430   std::vector<FormFieldData> cached_fields = form.fields;
    431   form.fields.clear();
    432   test::CreateTestFormField(
    433       "New field", "new field", "Tennessee", "text", &field);
    434   form.fields.push_back(field);
    435   form.fields.push_back(cached_fields[2]);
    436   form.fields.push_back(cached_fields[1]);
    437   form.fields.push_back(cached_fields[3]);
    438   form.fields.push_back(cached_fields[0]);
    439 
    440   // Establish our expectations.
    441   ::testing::InSequence dummy;
    442   // New field
    443   EXPECT_CALL(*autofill_manager_->metric_logger(),
    444               LogHeuristicTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    445                   ADDRESS_HOME_STATE));
    446   EXPECT_CALL(*autofill_manager_->metric_logger(),
    447               LogServerTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    448                   ADDRESS_HOME_STATE));
    449   EXPECT_CALL(*autofill_manager_->metric_logger(),
    450               LogOverallTypePrediction(AutofillMetrics::TYPE_UNKNOWN,
    451                   ADDRESS_HOME_STATE));
    452   // Only heuristics match
    453   EXPECT_CALL(*autofill_manager_->metric_logger(),
    454               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
    455                   ADDRESS_HOME_CITY));
    456   EXPECT_CALL(*autofill_manager_->metric_logger(),
    457               LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    458                   ADDRESS_HOME_CITY));
    459   EXPECT_CALL(*autofill_manager_->metric_logger(),
    460               LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    461                   ADDRESS_HOME_CITY));
    462   // Both mismatch
    463   EXPECT_CALL(*autofill_manager_->metric_logger(),
    464               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    465                   EMAIL_ADDRESS));
    466   EXPECT_CALL(*autofill_manager_->metric_logger(),
    467               LogServerTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    468                   EMAIL_ADDRESS));
    469   EXPECT_CALL(*autofill_manager_->metric_logger(),
    470               LogOverallTypePrediction(AutofillMetrics::TYPE_MISMATCH,
    471                   EMAIL_ADDRESS));
    472   // Unknown
    473   // Both match
    474   EXPECT_CALL(*autofill_manager_->metric_logger(),
    475               LogHeuristicTypePrediction(AutofillMetrics::TYPE_MATCH,
    476                   NAME_FULL));
    477   EXPECT_CALL(*autofill_manager_->metric_logger(),
    478               LogServerTypePrediction(AutofillMetrics::TYPE_MATCH,
    479                   NAME_FULL));
    480   EXPECT_CALL(*autofill_manager_->metric_logger(),
    481               LogOverallTypePrediction(AutofillMetrics::TYPE_MATCH,
    482                   NAME_FULL));
    483 
    484   // Simulate form submission.
    485   EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form,
    486                                                            TimeTicks::Now()));
    487 }
    488 
    489 // Verify that we correctly log metrics regarding developer engagement.
    490 TEST_F(AutofillMetricsTest, DeveloperEngagement) {
    491   // Start with a non-fillable form.
    492   FormData form;
    493   form.name = ASCIIToUTF16("TestForm");
    494   form.origin = GURL("http://example.com/form.html");
    495   form.action = GURL("http://example.com/submit.html");
    496 
    497   FormFieldData field;
    498   test::CreateTestFormField("Name", "name", "", "text", &field);
    499   form.fields.push_back(field);
    500   test::CreateTestFormField("Email", "email", "", "text", &field);
    501   form.fields.push_back(field);
    502 
    503   std::vector<FormData> forms(1, form);
    504 
    505   // Ensure no metrics are logged when loading a non-fillable form.
    506   {
    507     EXPECT_CALL(*autofill_manager_->metric_logger(),
    508                 LogDeveloperEngagementMetric(_)).Times(0);
    509     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    510     autofill_manager_->Reset();
    511     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
    512   }
    513 
    514   // Add another field to the form, so that it becomes fillable.
    515   test::CreateTestFormField("Phone", "phone", "", "text", &field);
    516   forms.back().fields.push_back(field);
    517 
    518   // Expect only the "form parsed" metric to be logged; no metrics about
    519   // author-specified field type hints.
    520   {
    521     EXPECT_CALL(
    522         *autofill_manager_->metric_logger(),
    523         LogDeveloperEngagementMetric(
    524             AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
    525     EXPECT_CALL(
    526         *autofill_manager_->metric_logger(),
    527         LogDeveloperEngagementMetric(
    528             AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(0);
    529     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    530     autofill_manager_->Reset();
    531     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
    532   }
    533 
    534   // Add some fields with an author-specified field type to the form.
    535   // We need to add at least three fields, because a form must have at least
    536   // three fillable fields to be considered to be autofillable; and if at least
    537   // one field specifies an explicit type hint, we don't apply any of our usual
    538   // local heuristics to detect field types in the rest of the form.
    539   test::CreateTestFormField("", "", "", "text", &field);
    540   field.autocomplete_attribute = "given-name";
    541   forms.back().fields.push_back(field);
    542   test::CreateTestFormField("", "", "", "text", &field);
    543   field.autocomplete_attribute = "email";
    544   forms.back().fields.push_back(field);
    545   test::CreateTestFormField("", "", "", "text", &field);
    546   field.autocomplete_attribute = "address-line1";
    547   forms.back().fields.push_back(field);
    548 
    549   // Expect both the "form parsed" metric and the author-specified field type
    550   // hints metric to be logged.
    551   {
    552     EXPECT_CALL(
    553         *autofill_manager_->metric_logger(),
    554         LogDeveloperEngagementMetric(
    555             AutofillMetrics::FILLABLE_FORM_PARSED)).Times(1);
    556     EXPECT_CALL(
    557         *autofill_manager_->metric_logger(),
    558         LogDeveloperEngagementMetric(
    559             AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS)).Times(1);
    560     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    561     autofill_manager_->Reset();
    562     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
    563   }
    564 }
    565 
    566 // Test that the profile count is logged correctly.
    567 TEST_F(AutofillMetricsTest, StoredProfileCount) {
    568   // The metric should be logged when the profiles are first loaded.
    569   EXPECT_CALL(*personal_data_->metric_logger(),
    570               LogStoredProfileCount(2)).Times(1);
    571   personal_data_->LoadProfiles();
    572 
    573   // The metric should only be logged once.
    574   EXPECT_CALL(*personal_data_->metric_logger(),
    575               LogStoredProfileCount(::testing::_)).Times(0);
    576   personal_data_->LoadProfiles();
    577 }
    578 
    579 // Test that we correctly log when Autofill is enabled.
    580 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtStartup) {
    581   personal_data_->set_autofill_enabled(true);
    582   EXPECT_CALL(*personal_data_->metric_logger(),
    583               LogIsAutofillEnabledAtStartup(true)).Times(1);
    584   personal_data_->Init(
    585       autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
    586 }
    587 
    588 // Test that we correctly log when Autofill is disabled.
    589 TEST_F(AutofillMetricsTest, AutofillIsDisabledAtStartup) {
    590   personal_data_->set_autofill_enabled(false);
    591   EXPECT_CALL(*personal_data_->metric_logger(),
    592               LogIsAutofillEnabledAtStartup(false)).Times(1);
    593   personal_data_->Init(
    594       autofill_client_.GetDatabase(), autofill_client_.GetPrefs(), false);
    595 }
    596 
    597 // Test that we log the number of Autofill suggestions when filling a form.
    598 TEST_F(AutofillMetricsTest, AddressSuggestionsCount) {
    599   // Set up our form data.
    600   FormData form;
    601   form.name = ASCIIToUTF16("TestForm");
    602   form.origin = GURL("http://example.com/form.html");
    603   form.action = GURL("http://example.com/submit.html");
    604   form.user_submitted = true;
    605 
    606   FormFieldData field;
    607   std::vector<ServerFieldType> field_types;
    608   test::CreateTestFormField("Name", "name", "", "text", &field);
    609   form.fields.push_back(field);
    610   field_types.push_back(NAME_FULL);
    611   test::CreateTestFormField("Email", "email", "", "email", &field);
    612   form.fields.push_back(field);
    613   field_types.push_back(EMAIL_ADDRESS);
    614   test::CreateTestFormField("Phone", "phone", "", "tel", &field);
    615   form.fields.push_back(field);
    616   field_types.push_back(PHONE_HOME_NUMBER);
    617 
    618   // Simulate having seen this form on page load.
    619   // |form_structure| will be owned by |autofill_manager_|.
    620   autofill_manager_->AddSeenForm(form, field_types, field_types);
    621 
    622   // Establish our expectations.
    623   ::testing::InSequence dummy;
    624   EXPECT_CALL(*autofill_manager_->metric_logger(),
    625               LogAddressSuggestionsCount(2)).Times(1);
    626 
    627   // Simulate activating the autofill popup for the phone field.
    628   autofill_manager_->OnQueryFormFieldAutofill(
    629       0, form, field, gfx::Rect(), false);
    630 
    631   // Simulate activating the autofill popup for the email field after typing.
    632   // No new metric should be logged, since we're still on the same page.
    633   test::CreateTestFormField("Email", "email", "b", "email", &field);
    634   autofill_manager_->OnQueryFormFieldAutofill(
    635       0, form, field, gfx::Rect(), false);
    636 
    637   // Reset the autofill manager state.
    638   autofill_manager_->Reset();
    639   autofill_manager_->AddSeenForm(form, field_types, field_types);
    640 
    641   // Establish our expectations.
    642   EXPECT_CALL(*autofill_manager_->metric_logger(),
    643               LogAddressSuggestionsCount(1)).Times(1);
    644 
    645   // Simulate activating the autofill popup for the email field after typing.
    646   autofill_manager_->OnQueryFormFieldAutofill(
    647       0, form, field, gfx::Rect(), false);
    648 
    649   // Reset the autofill manager state again.
    650   autofill_manager_->Reset();
    651   autofill_manager_->AddSeenForm(form, field_types, field_types);
    652 
    653   // Establish our expectations.
    654   EXPECT_CALL(*autofill_manager_->metric_logger(),
    655               LogAddressSuggestionsCount(::testing::_)).Times(0);
    656 
    657   // Simulate activating the autofill popup for the email field after typing.
    658   form.fields[0].is_autofilled = true;
    659   autofill_manager_->OnQueryFormFieldAutofill(
    660       0, form, field, gfx::Rect(), false);
    661 }
    662 
    663 // Test that we log whether Autofill is enabled when filling a form.
    664 TEST_F(AutofillMetricsTest, AutofillIsEnabledAtPageLoad) {
    665   // Establish our expectations.
    666   ::testing::InSequence dummy;
    667   EXPECT_CALL(*autofill_manager_->metric_logger(),
    668               LogIsAutofillEnabledAtPageLoad(true)).Times(1);
    669 
    670   autofill_manager_->set_autofill_enabled(true);
    671   autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
    672 
    673   // Reset the autofill manager state.
    674   autofill_manager_->Reset();
    675 
    676   // Establish our expectations.
    677   EXPECT_CALL(*autofill_manager_->metric_logger(),
    678               LogIsAutofillEnabledAtPageLoad(false)).Times(1);
    679 
    680   autofill_manager_->set_autofill_enabled(false);
    681   autofill_manager_->OnFormsSeen(std::vector<FormData>(), TimeTicks());
    682 }
    683 
    684 // Verify that we correctly log user happiness metrics dealing with form loading
    685 // and form submission.
    686 TEST_F(AutofillMetricsTest, UserHappinessFormLoadAndSubmission) {
    687   // Start with a form with insufficiently many fields.
    688   FormData form;
    689   form.name = ASCIIToUTF16("TestForm");
    690   form.origin = GURL("http://example.com/form.html");
    691   form.action = GURL("http://example.com/submit.html");
    692   form.user_submitted = true;
    693 
    694   FormFieldData field;
    695   test::CreateTestFormField("Name", "name", "", "text", &field);
    696   form.fields.push_back(field);
    697   test::CreateTestFormField("Email", "email", "", "text", &field);
    698   form.fields.push_back(field);
    699 
    700   std::vector<FormData> forms(1, form);
    701 
    702   // Expect no notifications when the form is first seen.
    703   {
    704     EXPECT_CALL(*autofill_manager_->metric_logger(),
    705                 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED)).Times(0);
    706     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    707   }
    708 
    709 
    710   // Expect no notifications when the form is submitted.
    711   {
    712     EXPECT_CALL(
    713         *autofill_manager_->metric_logger(),
    714         LogUserHappinessMetric(
    715             AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL)).Times(0);
    716     EXPECT_CALL(
    717         *autofill_manager_->metric_logger(),
    718         LogUserHappinessMetric(
    719             AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME)).Times(0);
    720     EXPECT_CALL(
    721         *autofill_manager_->metric_logger(),
    722         LogUserHappinessMetric(
    723             AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE)).Times(0);
    724     EXPECT_CALL(
    725         *autofill_manager_->metric_logger(),
    726         LogUserHappinessMetric(
    727             AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM)).Times(0);
    728     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    729   }
    730 
    731   // Add more fields to the form.
    732   test::CreateTestFormField("Phone", "phone", "", "text", &field);
    733   form.fields.push_back(field);
    734   test::CreateTestFormField("Unknown", "unknown", "", "text", &field);
    735   form.fields.push_back(field);
    736   forms.front() = form;
    737 
    738   // Expect a notification when the form is first seen.
    739   {
    740     EXPECT_CALL(*autofill_manager_->metric_logger(),
    741                 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
    742     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    743   }
    744 
    745   // Expect a notification when the form is submitted.
    746   {
    747     EXPECT_CALL(*autofill_manager_->metric_logger(),
    748                 LogUserHappinessMetric(
    749                     AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
    750     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    751   }
    752 
    753   // Fill in two of the fields.
    754   form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
    755   form.fields[1].value = ASCIIToUTF16("theking (at) gmail.com");
    756   forms.front() = form;
    757 
    758   // Expect a notification when the form is submitted.
    759   {
    760     EXPECT_CALL(*autofill_manager_->metric_logger(),
    761                 LogUserHappinessMetric(
    762                     AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
    763     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    764   }
    765 
    766   // Fill in the third field.
    767   form.fields[2].value = ASCIIToUTF16("12345678901");
    768   forms.front() = form;
    769 
    770   // Expect notifications when the form is submitted.
    771   {
    772     EXPECT_CALL(*autofill_manager_->metric_logger(),
    773                 LogUserHappinessMetric(
    774                     AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_NONE));
    775     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    776   }
    777 
    778 
    779   // Mark one of the fields as autofilled.
    780   form.fields[1].is_autofilled = true;
    781   forms.front() = form;
    782 
    783   // Expect notifications when the form is submitted.
    784   {
    785     EXPECT_CALL(*autofill_manager_->metric_logger(),
    786                 LogUserHappinessMetric(
    787                     AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_SOME));
    788     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    789   }
    790 
    791   // Mark all of the fillable fields as autofilled.
    792   form.fields[0].is_autofilled = true;
    793   form.fields[2].is_autofilled = true;
    794   forms.front() = form;
    795 
    796   // Expect notifications when the form is submitted.
    797   {
    798     EXPECT_CALL(*autofill_manager_->metric_logger(),
    799                 LogUserHappinessMetric(
    800                     AutofillMetrics::SUBMITTED_FILLABLE_FORM_AUTOFILLED_ALL));
    801     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    802   }
    803 
    804   // Clear out the third field's value.
    805   form.fields[2].value = base::string16();
    806   forms.front() = form;
    807 
    808   // Expect notifications when the form is submitted.
    809   {
    810     EXPECT_CALL(*autofill_manager_->metric_logger(),
    811                 LogUserHappinessMetric(
    812                     AutofillMetrics::SUBMITTED_NON_FILLABLE_FORM));
    813     autofill_manager_->FormSubmitted(form, TimeTicks::Now());
    814   }
    815 }
    816 
    817 // Verify that we correctly log user happiness metrics dealing with form
    818 // interaction.
    819 TEST_F(AutofillMetricsTest, UserHappinessFormInteraction) {
    820   // Load a fillable form.
    821   FormData form;
    822   form.name = ASCIIToUTF16("TestForm");
    823   form.origin = GURL("http://example.com/form.html");
    824   form.action = GURL("http://example.com/submit.html");
    825   form.user_submitted = true;
    826 
    827   FormFieldData field;
    828   test::CreateTestFormField("Name", "name", "", "text", &field);
    829   form.fields.push_back(field);
    830   test::CreateTestFormField("Email", "email", "", "text", &field);
    831   form.fields.push_back(field);
    832   test::CreateTestFormField("Phone", "phone", "", "text", &field);
    833   form.fields.push_back(field);
    834 
    835   std::vector<FormData> forms(1, form);
    836 
    837   // Expect a notification when the form is first seen.
    838   {
    839     EXPECT_CALL(*autofill_manager_->metric_logger(),
    840                 LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED));
    841     autofill_manager_->OnFormsSeen(forms, TimeTicks());
    842   }
    843 
    844   // Simulate typing.
    845   {
    846     EXPECT_CALL(*autofill_manager_->metric_logger(),
    847                 LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE));
    848     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
    849                                             TimeTicks());
    850   }
    851 
    852   // Simulate suggestions shown twice for a single edit (i.e. multiple
    853   // keystrokes in a single field).
    854   {
    855     EXPECT_CALL(*autofill_manager_->metric_logger(),
    856                 LogUserHappinessMetric(
    857                     AutofillMetrics::SUGGESTIONS_SHOWN)).Times(1);
    858     EXPECT_CALL(*autofill_manager_->metric_logger(),
    859                 LogUserHappinessMetric(
    860                     AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(1);
    861     autofill_manager_->DidShowSuggestions(true);
    862     autofill_manager_->DidShowSuggestions(false);
    863   }
    864 
    865   // Simulate suggestions shown for a different field.
    866   {
    867     EXPECT_CALL(*autofill_manager_->metric_logger(),
    868                 LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN));
    869     EXPECT_CALL(*autofill_manager_->metric_logger(),
    870                 LogUserHappinessMetric(
    871                     AutofillMetrics::SUGGESTIONS_SHOWN_ONCE)).Times(0);
    872     autofill_manager_->DidShowSuggestions(true);
    873   }
    874 
    875   // Simulate invoking autofill.
    876   {
    877     EXPECT_CALL(*autofill_manager_->metric_logger(),
    878                 LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
    879     EXPECT_CALL(*autofill_manager_->metric_logger(),
    880                 LogUserHappinessMetric(
    881                     AutofillMetrics::USER_DID_AUTOFILL_ONCE));
    882     autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
    883   }
    884 
    885   // Simulate editing an autofilled field.
    886   {
    887     EXPECT_CALL(*autofill_manager_->metric_logger(),
    888                 LogUserHappinessMetric(
    889                     AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
    890     EXPECT_CALL(*autofill_manager_->metric_logger(),
    891                 LogUserHappinessMetric(
    892                     AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE));
    893     PersonalDataManager::GUIDPair guid(
    894         "00000000-0000-0000-0000-000000000001", 0);
    895     PersonalDataManager::GUIDPair empty(std::string(), 0);
    896     autofill_manager_->FillOrPreviewForm(
    897         AutofillDriver::FORM_DATA_ACTION_FILL,
    898         0, form, form.fields.front(),
    899         autofill_manager_->PackGUIDs(empty, guid));
    900     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
    901                                             TimeTicks());
    902     // Simulate a second keystroke; make sure we don't log the metric twice.
    903     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
    904                                             TimeTicks());
    905   }
    906 
    907   // Simulate invoking autofill again.
    908   EXPECT_CALL(*autofill_manager_->metric_logger(),
    909               LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL));
    910   EXPECT_CALL(*autofill_manager_->metric_logger(),
    911               LogUserHappinessMetric(
    912                   AutofillMetrics::USER_DID_AUTOFILL_ONCE)).Times(0);
    913   autofill_manager_->OnDidFillAutofillFormData(TimeTicks());
    914 
    915   // Simulate editing another autofilled field.
    916   {
    917     EXPECT_CALL(*autofill_manager_->metric_logger(),
    918                 LogUserHappinessMetric(
    919                     AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD));
    920     autofill_manager_->OnTextFieldDidChange(form, form.fields[1], TimeTicks());
    921   }
    922 }
    923 
    924 // Verify that we correctly log metrics tracking the duration of form fill.
    925 TEST_F(AutofillMetricsTest, FormFillDuration) {
    926   // Load a fillable form.
    927   FormData form;
    928   form.name = ASCIIToUTF16("TestForm");
    929   form.origin = GURL("http://example.com/form.html");
    930   form.action = GURL("http://example.com/submit.html");
    931   form.user_submitted = true;
    932 
    933   FormFieldData field;
    934   test::CreateTestFormField("Name", "name", "", "text", &field);
    935   form.fields.push_back(field);
    936   test::CreateTestFormField("Email", "email", "", "text", &field);
    937   form.fields.push_back(field);
    938   test::CreateTestFormField("Phone", "phone", "", "text", &field);
    939   form.fields.push_back(field);
    940 
    941   std::vector<FormData> forms(1, form);
    942 
    943   // Fill additional form.
    944   FormData second_form = form;
    945   test::CreateTestFormField("Second Phone", "second_phone", "", "text", &field);
    946   second_form.fields.push_back(field);
    947 
    948   std::vector<FormData> second_forms(1, second_form);
    949 
    950   // Fill the field values for form submission.
    951   form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
    952   form.fields[1].value = ASCIIToUTF16("theking (at) gmail.com");
    953   form.fields[2].value = ASCIIToUTF16("12345678901");
    954 
    955   // Fill the field values for form submission.
    956   second_form.fields[0].value = ASCIIToUTF16("Elvis Aaron Presley");
    957   second_form.fields[1].value = ASCIIToUTF16("theking (at) gmail.com");
    958   second_form.fields[2].value = ASCIIToUTF16("12345678901");
    959   second_form.fields[3].value = ASCIIToUTF16("51512345678");
    960 
    961   // Expect only form load metrics to be logged if the form is submitted without
    962   // user interaction.
    963   {
    964     EXPECT_CALL(*autofill_manager_->metric_logger(),
    965                 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
    966     EXPECT_CALL(*autofill_manager_->metric_logger(),
    967                 LogFormFillDurationFromLoadWithoutAutofill(
    968                     TimeDelta::FromInternalValue(16)));
    969     EXPECT_CALL(*autofill_manager_->metric_logger(),
    970                 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
    971     EXPECT_CALL(*autofill_manager_->metric_logger(),
    972                 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
    973     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
    974     autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
    975     autofill_manager_->Reset();
    976     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
    977   }
    978 
    979   // Expect metric to be logged if the user manually edited a form field.
    980   {
    981     EXPECT_CALL(*autofill_manager_->metric_logger(),
    982                 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
    983     EXPECT_CALL(*autofill_manager_->metric_logger(),
    984                 LogFormFillDurationFromLoadWithoutAutofill(
    985                     TimeDelta::FromInternalValue(16)));
    986     EXPECT_CALL(*autofill_manager_->metric_logger(),
    987                 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
    988     EXPECT_CALL(*autofill_manager_->metric_logger(),
    989                 LogFormFillDurationFromInteractionWithoutAutofill(
    990                     TimeDelta::FromInternalValue(14)));
    991     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
    992     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
    993                                             TimeTicks::FromInternalValue(3));
    994     autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
    995     autofill_manager_->Reset();
    996     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
    997   }
    998 
    999   // Expect metric to be logged if the user autofilled the form.
   1000   form.fields[0].is_autofilled = true;
   1001   {
   1002     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1003                 LogFormFillDurationFromLoadWithAutofill(
   1004                     TimeDelta::FromInternalValue(16)));
   1005     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1006                 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
   1007     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1008                 LogFormFillDurationFromInteractionWithAutofill(
   1009                     TimeDelta::FromInternalValue(12)));
   1010     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1011                 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
   1012     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
   1013     autofill_manager_->OnDidFillAutofillFormData(
   1014         TimeTicks::FromInternalValue(5));
   1015     autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
   1016     autofill_manager_->Reset();
   1017     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
   1018   }
   1019 
   1020   // Expect metric to be logged if the user both manually filled some fields
   1021   // and autofilled others.  Messages can arrive out of order, so make sure they
   1022   // take precedence appropriately.
   1023   {
   1024     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1025                 LogFormFillDurationFromLoadWithAutofill(
   1026                     TimeDelta::FromInternalValue(16)));
   1027     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1028                 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
   1029     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1030                 LogFormFillDurationFromInteractionWithAutofill(
   1031                     TimeDelta::FromInternalValue(14)));
   1032     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1033                 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
   1034     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
   1035     autofill_manager_->OnDidFillAutofillFormData(
   1036         TimeTicks::FromInternalValue(5));
   1037     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
   1038                                             TimeTicks::FromInternalValue(3));
   1039     autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
   1040     autofill_manager_->Reset();
   1041     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
   1042   }
   1043 
   1044   // Make sure that loading another form doesn't affect metrics from the first
   1045   // form.
   1046   {
   1047     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1048                 LogFormFillDurationFromLoadWithAutofill(
   1049                     TimeDelta::FromInternalValue(16)));
   1050     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1051                 LogFormFillDurationFromLoadWithoutAutofill(_)).Times(0);
   1052     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1053                 LogFormFillDurationFromInteractionWithAutofill(
   1054                     TimeDelta::FromInternalValue(14)));
   1055     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1056                 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
   1057     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
   1058     autofill_manager_->OnFormsSeen(second_forms,
   1059                                    TimeTicks::FromInternalValue(3));
   1060     autofill_manager_->OnDidFillAutofillFormData(
   1061         TimeTicks::FromInternalValue(5));
   1062     autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
   1063                                             TimeTicks::FromInternalValue(3));
   1064     autofill_manager_->FormSubmitted(form, TimeTicks::FromInternalValue(17));
   1065     autofill_manager_->Reset();
   1066     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
   1067   }
   1068 
   1069   // Make sure that submitting a form that was loaded later will report the
   1070   // later loading time.
   1071   {
   1072     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1073                 LogFormFillDurationFromLoadWithoutAutofill(
   1074                     TimeDelta::FromInternalValue(12)));
   1075     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1076                 LogFormFillDurationFromLoadWithAutofill(_)).Times(0);
   1077     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1078                 LogFormFillDurationFromInteractionWithAutofill(_)).Times(0);
   1079     EXPECT_CALL(*autofill_manager_->metric_logger(),
   1080                 LogFormFillDurationFromInteractionWithoutAutofill(_)).Times(0);
   1081     autofill_manager_->OnFormsSeen(forms, TimeTicks::FromInternalValue(1));
   1082     autofill_manager_->OnFormsSeen(second_forms,
   1083                                    TimeTicks::FromInternalValue(5));
   1084     autofill_manager_->FormSubmitted(second_form,
   1085                                      TimeTicks::FromInternalValue(17));
   1086     autofill_manager_->Reset();
   1087     Mock::VerifyAndClearExpectations(autofill_manager_->metric_logger());
   1088   }
   1089 }
   1090 
   1091 }  // namespace autofill
   1092