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

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: Resolves merge conflicts with FORM_CONTAINS_UPI_VPA_HINT. 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>
11 #include <vector> 10 #include <vector>
12 11
13 #include "base/feature_list.h" 12 #include "base/feature_list.h"
14 #include "base/macros.h" 13 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
16 #include "base/metrics/metrics_hashes.h" 15 #include "base/metrics/metrics_hashes.h"
17 #include "base/run_loop.h" 16 #include "base/run_loop.h"
18 #include "base/strings/string16.h" 17 #include "base/strings/string16.h"
19 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
(...skipping 27 matching lines...) Expand all
47 #include "testing/gmock/include/gmock/gmock.h" 46 #include "testing/gmock/include/gmock/gmock.h"
48 #include "testing/gtest/include/gtest/gtest.h" 47 #include "testing/gtest/include/gtest/gtest.h"
49 #include "ui/gfx/geometry/rect.h" 48 #include "ui/gfx/geometry/rect.h"
50 #include "url/gurl.h" 49 #include "url/gurl.h"
51 50
52 using base::ASCIIToUTF16; 51 using base::ASCIIToUTF16;
53 using base::Bucket; 52 using base::Bucket;
54 using base::TimeTicks; 53 using base::TimeTicks;
55 using rappor::TestRapporServiceImpl; 54 using rappor::TestRapporServiceImpl;
56 using ::testing::ElementsAre; 55 using ::testing::ElementsAre;
56 using ::testing::UnorderedElementsAre;
57 57
58 namespace autofill { 58 namespace autofill {
59 namespace { 59 namespace {
60 60
61 class TestPersonalDataManager : public PersonalDataManager { 61 class TestPersonalDataManager : public PersonalDataManager {
62 public: 62 public:
63 TestPersonalDataManager() 63 TestPersonalDataManager()
64 : PersonalDataManager("en-US"), 64 : PersonalDataManager("en-US"),
65 autofill_enabled_(true) { 65 autofill_enabled_(true) {
66 CreateTestAutofillProfiles(&web_profiles_); 66 CreateTestAutofillProfiles(&web_profiles_);
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 form.fields.push_back(field); 759 form.fields.push_back(field);
760 760
761 // No autocomplete attribute. No metric logged. 761 // No autocomplete attribute. No metric logged.
762 test::CreateTestFormField("Address", "address", "", "text", &field); 762 test::CreateTestFormField("Address", "address", "", "text", &field);
763 field.autocomplete_attribute = ""; 763 field.autocomplete_attribute = "";
764 form.fields.push_back(field); 764 form.fields.push_back(field);
765 765
766 std::unique_ptr<TestFormStructure> form_structure = 766 std::unique_ptr<TestFormStructure> form_structure =
767 base::MakeUnique<TestFormStructure>(form); 767 base::MakeUnique<TestFormStructure>(form);
768 TestFormStructure* form_structure_ptr = form_structure.get(); 768 TestFormStructure* form_structure_ptr = form_structure.get();
769 form_structure->DetermineHeuristicTypes(); 769 form_structure->DetermineHeuristicTypes(nullptr /* ukm_service */);
770 autofill_manager_->form_structures()->push_back(std::move(form_structure)); 770 autofill_manager_->form_structures()->push_back(std::move(form_structure));
771 771
772 AutofillQueryResponseContents response; 772 AutofillQueryResponseContents response;
773 // Server response will match with autocomplete. 773 // Server response will match with autocomplete.
774 response.add_field()->set_autofill_type(NAME_LAST); 774 response.add_field()->set_autofill_type(NAME_LAST);
775 // Server response will NOT match with autocomplete. 775 // Server response will NOT match with autocomplete.
776 response.add_field()->set_autofill_type(NAME_FIRST); 776 response.add_field()->set_autofill_type(NAME_FIRST);
777 // Server response will have no data. 777 // Server response will have no data.
778 response.add_field()->set_autofill_type(NO_SERVER_DATA); 778 response.add_field()->set_autofill_type(NO_SERVER_DATA);
779 // Not logged. 779 // Not logged.
(...skipping 647 matching lines...) Expand 10 before | Expand all | Expand 10 after
1427 autofill_manager_->RunRunLoop(); 1427 autofill_manager_->RunRunLoop();
1428 1428
1429 // An autofillable form was uploaded, and the number of edited autofilled 1429 // An autofillable form was uploaded, and the number of edited autofilled
1430 // fields is logged. 1430 // fields is logged.
1431 histogram_tester.ExpectUniqueSample( 1431 histogram_tester.ExpectUniqueSample(
1432 "Autofill.NumberOfEditedAutofilledFieldsAtSubmission.NoSubmission", 1, 1); 1432 "Autofill.NumberOfEditedAutofilledFieldsAtSubmission.NoSubmission", 1, 1);
1433 } 1433 }
1434 1434
1435 // Verify that we correctly log metrics regarding developer engagement. 1435 // Verify that we correctly log metrics regarding developer engagement.
1436 TEST_F(AutofillMetricsTest, DeveloperEngagement) { 1436 TEST_F(AutofillMetricsTest, DeveloperEngagement) {
1437 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1438
1437 // Start with a non-fillable form. 1439 // Start with a non-fillable form.
1438 FormData form; 1440 FormData form;
1439 form.name = ASCIIToUTF16("TestForm"); 1441 form.name = ASCIIToUTF16("TestForm");
1440 form.origin = GURL("http://example.com/form.html"); 1442 form.origin = GURL("http://example.com/form.html");
1441 form.action = GURL("http://example.com/submit.html"); 1443 form.action = GURL("http://example.com/submit.html");
1442 1444
1443 FormFieldData field; 1445 FormFieldData field;
1444 test::CreateTestFormField("Name", "name", "", "text", &field); 1446 test::CreateTestFormField("Name", "name", "", "text", &field);
1445 form.fields.push_back(field); 1447 form.fields.push_back(field);
1446 test::CreateTestFormField("Email", "email", "", "text", &field); 1448 test::CreateTestFormField("Email", "email", "", "text", &field);
1447 form.fields.push_back(field); 1449 form.fields.push_back(field);
1448 1450
1449 std::vector<FormData> forms(1, form); 1451 std::vector<FormData> forms(1, form);
1450 1452
1451 // Ensure no metrics are logged when loading a non-fillable form. 1453 // Ensure no metrics are logged when loading a non-fillable form.
1452 { 1454 {
1453 base::HistogramTester histogram_tester; 1455 base::HistogramTester histogram_tester;
1454 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1456 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1455 autofill_manager_->Reset(); 1457 autofill_manager_->Reset();
1456 histogram_tester.ExpectTotalCount("Autofill.DeveloperEngagement", 0); 1458 histogram_tester.ExpectTotalCount("Autofill.DeveloperEngagement", 0);
1459
1460 // UKM must not be logged unless enabled.
1461 EXPECT_EQ(0U, ukm_service->sources_count());
1462 EXPECT_EQ(0U, ukm_service->entries_count());
1457 } 1463 }
1458 1464
1459 // Add another field to the form, so that it becomes fillable. 1465 // Add another field to the form, so that it becomes fillable.
1460 test::CreateTestFormField("Phone", "phone", "", "text", &field); 1466 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1461 forms.back().fields.push_back(field); 1467 forms.back().fields.push_back(field);
1462 1468
1463 // Expect only the "form parsed" metric to be logged; no metrics about 1469 // Expect the "form parsed without hints" metric to be logged.
1464 // author-specified field type hints.
1465 { 1470 {
1466 base::HistogramTester histogram_tester; 1471 base::HistogramTester histogram_tester;
1467 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1472 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1468 autofill_manager_->Reset(); 1473 autofill_manager_->Reset();
1469 histogram_tester.ExpectUniqueSample("Autofill.DeveloperEngagement", 1474 histogram_tester.ExpectUniqueSample(
1470 AutofillMetrics::FILLABLE_FORM_PARSED, 1475 "Autofill.DeveloperEngagement",
1471 1); 1476 AutofillMetrics::FILLABLE_FORM_PARSED_WITHOUT_TYPE_HINTS, 1);
1477
1478 // UKM must not be logged unless enabled.
1479 EXPECT_EQ(0U, ukm_service->sources_count());
1480 EXPECT_EQ(0U, ukm_service->entries_count());
1472 } 1481 }
1473 1482
1474 // Add some fields with an author-specified field type to the form. 1483 // 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 1484 // 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 1485 // 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 1486 // 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. 1487 // local heuristics to detect field types in the rest of the form.
1479 test::CreateTestFormField("", "", "", "text", &field); 1488 test::CreateTestFormField("", "", "", "text", &field);
1480 field.autocomplete_attribute = "given-name"; 1489 field.autocomplete_attribute = "given-name";
1481 forms.back().fields.push_back(field); 1490 forms.back().fields.push_back(field);
1482 test::CreateTestFormField("", "", "", "text", &field); 1491 test::CreateTestFormField("", "", "", "text", &field);
1483 field.autocomplete_attribute = "email"; 1492 field.autocomplete_attribute = "email";
1484 forms.back().fields.push_back(field); 1493 forms.back().fields.push_back(field);
1485 test::CreateTestFormField("", "", "", "text", &field); 1494 test::CreateTestFormField("", "", "", "text", &field);
1486 field.autocomplete_attribute = "address-line1"; 1495 field.autocomplete_attribute = "address-line1";
1487 forms.back().fields.push_back(field); 1496 forms.back().fields.push_back(field);
1488 1497
1489 // Expect both the "form parsed" metric and the author-specified field type 1498 // Expect the "form parsed with field type hints" metric to be logged.
1490 // hints metric to be logged.
1491 { 1499 {
1492 base::HistogramTester histogram_tester; 1500 base::HistogramTester histogram_tester;
1493 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1501 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1494 autofill_manager_->Reset(); 1502 autofill_manager_->Reset();
1495 histogram_tester.ExpectBucketCount("Autofill.DeveloperEngagement",
1496 AutofillMetrics::FILLABLE_FORM_PARSED,
1497 1);
1498 histogram_tester.ExpectBucketCount( 1503 histogram_tester.ExpectBucketCount(
1499 "Autofill.DeveloperEngagement", 1504 "Autofill.DeveloperEngagement",
1500 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS, 1); 1505 AutofillMetrics::FILLABLE_FORM_PARSED_WITH_TYPE_HINTS, 1);
1506
1507 // UKM must not be logged unless enabled.
1508 EXPECT_EQ(0U, ukm_service->sources_count());
1509 EXPECT_EQ(0U, ukm_service->entries_count());
1510
1501 histogram_tester.ExpectBucketCount( 1511 histogram_tester.ExpectBucketCount(
1502 "Autofill.DeveloperEngagement", 1512 "Autofill.DeveloperEngagement",
1503 AutofillMetrics::FORM_CONTAINS_UPI_VPA_HINT, 0); 1513 AutofillMetrics::FORM_CONTAINS_UPI_VPA_HINT, 0);
1504 } 1514 }
1505 1515
1506 // Add a field with an author-specified UPI-VPA field type in the form. 1516 // Add a field with an author-specified UPI-VPA field type in the form.
1507 test::CreateTestFormField("", "", "", "text", &field); 1517 test::CreateTestFormField("", "", "", "text", &field);
1508 field.autocomplete_attribute = "upi-vpa"; 1518 field.autocomplete_attribute = "upi-vpa";
1509 forms.back().fields.push_back(field); 1519 forms.back().fields.push_back(field);
1510 1520
1511 // Expect the "form parsed" metric, the "author-specified field type 1521 // Expect the "form parsed with type hints" metric, and the
1512 // hints" metric, and the "author-specified upi-vpa type" metric to be logged. 1522 // "author-specified upi-vpa type" metric to be logged.
1513 { 1523 {
1514 base::HistogramTester histogram_tester; 1524 base::HistogramTester histogram_tester;
1515 autofill_manager_->OnFormsSeen(forms, TimeTicks()); 1525 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1516 autofill_manager_->Reset(); 1526 autofill_manager_->Reset();
1517 histogram_tester.ExpectBucketCount("Autofill.DeveloperEngagement",
1518 AutofillMetrics::FILLABLE_FORM_PARSED,
1519 1);
1520 histogram_tester.ExpectBucketCount( 1527 histogram_tester.ExpectBucketCount(
1521 "Autofill.DeveloperEngagement", 1528 "Autofill.DeveloperEngagement",
1522 AutofillMetrics::FILLABLE_FORM_CONTAINS_TYPE_HINTS, 1); 1529 AutofillMetrics::FILLABLE_FORM_PARSED_WITH_TYPE_HINTS, 1);
1523 histogram_tester.ExpectBucketCount( 1530 histogram_tester.ExpectBucketCount(
1524 "Autofill.DeveloperEngagement", 1531 "Autofill.DeveloperEngagement",
1525 AutofillMetrics::FORM_CONTAINS_UPI_VPA_HINT, 1); 1532 AutofillMetrics::FORM_CONTAINS_UPI_VPA_HINT, 1);
1526 } 1533 }
1527 } 1534 }
1528 1535
1536 // Verify that we correctly log UKM for form parsed without type hints regarding
1537 // developer engagement.
1538 TEST_F(AutofillMetricsTest,
1539 UkmDeveloperEngagement_LogFillableFormParsedWithoutTypeHints) {
1540 EnableUkmLogging();
1541 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1542
1543 // Start with a non-fillable form.
1544 FormData form;
1545 form.name = ASCIIToUTF16("TestForm");
1546 form.origin = GURL("http://example.com/form.html");
1547 form.action = GURL("http://example.com/submit.html");
1548
1549 FormFieldData field;
1550 test::CreateTestFormField("Name", "name", "", "text", &field);
1551 form.fields.push_back(field);
1552 test::CreateTestFormField("Email", "email", "", "text", &field);
1553 form.fields.push_back(field);
1554
1555 std::vector<FormData> forms(1, form);
1556
1557 // Ensure no metrics are logged when loading a non-fillable form.
1558 {
1559 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1560 autofill_manager_->Reset();
1561
1562 EXPECT_EQ(0U, ukm_service->sources_count());
1563 EXPECT_EQ(0U, ukm_service->entries_count());
1564 }
1565
1566 // Add another field to the form, so that it becomes fillable.
1567 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1568 forms.back().fields.push_back(field);
1569
1570 // Expect the "form parsed without field type hints" metric to be logged.
1571 {
1572 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1573 autofill_manager_->Reset();
1574
1575 ASSERT_EQ(1U, ukm_service->sources_count());
1576 const ukm::UkmSource* source =
1577 ukm_service->GetSourceForUrl(form.origin.spec().c_str());
1578 ASSERT_NE(nullptr, source);
1579
1580 ASSERT_EQ(1U, ukm_service->entries_count());
1581 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
1582 EXPECT_EQ(source->id(), entry->source_id());
1583
1584 ukm::Entry entry_proto;
1585 entry->PopulateProto(&entry_proto);
1586 EXPECT_EQ(source->id(), entry_proto.source_id());
1587 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
1588 entry_proto.event_hash());
1589 const ukm::Entry_Metric* metric = FindMetric(
1590 internal::kUKMDeveloperEngagementMetricName, entry_proto.metrics());
1591 ASSERT_NE(nullptr, metric);
1592 EXPECT_EQ(AutofillMetrics::FILLABLE_FORM_PARSED_WITHOUT_TYPE_HINTS,
1593 metric->value());
1594 }
1595 }
1596
1597 // Verify that we correctly log UKM for form parsed with type hints regarding
1598 // developer engagement.
1599 TEST_F(AutofillMetricsTest,
1600 UkmDeveloperEngagement_LogFillableFormParsedWithTypeHints) {
1601 EnableUkmLogging();
1602 ukm::TestUkmService* ukm_service = autofill_client_.GetTestUkmService();
1603
1604 FormData form;
1605 form.name = ASCIIToUTF16("TestForm");
1606 form.origin = GURL("http://example.com/form.html");
1607 form.action = GURL("http://example.com/submit.html");
1608
1609 FormFieldData field;
1610 test::CreateTestFormField("Name", "name", "", "text", &field);
1611 form.fields.push_back(field);
1612 test::CreateTestFormField("Email", "email", "", "text", &field);
1613 form.fields.push_back(field);
1614
1615 std::vector<FormData> forms(1, form);
1616
1617 // Add another field to the form, so that it becomes fillable.
1618 test::CreateTestFormField("Phone", "phone", "", "text", &field);
1619 forms.back().fields.push_back(field);
1620
1621 // Add some fields with an author-specified field type to the form.
1622 // We need to add at least three fields, because a form must have at least
1623 // three fillable fields to be considered to be autofillable; and if at least
1624 // one field specifies an explicit type hint, we don't apply any of our usual
1625 // local heuristics to detect field types in the rest of the form.
1626 test::CreateTestFormField("", "", "", "text", &field);
1627 field.autocomplete_attribute = "given-name";
1628 forms.back().fields.push_back(field);
1629 test::CreateTestFormField("", "", "", "text", &field);
1630 field.autocomplete_attribute = "email";
1631 forms.back().fields.push_back(field);
1632 test::CreateTestFormField("", "", "", "text", &field);
1633 field.autocomplete_attribute = "address-line1";
1634 forms.back().fields.push_back(field);
1635
1636 // Expect the "form parsed with field type hints" metric to be logged.
1637 {
1638 autofill_manager_->OnFormsSeen(forms, TimeTicks());
1639 autofill_manager_->Reset();
1640
1641 ASSERT_EQ(1U, ukm_service->sources_count());
1642 const ukm::UkmSource* source =
1643 ukm_service->GetSourceForUrl(form.origin.spec().c_str());
1644 ASSERT_NE(nullptr, source);
1645
1646 ASSERT_EQ(1U, ukm_service->entries_count());
1647 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
1648 EXPECT_EQ(source->id(), entry->source_id());
1649
1650 ukm::Entry entry_proto;
1651 entry->PopulateProto(&entry_proto);
1652 EXPECT_EQ(source->id(), entry_proto.source_id());
1653 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
1654 entry_proto.event_hash());
1655 const ukm::Entry_Metric* metric = FindMetric(
1656 internal::kUKMDeveloperEngagementMetricName, entry_proto.metrics());
1657 ASSERT_NE(nullptr, metric);
1658 EXPECT_EQ(AutofillMetrics::FILLABLE_FORM_PARSED_WITH_TYPE_HINTS,
1659 metric->value());
1660 }
1661 }
1662
1529 // Test that the profile count is logged correctly. 1663 // Test that the profile count is logged correctly.
1530 TEST_F(AutofillMetricsTest, StoredProfileCount) { 1664 TEST_F(AutofillMetricsTest, StoredProfileCount) {
1531 // The metric should be logged when the profiles are first loaded. 1665 // The metric should be logged when the profiles are first loaded.
1532 { 1666 {
1533 base::HistogramTester histogram_tester; 1667 base::HistogramTester histogram_tester;
1534 personal_data_->LoadProfiles(); 1668 personal_data_->LoadProfiles();
1535 histogram_tester.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1); 1669 histogram_tester.ExpectUniqueSample("Autofill.StoredProfileCount", 2, 1);
1536 } 1670 }
1537 1671
1538 // The metric should only be logged once. 1672 // The metric should only be logged once.
(...skipping 2845 matching lines...) Expand 10 before | Expand all | Expand 10 after
4384 "Autofill.FormEvents.CreditCard", 4518 "Autofill.FormEvents.CreditCard",
4385 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1); 4519 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1);
4386 // Check that the nonsecure histogram was not recorded. ExpectBucketCount() 4520 // Check that the nonsecure histogram was not recorded. ExpectBucketCount()
4387 // can't be used here because it expects the histogram to exist. 4521 // can't be used here because it expects the histogram to exist.
4388 EXPECT_EQ( 4522 EXPECT_EQ(
4389 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard") 4523 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard")
4390 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]); 4524 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]);
4391 } 4525 }
4392 } 4526 }
4393 4527
4394 // Tests that logging a UKM works as expected. 4528 // Tests that logging CardUploadDecision UKM works as expected.
4395 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) { 4529 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) {
4396 EnableUkmLogging(); 4530 EnableUkmLogging();
4397 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4531 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4398 GURL url("https://www.google.com"); 4532 GURL url("https://www.google.com");
4399 int upload_decision = 1; 4533 int upload_decision = 1;
4400 std::map<std::string, int> metrics; 4534 std::map<std::string, int> metrics = {
4401 metrics.insert(std::make_pair(internal::kUKMCardUploadDecisionMetricName, 4535 {internal::kUKMCardUploadDecisionMetricName, upload_decision}};
4402 upload_decision));
4403 4536
4404 EXPECT_TRUE(AutofillMetrics::LogUkm( 4537 EXPECT_TRUE(AutofillMetrics::LogUkm(
4405 ukm_service_test_harness.test_ukm_service(), url, 4538 ukm_service_test_harness.test_ukm_service(), url,
4406 internal::kUKMCardUploadDecisionEntryName, metrics)); 4539 internal::kUKMCardUploadDecisionEntryName, metrics));
4407 4540
4408 // Make sure that the UKM was logged correctly. 4541 // Make sure that the UKM was logged correctly.
4409 ukm::TestUkmService* ukm_service = 4542 ukm::TestUkmService* ukm_service =
4410 ukm_service_test_harness.test_ukm_service(); 4543 ukm_service_test_harness.test_ukm_service();
4411 4544
4412 ASSERT_EQ(1U, ukm_service->sources_count()); 4545 ASSERT_EQ(1U, ukm_service->sources_count());
4413 const ukm::UkmSource* source = 4546 const ukm::UkmSource* source =
4414 ukm_service->GetSourceForUrl(url.spec().c_str()); 4547 ukm_service->GetSourceForUrl(url.spec().c_str());
4415 EXPECT_EQ(url.spec(), source->url().spec()); 4548 EXPECT_EQ(url.spec(), source->url().spec());
4416 4549
4417 EXPECT_EQ(1U, ukm_service->entries_count()); 4550 ASSERT_EQ(1U, ukm_service->entries_count());
4418 const ukm::UkmEntry* entry = ukm_service->GetEntry(0); 4551 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
4419 EXPECT_EQ(source->id(), entry->source_id()); 4552 EXPECT_EQ(source->id(), entry->source_id());
4420 4553
4421 // Make sure that an card upload decision entry was logged. 4554 // Make sure that a card upload decision entry was logged.
4422 ukm::Entry entry_proto; 4555 ukm::Entry entry_proto;
4423 entry->PopulateProto(&entry_proto); 4556 entry->PopulateProto(&entry_proto);
4424 EXPECT_EQ(source->id(), entry_proto.source_id()); 4557 EXPECT_EQ(source->id(), entry_proto.source_id());
4425 EXPECT_EQ(base::HashMetricName(internal::kUKMCardUploadDecisionEntryName), 4558 EXPECT_EQ(base::HashMetricName(internal::kUKMCardUploadDecisionEntryName),
4426 entry_proto.event_hash()); 4559 entry_proto.event_hash());
4427 EXPECT_EQ(1, entry_proto.metrics_size()); 4560 EXPECT_EQ(1, entry_proto.metrics_size());
4428 4561
4429 // Make sure that the correct upload decision was logged. 4562 // Make sure that the correct upload decision was logged.
4430 const ukm::Entry_Metric* metric = FindMetric( 4563 const ukm::Entry_Metric* metric = FindMetric(
4431 internal::kUKMCardUploadDecisionMetricName, entry_proto.metrics()); 4564 internal::kUKMCardUploadDecisionMetricName, entry_proto.metrics());
4432 ASSERT_NE(nullptr, metric); 4565 ASSERT_NE(nullptr, metric);
4433 EXPECT_EQ(upload_decision, metric->value()); 4566 EXPECT_EQ(upload_decision, metric->value());
4434 } 4567 }
4435 4568
4569 // Tests that logging DeveloperEngagement UKM works as expected.
4570 TEST_F(AutofillMetricsTest, RecordDeveloperEngagementMetric) {
4571 EnableUkmLogging();
4572 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4573 GURL url("https://www.google.com");
4574 int form_structure_metric = 1;
4575 std::map<std::string, int> metrics = {
4576 {internal::kUKMDeveloperEngagementMetricName, form_structure_metric}};
4577
4578 EXPECT_TRUE(AutofillMetrics::LogUkm(
4579 ukm_service_test_harness.test_ukm_service(), url,
4580 internal::kUKMDeveloperEngagementEntryName, metrics));
4581
4582 // Make sure that the UKM was logged correctly.
4583 ukm::TestUkmService* ukm_service =
4584 ukm_service_test_harness.test_ukm_service();
4585
4586 ASSERT_EQ(1U, ukm_service->sources_count());
4587 const ukm::UkmSource* source =
4588 ukm_service->GetSourceForUrl(url.spec().c_str());
4589 EXPECT_EQ(url.spec(), source->url().spec());
4590
4591 ASSERT_EQ(1U, ukm_service->entries_count());
4592 const ukm::UkmEntry* entry = ukm_service->GetEntry(0);
4593 EXPECT_EQ(source->id(), entry->source_id());
4594
4595 // Make sure that a developer engagement entry was logged.
4596 ukm::Entry entry_proto;
4597 entry->PopulateProto(&entry_proto);
4598 EXPECT_EQ(source->id(), entry_proto.source_id());
4599 EXPECT_EQ(base::HashMetricName(internal::kUKMDeveloperEngagementEntryName),
4600 entry_proto.event_hash());
4601 EXPECT_EQ(1, entry_proto.metrics_size());
4602
4603 // Make sure that the correct developer engagement metric was logged.
4604 const ukm::Entry_Metric* metric = FindMetric(
4605 internal::kUKMDeveloperEngagementMetricName, entry_proto.metrics());
4606 ASSERT_NE(nullptr, metric);
4607 EXPECT_EQ(form_structure_metric, metric->value());
4608 }
4609
4436 // Tests that no UKM is logged when the URL is not valid. 4610 // Tests that no UKM is logged when the URL is not valid.
4437 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) { 4611 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) {
4438 EnableUkmLogging(); 4612 EnableUkmLogging();
4439 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4613 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4440 GURL url(""); 4614 GURL url("");
4441 std::map<std::string, int> metrics; 4615 std::map<std::string, int> metrics = {{"metric", 1}};
4442 metrics.insert(std::make_pair("metric", 1));
4443 4616
4444 EXPECT_FALSE(AutofillMetrics::LogUkm( 4617 EXPECT_FALSE(AutofillMetrics::LogUkm(
4445 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4618 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4446 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4619 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4447 } 4620 }
4448 4621
4449 // Tests that no UKM is logged when the metrics map is empty. 4622 // Tests that no UKM is logged when the metrics map is empty.
4450 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) { 4623 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) {
4451 EnableUkmLogging(); 4624 EnableUkmLogging();
4452 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4625 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4453 GURL url("https://www.google.com"); 4626 GURL url("https://www.google.com");
4454 std::map<std::string, int> metrics; 4627 std::map<std::string, int> metrics;
4455 4628
4456 EXPECT_FALSE(AutofillMetrics::LogUkm( 4629 EXPECT_FALSE(AutofillMetrics::LogUkm(
4457 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4630 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4458 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4631 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4459 } 4632 }
4460 4633
4461 // Tests that no UKM is logged when the ukm service is null. 4634 // Tests that no UKM is logged when the ukm service is null.
4462 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) { 4635 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) {
4463 EnableUkmLogging(); 4636 EnableUkmLogging();
4464 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4637 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4465 GURL url("https://www.google.com"); 4638 GURL url("https://www.google.com");
4466 std::map<std::string, int> metrics; 4639 std::map<std::string, int> metrics = {{"metric", 1}};
4467 metrics.insert(std::make_pair("metric", 1));
4468 4640
4469 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics)); 4641 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics));
4470 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4642 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4471 } 4643 }
4472 4644
4473 // Tests that no UKM is logged when the ukm logging feature is disabled. 4645 // Tests that no UKM is logged when the ukm logging feature is disabled.
4474 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) { 4646 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) {
4475 ukm::UkmServiceTestingHarness ukm_service_test_harness; 4647 ukm::UkmServiceTestingHarness ukm_service_test_harness;
4476 GURL url("https://www.google.com"); 4648 GURL url("https://www.google.com");
4477 std::map<std::string, int> metrics; 4649 std::map<std::string, int> metrics = {{"metric", 1}};
4478 metrics.insert(std::make_pair("metric", 1));
4479 4650
4480 EXPECT_FALSE(AutofillMetrics::LogUkm( 4651 EXPECT_FALSE(AutofillMetrics::LogUkm(
4481 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); 4652 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics));
4482 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); 4653 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count());
4483 } 4654 }
4484 4655
4485 } // namespace autofill 4656 } // 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