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

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

Issue 2776223002: Adds UKM for autofill attributes in form_structure. (Closed)
Patch Set: Uses vector of pairs instead of map for metrics. Created 3 years, 8 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>
10 #include <memory> 9 #include <memory>
10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/feature_list.h" 13 #include "base/feature_list.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/metrics/metrics_hashes.h" 16 #include "base/metrics/metrics_hashes.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/strings/string16.h" 18 #include "base/strings/string16.h"
19 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
20 #include "base/test/histogram_tester.h" 20 #include "base/test/histogram_tester.h"
(...skipping 26 matching lines...) Expand all
47 #include "testing/gmock/include/gmock/gmock.h" 47 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h" 48 #include "testing/gtest/include/gtest/gtest.h"
49 #include "ui/gfx/geometry/rect.h" 49 #include "ui/gfx/geometry/rect.h"
50 #include "url/gurl.h" 50 #include "url/gurl.h"
51 51
52 using base::ASCIIToUTF16; 52 using base::ASCIIToUTF16;
53 using base::Bucket; 53 using base::Bucket;
54 using base::TimeTicks; 54 using base::TimeTicks;
55 using rappor::TestRapporServiceImpl; 55 using rappor::TestRapporServiceImpl;
56 using ::testing::ElementsAre; 56 using ::testing::ElementsAre;
57 using ::testing::UnorderedElementsAre;
57 58
58 namespace autofill { 59 namespace autofill {
59 namespace { 60 namespace {
60 61
61 class TestPersonalDataManager : public PersonalDataManager { 62 class TestPersonalDataManager : public PersonalDataManager {
62 public: 63 public:
63 TestPersonalDataManager() 64 TestPersonalDataManager()
64 : PersonalDataManager("en-US"), 65 : PersonalDataManager("en-US"),
65 autofill_enabled_(true) { 66 autofill_enabled_(true) {
66 CreateTestAutofillProfiles(&web_profiles_); 67 CreateTestAutofillProfiles(&web_profiles_);
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 const ukm::Entry_Metric* FindMetric( 310 const ukm::Entry_Metric* FindMetric(
310 const char* name, 311 const char* name,
311 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) { 312 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) {
312 for (const auto& metric : metrics) { 313 for (const auto& metric : metrics) {
313 if (metric.metric_hash() == base::HashMetricName(name)) 314 if (metric.metric_hash() == base::HashMetricName(name))
314 return &metric; 315 return &metric;
315 } 316 }
316 return nullptr; 317 return nullptr;
317 } 318 }
318 319
320 // Finds all the UKM values for |name| in the specified |metrics|.
321 const std::vector<int64_t> FindMetricValues(
322 const char* name,
323 const google::protobuf::RepeatedPtrField<ukm::Entry_Metric>& metrics) {
324 std::vector<int64_t> values;
325 for (const auto& metric : metrics) {
326 if (metric.metric_hash() == base::HashMetricName(name))
327 values.push_back(metric.value());
328 }
329 return values;
330 }
331
319 } // namespace 332 } // namespace
320 333
321 // This is defined in the autofill_metrics.cc implementation file. 334 // This is defined in the autofill_metrics.cc implementation file.
322 int GetFieldTypeGroupMetric(ServerFieldType field_type, 335 int GetFieldTypeGroupMetric(ServerFieldType field_type,
323 AutofillMetrics::FieldTypeQualityMetric metric); 336 AutofillMetrics::FieldTypeQualityMetric metric);
324 337
325 class AutofillMetricsTest : public testing::Test { 338 class AutofillMetricsTest : public testing::Test {
326 public: 339 public:
327 ~AutofillMetricsTest() override; 340 ~AutofillMetricsTest() override;
328 341
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 account_tracker_.reset(); 406 account_tracker_.reset();
394 signin_client_.reset(); 407 signin_client_.reset();
395 test::ReenableSystemServices(); 408 test::ReenableSystemServices();
396 } 409 }
397 410
398 void AutofillMetricsTest::EnableWalletSync() { 411 void AutofillMetricsTest::EnableWalletSync() {
399 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com"); 412 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com");
400 } 413 }
401 414
402 void AutofillMetricsTest::EnableUkmLogging() { 415 void AutofillMetricsTest::EnableUkmLogging() {
403 scoped_feature_list_.InitAndEnableFeature(kAutofillUkmLogging); 416 scoped_feature_list_.InitAndEnableFeature(kAutofillUkmLogging);
Mathieu 2017/03/27 23:59:31 Sorry that I didn't catch this earlier, but you mi
csashi 2017/03/28 00:14:24 Acknowledged. I remembered that you told me to add
404 } 417 }
405 418
406 // Test that we log quality metrics appropriately. 419 // Test that we log quality metrics appropriately.
407 TEST_F(AutofillMetricsTest, QualityMetrics) { 420 TEST_F(AutofillMetricsTest, QualityMetrics) {
408 // Set up our form data. 421 // Set up our form data.
409 FormData form; 422 FormData form;
410 form.name = ASCIIToUTF16("TestForm"); 423 form.name = ASCIIToUTF16("TestForm");
411 form.origin = GURL("http://example.com/form.html"); 424 form.origin = GURL("http://example.com/form.html");
412 form.action = GURL("http://example.com/submit.html"); 425 form.action = GURL("http://example.com/submit.html");
413 426
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 form.fields.push_back(field); 772 form.fields.push_back(field);
760 773
761 // No autocomplete attribute. No metric logged. 774 // No autocomplete attribute. No metric logged.
762 test::CreateTestFormField("Address", "address", "", "text", &field); 775 test::CreateTestFormField("Address", "address", "", "text", &field);
763 field.autocomplete_attribute = ""; 776 field.autocomplete_attribute = "";
764 form.fields.push_back(field); 777 form.fields.push_back(field);
765 778
766 std::unique_ptr<TestFormStructure> form_structure = 779 std::unique_ptr<TestFormStructure> form_structure =
767 base::MakeUnique<TestFormStructure>(form); 780 base::MakeUnique<TestFormStructure>(form);
768 TestFormStructure* form_structure_ptr = form_structure.get(); 781 TestFormStructure* form_structure_ptr = form_structure.get();
769 form_structure->DetermineHeuristicTypes(); 782 form_structure->DetermineHeuristicTypes(nullptr /* ukm_service */);
770 autofill_manager_->form_structures()->push_back(std::move(form_structure)); 783 autofill_manager_->form_structures()->push_back(std::move(form_structure));
771 784
772 AutofillQueryResponseContents response; 785 AutofillQueryResponseContents response;
773 // Server response will match with autocomplete. 786 // Server response will match with autocomplete.
774 response.add_field()->set_autofill_type(NAME_LAST); 787 response.add_field()->set_autofill_type(NAME_LAST);
775 // Server response will NOT match with autocomplete. 788 // Server response will NOT match with autocomplete.
776 response.add_field()->set_autofill_type(NAME_FIRST); 789 response.add_field()->set_autofill_type(NAME_FIRST);
777 // Server response will have no data. 790 // Server response will have no data.
778 response.add_field()->set_autofill_type(NO_SERVER_DATA); 791 response.add_field()->set_autofill_type(NO_SERVER_DATA);
779 // Not logged. 792 // Not logged.
(...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 autofill_manager_->RunRunLoop(); 1440 autofill_manager_->RunRunLoop();
1428 1441
1429 // An autofillable form was uploaded, and the number of edited autofilled 1442 // An autofillable form was uploaded, and the number of edited autofilled
1430 // fields is logged. 1443 // fields is logged.
1431 histogram_tester.ExpectUniqueSample( 1444 histogram_tester.ExpectUniqueSample(
1432 "Autofill.NumberOfEditedAutofilledFieldsAtSubmission.NoSubmission", 1, 1); 1445 "Autofill.NumberOfEditedAutofilledFieldsAtSubmission.NoSubmission", 1, 1);
1433 } 1446 }
1434 1447
1435 // Verify that we correctly log metrics regarding developer engagement. 1448 // Verify that we correctly log metrics regarding developer engagement.
1436 TEST_F(AutofillMetricsTest, DeveloperEngagement) { 1449 TEST_F(AutofillMetricsTest, DeveloperEngagement) {
1450 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1451
1437 // Start with a non-fillable form. 1452 // Start with a non-fillable form.
1438 FormData form; 1453 FormData form;
1439 form.name = ASCIIToUTF16("TestForm"); 1454 form.name = ASCIIToUTF16("TestForm");
1440 form.origin = GURL("http://example.com/form.html"); 1455 form.origin = GURL("http://example.com/form.html");
1441 form.action = GURL("http://example.com/submit.html"); 1456 form.action = GURL("http://example.com/submit.html");
1442 1457
1443 FormFieldData field; 1458 FormFieldData field;
1444 test::CreateTestFormField("Name", "name", "", "text", &field); 1459 test::CreateTestFormField("Name", "name", "", "text", &field);
1445 form.fields.push_back(field); 1460 form.fields.push_back(field);
1446 test::CreateTestFormField("Email", "email", "", "text", &field); 1461 test::CreateTestFormField("Email", "email", "", "text", &field);
1447 form.fields.push_back(field); 1462 form.fields.push_back(field);
1448 1463
1449 std::vector<FormData> forms(1, form); 1464 std::vector<FormData> forms(1, form);
1450 1465
1451 // Ensure no metrics are logged when loading a non-fillable form. 1466 // Ensure no metrics are logged when loading a non-fillable form.
1452 { 1467 {
1453 base::HistogramTester histogram_tester; 1468 base::HistogramTester histogram_tester;
1454 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1469 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1455 autofill_manager_->Reset(); 1470 autofill_manager_->Reset();
1456 histogram_tester.ExpectTotalCount("Autofill.DeveloperEngagement", 0); 1471 histogram_tester.ExpectTotalCount("Autofill.DeveloperEngagement", 0);
1472
1473 // UKM must not be logged unless enabled.
1474 EXPECT_EQ(0U, ukm_service->sources_count());
1475 EXPECT_EQ(0U, ukm_service->entries_count());
1457 } 1476 }
1458 1477
1459 // Add another field to the form, so that it becomes fillable. 1478 // Add another field to the form, so that it becomes fillable.
1460 test::CreateTestFormField("Phone", "phone", "", "text", &field); 1479 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1461 forms.back().fields.push_back(field); 1480 forms.back().fields.push_back(field);
1462 1481
1463 // Expect only the "form parsed" metric to be logged; no metrics about 1482 // Expect only the "form parsed" metric to be logged; no metrics about
1464 // author-specified field type hints. 1483 // author-specified field type hints.
1465 { 1484 {
1466 base::HistogramTester histogram_tester; 1485 base::HistogramTester histogram_tester;
1467 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1486 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1468 autofill_manager_->Reset(); 1487 autofill_manager_->Reset();
1469 histogram_tester.ExpectUniqueSample("Autofill.DeveloperEngagement", 1488 histogram_tester.ExpectUniqueSample("Autofill.DeveloperEngagement",
1470 AutofillMetrics::FILLABLE_FORM_PARSED, 1489 AutofillMetrics::FILLABLE_FORM_PARSED,
1471 1); 1490 1);
1491
1492 // UKM must not be logged unless enabled.
1493 EXPECT_EQ(0U, ukm_service->sources_count());
1494 EXPECT_EQ(0U, ukm_service->entries_count());
1472 } 1495 }
1473 1496
1474 // Add some fields with an author-specified field type to the form. 1497 // Add some fields with an author-specified field type to the form.
1475 // We need to add at least three fields, because a form must have at least 1498 // We need to add at least three fields, because a form must have at least
1476 // three fillable fields to be considered to be autofillable; and if at least 1499 // three fillable fields to be considered to be autofillable; and if at least
1477 // one field specifies an explicit type hint, we don't apply any of our usual 1500 // one field specifies an explicit type hint, we don't apply any of our usual
1478 // local heuristics to detect field types in the rest of the form. 1501 // local heuristics to detect field types in the rest of the form.
1479 test::CreateTestFormField("", "", "", "text", &field); 1502 test::CreateTestFormField("", "", "", "text", &field);
1480 field.autocomplete_attribute = "given-name"; 1503 field.autocomplete_attribute = "given-name";
1481 forms.back().fields.push_back(field); 1504 forms.back().fields.push_back(field);
1482 test::CreateTestFormField("", "", "", "text", &field); 1505 test::CreateTestFormField("", "", "", "text", &field);
1483 field.autocomplete_attribute = "email"; 1506 field.autocomplete_attribute = "email";
1484 forms.back().fields.push_back(field); 1507 forms.back().fields.push_back(field);
1485 test::CreateTestFormField("", "", "", "text", &field); 1508 test::CreateTestFormField("", "", "", "text", &field);
1486 field.autocomplete_attribute = "address-line1"; 1509 field.autocomplete_attribute = "address-line1";
1487 forms.back().fields.push_back(field); 1510 forms.back().fields.push_back(field);
1488 1511
1489 // Expect both the "form parsed" metric and the author-specified field type 1512 // Expect both the "form parsed" metric and the author-specified field type
1490 // hints metric to be logged. 1513 // hints metric to be logged.
1491 { 1514 {
1492 base::HistogramTester histogram_tester; 1515 base::HistogramTester histogram_tester;
1493 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1516 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1494 autofill_manager_->Reset(); 1517 autofill_manager_->Reset();
1495 histogram_tester.ExpectBucketCount("Autofill.DeveloperEngagement", 1518 histogram_tester.ExpectBucketCount("Autofill.DeveloperEngagement",
1496 AutofillMetrics::FILLABLE_FORM_PARSED, 1519 AutofillMetrics::FILLABLE_FORM_PARSED,
1497 1); 1520 1);
1498 histogram_tester.ExpectBucketCount( 1521 histogram_tester.ExpectBucketCount(
1499 "Autofill.DeveloperEngagement", 1522 "Autofill.DeveloperEngagement",
1500 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS, 1); 1523 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS, 1);
1524
1525 // UKM must not be logged unless enabled.
1526 EXPECT_EQ(0U, ukm_service->sources_count());
1527 EXPECT_EQ(0U, ukm_service->entries_count());
1501 } 1528 }
1502 } 1529 }
1503 1530
1531 // Verify that we correctly log UKM regarding developer engagement.
1532 TEST_F(AutofillMetricsTest, UkmDeveloperEngagement_LogFillableFormParsed) {
1533 EnableUkmLogging();
1534 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1535
1536 // Start with a non-fillable form.
1537 FormData form;
1538 form.name = ASCIIToUTF16("TestForm");
1539 form.origin = GURL("http://example.com/form.html");
1540 form.action = GURL("http://example.com/submit.html");
1541
1542 FormFieldData field;
1543 test::CreateTestFormField("Name", "name", "", "text", &field);
1544 form.fields.push_back(field);
1545 test::CreateTestFormField("Email", "email", "", "text", &field);
1546 form.fields.push_back(field);
1547
1548 std::vector<FormData> forms(1, form);
1549
1550 // Ensure no metrics are logged when loading a non-fillable form.
1551 {
1552 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1553 autofill_manager_->Reset();
1554
1555 EXPECT_EQ(0U, ukm_service->sources_count());
1556 EXPECT_EQ(0U, ukm_service->entries_count());
1557 }
1558
1559 // Add another field to the form, so that it becomes fillable.
1560 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1561 forms.back().fields.push_back(field);
1562
1563 // Expect only the "form parsed" metric to be logged; no metrics about
1564 // author-specified field type hints.
1565 {
1566 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1567 autofill_manager_->Reset();
1568
1569 ASSERT_EQ(1U, ukm_service->sources_count());
1570 const ukm::UkmSource* source =
1571 ukm_service->GetSourceForUrl(form.origin.spec().c_str());
1572 ASSERT_NE(nullptr, source);
1573
1574 ASSERT_EQ(1U, ukm_service->entries_count());
1575 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
1576 EXPECT_EQ(source->id(), entry->source_id());
1577
1578 ukm::Entry entry_proto;
1579 entry->PopulateProto(&entry_proto);
1580 EXPECT_EQ(source->id(), entry_proto.source_id());
1581 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
1582 entry_proto.event_hash());
1583 EXPECT_THAT(FindMetricValues(internal::kUKMDeveloperEngagementMetricName,
1584 entry_proto.metrics()),
1585 UnorderedElementsAre(AutofillMetrics::FILLABLE_FORM_PARSED));
1586 }
1587 }
1588
1589 // Verify that we correctly log UKM regarding developer engagement.
1590 TEST_F(AutofillMetricsTest, UkmDeveloperEngagement_LogAllMetrics) {
1591 EnableUkmLogging();
1592 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1593
1594 FormData form;
1595 form.name = ASCIIToUTF16("TestForm");
1596 form.origin = GURL("http://example.com/form.html");
1597 form.action = GURL("http://example.com/submit.html");
1598
1599 FormFieldData field;
1600 test::CreateTestFormField("Name", "name", "", "text", &field);
1601 form.fields.push_back(field);
1602 test::CreateTestFormField("Email", "email", "", "text", &field);
1603 form.fields.push_back(field);
1604
1605 std::vector<FormData> forms(1, form);
1606
1607 // Add another field to the form, so that it becomes fillable.
1608 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1609 forms.back().fields.push_back(field);
1610
1611 // Add some fields with an author-specified field type to the form.
1612 // We need to add at least three fields, because a form must have at least
1613 // three fillable fields to be considered to be autofillable; and if at least
1614 // one field specifies an explicit type hint, we don't apply any of our usual
1615 // local heuristics to detect field types in the rest of the form.
1616 test::CreateTestFormField("", "", "", "text", &field);
1617 field.autocomplete_attribute = "given-name";
1618 forms.back().fields.push_back(field);
1619 test::CreateTestFormField("", "", "", "text", &field);
1620 field.autocomplete_attribute = "email";
1621 forms.back().fields.push_back(field);
1622 test::CreateTestFormField("", "", "", "text", &field);
1623 field.autocomplete_attribute = "address-line1";
1624 forms.back().fields.push_back(field);
1625
1626 // Expect both the "form parsed" metric and the author-specified field type
1627 // hints metric to be logged.
1628 {
1629 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1630 autofill_manager_->Reset();
1631
1632 ASSERT_EQ(1U, ukm_service->sources_count());
1633 const ukm::UkmSource* source =
1634 ukm_service->GetSourceForUrl(form.origin.spec().c_str());
1635 ASSERT_NE(nullptr, source);
1636
1637 ASSERT_EQ(1U, ukm_service->entries_count());
1638 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
1639 EXPECT_EQ(source->id(), entry->source_id());
1640
1641 ukm::Entry entry_proto;
1642 entry->PopulateProto(&entry_proto);
1643 EXPECT_EQ(source->id(), entry_proto.source_id());
1644 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
1645 entry_proto.event_hash());
1646 EXPECT_THAT(FindMetricValues(internal::kUKMDeveloperEngagementMetricName,
1647 entry_proto.metrics()),
1648 UnorderedElementsAre(
1649 AutofillMetrics::FILLABLE_FORM_PARSED,
1650 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS));
1651 }
1652 }
1653
1504 // Test that the profile count is logged correctly. 1654 // Test that the profile count is logged correctly.
1505 TEST_F(AutofillMetricsTest, StoredProfileCount) { 1655 TEST_F(AutofillMetricsTest, StoredProfileCount) {
1506 // The metric should be logged when the profiles are first loaded. 1656 // The metric should be logged when the profiles are first loaded.
1507 { 1657 {
1508 base::HistogramTester histogram_tester; 1658 base::HistogramTester histogram_tester;
1509 personal_data_->LoadProfiles(); 1659 personal_data_->LoadProfiles();
1510 histogram_tester.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1); 1660 histogram_tester.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1);
1511 } 1661 }
1512 1662
1513 // The metric should only be logged once. 1663 // The metric should only be logged once.
(...skipping 2845 matching lines...) Expand 10 before | Expand all | Expand 10 after
4359 "Autofill.FormEvents.CreditCard", 4509 "Autofill.FormEvents.CreditCard",
4360 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1); 4510 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1);
4361 // Check that the nonsecure histogram was not recorded. ExpectBucketCount() 4511 // Check that the nonsecure histogram was not recorded. ExpectBucketCount()
4362 // can't be used here because it expects the histogram to exist. 4512 // can't be used here because it expects the histogram to exist.
4363 EXPECT_EQ( 4513 EXPECT_EQ(
4364 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard") 4514 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard")
4365 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]); 4515 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]);
4366 } 4516 }
4367 } 4517 }
4368 4518
4369 // Tests that logging a UKM works as expected. 4519 // Tests that logging CardUploadDecision UKM works as expected.
4370 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) { 4520 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) {
4371 EnableUkmLogging(); 4521 EnableUkmLogging();
4372 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4522 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4373 GURL url("https://www.google.com"); 4523 GURL url("https://www.google.com");
4374 int upload_decision = 1; 4524 int upload_decision = 1;
4375 std::map<std::string, int> metrics; 4525 std::vector<std::pair<const char*, int>> metrics = {
4376 metrics.insert(std::make_pair(internal::kUKMCardUploadDecisionMetricName, 4526 {internal::kUKMCardUploadDecisionMetricName, upload_decision}};
4377 upload_decision));
4378 4527
4379 EXPECT_TRUE(AutofillMetrics::LogUkm( 4528 EXPECT_TRUE(AutofillMetrics::LogUkm(
4380 ukm_service_test_harness.test_ukm_service(), url, 4529 ukm_service_test_harness.test_ukm_service(), url,
4381 internal::kUKMCardUploadDecisionEntryName, metrics)); 4530 internal::kUKMCardUploadDecisionEntryName, metrics));
4382 4531
4383 // Make sure that the UKM was logged correctly. 4532 // Make sure that the UKM was logged correctly.
4384 ukm::TestUkmService* ukm_service = 4533 ukm::TestUkmService* ukm_service =
4385 ukm_service_test_harness.test_ukm_service(); 4534 ukm_service_test_harness.test_ukm_service();
4386 4535
4387 ASSERT_EQ(1U, ukm_service->sources_count()); 4536 ASSERT_EQ(1U, ukm_service->sources_count());
4388 const ukm::UkmSource* source = 4537 const ukm::UkmSource* source =
4389 ukm_service->GetSourceForUrl(url.spec().c_str()); 4538 ukm_service->GetSourceForUrl(url.spec().c_str());
4390 EXPECT_EQ(url.spec(), source->url().spec()); 4539 EXPECT_EQ(url.spec(), source->url().spec());
4391 4540
4392 EXPECT_EQ(1U, ukm_service->entries_count()); 4541 ASSERT_EQ(1U, ukm_service->entries_count());
4393 const ukm::UkmEntry* entry = ukm_service->GetEntry(0); 4542 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
4394 EXPECT_EQ(source->id(), entry->source_id()); 4543 EXPECT_EQ(source->id(), entry->source_id());
4395 4544
4396 // Make sure that an card upload decision entry was logged. 4545 // Make sure that a card upload decision entry was logged.
4397 ukm::Entry entry_proto; 4546 ukm::Entry entry_proto;
4398 entry->PopulateProto(&entry_proto); 4547 entry->PopulateProto(&entry_proto);
4399 EXPECT_EQ(source->id(), entry_proto.source_id()); 4548 EXPECT_EQ(source->id(), entry_proto.source_id());
4400 EXPECT_EQ(base::HashMetricName(internal::kUKMCardUploadDecisionEntryName), 4549 EXPECT_EQ(base::HashMetricName(internal::kUKMCardUploadDecisionEntryName),
4401 entry_proto.event_hash()); 4550 entry_proto.event_hash());
4402 EXPECT_EQ(1, entry_proto.metrics_size()); 4551 EXPECT_EQ(1, entry_proto.metrics_size());
4403 4552
4404 // Make sure that the correct upload decision was logged. 4553 // Make sure that the correct upload decision was logged.
4405 const ukm::Entry_Metric* metric = FindMetric( 4554 const ukm::Entry_Metric* metric = FindMetric(
4406 internal::kUKMCardUploadDecisionMetricName, entry_proto.metrics()); 4555 internal::kUKMCardUploadDecisionMetricName, entry_proto.metrics());
4407 ASSERT_NE(nullptr, metric); 4556 ASSERT_NE(nullptr, metric);
4408 EXPECT_EQ(upload_decision, metric->value()); 4557 EXPECT_EQ(upload_decision, metric->value());
4409 } 4558 }
4410 4559
4560 // Tests that logging DeveloperEngagement UKM works as expected.
4561 TEST_F(AutofillMetricsTest, RecordDeveloperEngagementMetric) {
4562 EnableUkmLogging();
4563 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4564 GURL url("https://www.google.com");
4565 int form_structure_metric = 1;
4566 std::vector<std::pair<const char*, int>> metrics = {
4567 {internal::kUKMDeveloperEngagementMetricName, form_structure_metric}};
4568
4569 EXPECT_TRUE(AutofillMetrics::LogUkm(
4570 ukm_service_test_harness.test_ukm_service(), url,
4571 internal::kUKMDeveloperEngagementEntryName, metrics));
4572
4573 // Make sure that the UKM was logged correctly.
4574 ukm::TestUkmService* ukm_service =
4575 ukm_service_test_harness.test_ukm_service();
4576
4577 ASSERT_EQ(1U, ukm_service->sources_count());
4578 const ukm::UkmSource* source =
4579 ukm_service->GetSourceForUrl(url.spec().c_str());
4580 EXPECT_EQ(url.spec(), source->url().spec());
4581
4582 ASSERT_EQ(1U, ukm_service->entries_count());
4583 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
4584 EXPECT_EQ(source->id(), entry->source_id());
4585
4586 // Make sure that a developer engagement entry was logged.
4587 ukm::Entry entry_proto;
4588 entry->PopulateProto(&entry_proto);
4589 EXPECT_EQ(source->id(), entry_proto.source_id());
4590 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
4591 entry_proto.event_hash());
4592 EXPECT_EQ(1, entry_proto.metrics_size());
4593
4594 // Make sure that the correct developer engagement metric was logged.
4595 const ukm::Entry_Metric* metric = FindMetric(
4596 internal::kUKMDeveloperEngagementMetricName, entry_proto.metrics());
4597 ASSERT_NE(nullptr, metric);
4598 EXPECT_EQ(form_structure_metric, metric->value());
4599 }
4600
4411 // Tests that no UKM is logged when the URL is not valid. 4601 // Tests that no UKM is logged when the URL is not valid.
4412 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) { 4602 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) {
4413 EnableUkmLogging(); 4603 EnableUkmLogging();
4414 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4604 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4415 GURL url(""); 4605 GURL url("");
4416 std::map<std::string, int> metrics; 4606 std::vector<std::pair<const char*, int>> metrics = {{"metric", 1}};
4417 metrics.insert(std::make_pair("metric", 1));
4418 4607
4419 EXPECT_FALSE(AutofillMetrics::LogUkm( 4608 EXPECT_FALSE(AutofillMetrics::LogUkm(
4420 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4609 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4421 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4610 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4422 } 4611 }
4423 4612
4424 // Tests that no UKM is logged when the metrics map is empty. 4613 // Tests that no UKM is logged when the metrics map is empty.
4425 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) { 4614 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) {
4426 EnableUkmLogging(); 4615 EnableUkmLogging();
4427 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4616 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4428 GURL url("https://www.google.com"); 4617 GURL url("https://www.google.com");
4429 std::map<std::string, int> metrics; 4618 std::vector<std::pair<const char*, int>> metrics;
4430 4619
4431 EXPECT_FALSE(AutofillMetrics::LogUkm( 4620 EXPECT_FALSE(AutofillMetrics::LogUkm(
4432 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4621 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4433 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4622 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4434 } 4623 }
4435 4624
4436 // Tests that no UKM is logged when the ukm service is null. 4625 // Tests that no UKM is logged when the ukm service is null.
4437 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) { 4626 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) {
4438 EnableUkmLogging(); 4627 EnableUkmLogging();
4439 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4628 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4440 GURL url("https://www.google.com"); 4629 GURL url("https://www.google.com");
4441 std::map<std::string, int> metrics; 4630 std::vector<std::pair<const char*, int>> metrics = {{"metric", 1}};
4442 metrics.insert(std::make_pair("metric", 1));
4443 4631
4444 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics)); 4632 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics));
4445 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4633 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4446 } 4634 }
4447 4635
4448 // Tests that no UKM is logged when the ukm logging feature is disabled. 4636 // Tests that no UKM is logged when the ukm logging feature is disabled.
4449 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) { 4637 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) {
4450 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4638 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4451 GURL url("https://www.google.com"); 4639 GURL url("https://www.google.com");
4452 std::map<std::string, int> metrics; 4640 std::vector<std::pair<const char*, int>> metrics = {{"metric", 1}};
4453 metrics.insert(std::make_pair("metric", 1));
4454 4641
4455 EXPECT_FALSE(AutofillMetrics::LogUkm( 4642 EXPECT_FALSE(AutofillMetrics::LogUkm(
4456 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4643 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4457 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4644 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4458 } 4645 }
4459 4646
4460 } // namespace autofill 4647 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_metrics.cc ('k') | components/autofill/core/browser/form_structure.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698