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

Side by Side Diff: chrome/browser/geolocation/geolocation_permission_context_unittest.cc

Issue 2791193002: Log metrics for the Location Settings Dialog prompt for geolocation. (Closed)
Patch Set: Undo histograms change 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/geolocation/geolocation_permission_context.h" 5 #include "chrome/browser/geolocation/geolocation_permission_context.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/containers/hash_tables.h" 16 #include "base/containers/hash_tables.h"
17 #include "base/gtest_prod_util.h" 17 #include "base/gtest_prod_util.h"
18 #include "base/id_map.h" 18 #include "base/id_map.h"
19 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
20 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/waitable_event.h" 21 #include "base/synchronization/waitable_event.h"
22 #include "base/test/histogram_tester.h"
22 #include "base/test/scoped_feature_list.h" 23 #include "base/test/scoped_feature_list.h"
23 #include "base/test/simple_test_clock.h" 24 #include "base/test/simple_test_clock.h"
24 #include "base/time/clock.h" 25 #include "base/time/clock.h"
25 #include "chrome/browser/chrome_notification_types.h" 26 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 27 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
27 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 28 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
28 #include "chrome/browser/infobars/infobar_service.h" 29 #include "chrome/browser/infobars/infobar_service.h"
29 #include "chrome/browser/permissions/permission_context_base.h" 30 #include "chrome/browser/permissions/permission_context_base.h"
30 #include "chrome/browser/permissions/permission_manager.h" 31 #include "chrome/browser/permissions/permission_manager.h"
31 #include "chrome/browser/permissions/permission_request.h" 32 #include "chrome/browser/permissions/permission_request.h"
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 ConfirmInfoBarDelegate* infobar_delegate = 614 ConfirmInfoBarDelegate* infobar_delegate =
614 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 615 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
615 ASSERT_TRUE(infobar_delegate); 616 ASSERT_TRUE(infobar_delegate);
616 infobar_delegate->Accept(); 617 infobar_delegate->Accept();
617 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); 618 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
618 CheckPermissionMessageSent(0, true); 619 CheckPermissionMessageSent(0, true);
619 EXPECT_FALSE(MockLocationSettings::HasShownLocationSettingsDialog()); 620 EXPECT_FALSE(MockLocationSettings::HasShownLocationSettingsDialog());
620 } 621 }
621 622
622 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDAccept) { 623 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDAccept) {
624 base::HistogramTester tester;
623 base::test::ScopedFeatureList scoped_feature_list; 625 base::test::ScopedFeatureList scoped_feature_list;
624 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 626 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
625 627
626 GURL requesting_frame("https://www.example.com/geolocation"); 628 GURL requesting_frame("https://www.example.com/geolocation");
627 NavigateAndCommit(requesting_frame); 629 NavigateAndCommit(requesting_frame);
628 MockLocationSettings::SetLocationStatus(true /* android */, 630 MockLocationSettings::SetLocationStatus(true /* android */,
629 false /* system */); 631 false /* system */);
630 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 632 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
631 GRANTED); 633 GRANTED);
632 EXPECT_EQ(0U, infobar_service()->infobar_count()); 634 EXPECT_EQ(0U, infobar_service()->infobar_count());
633 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame, 635 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame,
634 true); 636 true);
635 EXPECT_EQ(1U, infobar_service()->infobar_count()); 637 EXPECT_EQ(1U, infobar_service()->infobar_count());
636 ConfirmInfoBarDelegate* infobar_delegate = 638 ConfirmInfoBarDelegate* infobar_delegate =
637 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 639 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
638 ASSERT_TRUE(infobar_delegate); 640 ASSERT_TRUE(infobar_delegate);
639 infobar_delegate->Accept(); 641 infobar_delegate->Accept();
640 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW); 642 CheckTabContentsState(requesting_frame, CONTENT_SETTING_ALLOW);
641 CheckPermissionMessageSent(0, true); 643 CheckPermissionMessageSent(0, true);
642 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog()); 644 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog());
645
646 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 1);
647 tester.ExpectTotalCount("Geolocation.SettingsDialog.AcceptEvent.NonDSE", 1);
648 tester.ExpectTotalCount("Geolocation.SettingsDialog.DenyEvent.NonDSE", 0);
643 } 649 }
644 650
645 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDReject) { 651 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDReject) {
652 base::HistogramTester tester;
646 base::test::ScopedFeatureList scoped_feature_list; 653 base::test::ScopedFeatureList scoped_feature_list;
647 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 654 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
648 655
649 GURL requesting_frame("https://www.example.com/geolocation"); 656 GURL requesting_frame("https://www.example.com/geolocation");
650 NavigateAndCommit(requesting_frame); 657 NavigateAndCommit(requesting_frame);
651 MockLocationSettings::SetLocationStatus(true /* android */, 658 MockLocationSettings::SetLocationStatus(true /* android */,
652 false /* system */); 659 false /* system */);
653 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 660 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
654 DENIED); 661 DENIED);
655 EXPECT_EQ(0U, infobar_service()->infobar_count()); 662 EXPECT_EQ(0U, infobar_service()->infobar_count());
656 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame, 663 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame,
657 true); 664 true);
658 EXPECT_EQ(1U, infobar_service()->infobar_count()); 665 EXPECT_EQ(1U, infobar_service()->infobar_count());
659 ConfirmInfoBarDelegate* infobar_delegate = 666 ConfirmInfoBarDelegate* infobar_delegate =
660 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 667 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
661 ASSERT_TRUE(infobar_delegate); 668 ASSERT_TRUE(infobar_delegate);
662 infobar_delegate->Accept(); 669 infobar_delegate->Accept();
663 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK); 670 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK);
664 CheckPermissionMessageSent(0, false); 671 CheckPermissionMessageSent(0, false);
665 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog()); 672 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog());
673
674 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 1);
675 tester.ExpectTotalCount("Geolocation.SettingsDialog.AcceptEvent.NonDSE", 0);
676 tester.ExpectTotalCount("Geolocation.SettingsDialog.DenyEvent.NonDSE", 1);
666 } 677 }
667 678
668 TEST_F(GeolocationPermissionContextTests, LSDBackOffDifferentSites) { 679 TEST_F(GeolocationPermissionContextTests, LSDBackOffDifferentSites) {
680 base::HistogramTester tester;
669 base::test::ScopedFeatureList scoped_feature_list; 681 base::test::ScopedFeatureList scoped_feature_list;
670 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 682 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
671 683
672 GURL requesting_frame_1("https://www.example.com/geolocation"); 684 GURL requesting_frame_1("https://www.example.com/geolocation");
673 GURL requesting_frame_2("https://www.example-2.com/geolocation"); 685 GURL requesting_frame_2("https://www.example-2.com/geolocation");
674 const char* requesting_frame_dse_ptr = "https://www.dse.com/geolocation"; 686 const char* requesting_frame_dse_ptr = "https://www.dse.com/geolocation";
675 GURL requesting_frame_dse(requesting_frame_dse_ptr); 687 GURL requesting_frame_dse(requesting_frame_dse_ptr);
676 688
677 SetDSEOriginForTesting(requesting_frame_dse_ptr); 689 SetDSEOriginForTesting(requesting_frame_dse_ptr);
678 690
(...skipping 16 matching lines...) Expand all
695 // origins share the same backoff, which is distinct to the DSE origin. 707 // origins share the same backoff, which is distinct to the DSE origin.
696 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff. 708 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff.
697 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_1)); 709 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_1));
698 710
699 // Now check that the LSD is prevented on this origin. 711 // Now check that the LSD is prevented on this origin.
700 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_1)); 712 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_1));
701 713
702 // Now ask on the other non-DSE origin and check backoff prevented the prompt. 714 // Now ask on the other non-DSE origin and check backoff prevented the prompt.
703 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_2)); 715 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_2));
704 716
717 // Test that the right histograms are updated.
718 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 1);
719 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.DSE", 0);
720 tester.ExpectTotalCount("Geolocation.SettingsDialog.SuppressEvent.NonDSE", 2);
721 tester.ExpectTotalCount("Geolocation.SettingsDialog.SuppressEvent.DSE", 0);
722
705 // Now request on the DSE and check that the LSD is shown, as the non-DSE 723 // Now request on the DSE and check that the LSD is shown, as the non-DSE
706 // backoff should not apply. 724 // backoff should not apply.
707 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_dse)); 725 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_dse));
708 726
709 // Now check that the DSE is in backoff. 727 // Now check that the DSE is in backoff.
710 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_dse)); 728 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_dse));
729
730 // Test that the right histograms are updated.
731 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 1);
732 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.DSE", 1);
733 tester.ExpectTotalCount("Geolocation.SettingsDialog.SuppressEvent.NonDSE", 2);
734 tester.ExpectTotalCount("Geolocation.SettingsDialog.SuppressEvent.DSE", 1);
711 } 735 }
712 736
713 TEST_F(GeolocationPermissionContextTests, LSDBackOffTiming) { 737 TEST_F(GeolocationPermissionContextTests, LSDBackOffTiming) {
738 base::HistogramTester tester;
714 base::test::ScopedFeatureList scoped_feature_list; 739 base::test::ScopedFeatureList scoped_feature_list;
715 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 740 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
716 741
717 GURL requesting_frame("https://www.example.com/geolocation"); 742 GURL requesting_frame("https://www.example.com/geolocation");
718 SetGeolocationContentSetting(requesting_frame, requesting_frame, 743 SetGeolocationContentSetting(requesting_frame, requesting_frame,
719 CONTENT_SETTING_ALLOW); 744 CONTENT_SETTING_ALLOW);
720 745
721 // Turn off system location but allow the LSD to be shown, and denied. 746 // Turn off system location but allow the LSD to be shown, and denied.
722 MockLocationSettings::SetLocationStatus(true /* android */, 747 MockLocationSettings::SetLocationStatus(true /* android */,
723 false /* system */); 748 false /* system */);
724 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 749 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
725 DENIED); 750 DENIED);
726 751
727 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff. 752 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff.
728 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 753 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
729 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 754 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
730 755
731 // Check the LSD is prevented in 6 days time. 756 // Check the LSD is prevented in 6 days time.
732 AddDayOffsetForTesting(6); 757 AddDayOffsetForTesting(6);
733 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 758 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
734 759
760 // Check histograms so far.
761 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 1);
762 tester.ExpectBucketCount("Geolocation.SettingsDialog.ShowEvent.NonDSE",
763 static_cast<base::HistogramBase::Sample>(
764 GeolocationPermissionContextAndroid::
765 LocationSettingsDialogBackOff::kNoBackOff),
766 1);
767
735 // Check it is shown in one more days time, but then not straight after.. 768 // Check it is shown in one more days time, but then not straight after..
736 AddDayOffsetForTesting(1); 769 AddDayOffsetForTesting(1);
737 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 770 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
738 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 771 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
739 772
740 // Check that it isn't shown 29 days after that. 773 // Check that it isn't shown 29 days after that.
741 AddDayOffsetForTesting(29); 774 AddDayOffsetForTesting(29);
742 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 775 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
743 776
777 // Check histograms so far.
778 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 2);
779 tester.ExpectBucketCount("Geolocation.SettingsDialog.ShowEvent.NonDSE",
780 static_cast<base::HistogramBase::Sample>(
781 GeolocationPermissionContextAndroid::
782 LocationSettingsDialogBackOff::kOneWeek),
783 1);
784
744 // Check it is shown in one more days time, but then not straight after.. 785 // Check it is shown in one more days time, but then not straight after..
745 AddDayOffsetForTesting(1); 786 AddDayOffsetForTesting(1);
746 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 787 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
747 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 788 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
748 789
749 // Check that it isn't shown 89 days after that. 790 // Check that it isn't shown 89 days after that.
750 AddDayOffsetForTesting(89); 791 AddDayOffsetForTesting(89);
751 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 792 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
752 793
794 // Check histograms so far.
795 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 3);
796 tester.ExpectBucketCount("Geolocation.SettingsDialog.ShowEvent.NonDSE",
797 static_cast<base::HistogramBase::Sample>(
798 GeolocationPermissionContextAndroid::
799 LocationSettingsDialogBackOff::kOneMonth),
800 1);
801
753 // Check it is shown in one more days time, but then not straight after.. 802 // Check it is shown in one more days time, but then not straight after..
754 AddDayOffsetForTesting(1); 803 AddDayOffsetForTesting(1);
755 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 804 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
756 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 805 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
757 806
758 // Check that it isn't shown 89 days after that. 807 // Check that it isn't shown 89 days after that.
759 AddDayOffsetForTesting(89); 808 AddDayOffsetForTesting(89);
760 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 809 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
761 810
762 // Check it is shown in one more days time, but then not straight after.. 811 // Check it is shown in one more days time, but then not straight after..
763 AddDayOffsetForTesting(1); 812 AddDayOffsetForTesting(1);
764 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 813 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
765 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 814 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
815
816 // Check histograms so far.
817 tester.ExpectTotalCount("Geolocation.SettingsDialog.ShowEvent.NonDSE", 5);
818 tester.ExpectBucketCount("Geolocation.SettingsDialog.ShowEvent.NonDSE",
819 static_cast<base::HistogramBase::Sample>(
820 GeolocationPermissionContextAndroid::
821 LocationSettingsDialogBackOff::kThreeMonths),
822 2);
766 } 823 }
767 824
768 TEST_F(GeolocationPermissionContextTests, LSDBackOffPermissionStatus) { 825 TEST_F(GeolocationPermissionContextTests, LSDBackOffPermissionStatus) {
769 base::test::ScopedFeatureList scoped_feature_list; 826 base::test::ScopedFeatureList scoped_feature_list;
770 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 827 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
771 828
772 GURL requesting_frame("https://www.example.com/geolocation"); 829 GURL requesting_frame("https://www.example.com/geolocation");
773 SetGeolocationContentSetting(requesting_frame, requesting_frame, 830 SetGeolocationContentSetting(requesting_frame, requesting_frame,
774 CONTENT_SETTING_ALLOW); 831 CONTENT_SETTING_ALLOW);
775 832
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
1447 SetGeolocationContentSetting(requesting_frame, requesting_frame, 1504 SetGeolocationContentSetting(requesting_frame, requesting_frame,
1448 CONTENT_SETTING_BLOCK); 1505 CONTENT_SETTING_BLOCK);
1449 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 1506 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
1450 GRANTED); 1507 GRANTED);
1451 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED, 1508 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED,
1452 PermissionManager::Get(profile())->GetPermissionStatus( 1509 PermissionManager::Get(profile())->GetPermissionStatus(
1453 content::PermissionType::GEOLOCATION, requesting_frame, 1510 content::PermissionType::GEOLOCATION, requesting_frame,
1454 requesting_frame)); 1511 requesting_frame));
1455 } 1512 }
1456 #endif // defined(OS_ANDROID) 1513 #endif // defined(OS_ANDROID)
OLDNEW
« no previous file with comments | « chrome/browser/geolocation/geolocation_permission_context_android.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698