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