From be2499efb18ba7588b36eef1483c92c4bccbde3a Mon Sep 17 00:00:00 2001
From: "anthie@google.com" <anthie@google.com>
Date: Wed, 5 Jun 2024 13:58:10 +0000
Subject: [PATCH] Add FL Permissions toggle test

Configure the existing advanced setting toggle test to
check the correct configuration of both Permissions and
Cookies toggles.
Currently the Permissions toggle is also covered by the
c/b/su/extensions_interractive_ui_test as parent of the
Extensions testing via the old toggle, however this will
be replaced by the new extension approval mode which will
not consume this toggle anymore.

Bug: b/321240777
Change-Id: I99ca27fb0670da4fdf33a0ac8a7427443aa1c962
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5588667
Reviewed-by: Tomek Jurkiewicz <tju@google.com>
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Commit-Queue: Anthi Orfanou <anthie@google.com>
Cr-Commit-Position: refs/heads/main@{#1310589}
---
 ...content_restrictions_interactive_uitest.cc | 53 +++++++++------
 .../test_state_seeded_observer.cc             | 65 +++++++++++++------
 2 files changed, 76 insertions(+), 42 deletions(-)

diff --git a/chrome/browser/supervised_user/advanced_content_restrictions_interactive_uitest.cc b/chrome/browser/supervised_user/advanced_content_restrictions_interactive_uitest.cc
index 99079d1f6cd70..d5871f5775bf4 100644
--- a/chrome/browser/supervised_user/advanced_content_restrictions_interactive_uitest.cc
+++ b/chrome/browser/supervised_user/advanced_content_restrictions_interactive_uitest.cc
@@ -31,24 +31,29 @@
 namespace supervised_user {
 namespace {
 
-// UI test for the "Cookies" switch from Family Link parental controls.
-class SupervisedUserFamilyLinkCookiesSwitchUiTest
-    : public InteractiveFamilyLiveTest,
-      public testing::WithParamInterface<
-          std::tuple<FamilyIdentifier, FamilyLinkToggleState>> {
- public:
-  SupervisedUserFamilyLinkCookiesSwitchUiTest()
-      : InteractiveFamilyLiveTest(std::get<0>(GetParam())) {}
+FamilyLinkToggleType GetSwitchType(auto test_param) {
+  return std::get<1>(test_param);
+}
 
-  static FamilyLinkToggleState GetSwitchTargetState() {
-    return std::get<1>(GetParam());
-  }
+FamilyLinkToggleState GetSwitchTargetState(auto test_param) {
+  return std::get<2>(test_param);
+}
+
+// Live test for the Family Link Advanced Settings parental controls switches.
+class SupervisedUserFamilyLinkSwitchTest
+    : public InteractiveFamilyLiveTest,
+      public testing::WithParamInterface<std::tuple<FamilyIdentifier,
+                                                    FamilyLinkToggleType,
+                                                    FamilyLinkToggleState>> {
+ public:
+  SupervisedUserFamilyLinkSwitchTest()
+      : InteractiveFamilyLiveTest(std::get<0>(GetParam())) {}
 };
 
-// Tests that Chrome receives the value of the "Cookies" switch from
+// Tests that Chrome receives the value of the given switch from
 // Family Link parental controls.
-IN_PROC_BROWSER_TEST_P(SupervisedUserFamilyLinkCookiesSwitchUiTest,
-                       CookiesSwitchToggleReceivedByChromeTest) {
+IN_PROC_BROWSER_TEST_P(SupervisedUserFamilyLinkSwitchTest,
+                       SwitchToggleReceivedByChromeTest) {
   DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(BrowserState::Observer,
                                       kDefineStateObserverId);
   TurnOnSyncFor(head_of_household());
@@ -58,25 +63,31 @@ IN_PROC_BROWSER_TEST_P(SupervisedUserFamilyLinkCookiesSwitchUiTest,
   RunTestSequence(WaitForStateSeeding(
       kDefineStateObserverId, head_of_household(), child(),
       BrowserState::AdvancedSettingsToggles({FamilyLinkToggleConfiguration(
-          {.type = FamilyLinkToggleType::kCookiesToggle,
-           .state = GetSwitchTargetState()})})));
+          {.type = GetSwitchType(GetParam()),
+           .state = GetSwitchTargetState(GetParam())})})));
 }
 
 INSTANTIATE_TEST_SUITE_P(
     All,
-    SupervisedUserFamilyLinkCookiesSwitchUiTest,
+    SupervisedUserFamilyLinkSwitchTest,
     testing::Combine(
         testing::Values(FamilyIdentifier("FAMILY_DMA_ELIGIBLE_WITH_CONSENT"),
                         FamilyIdentifier("FAMILY_DMA_ELIGIBLE_NO_CONSENT"),
                         FamilyIdentifier("FAMILY_DMA_INELIGIBLE")),
+        testing::Values(FamilyLinkToggleType::kPermissionsToggle,
+                        FamilyLinkToggleType::kCookiesToggle),
         testing::Values(FamilyLinkToggleState::kEnabled,
                         FamilyLinkToggleState::kDisabled)),
     [](const auto& info) {
       return std::string(std::get<0>(info.param)->data()) +
-             std::string(
-                 (std::get<1>(info.param) == FamilyLinkToggleState::kEnabled
-                      ? "_WithCookiesSwitchOn"
-                      : "_WithCookiesSwitchOff"));
+             std::string((GetSwitchType(info.param) ==
+                                  FamilyLinkToggleType::kCookiesToggle
+                              ? "_ForCookiesSwitch"
+                              : "_ForPermissionsSwitch")) +
+             std::string((GetSwitchTargetState(info.param) ==
+                                  FamilyLinkToggleState::kEnabled
+                              ? "_WithSwitchOn"
+                              : "_WithSwitchOff"));
     });
 }  // namespace
 }  // namespace supervised_user
diff --git a/chrome/test/supervised_user/test_state_seeded_observer.cc b/chrome/test/supervised_user/test_state_seeded_observer.cc
index 2747ef83b087a..f671c9ec8b938 100644
--- a/chrome/test/supervised_user/test_state_seeded_observer.cc
+++ b/chrome/test/supervised_user/test_state_seeded_observer.cc
@@ -19,6 +19,7 @@
 #include "chrome/browser/ui/browser_element_identifiers.h"
 #include "chrome/test/supervised_user/family_member.h"
 #include "components/content_settings/core/browser/host_content_settings_map.h"
+#include "components/content_settings/core/common/content_settings.h"
 #include "components/prefs/pref_service.h"
 #include "components/signin/public/identity_manager/identity_manager.h"
 #include "components/supervised_user/core/browser/fetcher_config.h"
@@ -65,27 +66,6 @@ std::string GetToggleAbbrev(FamilyLinkToggleType toggle) {
   }
 }
 
-bool ToggleHasExpectedValue(const FamilyMember& browser_user,
-                            FamilyLinkToggleConfiguration toggle) {
-  if (toggle.type == FamilyLinkToggleType::kCookiesToggle) {
-    content_settings::ProviderType provider_type;
-    HostContentSettingsMap* map = HostContentSettingsMapFactory::GetForProfile(
-        browser_user.browser()->profile());
-    map->GetDefaultContentSetting(ContentSettingsType::COOKIES, &provider_type);
-    bool can_block_cookies = static_cast<bool>(toggle.state);
-    return can_block_cookies ==
-           (provider_type !=
-            content_settings::ProviderType::kSupervisedProvider);
-  }
-
-  std::string_view pref =
-      toggle.type == FamilyLinkToggleType::kExtensionsToggle
-          ? prefs::kSkipParentApprovalToInstallExtensions
-          : prefs::kSupervisedUserExtensionsMayRequestPermissions;
-  return browser_user.browser()->profile()->GetPrefs()->GetBoolean(pref) ==
-         static_cast<bool>(toggle.state);
-}
-
 net::NetworkTrafficAnnotationTag TestStateSeedTag() {
   return net::DefineNetworkTrafficAnnotation(
       "supervised_user_test_state_seeding",
@@ -247,6 +227,49 @@ bool UrlFiltersAreEmpty(const FamilyMember& family_member) {
       ->GetURLFilter()
       ->IsManualHostsEmpty();
 }
+
+bool ToggleHasExpectedValue(const FamilyMember& browser_user,
+                            FamilyLinkToggleConfiguration toggle) {
+  content_settings::ProviderType provider_type;
+  const HostContentSettingsMap& map =
+      *HostContentSettingsMapFactory::GetForProfile(
+          browser_user.browser()->profile());
+  PrefService& prefs = *browser_user.browser()->profile()->GetPrefs();
+
+  if (toggle.type == FamilyLinkToggleType::kCookiesToggle) {
+    bool can_block_cookies = static_cast<bool>(toggle.state);
+    map.GetDefaultContentSetting(ContentSettingsType::COOKIES, &provider_type);
+    // The supervised user can block the cookies if the corresponding content
+    // provider is not supervised.
+    return can_block_cookies ==
+           (provider_type !=
+            content_settings::ProviderType::kSupervisedProvider);
+  }
+  if (toggle.type == FamilyLinkToggleType::kPermissionsToggle) {
+    bool permission_pref_has_expected_value =
+        prefs.GetBoolean(
+            prefs::kSupervisedUserExtensionsMayRequestPermissions) ==
+        static_cast<bool>(toggle.state);
+
+    // Note: The Family Link permissions toggle is mapped to the above
+    // preference, but with the transition to the updated extension flow the
+    // preference will become deprecated. The switch will still apply to other
+    // features such as blocking geolocation.
+    bool is_geolocation_blocked = !static_cast<bool>(toggle.state);
+    // The supervised user has the geolocation blocked if the corresponding
+    // content setting is blocked.
+    bool is_geolocation_configured =
+        is_geolocation_blocked ==
+        (map.GetDefaultContentSetting(ContentSettingsType::GEOLOCATION,
+                                      &provider_type) ==
+         ContentSetting::CONTENT_SETTING_BLOCK);
+
+    return permission_pref_has_expected_value && is_geolocation_configured;
+  }
+  CHECK(toggle.type == FamilyLinkToggleType::kExtensionsToggle);
+  return prefs.GetBoolean(prefs::kSkipParentApprovalToInstallExtensions) ==
+         static_cast<bool>(toggle.state);
+}
 }  // namespace
 
 BrowserState::~BrowserState() = default;