Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/supervised_user/supervised_user_test_util.h" |
| 6 | |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 7 | #include <string> |
Tomasz Jurkiewicz | 50e6dec | 2025-05-23 07:59:27 | [diff] [blame] | 8 | #include <string_view> |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 9 | |
| 10 | #include "base/check.h" |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 11 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 12 | #include "chrome/browser/profiles/profile.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile_key.h" |
| 14 | #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h" |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 15 | #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 | #include "components/content_settings/core/common/content_settings.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 17 | #include "components/prefs/pref_service.h" |
Yann Dago | d2c2d519 | 2025-06-18 14:32:04 | [diff] [blame] | 18 | #include "components/signin/public/identity_manager/account_capabilities_test_mutator.h" |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 19 | #include "components/signin/public/identity_manager/account_info.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 20 | #include "components/supervised_user/core/browser/supervised_user_settings_service.h" |
Tomasz Jurkiewicz | 83b09380 | 2025-05-27 17:00:31 | [diff] [blame] | 21 | #include "components/supervised_user/core/browser/supervised_user_test_environment.h" |
Tomasz Jurkiewicz | e66d488 | 2025-05-15 10:57:51 | [diff] [blame] | 22 | #include "components/supervised_user/core/browser/supervised_user_utils.h" |
[email protected] | f1a9899c | 2023-01-26 16:48:24 | [diff] [blame] | 23 | #include "components/supervised_user/core/common/pref_names.h" |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 24 | #include "components/supervised_user/core/common/supervised_user_constants.h" |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 25 | |
| 26 | namespace supervised_user_test_util { |
| 27 | |
| 28 | void AddCustodians(Profile* profile) { |
Nohemi Fernandez | 1ee7df1 | 2021-12-06 10:34:05 | [diff] [blame] | 29 | DCHECK(profile->IsChild()); |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 30 | PrefService* prefs = profile->GetPrefs(); |
| 31 | prefs->SetString(prefs::kSupervisedUserCustodianEmail, |
| 32 | "[email protected]"); |
| 33 | prefs->SetString(prefs::kSupervisedUserCustodianObfuscatedGaiaId, |
| 34 | "239029320"); |
| 35 | |
| 36 | prefs->SetString(prefs::kSupervisedUserSecondCustodianEmail, |
| 37 | "[email protected]"); |
| 38 | prefs->SetString(prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId, |
| 39 | "85948533"); |
| 40 | } |
| 41 | |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 42 | void SetSupervisedUserExtensionsMayRequestPermissionsPref(Profile* profile, |
| 43 | bool enabled) { |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 44 | supervised_user::SupervisedUserSettingsService* settings_service = |
| 45 | SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey( |
| 46 | profile->GetProfileKey()); |
| 47 | settings_service->SetLocalSetting(supervised_user::kGeolocationDisabled, |
| 48 | base::Value(!enabled)); |
| 49 | profile->GetPrefs()->SetBoolean( |
| 50 | prefs::kSupervisedUserExtensionsMayRequestPermissions, enabled); |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 51 | |
| 52 | // Geolocation content setting is also set to the same value. See |
| 53 | // SupervisedUsePrefStore. |
| 54 | content_settings::ProviderType provider; |
| 55 | bool is_geolocation_allowed = |
| 56 | HostContentSettingsMapFactory::GetForProfile(profile) |
| 57 | ->GetDefaultContentSetting(ContentSettingsType::GEOLOCATION, |
| 58 | &provider) == CONTENT_SETTING_ALLOW; |
| 59 | if (is_geolocation_allowed != enabled) { |
| 60 | SetSupervisedUserGeolocationEnabledContentSetting(profile, enabled); |
| 61 | } |
Nohemi Fernandez | dfc72df6 | 2023-03-22 13:08:19 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 447cb86c | 2024-02-15 19:12:54 | [diff] [blame] | 64 | void SetSkipParentApprovalToInstallExtensionsPref(Profile* profile, |
| 65 | bool enabled) { |
| 66 | // TODO(b/324898798): Once the new extension handling mode is releaded, this |
| 67 | // method replaces `SetSupervisedUserExtensionsMayRequestPermissionsPref` for |
| 68 | // handling the Extensions behaviour. |
| 69 | supervised_user::SupervisedUserSettingsService* settings_service = |
| 70 | SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey( |
| 71 | profile->GetProfileKey()); |
| 72 | settings_service->SetLocalSetting( |
| 73 | supervised_user::kSkipParentApprovalToInstallExtensions, |
| 74 | base::Value(enabled)); |
| 75 | profile->GetPrefs()->SetBoolean(prefs::kSkipParentApprovalToInstallExtensions, |
| 76 | enabled); |
| 77 | } |
| 78 | |
[email protected] | 0ae8f39e | 2024-07-11 13:51:31 | [diff] [blame] | 79 | void SetSupervisedUserGeolocationEnabledContentSetting(Profile* profile, |
| 80 | bool enabled) { |
| 81 | HostContentSettingsMapFactory::GetForProfile(profile) |
| 82 | ->SetDefaultContentSetting( |
| 83 | ContentSettingsType::GEOLOCATION, |
| 84 | enabled ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK); |
| 85 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 86 | if (profile->GetPrefs()->GetBoolean( |
| 87 | prefs::kSupervisedUserExtensionsMayRequestPermissions) != enabled) { |
| 88 | // Permissions preference is also set to the same value. See |
| 89 | // SupervisedUsePrefStore. |
| 90 | SetSupervisedUserExtensionsMayRequestPermissionsPref(profile, enabled); |
| 91 | } |
| 92 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
| 93 | } |
| 94 | |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 95 | void PopulateAccountInfoWithName(AccountInfo& info, |
| 96 | const std::string& given_name) { |
| 97 | info.given_name = given_name; |
| 98 | info.full_name = "fullname"; |
| 99 | info.hosted_domain = "example.com"; |
| 100 | info.locale = "en"; |
| 101 | info.picture_url = "https://example.com"; |
Yann Dago | d2c2d519 | 2025-06-18 14:32:04 | [diff] [blame] | 102 | AccountCapabilitiesTestMutator(&info.capabilities) |
| 103 | .set_is_subject_to_enterprise_policies(true); |
Aga Wronska | e482265 | 2023-05-22 16:45:23 | [diff] [blame] | 104 | |
| 105 | CHECK(info.IsValid()); |
| 106 | } |
| 107 | |
Liza Bipin | 1302f07 | 2023-09-19 11:37:50 | [diff] [blame] | 108 | void SetManualFilterForHost(Profile* profile, |
Tomasz Jurkiewicz | 50e6dec | 2025-05-23 07:59:27 | [diff] [blame] | 109 | std::string_view host, |
Liza Bipin | 1302f07 | 2023-09-19 11:37:50 | [diff] [blame] | 110 | bool allowlist) { |
Tomasz Jurkiewicz | 83b09380 | 2025-05-27 17:00:31 | [diff] [blame] | 111 | supervised_user::SupervisedUserTestEnvironment::SetManualFilterForHost( |
| 112 | host, allowlist, |
| 113 | *SupervisedUserSettingsServiceFactory::GetForKey( |
| 114 | profile->GetProfileKey())); |
Tomasz Jurkiewicz | 50e6dec | 2025-05-23 07:59:27 | [diff] [blame] | 115 | } |
Liza Bipin | 1302f07 | 2023-09-19 11:37:50 | [diff] [blame] | 116 | |
Tomasz Jurkiewicz | 50e6dec | 2025-05-23 07:59:27 | [diff] [blame] | 117 | void SetManualFilterForUrl(Profile* profile, |
| 118 | std::string_view url, |
| 119 | bool allowlist) { |
Tomasz Jurkiewicz | 83b09380 | 2025-05-27 17:00:31 | [diff] [blame] | 120 | supervised_user::SupervisedUserTestEnvironment::SetManualFilterForUrl( |
| 121 | url, allowlist, |
| 122 | *SupervisedUserSettingsServiceFactory::GetForKey( |
| 123 | profile->GetProfileKey())); |
Liza Bipin | 1302f07 | 2023-09-19 11:37:50 | [diff] [blame] | 124 | } |
| 125 | |
Tomasz Jurkiewicz | e66d488 | 2025-05-15 10:57:51 | [diff] [blame] | 126 | void SetWebFilterType(const Profile* profile, |
| 127 | supervised_user::WebFilterType web_filter_type) { |
| 128 | supervised_user::SupervisedUserSettingsService* service = |
| 129 | SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey()); |
| 130 | CHECK(service) << "Missing settings service might indicate misconfigured " |
| 131 | "test environment. If this is a unittest, consider using " |
| 132 | "SupervisedUserSyncDataFake"; |
| 133 | CHECK(service->IsReady()) |
| 134 | << "If settings service is not ready, the change will not be successful"; |
Tomasz Jurkiewicz | 83b09380 | 2025-05-27 17:00:31 | [diff] [blame] | 135 | supervised_user::SupervisedUserTestEnvironment::SetWebFilterType( |
| 136 | web_filter_type, *service); |
Tomasz Jurkiewicz | e66d488 | 2025-05-15 10:57:51 | [diff] [blame] | 137 | } |
| 138 | |
James Cook | 3bd49ab | 2020-04-02 23:47:55 | [diff] [blame] | 139 | } // namespace supervised_user_test_util |