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

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: Fix tests 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(
647 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 1);
648 tester.ExpectTotalCount(
649 GeolocationPermissionContextAndroid::kGeolocationLSDAcceptNonDSEMetric,
650 1);
651 tester.ExpectTotalCount(
652 GeolocationPermissionContextAndroid::kGeolocationLSDDenyNonDSEMetric, 0);
643 } 653 }
644 654
645 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDReject) { 655 TEST_F(GeolocationPermissionContextTests, SystemLocationOffLSDReject) {
656 base::HistogramTester tester;
646 base::test::ScopedFeatureList scoped_feature_list; 657 base::test::ScopedFeatureList scoped_feature_list;
647 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 658 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
648 659
649 GURL requesting_frame("https://www.example.com/geolocation"); 660 GURL requesting_frame("https://www.example.com/geolocation");
650 NavigateAndCommit(requesting_frame); 661 NavigateAndCommit(requesting_frame);
651 MockLocationSettings::SetLocationStatus(true /* android */, 662 MockLocationSettings::SetLocationStatus(true /* android */,
652 false /* system */); 663 false /* system */);
653 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 664 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
654 DENIED); 665 DENIED);
655 EXPECT_EQ(0U, infobar_service()->infobar_count()); 666 EXPECT_EQ(0U, infobar_service()->infobar_count());
656 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame, 667 RequestGeolocationPermission(web_contents(), RequestID(0), requesting_frame,
657 true); 668 true);
658 EXPECT_EQ(1U, infobar_service()->infobar_count()); 669 EXPECT_EQ(1U, infobar_service()->infobar_count());
659 ConfirmInfoBarDelegate* infobar_delegate = 670 ConfirmInfoBarDelegate* infobar_delegate =
660 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 671 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
661 ASSERT_TRUE(infobar_delegate); 672 ASSERT_TRUE(infobar_delegate);
662 infobar_delegate->Accept(); 673 infobar_delegate->Accept();
663 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK); 674 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK);
664 CheckPermissionMessageSent(0, false); 675 CheckPermissionMessageSent(0, false);
665 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog()); 676 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog());
677
678 tester.ExpectTotalCount(
679 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 1);
680 tester.ExpectTotalCount(
681 GeolocationPermissionContextAndroid::kGeolocationLSDAcceptNonDSEMetric,
682 0);
683 tester.ExpectTotalCount(
684 GeolocationPermissionContextAndroid::kGeolocationLSDDenyNonDSEMetric, 1);
666 } 685 }
667 686
668 TEST_F(GeolocationPermissionContextTests, LSDBackOffDifferentSites) { 687 TEST_F(GeolocationPermissionContextTests, LSDBackOffDifferentSites) {
688 base::HistogramTester tester;
669 base::test::ScopedFeatureList scoped_feature_list; 689 base::test::ScopedFeatureList scoped_feature_list;
670 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 690 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
671 691
672 GURL requesting_frame_1("https://www.example.com/geolocation"); 692 GURL requesting_frame_1("https://www.example.com/geolocation");
673 GURL requesting_frame_2("https://www.example-2.com/geolocation"); 693 GURL requesting_frame_2("https://www.example-2.com/geolocation");
674 const char* requesting_frame_dse_ptr = "https://www.dse.com/geolocation"; 694 const char* requesting_frame_dse_ptr = "https://www.dse.com/geolocation";
675 GURL requesting_frame_dse(requesting_frame_dse_ptr); 695 GURL requesting_frame_dse(requesting_frame_dse_ptr);
676 696
677 SetDSEOriginForTesting(requesting_frame_dse_ptr); 697 SetDSEOriginForTesting(requesting_frame_dse_ptr);
678 698
(...skipping 16 matching lines...) Expand all
695 // origins share the same backoff, which is distinct to the DSE origin. 715 // 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. 716 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff.
697 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_1)); 717 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_1));
698 718
699 // Now check that the LSD is prevented on this origin. 719 // Now check that the LSD is prevented on this origin.
700 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_1)); 720 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_1));
701 721
702 // Now ask on the other non-DSE origin and check backoff prevented the prompt. 722 // Now ask on the other non-DSE origin and check backoff prevented the prompt.
703 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_2)); 723 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_2));
704 724
725 // Test that the right histograms are updated.
726 tester.ExpectTotalCount(
727 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 1);
728 tester.ExpectTotalCount(
729 GeolocationPermissionContextAndroid::kGeolocationLSDShowDSEMetric, 0);
730 tester.ExpectTotalCount(
731 GeolocationPermissionContextAndroid::kGeolocationLSDSuppressNonDSEMetric,
732 2);
733 tester.ExpectTotalCount(
734 GeolocationPermissionContextAndroid::kGeolocationLSDSuppressDSEMetric, 0);
735
705 // Now request on the DSE and check that the LSD is shown, as the non-DSE 736 // Now request on the DSE and check that the LSD is shown, as the non-DSE
706 // backoff should not apply. 737 // backoff should not apply.
707 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_dse)); 738 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame_dse));
708 739
709 // Now check that the DSE is in backoff. 740 // Now check that the DSE is in backoff.
710 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_dse)); 741 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame_dse));
742
743 // Test that the right histograms are updated.
744 tester.ExpectTotalCount(
745 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 1);
746 tester.ExpectTotalCount(
747 GeolocationPermissionContextAndroid::kGeolocationLSDShowDSEMetric, 1);
748 tester.ExpectTotalCount(
749 GeolocationPermissionContextAndroid::kGeolocationLSDSuppressNonDSEMetric,
750 2);
751 tester.ExpectTotalCount(
752 GeolocationPermissionContextAndroid::kGeolocationLSDSuppressDSEMetric, 1);
711 } 753 }
712 754
713 TEST_F(GeolocationPermissionContextTests, LSDBackOffTiming) { 755 TEST_F(GeolocationPermissionContextTests, LSDBackOffTiming) {
756 base::HistogramTester tester;
714 base::test::ScopedFeatureList scoped_feature_list; 757 base::test::ScopedFeatureList scoped_feature_list;
715 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 758 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
716 759
717 GURL requesting_frame("https://www.example.com/geolocation"); 760 GURL requesting_frame("https://www.example.com/geolocation");
718 SetGeolocationContentSetting(requesting_frame, requesting_frame, 761 SetGeolocationContentSetting(requesting_frame, requesting_frame,
719 CONTENT_SETTING_ALLOW); 762 CONTENT_SETTING_ALLOW);
720 763
721 // Turn off system location but allow the LSD to be shown, and denied. 764 // Turn off system location but allow the LSD to be shown, and denied.
722 MockLocationSettings::SetLocationStatus(true /* android */, 765 MockLocationSettings::SetLocationStatus(true /* android */,
723 false /* system */); 766 false /* system */);
724 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 767 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
725 DENIED); 768 DENIED);
726 769
727 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff. 770 // First, cancel a LSD prompt on the first non-DSE origin to go into backoff.
728 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 771 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
729 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 772 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
730 773
731 // Check the LSD is prevented in 6 days time. 774 // Check the LSD is prevented in 6 days time.
732 AddDayOffsetForTesting(6); 775 AddDayOffsetForTesting(6);
733 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 776 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
734 777
778 // Check histograms so far.
779 tester.ExpectTotalCount(
780 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 1);
781 tester.ExpectBucketCount(
782 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric,
783 static_cast<base::HistogramBase::Sample>(
784 GeolocationPermissionContextAndroid::LocationSettingsDialogBackOff::
785 kOneWeek),
786 1);
787
735 // Check it is shown in one more days time, but then not straight after.. 788 // Check it is shown in one more days time, but then not straight after..
736 AddDayOffsetForTesting(1); 789 AddDayOffsetForTesting(1);
737 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 790 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
738 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 791 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
739 792
740 // Check that it isn't shown 29 days after that. 793 // Check that it isn't shown 29 days after that.
741 AddDayOffsetForTesting(29); 794 AddDayOffsetForTesting(29);
742 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 795 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
743 796
797 // Check histograms so far.
798 tester.ExpectTotalCount(
799 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 2);
800 tester.ExpectBucketCount(
801 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric,
802 static_cast<base::HistogramBase::Sample>(
803 GeolocationPermissionContextAndroid::LocationSettingsDialogBackOff::
804 kOneWeek),
805 1);
806
744 // Check it is shown in one more days time, but then not straight after.. 807 // Check it is shown in one more days time, but then not straight after..
745 AddDayOffsetForTesting(1); 808 AddDayOffsetForTesting(1);
746 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 809 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
747 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 810 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
748 811
749 // Check that it isn't shown 89 days after that. 812 // Check that it isn't shown 89 days after that.
750 AddDayOffsetForTesting(89); 813 AddDayOffsetForTesting(89);
751 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 814 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
752 815
753 // Check it is shown in one more days time, but then not straight after.. 816 // Check it is shown in one more days time, but then not straight after..
754 AddDayOffsetForTesting(1); 817 AddDayOffsetForTesting(1);
755 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 818 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
756 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 819 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
757 820
758 // Check that it isn't shown 89 days after that. 821 // Check that it isn't shown 89 days after that.
759 AddDayOffsetForTesting(89); 822 AddDayOffsetForTesting(89);
760 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 823 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
761 824
762 // Check it is shown in one more days time, but then not straight after.. 825 // Check it is shown in one more days time, but then not straight after..
763 AddDayOffsetForTesting(1); 826 AddDayOffsetForTesting(1);
764 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame)); 827 EXPECT_TRUE(RequestPermissionIsLSDShown(requesting_frame));
765 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame)); 828 EXPECT_FALSE(RequestPermissionIsLSDShown(requesting_frame));
829
830 // Check histograms so far.
831 tester.ExpectTotalCount(
832 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric, 5);
833 tester.ExpectBucketCount(
834 GeolocationPermissionContextAndroid::kGeolocationLSDShowNonDSEMetric,
835 static_cast<base::HistogramBase::Sample>(
836 GeolocationPermissionContextAndroid::LocationSettingsDialogBackOff::
837 kThreeMonths),
838 3);
766 } 839 }
767 840
768 TEST_F(GeolocationPermissionContextTests, LSDBackOffPermissionStatus) { 841 TEST_F(GeolocationPermissionContextTests, LSDBackOffPermissionStatus) {
769 base::test::ScopedFeatureList scoped_feature_list; 842 base::test::ScopedFeatureList scoped_feature_list;
770 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt); 843 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
771 844
772 GURL requesting_frame("https://www.example.com/geolocation"); 845 GURL requesting_frame("https://www.example.com/geolocation");
773 SetGeolocationContentSetting(requesting_frame, requesting_frame, 846 SetGeolocationContentSetting(requesting_frame, requesting_frame,
774 CONTENT_SETTING_ALLOW); 847 CONTENT_SETTING_ALLOW);
775 848
(...skipping 671 matching lines...) Expand 10 before | Expand all | Expand 10 after
1447 SetGeolocationContentSetting(requesting_frame, requesting_frame, 1520 SetGeolocationContentSetting(requesting_frame, requesting_frame,
1448 CONTENT_SETTING_BLOCK); 1521 CONTENT_SETTING_BLOCK);
1449 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */, 1522 MockLocationSettings::SetLocationSettingsDialogStatus(true /* enabled */,
1450 GRANTED); 1523 GRANTED);
1451 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED, 1524 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED,
1452 PermissionManager::Get(profile())->GetPermissionStatus( 1525 PermissionManager::Get(profile())->GetPermissionStatus(
1453 content::PermissionType::GEOLOCATION, requesting_frame, 1526 content::PermissionType::GEOLOCATION, requesting_frame,
1454 requesting_frame)); 1527 requesting_frame));
1455 } 1528 }
1456 #endif // defined(OS_ANDROID) 1529 #endif // defined(OS_ANDROID)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698