Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2010 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 <queue> | |
| 6 | |
| 7 #include "base/ref_counted.h" | |
| 8 #include "base/scoped_ptr.h" | |
| 9 #include "base/string16.h" | |
| 10 #include "base/utf_string_conversions.h" | |
| 11 #include "chrome/browser/autofill/autofill_common_test.h" | |
| 12 #include "chrome/browser/autofill/autofill_manager.h" | |
| 13 #include "chrome/browser/autofill/autofill_metrics.h" | |
| 14 #include "chrome/browser/autofill/personal_data_manager.h" | |
| 15 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | |
| 16 #include "chrome/browser/tab_contents/test_tab_contents.h" | |
| 17 #include "webkit/glue/form_data.h" | |
| 18 #include "webkit/glue/form_field.h" | |
| 19 | |
| 20 using autofill_metrics::QualityMetricType; | |
| 21 using webkit_glue::FormData; | |
| 22 using webkit_glue::FormField; | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // TODO(isherman): Move this into autofill_common_test.h? | |
| 27 class TestPersonalDataManager : public PersonalDataManager { | |
| 28 public: | |
| 29 TestPersonalDataManager() { | |
| 30 CreateTestAutoFillProfiles(&web_profiles_); | |
| 31 CreateTestCreditCards(&credit_cards_); | |
| 32 } | |
| 33 | |
| 34 virtual void InitializeIfNeeded() {} | |
| 35 virtual void SaveImportedFormData() {} | |
| 36 virtual bool IsDataLoaded() const { return true; } | |
| 37 | |
| 38 private: | |
| 39 void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) { | |
| 40 AutoFillProfile* profile = new AutoFillProfile; | |
| 41 autofill_test::SetProfileInfo(profile, "Home", "Elvis", "Aaron", | |
| 42 "Presley", "theking@gmail.com", "RCA", | |
| 43 "3734 Elvis Presley Blvd.", "Apt. 10", | |
| 44 "Memphis", "Tennessee", "38116", "USA", | |
| 45 "12345678901", ""); | |
| 46 profile->set_guid("00000000-0000-0000-0000-000000000001"); | |
| 47 profiles->push_back(profile); | |
| 48 profile = new AutoFillProfile; | |
| 49 autofill_test::SetProfileInfo(profile, "Work", "Charles", "Hardin", | |
| 50 "Holley", "buddy@gmail.com", "Decca", | |
| 51 "123 Apple St.", "unit 6", "Lubbock", | |
| 52 "Texas", "79401", "USA", "23456789012", | |
| 53 ""); | |
| 54 profile->set_guid("00000000-0000-0000-0000-000000000002"); | |
| 55 profiles->push_back(profile); | |
| 56 profile = new AutoFillProfile; | |
| 57 autofill_test::SetProfileInfo(profile, "Empty", "", "", "", "", "", "", "", | |
| 58 "", "", "", "", "", ""); | |
| 59 profile->set_guid("00000000-0000-0000-0000-000000000003"); | |
| 60 profiles->push_back(profile); | |
| 61 } | |
| 62 | |
| 63 void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) { | |
| 64 CreditCard* credit_card = new CreditCard; | |
| 65 autofill_test::SetCreditCardInfo(credit_card, "First", "Elvis Presley", | |
| 66 "4234567890123456", // Visa | |
| 67 "04", "2012"); | |
| 68 credit_card->set_guid("00000000-0000-0000-0000-000000000004"); | |
| 69 credit_cards->push_back(credit_card); | |
| 70 credit_card = new CreditCard; | |
| 71 autofill_test::SetCreditCardInfo(credit_card, "Second", "Buddy Holly", | |
| 72 "5187654321098765", // Mastercard | |
| 73 "10", "2014"); | |
| 74 credit_card->set_guid("00000000-0000-0000-0000-000000000005"); | |
| 75 credit_cards->push_back(credit_card); | |
| 76 credit_card = new CreditCard; | |
| 77 autofill_test::SetCreditCardInfo(credit_card, "Empty", "", "", "", ""); | |
| 78 credit_card->set_guid("00000000-0000-0000-0000-000000000006"); | |
| 79 credit_cards->push_back(credit_card); | |
| 80 } | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); | |
| 83 }; | |
| 84 | |
| 85 // A slightly hacky way to verify that the right log calls are being made. | |
|
Ilya Sherman
2010/12/10 09:07:42
This is pretty hacky -- any thoughts on how to mak
| |
| 86 class LogTracker { | |
| 87 public: | |
| 88 static void LogQualityMetric(QualityMetricType type) { | |
| 89 ASSERT_TRUE(!expected_quality_metrics_.empty()); | |
| 90 EXPECT_EQ(expected_quality_metrics_.front(), type); | |
| 91 expected_quality_metrics_.pop(); | |
| 92 } | |
| 93 | |
| 94 static bool IsExpectedQualityMetricsEmpty() { | |
| 95 return expected_quality_metrics_.empty(); | |
| 96 } | |
| 97 | |
| 98 static void set_expected_quality_metrics( | |
| 99 const std::queue<QualityMetricType>& expected_quality_metrics) { | |
| 100 expected_quality_metrics_ = expected_quality_metrics; | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 // TODO(isherman): I hear tell static objects are bad... | |
| 105 static std::queue<QualityMetricType> expected_quality_metrics_; | |
| 106 }; | |
| 107 std::queue<QualityMetricType> LogTracker::expected_quality_metrics_; | |
| 108 | |
| 109 class TestAutoFillManager : public AutoFillManager { | |
| 110 public: | |
| 111 TestAutoFillManager(TabContents* tab_contents, | |
| 112 TestPersonalDataManager* personal_manager) | |
| 113 : AutoFillManager(tab_contents, personal_manager), | |
| 114 autofill_enabled_(true) { | |
| 115 set_log_quality_metric_fn(&LogTracker::LogQualityMetric); | |
| 116 } | |
| 117 | |
| 118 virtual bool IsAutoFillEnabled() const { return autofill_enabled_; } | |
| 119 | |
| 120 void set_autofill_enabled(bool autofill_enabled) { | |
| 121 autofill_enabled_ = autofill_enabled; | |
| 122 } | |
| 123 | |
| 124 private: | |
| 125 bool autofill_enabled_; | |
| 126 | |
| 127 DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); | |
| 128 }; | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 class AutoFillMetricsTest : public RenderViewHostTestHarness { | |
| 133 public: | |
| 134 AutoFillMetricsTest() {} | |
| 135 virtual ~AutoFillMetricsTest() { | |
| 136 // Order of destruction is important as AutoFillManager relies on | |
| 137 // PersonalDataManager to be around when it gets destroyed. | |
| 138 autofill_manager_.reset(NULL); | |
| 139 test_personal_data_ = NULL; | |
| 140 } | |
| 141 | |
| 142 virtual void SetUp() { | |
| 143 RenderViewHostTestHarness::SetUp(); | |
| 144 test_personal_data_ = new TestPersonalDataManager(); | |
| 145 autofill_manager_.reset(new TestAutoFillManager(contents(), | |
| 146 test_personal_data_.get())); | |
| 147 } | |
| 148 | |
| 149 protected: | |
| 150 scoped_ptr<TestAutoFillManager> autofill_manager_; | |
| 151 scoped_refptr<TestPersonalDataManager> test_personal_data_; | |
| 152 | |
| 153 private: | |
| 154 DISALLOW_COPY_AND_ASSIGN(AutoFillMetricsTest); | |
| 155 }; | |
| 156 | |
| 157 // Test that we log quality metrics appropriately. | |
| 158 TEST_F(AutoFillMetricsTest, QualityMetrics) { | |
| 159 // Set up our form data. | |
| 160 FormData form; | |
| 161 form.name = ASCIIToUTF16("TestForm"); | |
| 162 form.method = ASCIIToUTF16("POST"); | |
| 163 form.origin = GURL("http://example.com/form.html"); | |
| 164 form.action = GURL("http://example.com/submit.html"); | |
| 165 form.user_submitted = true; | |
| 166 | |
| 167 FormField field; | |
| 168 autofill_test::CreateTestFormField( | |
| 169 "Autofilled", "autofilled", "Elvis Presley", "text", &field); | |
| 170 field.set_autofilled(true); | |
| 171 form.fields.push_back(field); | |
| 172 autofill_test::CreateTestFormField( | |
| 173 "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field); | |
| 174 form.fields.push_back(field); | |
| 175 autofill_test::CreateTestFormField( | |
| 176 "Empty", "empty", "", "text", &field); | |
| 177 form.fields.push_back(field); | |
| 178 autofill_test::CreateTestFormField( | |
| 179 "Unknown", "unknown", "garbage", "text", &field); | |
| 180 form.fields.push_back(field); | |
| 181 | |
| 182 // Establish our expectations. | |
| 183 std::queue<QualityMetricType> expected_quality_metrics; | |
| 184 expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); | |
| 185 expected_quality_metrics.push(autofill_metrics::FIELD_AUTOFILLED); | |
| 186 expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); | |
| 187 expected_quality_metrics.push(autofill_metrics::FIELD_AUTOFILL_FAILED); | |
| 188 expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); | |
| 189 expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); | |
| 190 LogTracker::set_expected_quality_metrics(expected_quality_metrics); | |
| 191 | |
| 192 // Simulate form submission. | |
| 193 EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form)); | |
| 194 | |
| 195 // Ensure that all our expectations were met. | |
| 196 EXPECT_TRUE(LogTracker::IsExpectedQualityMetricsEmpty()); | |
| 197 } | |
| 198 | |
| OLD | NEW |