Chromium Code Reviews| Index: components/autofill/core/browser/autofill_ukm_unittest.cc |
| diff --git a/components/autofill/core/browser/autofill_ukm_unittest.cc b/components/autofill/core/browser/autofill_ukm_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..83e7d33b1a51370e19158cb57a78f81fb40432ed |
| --- /dev/null |
| +++ b/components/autofill/core/browser/autofill_ukm_unittest.cc |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2017 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 "components/autofill/core/browser/autofill_ukm.h" |
| + |
| +#include "components/autofill/core/browser/autofill_metrics.h" |
| +#include "components/ukm/test_ukm_service.h" |
| +#include "components/ukm/ukm_source.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +namespace autofill { |
| + |
| +namespace { |
| + |
| +class AutofillUkmTest : public ::testing::Test { |
| + protected: |
| + AutofillUkmTest(){}; |
| + |
| + void SetUp() override; |
| + |
| + void TearDown() override; |
| + |
| + ukm::TestUkmService* GetTestUkmService() { |
| + return ukm_service_test_harness_.test_ukm_service(); |
| + } |
| + |
| + std::unique_ptr<AutofillUkm> autofill_ukm_; |
| + |
| + private: |
| + ukm::UkmServiceTestingHarness ukm_service_test_harness_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AutofillUkmTest); |
| +}; |
| + |
| +void AutofillUkmTest::SetUp() { |
| + autofill_ukm_.reset(new AutofillUkm(GetTestUkmService())); |
|
Mathieu
2017/03/08 22:33:49
you can inline SetUp and TearDown above
|
| +} |
| + |
| +void AutofillUkmTest::TearDown() { |
| + autofill_ukm_.reset(); |
| +} |
| + |
| +} // namespace |
| + |
| +// Tests that logging a card upload decision UKM works as expected. |
| +TEST_F(AutofillUkmTest, RecordCardUploadDecisionMetric) { |
| + GURL url("https://www.google.com"); |
| + autofill_ukm_->SetCardUploadDecisionMetricUrl(url); |
| + |
| + ASSERT_EQ(0U, GetTestUkmService()->sources_count()); |
| + |
| + autofill_ukm_->LogCardUploadDecisionMetric( |
| + AutofillMetrics::UPLOAD_NOT_OFFERED_NO_CVC); |
| + |
| + ASSERT_EQ(1U, GetTestUkmService()->sources_count()); |
| + EXPECT_EQ(url.spec(), GetTestUkmService()->GetSource(0)->url().spec()); |
| +} |
| + |
| +// Tests that only the most recent URL is used when logging the card upload |
| +// decision UKM. |
| +TEST_F(AutofillUkmTest, RecordCardUploadDecisionMetric_ChangeUrl) { |
| + GURL url1("https://www.google.com"); |
| + GURL url2("https://www.gmail.com"); |
| + autofill_ukm_->SetCardUploadDecisionMetricUrl(url1); |
| + autofill_ukm_->SetCardUploadDecisionMetricUrl(url2); |
| + |
| + autofill_ukm_->LogCardUploadDecisionMetric( |
| + AutofillMetrics::UPLOAD_NOT_OFFERED_NO_CVC); |
| + |
| + ASSERT_EQ(1U, GetTestUkmService()->sources_count()); |
| + EXPECT_EQ(url2.spec(), GetTestUkmService()->GetSource(0)->url().spec()); |
| +} |
| + |
| +// Tests that no card upload decision UKM is logged when the URL is not set. |
| +TEST_F(AutofillUkmTest, RecordCardUploadDecisionMetric_NoUrl) { |
| + autofill_ukm_->LogCardUploadDecisionMetric( |
| + AutofillMetrics::UPLOAD_NOT_OFFERED_NO_CVC); |
| + |
| + ASSERT_EQ(0U, GetTestUkmService()->sources_count()); |
| +} |
| + |
| +// Tests that no card upload decision UKM is set if the decision is not valid. |
| +TEST_F(AutofillUkmTest, RecordCardUploadDecisionMetric_InvalidDecision) { |
| + GURL url("https://www.google.com"); |
| + autofill_ukm_->SetCardUploadDecisionMetricUrl(url); |
| + autofill_ukm_->LogCardUploadDecisionMetric( |
| + AutofillMetrics::NUM_CARD_UPLOAD_DECISION_METRICS); |
| + |
| + ASSERT_EQ(0U, GetTestUkmService()->sources_count()); |
| +} |
| + |
| +} // namespace autofill |