Chromium Code Reviews| Index: chrome/browser/autofill/autofill_metrics_unittest.cc |
| diff --git a/chrome/browser/autofill/autofill_metrics_unittest.cc b/chrome/browser/autofill/autofill_metrics_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4ffd9216f09442818421fd547f8eca836010e64f |
| --- /dev/null |
| +++ b/chrome/browser/autofill/autofill_metrics_unittest.cc |
| @@ -0,0 +1,198 @@ |
| +// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <queue> |
| + |
| +#include "base/ref_counted.h" |
| +#include "base/scoped_ptr.h" |
| +#include "base/string16.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/autofill/autofill_common_test.h" |
| +#include "chrome/browser/autofill/autofill_manager.h" |
| +#include "chrome/browser/autofill/autofill_metrics.h" |
| +#include "chrome/browser/autofill/personal_data_manager.h" |
| +#include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| +#include "chrome/browser/tab_contents/test_tab_contents.h" |
| +#include "webkit/glue/form_data.h" |
| +#include "webkit/glue/form_field.h" |
| + |
| +using autofill_metrics::QualityMetricType; |
| +using webkit_glue::FormData; |
| +using webkit_glue::FormField; |
| + |
| +namespace { |
| + |
| +// TODO(isherman): Move this into autofill_common_test.h? |
| +class TestPersonalDataManager : public PersonalDataManager { |
| + public: |
| + TestPersonalDataManager() { |
| + CreateTestAutoFillProfiles(&web_profiles_); |
| + CreateTestCreditCards(&credit_cards_); |
| + } |
| + |
| + virtual void InitializeIfNeeded() {} |
| + virtual void SaveImportedFormData() {} |
| + virtual bool IsDataLoaded() const { return true; } |
| + |
| + private: |
| + void CreateTestAutoFillProfiles(ScopedVector<AutoFillProfile>* profiles) { |
| + AutoFillProfile* profile = new AutoFillProfile; |
| + autofill_test::SetProfileInfo(profile, "Home", "Elvis", "Aaron", |
| + "Presley", "theking@gmail.com", "RCA", |
| + "3734 Elvis Presley Blvd.", "Apt. 10", |
| + "Memphis", "Tennessee", "38116", "USA", |
| + "12345678901", ""); |
| + profile->set_guid("00000000-0000-0000-0000-000000000001"); |
| + profiles->push_back(profile); |
| + profile = new AutoFillProfile; |
| + autofill_test::SetProfileInfo(profile, "Work", "Charles", "Hardin", |
| + "Holley", "buddy@gmail.com", "Decca", |
| + "123 Apple St.", "unit 6", "Lubbock", |
| + "Texas", "79401", "USA", "23456789012", |
| + ""); |
| + profile->set_guid("00000000-0000-0000-0000-000000000002"); |
| + profiles->push_back(profile); |
| + profile = new AutoFillProfile; |
| + autofill_test::SetProfileInfo(profile, "Empty", "", "", "", "", "", "", "", |
| + "", "", "", "", "", ""); |
| + profile->set_guid("00000000-0000-0000-0000-000000000003"); |
| + profiles->push_back(profile); |
| + } |
| + |
| + void CreateTestCreditCards(ScopedVector<CreditCard>* credit_cards) { |
| + CreditCard* credit_card = new CreditCard; |
| + autofill_test::SetCreditCardInfo(credit_card, "First", "Elvis Presley", |
| + "4234567890123456", // Visa |
| + "04", "2012"); |
| + credit_card->set_guid("00000000-0000-0000-0000-000000000004"); |
| + credit_cards->push_back(credit_card); |
| + credit_card = new CreditCard; |
| + autofill_test::SetCreditCardInfo(credit_card, "Second", "Buddy Holly", |
| + "5187654321098765", // Mastercard |
| + "10", "2014"); |
| + credit_card->set_guid("00000000-0000-0000-0000-000000000005"); |
| + credit_cards->push_back(credit_card); |
| + credit_card = new CreditCard; |
| + autofill_test::SetCreditCardInfo(credit_card, "Empty", "", "", "", ""); |
| + credit_card->set_guid("00000000-0000-0000-0000-000000000006"); |
| + credit_cards->push_back(credit_card); |
| + } |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestPersonalDataManager); |
| +}; |
| + |
| +// 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
|
| +class LogTracker { |
| + public: |
| + static void LogQualityMetric(QualityMetricType type) { |
| + ASSERT_TRUE(!expected_quality_metrics_.empty()); |
| + EXPECT_EQ(expected_quality_metrics_.front(), type); |
| + expected_quality_metrics_.pop(); |
| + } |
| + |
| + static bool IsExpectedQualityMetricsEmpty() { |
| + return expected_quality_metrics_.empty(); |
| + } |
| + |
| + static void set_expected_quality_metrics( |
| + const std::queue<QualityMetricType>& expected_quality_metrics) { |
| + expected_quality_metrics_ = expected_quality_metrics; |
| + } |
| + |
| + private: |
| + // TODO(isherman): I hear tell static objects are bad... |
| + static std::queue<QualityMetricType> expected_quality_metrics_; |
| +}; |
| +std::queue<QualityMetricType> LogTracker::expected_quality_metrics_; |
| + |
| +class TestAutoFillManager : public AutoFillManager { |
| + public: |
| + TestAutoFillManager(TabContents* tab_contents, |
| + TestPersonalDataManager* personal_manager) |
| + : AutoFillManager(tab_contents, personal_manager), |
| + autofill_enabled_(true) { |
| + set_log_quality_metric_fn(&LogTracker::LogQualityMetric); |
| + } |
| + |
| + virtual bool IsAutoFillEnabled() const { return autofill_enabled_; } |
| + |
| + void set_autofill_enabled(bool autofill_enabled) { |
| + autofill_enabled_ = autofill_enabled; |
| + } |
| + |
| + private: |
| + bool autofill_enabled_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestAutoFillManager); |
| +}; |
| + |
| +} // namespace |
| + |
| +class AutoFillMetricsTest : public RenderViewHostTestHarness { |
| + public: |
| + AutoFillMetricsTest() {} |
| + virtual ~AutoFillMetricsTest() { |
| + // Order of destruction is important as AutoFillManager relies on |
| + // PersonalDataManager to be around when it gets destroyed. |
| + autofill_manager_.reset(NULL); |
| + test_personal_data_ = NULL; |
| + } |
| + |
| + virtual void SetUp() { |
| + RenderViewHostTestHarness::SetUp(); |
| + test_personal_data_ = new TestPersonalDataManager(); |
| + autofill_manager_.reset(new TestAutoFillManager(contents(), |
| + test_personal_data_.get())); |
| + } |
| + |
| + protected: |
| + scoped_ptr<TestAutoFillManager> autofill_manager_; |
| + scoped_refptr<TestPersonalDataManager> test_personal_data_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(AutoFillMetricsTest); |
| +}; |
| + |
| +// Test that we log quality metrics appropriately. |
| +TEST_F(AutoFillMetricsTest, QualityMetrics) { |
| + // Set up our form data. |
| + FormData form; |
| + form.name = ASCIIToUTF16("TestForm"); |
| + form.method = ASCIIToUTF16("POST"); |
| + form.origin = GURL("http://example.com/form.html"); |
| + form.action = GURL("http://example.com/submit.html"); |
| + form.user_submitted = true; |
| + |
| + FormField field; |
| + autofill_test::CreateTestFormField( |
| + "Autofilled", "autofilled", "Elvis Presley", "text", &field); |
| + field.set_autofilled(true); |
| + form.fields.push_back(field); |
| + autofill_test::CreateTestFormField( |
| + "Autofill Failed", "autofillfailed", "buddy@gmail.com", "text", &field); |
| + form.fields.push_back(field); |
| + autofill_test::CreateTestFormField( |
| + "Empty", "empty", "", "text", &field); |
| + form.fields.push_back(field); |
| + autofill_test::CreateTestFormField( |
| + "Unknown", "unknown", "garbage", "text", &field); |
| + form.fields.push_back(field); |
| + |
| + // Establish our expectations. |
| + std::queue<QualityMetricType> expected_quality_metrics; |
| + expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); |
| + expected_quality_metrics.push(autofill_metrics::FIELD_AUTOFILLED); |
| + expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); |
| + expected_quality_metrics.push(autofill_metrics::FIELD_AUTOFILL_FAILED); |
| + expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); |
| + expected_quality_metrics.push(autofill_metrics::FIELD_SUBMITTED); |
| + LogTracker::set_expected_quality_metrics(expected_quality_metrics); |
| + |
| + // Simulate form submission. |
| + EXPECT_NO_FATAL_FAILURE(autofill_manager_->FormSubmitted(form)); |
| + |
| + // Ensure that all our expectations were met. |
| + EXPECT_TRUE(LogTracker::IsExpectedQualityMetricsEmpty()); |
| +} |
| + |