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

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

Issue 2742373003: Limit the amount the Location Settings Dialog will be shown to users. (Closed)
Patch Set: Created 3 years, 9 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>
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 void AddNewTab(const GURL& url); 173 void AddNewTab(const GURL& url);
174 void CheckTabContentsState(const GURL& requesting_frame, 174 void CheckTabContentsState(const GURL& requesting_frame,
175 ContentSetting expected_content_setting); 175 ContentSetting expected_content_setting);
176 #if !defined(OS_ANDROID) 176 #if !defined(OS_ANDROID)
177 void SetupRequestManager(content::WebContents* web_contents); 177 void SetupRequestManager(content::WebContents* web_contents);
178 size_t GetBubblesQueueSize(PermissionRequestManager* manager); 178 size_t GetBubblesQueueSize(PermissionRequestManager* manager);
179 void AcceptBubble(PermissionRequestManager* manager); 179 void AcceptBubble(PermissionRequestManager* manager);
180 void DenyBubble(PermissionRequestManager* manager); 180 void DenyBubble(PermissionRequestManager* manager);
181 void CloseBubble(PermissionRequestManager* manager); 181 void CloseBubble(PermissionRequestManager* manager);
182 #endif 182 #endif
183 #if defined(OS_ANDROID)
184 void CheckThatLSDIsShown(const GURL& origin, bool expected_shown);
185 void CheckThatLSDIsShownWithPermissionPrompt(const GURL& origin,
186 bool expected_shown);
187 void AddDayOffsetForTesting(int days);
188 void SetDSEOriginForTesting(const GURL& dse_origin);
189 #endif
183 void RequestManagerDocumentLoadCompleted(); 190 void RequestManagerDocumentLoadCompleted();
184 void RequestManagerDocumentLoadCompleted(content::WebContents* web_contents); 191 void RequestManagerDocumentLoadCompleted(content::WebContents* web_contents);
185 ContentSetting GetGeolocationContentSetting(GURL frame_0, GURL frame_1); 192 ContentSetting GetGeolocationContentSetting(GURL frame_0, GURL frame_1);
186 void SetGeolocationContentSetting(GURL frame_0, 193 void SetGeolocationContentSetting(GURL frame_0,
187 GURL frame_1, 194 GURL frame_1,
188 ContentSetting content_setting); 195 ContentSetting content_setting);
189 size_t GetNumberOfPrompts(); 196 size_t GetNumberOfPrompts();
190 void AcceptPrompt(); 197 void AcceptPrompt();
191 base::string16 GetPromptText(); 198 base::string16 GetPromptText();
192 199
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 PermissionRequestManager* manager) { 373 PermissionRequestManager* manager) {
367 manager->Deny(); 374 manager->Deny();
368 } 375 }
369 376
370 void GeolocationPermissionContextTests::CloseBubble( 377 void GeolocationPermissionContextTests::CloseBubble(
371 PermissionRequestManager* manager) { 378 PermissionRequestManager* manager) {
372 manager->Closing(); 379 manager->Closing();
373 } 380 }
374 #endif 381 #endif
375 382
383 #if defined(OS_ANDROID)
384 void GeolocationPermissionContextTests::CheckThatLSDIsShown(
385 const GURL& origin,
386 bool expected_shown) {
387 NavigateAndCommit(origin);
388 MockLocationSettings::ClearHasShownLocationSettingsDialog();
389 RequestGeolocationPermission(web_contents(), RequestID(0), origin, true);
390
391 EXPECT_EQ(MockLocationSettings::HasShownLocationSettingsDialog(),
392 expected_shown);
raymes 2017/03/15 04:28:25 optional: alternatively you can return HasShownLoc
benwells 2017/03/15 23:10:49 Yeah that's much nicer, done.
393 }
394
395 void GeolocationPermissionContextTests::CheckThatLSDIsShownWithPermissionPrompt(
396 const GURL& origin,
397 bool expected_shown) {
398 NavigateAndCommit(origin);
399 MockLocationSettings::ClearHasShownLocationSettingsDialog();
400 RequestGeolocationPermission(web_contents(), RequestID(0), origin, true);
401
402 EXPECT_EQ(1U, infobar_service()->infobar_count());
403 ConfirmInfoBarDelegate* infobar_delegate =
404 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
405 ASSERT_TRUE(infobar_delegate);
406 infobar_delegate->Accept();
407
408 EXPECT_EQ(MockLocationSettings::HasShownLocationSettingsDialog(),
409 expected_shown);
410 }
411
412 void GeolocationPermissionContextTests::AddDayOffsetForTesting(int days) {
413 GeolocationPermissionContextAndroid::AddDayOffsetForTesting(days);
414 }
415
416 void GeolocationPermissionContextTests::SetDSEOriginForTesting(
417 const GURL& dse_origin) {
418 GeolocationPermissionContextAndroid::SetDSEOriginForTesting(dse_origin);
419 }
420 #endif
421
376 void GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted() { 422 void GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted() {
377 GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted( 423 GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted(
378 web_contents()); 424 web_contents());
379 } 425 }
380 426
381 void GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted( 427 void GeolocationPermissionContextTests::RequestManagerDocumentLoadCompleted(
382 content::WebContents* web_contents) { 428 content::WebContents* web_contents) {
383 #if !defined(OS_ANDROID) 429 #if !defined(OS_ANDROID)
384 PermissionRequestManager::FromWebContents(web_contents)-> 430 PermissionRequestManager::FromWebContents(web_contents)->
385 DocumentOnLoadCompletedInMainFrame(); 431 DocumentOnLoadCompletedInMainFrame();
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 true); 657 true);
612 EXPECT_EQ(1U, infobar_service()->infobar_count()); 658 EXPECT_EQ(1U, infobar_service()->infobar_count());
613 ConfirmInfoBarDelegate* infobar_delegate = 659 ConfirmInfoBarDelegate* infobar_delegate =
614 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate(); 660 infobar_service()->infobar_at(0)->delegate()->AsConfirmInfoBarDelegate();
615 ASSERT_TRUE(infobar_delegate); 661 ASSERT_TRUE(infobar_delegate);
616 infobar_delegate->Accept(); 662 infobar_delegate->Accept();
617 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK); 663 CheckTabContentsState(requesting_frame, CONTENT_SETTING_BLOCK);
618 CheckPermissionMessageSent(0, false); 664 CheckPermissionMessageSent(0, false);
619 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog()); 665 EXPECT_TRUE(MockLocationSettings::HasShownLocationSettingsDialog());
620 } 666 }
667
668 TEST_F(GeolocationPermissionContextTests, LSDBackOffDifferentSites) {
669 base::test::ScopedFeatureList scoped_feature_list;
670 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
671
672 GURL requesting_frame_1("https://www.example.com/geolocation");
673 GURL requesting_frame_2("https://www.example-2.com/geolocation");
674 GURL requesting_frame_dse("https://www.dse.com/geolocation");
675
676 SetDSEOriginForTesting(requesting_frame_dse);
677
678 // Set all origin geolocation permissions to ALLOW.
679 SetGeolocationContentSetting(requesting_frame_1, requesting_frame_1,
680 CONTENT_SETTING_ALLOW);
681 SetGeolocationContentSetting(requesting_frame_2, requesting_frame_2,
682 CONTENT_SETTING_ALLOW);
683 SetGeolocationContentSetting(requesting_frame_dse, requesting_frame_dse,
684 CONTENT_SETTING_ALLOW);
685
686 // Turn off system location but allow the LSD to be shown, and denied.
687 MockLocationSettings::SetLocationStatus(true /* android */,
688 false /* system */);
689 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
raymes 2017/03/15 04:28:25 nit: here and elsewhere, please document the boole
benwells 2017/03/15 23:10:49 Done.
690
691 // Now permission requests should trigger the LSD, but the LSD will be denied,
692 // putting the requesting origins into backoff. Check that the two non-DSE
693 // origins share the same backoff, which is distinct to the DSE origin.
694 // First, cancel a LSE prompt on the first non-DSE origin to go into backoff.
695 CheckThatLSDIsShown(requesting_frame_1, true /* expected_shown */);
696
697 // Now check that the LSD is prevented on this origin.
698 CheckThatLSDIsShown(requesting_frame_1, false /* expected_shown */);
699
700 // Now ask on the other non-DSE origin and check backoff prevented the prompt.
701 CheckThatLSDIsShown(requesting_frame_2, false /* expected_shown */);
702
703 // Now request on the DSE and check that the LSD is shown, as the non-DSE
704 // backoff should not apply.
705 CheckThatLSDIsShown(requesting_frame_dse, true /* expected_shown */);
706
707 // Now check that the DSE is in backoff.
708 CheckThatLSDIsShown(requesting_frame_dse, false /* expected_shown */);
709 }
710
711 TEST_F(GeolocationPermissionContextTests, LSDBackOffTiming) {
712 base::test::ScopedFeatureList scoped_feature_list;
713 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
714
715 GURL requesting_frame("https://www.example.com/geolocation");
716 SetGeolocationContentSetting(requesting_frame, requesting_frame,
717 CONTENT_SETTING_ALLOW);
718
719 // Turn off system location but allow the LSD to be shown, and denied.
720 MockLocationSettings::SetLocationStatus(true /* android */,
721 false /* system */);
722 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
723
724 // First, cancel a LSE prompt on the first non-DSE origin to go into backoff.
raymes 2017/03/15 04:28:25 nit: LSD
benwells 2017/03/15 23:10:49 Done.
725 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
726 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
727
728 // Check the LSD is prevented in 6 days time.
729 AddDayOffsetForTesting(6);
730 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
731
732 // Check it is shown in one more days time, but then not straight after..
733 AddDayOffsetForTesting(1);
734 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
735 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
736
737 // Check that it isn't shown 29 days after that.
738 AddDayOffsetForTesting(29);
739 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
740
741 // Check it is shown in one more days time, but then not straight after..
742 AddDayOffsetForTesting(1);
743 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
744 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
745
746 // Check that it isn't shown 89 days after that.
747 AddDayOffsetForTesting(89);
748 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
749
750 // Check it is shown in one more days time, but then not straight after..
751 AddDayOffsetForTesting(1);
752 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
753 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
754
755 // Check that it isn't shown 89 days after that.
756 AddDayOffsetForTesting(89);
757 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
758
759 // Check it is shown in one more days time, but then not straight after..
760 AddDayOffsetForTesting(1);
761 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
762 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
763 }
764
765 TEST_F(GeolocationPermissionContextTests, LSDBackOffPermissionStatus) {
766 base::test::ScopedFeatureList scoped_feature_list;
767 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
768
769 GURL requesting_frame("https://www.example.com/geolocation");
770 SetGeolocationContentSetting(requesting_frame, requesting_frame,
771 CONTENT_SETTING_ALLOW);
772
773 // Turn off system location but allow the LSD to be shown, and denied.
774 MockLocationSettings::SetLocationStatus(true /* android */,
775 false /* system */);
776 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
777
778 // The permission status should reflect that the LSD will be shown.
779 ASSERT_EQ(blink::mojom::PermissionStatus::ASK,
780 PermissionManager::Get(profile())->GetPermissionStatus(
781 content::PermissionType::GEOLOCATION, requesting_frame,
782 requesting_frame));
783 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
784
785 // Now that the LSD is in backoff, the permission status should reflect it.
786 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
787 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED,
788 PermissionManager::Get(profile())->GetPermissionStatus(
789 content::PermissionType::GEOLOCATION, requesting_frame,
790 requesting_frame));
791 }
792
793 TEST_F(GeolocationPermissionContextTests, LSDBackOffAskPromptsDespiteBackOff) {
794 base::test::ScopedFeatureList scoped_feature_list;
795 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
796
797 GURL requesting_frame("https://www.example.com/geolocation");
798 SetGeolocationContentSetting(requesting_frame, requesting_frame,
799 CONTENT_SETTING_ALLOW);
800
801 // Turn off system location but allow the LSD to be shown, and denied.
802 MockLocationSettings::SetLocationStatus(true /* android */,
803 false /* system */);
804 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
805
806 // First, cancel a LSE prompt on the first non-DSE origin to go into backoff.
raymes 2017/03/15 04:28:25 nit: LSD
benwells 2017/03/15 23:10:49 Done.
807 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
808 CheckThatLSDIsShown(requesting_frame, false) /* expected_shown */;
809
810 // Set the content setting back to ASK. The permission status should be
811 // prompt, and the LSD prompt should now be shown.
812 SetGeolocationContentSetting(requesting_frame, requesting_frame,
813 CONTENT_SETTING_ASK);
814 ASSERT_EQ(blink::mojom::PermissionStatus::ASK,
815 PermissionManager::Get(profile())->GetPermissionStatus(
816 content::PermissionType::GEOLOCATION, requesting_frame,
817 requesting_frame));
818 CheckThatLSDIsShownWithPermissionPrompt(requesting_frame,
819 true /* expected_shown */);
820 }
821
822 TEST_F(GeolocationPermissionContextTests,
823 LSDBackOffAcceptPermissionResetsBackOff) {
824 base::test::ScopedFeatureList scoped_feature_list;
825 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
826
827 GURL requesting_frame("https://www.example.com/geolocation");
828 SetGeolocationContentSetting(requesting_frame, requesting_frame,
829 CONTENT_SETTING_ALLOW);
830
831 // Turn off system location but allow the LSD to be shown, and denied.
832 MockLocationSettings::SetLocationStatus(true /* android */,
833 false /* system */);
834 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
835
836 // First, get into the highest backoff state.
837 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
838 AddDayOffsetForTesting(7);
839 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
840 AddDayOffsetForTesting(30);
841 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
842 AddDayOffsetForTesting(90);
843 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
844
845 // Now accept a permissions prompt.
846 SetGeolocationContentSetting(requesting_frame, requesting_frame,
847 CONTENT_SETTING_ASK);
848 CheckThatLSDIsShownWithPermissionPrompt(requesting_frame,
849 true /* expected_shown */);
850
851 // And check that back in the lowest backoff state.
852 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
853 AddDayOffsetForTesting(7);
854 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
855 }
856
857 TEST_F(GeolocationPermissionContextTests, LSDBackOffAcceptLSDResetsBackOff) {
858 base::test::ScopedFeatureList scoped_feature_list;
859 scoped_feature_list.InitAndEnableFeature(features::kLsdPermissionPrompt);
860
861 GURL requesting_frame("https://www.example.com/geolocation");
862 SetGeolocationContentSetting(requesting_frame, requesting_frame,
863 CONTENT_SETTING_ALLOW);
864
865 // Turn off system location but allow the LSD to be shown, and denied.
866 MockLocationSettings::SetLocationStatus(true /* android */,
867 false /* system */);
868 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
869
870 // First, get into the highest backoff state.
871 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
872 AddDayOffsetForTesting(7);
873 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
874 AddDayOffsetForTesting(30);
875 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
876
877 // Now accept the LSD.
878 AddDayOffsetForTesting(90);
879 MockLocationSettings::SetLocationSettingsDialogStatus(true, GRANTED);
880 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
881
882 // Check that not in backoff, and that at the lowest backoff state.
883 MockLocationSettings::SetLocationSettingsDialogStatus(true, DENIED);
884 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
885 CheckThatLSDIsShown(requesting_frame, false /* expected_shown */);
886 AddDayOffsetForTesting(7);
887 CheckThatLSDIsShown(requesting_frame, true /* expected_shown */);
888 }
621 #endif 889 #endif
622 890
623 TEST_F(GeolocationPermissionContextTests, QueuedPermission) { 891 TEST_F(GeolocationPermissionContextTests, QueuedPermission) {
624 GURL requesting_frame_0("https://www.example.com/geolocation"); 892 GURL requesting_frame_0("https://www.example.com/geolocation");
625 GURL requesting_frame_1("https://www.example-2.com/geolocation"); 893 GURL requesting_frame_1("https://www.example-2.com/geolocation");
626 EXPECT_EQ( 894 EXPECT_EQ(
627 CONTENT_SETTING_ASK, 895 CONTENT_SETTING_ASK,
628 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_1)); 896 GetGeolocationContentSetting(requesting_frame_0, requesting_frame_1));
629 EXPECT_EQ( 897 EXPECT_EQ(
630 CONTENT_SETTING_ASK, 898 CONTENT_SETTING_ASK,
(...skipping 533 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 // permission status should still be BLOCK. 1432 // permission status should still be BLOCK.
1165 SetGeolocationContentSetting(requesting_frame, requesting_frame, 1433 SetGeolocationContentSetting(requesting_frame, requesting_frame,
1166 CONTENT_SETTING_BLOCK); 1434 CONTENT_SETTING_BLOCK);
1167 MockLocationSettings::SetLocationSettingsDialogStatus(true, GRANTED); 1435 MockLocationSettings::SetLocationSettingsDialogStatus(true, GRANTED);
1168 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED, 1436 ASSERT_EQ(blink::mojom::PermissionStatus::DENIED,
1169 PermissionManager::Get(profile())->GetPermissionStatus( 1437 PermissionManager::Get(profile())->GetPermissionStatus(
1170 content::PermissionType::GEOLOCATION, requesting_frame, 1438 content::PermissionType::GEOLOCATION, requesting_frame,
1171 requesting_frame)); 1439 requesting_frame));
1172 } 1440 }
1173 #endif // defined(OS_ANDROID) 1441 #endif // defined(OS_ANDROID)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698