OLD | NEW |
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" |
14 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
15 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
17 #include "base/test/histogram_tester.h" | 19 #include "base/test/histogram_tester.h" |
| 20 #include "base/test/scoped_feature_list.h" |
18 #include "base/test/user_action_tester.h" | 21 #include "base/test/user_action_tester.h" |
19 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "components/autofill/core/browser/autofill_experiments.h" |
20 #include "components/autofill/core/browser/autofill_external_delegate.h" | 24 #include "components/autofill/core/browser/autofill_external_delegate.h" |
21 #include "components/autofill/core/browser/autofill_manager.h" | 25 #include "components/autofill/core/browser/autofill_manager.h" |
22 #include "components/autofill/core/browser/autofill_test_utils.h" | 26 #include "components/autofill/core/browser/autofill_test_utils.h" |
23 #include "components/autofill/core/browser/payments/payments_client.h" | 27 #include "components/autofill/core/browser/payments/payments_client.h" |
24 #include "components/autofill/core/browser/personal_data_manager.h" | 28 #include "components/autofill/core/browser/personal_data_manager.h" |
25 #include "components/autofill/core/browser/popup_item_ids.h" | 29 #include "components/autofill/core/browser/popup_item_ids.h" |
26 #include "components/autofill/core/browser/test_autofill_client.h" | 30 #include "components/autofill/core/browser/test_autofill_client.h" |
27 #include "components/autofill/core/browser/test_autofill_driver.h" | 31 #include "components/autofill/core/browser/test_autofill_driver.h" |
28 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 32 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
29 #include "components/autofill/core/common/form_data.h" | 33 #include "components/autofill/core/common/form_data.h" |
30 #include "components/autofill/core/common/form_field_data.h" | 34 #include "components/autofill/core/common/form_field_data.h" |
31 #include "components/prefs/pref_service.h" | 35 #include "components/prefs/pref_service.h" |
32 #include "components/rappor/test_rappor_service.h" | 36 #include "components/rappor/test_rappor_service.h" |
33 #include "components/signin/core/browser/account_tracker_service.h" | 37 #include "components/signin/core/browser/account_tracker_service.h" |
34 #include "components/signin/core/browser/fake_signin_manager.h" | 38 #include "components/signin/core/browser/fake_signin_manager.h" |
35 #include "components/signin/core/browser/test_signin_client.h" | 39 #include "components/signin/core/browser/test_signin_client.h" |
36 #include "components/signin/core/common/signin_pref_names.h" | 40 #include "components/signin/core/common/signin_pref_names.h" |
| 41 #include "components/ukm/test_ukm_service.h" |
| 42 #include "components/ukm/ukm_source.h" |
37 #include "components/webdata/common/web_data_results.h" | 43 #include "components/webdata/common/web_data_results.h" |
38 #include "testing/gmock/include/gmock/gmock.h" | 44 #include "testing/gmock/include/gmock/gmock.h" |
39 #include "testing/gtest/include/gtest/gtest.h" | 45 #include "testing/gtest/include/gtest/gtest.h" |
40 #include "ui/gfx/geometry/rect.h" | 46 #include "ui/gfx/geometry/rect.h" |
41 #include "url/gurl.h" | 47 #include "url/gurl.h" |
42 | 48 |
43 using base::ASCIIToUTF16; | 49 using base::ASCIIToUTF16; |
44 using base::Bucket; | 50 using base::Bucket; |
45 using base::TimeTicks; | 51 using base::TimeTicks; |
46 using rappor::TestRapporServiceImpl; | 52 using rappor::TestRapporServiceImpl; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 | 310 |
305 class AutofillMetricsTest : public testing::Test { | 311 class AutofillMetricsTest : public testing::Test { |
306 public: | 312 public: |
307 ~AutofillMetricsTest() override; | 313 ~AutofillMetricsTest() override; |
308 | 314 |
309 void SetUp() override; | 315 void SetUp() override; |
310 void TearDown() override; | 316 void TearDown() override; |
311 | 317 |
312 protected: | 318 protected: |
313 void EnableWalletSync(); | 319 void EnableWalletSync(); |
| 320 void EnableUkmLogging(); |
314 | 321 |
315 base::MessageLoop message_loop_; | 322 base::MessageLoop message_loop_; |
316 TestAutofillClient autofill_client_; | 323 TestAutofillClient autofill_client_; |
317 std::unique_ptr<AccountTrackerService> account_tracker_; | 324 std::unique_ptr<AccountTrackerService> account_tracker_; |
318 std::unique_ptr<FakeSigninManagerBase> signin_manager_; | 325 std::unique_ptr<FakeSigninManagerBase> signin_manager_; |
319 std::unique_ptr<TestSigninClient> signin_client_; | 326 std::unique_ptr<TestSigninClient> signin_client_; |
320 std::unique_ptr<TestAutofillDriver> autofill_driver_; | 327 std::unique_ptr<TestAutofillDriver> autofill_driver_; |
321 std::unique_ptr<TestAutofillManager> autofill_manager_; | 328 std::unique_ptr<TestAutofillManager> autofill_manager_; |
322 std::unique_ptr<TestPersonalDataManager> personal_data_; | 329 std::unique_ptr<TestPersonalDataManager> personal_data_; |
323 std::unique_ptr<AutofillExternalDelegate> external_delegate_; | 330 std::unique_ptr<AutofillExternalDelegate> external_delegate_; |
| 331 base::test::ScopedFeatureList scoped_feature_list_; |
324 }; | 332 }; |
325 | 333 |
326 AutofillMetricsTest::~AutofillMetricsTest() { | 334 AutofillMetricsTest::~AutofillMetricsTest() { |
327 // Order of destruction is important as AutofillManager relies on | 335 // Order of destruction is important as AutofillManager relies on |
328 // PersonalDataManager to be around when it gets destroyed. | 336 // PersonalDataManager to be around when it gets destroyed. |
329 autofill_manager_.reset(); | 337 autofill_manager_.reset(); |
330 } | 338 } |
331 | 339 |
332 void AutofillMetricsTest::SetUp() { | 340 void AutofillMetricsTest::SetUp() { |
333 autofill_client_.SetPrefs(test::PrefServiceForTesting()); | 341 autofill_client_.SetPrefs(test::PrefServiceForTesting()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 account_tracker_->Shutdown(); | 378 account_tracker_->Shutdown(); |
371 account_tracker_.reset(); | 379 account_tracker_.reset(); |
372 signin_client_.reset(); | 380 signin_client_.reset(); |
373 test::ReenableSystemServices(); | 381 test::ReenableSystemServices(); |
374 } | 382 } |
375 | 383 |
376 void AutofillMetricsTest::EnableWalletSync() { | 384 void AutofillMetricsTest::EnableWalletSync() { |
377 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com"); | 385 signin_manager_->SetAuthenticatedAccountInfo("12345", "syncuser@example.com"); |
378 } | 386 } |
379 | 387 |
| 388 void AutofillMetricsTest::EnableUkmLogging() { |
| 389 scoped_feature_list_.InitAndEnableFeature(kAutofillUkmLogging); |
| 390 } |
| 391 |
380 // Test that we log quality metrics appropriately. | 392 // Test that we log quality metrics appropriately. |
381 TEST_F(AutofillMetricsTest, QualityMetrics) { | 393 TEST_F(AutofillMetricsTest, QualityMetrics) { |
382 // Set up our form data. | 394 // Set up our form data. |
383 FormData form; | 395 FormData form; |
384 form.name = ASCIIToUTF16("TestForm"); | 396 form.name = ASCIIToUTF16("TestForm"); |
385 form.origin = GURL("http://example.com/form.html"); | 397 form.origin = GURL("http://example.com/form.html"); |
386 form.action = GURL("http://example.com/submit.html"); | 398 form.action = GURL("http://example.com/submit.html"); |
387 | 399 |
388 std::vector<ServerFieldType> heuristic_types, server_types; | 400 std::vector<ServerFieldType> heuristic_types, server_types; |
389 FormFieldData field; | 401 FormFieldData field; |
(...skipping 3943 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4333 "Autofill.FormEvents.CreditCard", | 4345 "Autofill.FormEvents.CreditCard", |
4334 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1); | 4346 AutofillMetrics::FORM_EVENT_NO_SUGGESTION_SUBMITTED_ONCE, 1); |
4335 // Check that the nonsecure histogram was not recorded. ExpectBucketCount() | 4347 // Check that the nonsecure histogram was not recorded. ExpectBucketCount() |
4336 // can't be used here because it expects the histogram to exist. | 4348 // can't be used here because it expects the histogram to exist. |
4337 EXPECT_EQ( | 4349 EXPECT_EQ( |
4338 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard") | 4350 0, histograms.GetTotalCountsForPrefix("Autofill.FormEvents.CreditCard") |
4339 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]); | 4351 ["Autofill.FormEvents.CreditCard.OnNonsecurePage"]); |
4340 } | 4352 } |
4341 } | 4353 } |
4342 | 4354 |
| 4355 // Tests that logging a UKM works as expected. |
| 4356 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric) { |
| 4357 EnableUkmLogging(); |
| 4358 ukm::UkmServiceTestingHarness ukm_service_test_harness; |
| 4359 GURL url("https://www.google.com"); |
| 4360 std::map<std::string, int> metrics; |
| 4361 metrics.insert(std::make_pair("metric", 1)); |
| 4362 |
| 4363 EXPECT_TRUE(AutofillMetrics::LogUkm( |
| 4364 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); |
| 4365 |
| 4366 ASSERT_EQ(1U, ukm_service_test_harness.test_ukm_service()->sources_count()); |
| 4367 EXPECT_EQ( |
| 4368 url.spec(), |
| 4369 ukm_service_test_harness.test_ukm_service()->GetSource(0)->url().spec()); |
| 4370 } |
| 4371 |
| 4372 // Tests that no UKM is logged when the URL is not valid. |
| 4373 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_InvalidUrl) { |
| 4374 EnableUkmLogging(); |
| 4375 ukm::UkmServiceTestingHarness ukm_service_test_harness; |
| 4376 GURL url(""); |
| 4377 std::map<std::string, int> metrics; |
| 4378 metrics.insert(std::make_pair("metric", 1)); |
| 4379 |
| 4380 EXPECT_FALSE(AutofillMetrics::LogUkm( |
| 4381 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); |
| 4382 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); |
| 4383 } |
| 4384 |
| 4385 // Tests that no UKM is logged when the metrics map is empty. |
| 4386 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoMetrics) { |
| 4387 EnableUkmLogging(); |
| 4388 ukm::UkmServiceTestingHarness ukm_service_test_harness; |
| 4389 GURL url("https://www.google.com"); |
| 4390 std::map<std::string, int> metrics; |
| 4391 |
| 4392 EXPECT_FALSE(AutofillMetrics::LogUkm( |
| 4393 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); |
| 4394 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); |
| 4395 } |
| 4396 |
| 4397 // Tests that no UKM is logged when the ukm service is null. |
| 4398 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_NoUkmService) { |
| 4399 EnableUkmLogging(); |
| 4400 ukm::UkmServiceTestingHarness ukm_service_test_harness; |
| 4401 GURL url("https://www.google.com"); |
| 4402 std::map<std::string, int> metrics; |
| 4403 metrics.insert(std::make_pair("metric", 1)); |
| 4404 |
| 4405 EXPECT_FALSE(AutofillMetrics::LogUkm(nullptr, url, "test_ukm", metrics)); |
| 4406 ASSERT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); |
| 4407 } |
| 4408 |
| 4409 // Tests that no UKM is logged when the ukm logging feature is disabled. |
| 4410 TEST_F(AutofillMetricsTest, RecordCardUploadDecisionMetric_FeatureDisabled) { |
| 4411 ukm::UkmServiceTestingHarness ukm_service_test_harness; |
| 4412 GURL url("https://www.google.com"); |
| 4413 std::map<std::string, int> metrics; |
| 4414 metrics.insert(std::make_pair("metric", 1)); |
| 4415 |
| 4416 EXPECT_FALSE(AutofillMetrics::LogUkm( |
| 4417 ukm_service_test_harness.test_ukm_service(), url, "test_ukm", metrics)); |
| 4418 EXPECT_EQ(0U, ukm_service_test_harness.test_ukm_service()->sources_count()); |
| 4419 } |
| 4420 |
4343 } // namespace autofill | 4421 } // namespace autofill |
OLD | NEW |