Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: components/autofill/core/browser/autofill_metrics_unittest.cc

Issue 2740633002: [Autofill] Add upstreaming UKM (Closed)
Patch Set: Added tests Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/autofill/core/browser/autofill_metrics.h" 5 #include "components/autofill/core/browser/autofill_metrics.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <map>
9 #include <memory> 10 #include <memory>
10 #include <vector> 11 #include <vector>
11 12
13 #include "base/feature_list.h"
12 #include "base/macros.h" 14 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/metrics_hashes.h"
14 #include "base/run_loop.h" 17 #include "base/run_loop.h"
15 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
17 #include "base/test/histogram_tester.h" 20 #include "base/test/histogram_tester.h"
21 #include "base/test/scoped_feature_list.h"
18 #include "base/test/user_action_tester.h" 22 #include "base/test/user_action_tester.h"
19 #include "base/time/time.h" 23 #include "base/time/time.h"
24 #include "components/autofill/core/browser/autofill_experiments.h"
20 #include "components/autofill/core/browser/autofill_external_delegate.h" 25 #include "components/autofill/core/browser/autofill_external_delegate.h"
21 #include "components/autofill/core/browser/autofill_manager.h" 26 #include "components/autofill/core/browser/autofill_manager.h"
22 #include "components/autofill/core/browser/autofill_test_utils.h" 27 #include "components/autofill/core/browser/autofill_test_utils.h"
23 #include "components/autofill/core/browser/payments/payments_client.h" 28 #include "components/autofill/core/browser/payments/payments_client.h"
24 #include "components/autofill/core/browser/personal_data_manager.h" 29 #include "components/autofill/core/browser/personal_data_manager.h"
25 #include "components/autofill/core/browser/popup_item_ids.h" 30 #include "components/autofill/core/browser/popup_item_ids.h"
26 #include "components/autofill/core/browser/test_autofill_client.h" 31 #include "components/autofill/core/browser/test_autofill_client.h"
27 #include "components/autofill/core/browser/test_autofill_driver.h" 32 #include "components/autofill/core/browser/test_autofill_driver.h"
28 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" 33 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
29 #include "components/autofill/core/common/form_data.h" 34 #include "components/autofill/core/common/form_data.h"
30 #include "components/autofill/core/common/form_field_data.h" 35 #include "components/autofill/core/common/form_field_data.h"
36 #include "components/metrics/proto/ukm/entry.pb.h"
31 #include "components/prefs/pref_service.h" 37 #include "components/prefs/pref_service.h"
32 #include "components/rappor/test_rappor_service.h" 38 #include "components/rappor/test_rappor_service.h"
33 #include "components/signin/core/browser/account_tracker_service.h" 39 #include "components/signin/core/browser/account_tracker_service.h"
34 #include "components/signin/core/browser/fake_signin_manager.h" 40 #include "components/signin/core/browser/fake_signin_manager.h"
35 #include "components/signin/core/browser/test_signin_client.h" 41 #include "components/signin/core/browser/test_signin_client.h"
36 #include "components/signin/core/common/signin_pref_names.h" 42 #include "components/signin/core/common/signin_pref_names.h"
43 #include "components/ukm/test_ukm_service.h"
44 #include "components/ukm/ukm_entry.h"
45 #include "components/ukm/ukm_source.h"
37 #include "components/webdata/common/web_data_results.h" 46 #include "components/webdata/common/web_data_results.h"
38 #include "testing/gmock/include/gmock/gmock.h" 47 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h" 48 #include "testing/gtest/include/gtest/gtest.h"
40 #include "ui/gfx/geometry/rect.h" 49 #include "ui/gfx/geometry/rect.h"
41 #include "url/gurl.h" 50 #include "url/gurl.h"
42 51
43 using base::ASCIIToUTF16; 52 using base::ASCIIToUTF16;
44 using base::Bucket; 53 using base::Bucket;
45 using base::TimeTicks; 54 using base::TimeTicks;
46 using rappor::TestRapporServiceImpl; 55 using rappor::TestRapporServiceImpl;
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 observed_submission); 298 observed_submission);
290 } 299 }
291 300
292 private: 301 private:
293 bool autofill_enabled_; 302 bool autofill_enabled_;
294 std::unique_ptr<base::RunLoop> run_loop_; 303 std::unique_ptr<base::RunLoop> run_loop_;
295 304
296 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); 305 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
297 }; 306 };
298 307
308 // Finds the specified UKM metric by |name| in the specified UKM |metrics|.
309 const ukm::Entry_Metric* FindMetric(
310 const char* name,
311 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) {
312 for (const auto& metric : metrics) {
313 if (metric.metric_hash() == base::HashMetricName(name))
314 return &metric;
315 }
316 return nullptr;
317 }
318
299 } // namespace 319 } // namespace
300 320
301 // This is defined in the autofill_metrics.cc implementation file. 321 // This is defined in the autofill_metrics.cc implementation file.
302 int GetFieldTypeGroupMetric(ServerFieldType field_type, 322 int GetFieldTypeGroupMetric(ServerFieldType field_type,
303 AutofillMetrics::FieldTypeQualityMetric metric); 323 AutofillMetrics::FieldTypeQualityMetric metric);
304 324
305 class AutofillMetricsTest : public testing::Test { 325 class AutofillMetricsTest : public testing::Test {
306 public: 326 public:
307 ~AutofillMetricsTest() override; 327 ~AutofillMetricsTest() override;
308 328
309 void SetUp() override; 329 void SetUp() override;
310 void TearDown() override; 330 void TearDown() override;
311 331
312 protected: 332 protected:
313 void EnableWalletSync(); 333 void EnableWalletSync();
334 void EnableUkmLogging();
314 335
315 base::MessageLoop message_loop_; 336 base::MessageLoop message_loop_;
316 TestAutofillClient autofill_client_; 337 TestAutofillClient autofill_client_;
317 std::unique_ptr<AccountTrackerService> account_tracker_; 338 std::unique_ptr<AccountTrackerService> account_tracker_;
318 std::unique_ptr<FakeSigninManagerBase> signin_manager_; 339 std::unique_ptr<FakeSigninManagerBase> signin_manager_;
319 std::unique_ptr<TestSigninClient> signin_client_; 340 std::unique_ptr<TestSigninClient> signin_client_;
320 std::unique_ptr<TestAutofillDriver> autofill_driver_; 341 std::unique_ptr<TestAutofillDriver> autofill_driver_;
321 std::unique_ptr<TestAutofillManager> autofill_manager_; 342 std::unique_ptr<TestAutofillManager> autofill_manager_;
322 std::unique_ptr<TestPersonalDataManager> personal_data_; 343 std::unique_ptr<TestPersonalDataManager> personal_data_;
323 std::unique_ptr<AutofillExternalDelegate> external_delegate_; 344 std::unique_ptr<AutofillExternalDelegate> external_delegate_;
345 base::test::ScopedFeatureList scoped_feature_list_;
324 }; 346 };
325 347
326 AutofillMetricsTest::~AutofillMetricsTest() { 348 AutofillMetricsTest::~AutofillMetricsTest() {
327 // Order of destruction is important as AutofillManager relies on 349 // Order of destruction is important as AutofillManager relies on
328 // PersonalDataManager to be around when it gets destroyed. 350 // PersonalDataManager to be around when it gets destroyed.
329 autofill_manager_.reset(); 351 autofill_manager_.reset();
330 } 352 }
331 353
332 void AutofillMetricsTest::SetUp() { 354 void AutofillMetricsTest::SetUp() {
333 autofill_client_.SetPrefs(test::PrefServiceForTesting()); 355 autofill_client_.SetPrefs(test::PrefServiceForTesting());
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 account_tracker_->Shutdown(); 392 account_tracker_->Shutdown();
371 account_tracker_.reset(); 393 account_tracker_.reset();
372 signin_client_.reset(); 394 signin_client_.reset();
373 test::ReenableSystemServices(); 395 test::ReenableSystemServices();
374 } 396 }
375 397
376 void AutofillMetricsTest::EnableWalletSync() { 398 void AutofillMetricsTest::EnableWalletSync() {
377 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com"); 399 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com");
378 } 400 }
379 401
402 void AutofillMetricsTest::EnableUkmLogging() {
403 scoped_feature_list_.InitAndEnableFeature(kAutofillUkmLogging);
404 }
405
380 // Test that we log quality metrics appropriately. 406 // Test that we log quality metrics appropriately.
381 TEST_F(AutofillMetricsTest, QualityMetrics) { 407 TEST_F(AutofillMetricsTest, QualityMetrics) {
382 // Set up our form data. 408 // Set up our form data.
383 FormData form; 409 FormData form;
384 form.name = ASCIIToUTF16("TestForm"); 410 form.name = ASCIIToUTF16("TestForm");
385 form.origin = GURL("http://example.com/form.html"); 411 form.origin = GURL("http://example.com/form.html");
386 form.action = GURL("http://example.com/submit.html"); 412 form.action = GURL("http://example.com/submit.html");
387 413
388 std::vector<ServerFieldType> heuristic_types, server_types; 414 std::vector<ServerFieldType> heuristic_types, server_types;
389 FormFieldData field; 415 FormFieldData field;
(...skipping 3943 matching lines...) Expand 10 before | Expand all | Expand 10 after
4333 "Autofill.FormEvents.CreditCard", 4359 "Autofill.FormEvents.CreditCard",
4334 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1); 4360 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1);
4335 // Check that the nonsecure histogram was not recorded. ExpectBucketCount() 4361 // Check that the nonsecure histogram was not recorded. ExpectBucketCount()
4336 // can't be used here because it expects the histogram to exist. 4362 // can't be used here because it expects the histogram to exist.
4337 EXPECT_EQ( 4363 EXPECT_EQ(
4338 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard") 4364 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard")
4339 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]); 4365 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]);
4340 } 4366 }
4341 } 4367 }
4342 4368
4369 // Tests that logging a UKM works as expected.
4370 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) {
4371 EnableUkmLogging();
4372 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4373 GURL url("https://www.google.com");
4374 int upload_decision = 1;
4375 std::map<std::string, int> metrics;
4376 metrics.insert(std::make_pair(internal::kUKMCardUploadDecisionMetricName,
4377 upload_decision));
4378
4379 EXPECT_TRUE(AutofillMetrics::LogUkm(
4380 ukm_service_test_harness.test_ukm_service(), url,
4381 internal::kUKMCardUploadDecisionEntryName, metrics));
4382
4383 // Make sure that the UKM was logged correctly.
4384 ukm::TestUkmService* ukm_service =
4385 ukm_service_test_harness.test_ukm_service();
4386
4387 ASSERT_EQ(1U, ukm_service->sources_count());
4388 const ukm::UkmSource* source = ukm_service->GetSource(0);
4389 EXPECT_EQ(url.spec(), source->url().spec());
4390
4391 EXPECT_EQ(1U, ukm_service->entries_count());
4392 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
4393 EXPECT_EQ(entry->source_id(), source->id());
4394
4395 // Make sure that an card upload decision entry was logged.
4396 ukm::Entry entry_proto;
4397 entry->PopulateProto(&entry_proto);
4398 EXPECT_EQ(entry_proto.source_id(), source->id());
4399 EXPECT_EQ(entry_proto.event_hash(),
4400 base::HashMetricName(internal::kUKMCardUploadDecisionEntryName));
4401 EXPECT_EQ(entry_proto.metrics_size(), 1);
4402
4403 // Make sure that the correct upload decision was logged.
4404 const ukm::Entry_Metric* metric = FindMetric(
4405 internal::kUKMCardUploadDecisionMetricName, entry_proto.metrics());
4406 ASSERT_NE(nullptr, metric);
4407 EXPECT_EQ(upload_decision, metric->value());
4408 }
4409
4410 // Tests that no UKM is logged when the URL is not valid.
4411 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) {
4412 EnableUkmLogging();
4413 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4414 GURL url("");
4415 std::map<std::string, int> metrics;
4416 metrics.insert(std::make_pair("metric", 1));
4417
4418 EXPECT_FALSE(AutofillMetrics::LogUkm(
4419 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4420 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4421 }
4422
4423 // Tests that no UKM is logged when the metrics map is empty.
4424 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) {
4425 EnableUkmLogging();
4426 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4427 GURL url("https://www.google.com");
4428 std::map<std::string, int> metrics;
4429
4430 EXPECT_FALSE(AutofillMetrics::LogUkm(
4431 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4432 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4433 }
4434
4435 // Tests that no UKM is logged when the ukm service is null.
4436 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) {
4437 EnableUkmLogging();
4438 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4439 GURL url("https://www.google.com");
4440 std::map<std::string, int> metrics;
4441 metrics.insert(std::make_pair("metric", 1));
4442
4443 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics));
4444 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4445 }
4446
4447 // Tests that no UKM is logged when the ukm logging feature is disabled.
4448 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) {
4449 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4450 GURL url("https://www.google.com");
4451 std::map<std::string, int> metrics;
4452 metrics.insert(std::make_pair("metric", 1));
4453
4454 EXPECT_FALSE(AutofillMetrics::LogUkm(
4455 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4456 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4457 }
4458
4343 } // namespace autofill 4459 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698