blob: 0b6843e0510b4e1a6fbf98227692e3ee12f7a951 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2020 The Chromium Authors
James Cook3bd49ab2020-04-02 23:47:552// 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 Wronskae4822652023-05-22 16:45:237#include <string>
Tomasz Jurkiewicz50e6dec2025-05-23 07:59:278#include <string_view>
Aga Wronskae4822652023-05-22 16:45:239
10#include "base/check.h"
[email protected]0ae8f39e2024-07-11 13:51:3111#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
James Cook3bd49ab2020-04-02 23:47:5512#include "chrome/browser/profiles/profile.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1913#include "chrome/browser/profiles/profile_key.h"
14#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]0ae8f39e2024-07-11 13:51:3115#include "components/content_settings/core/browser/host_content_settings_map.h"
16#include "components/content_settings/core/common/content_settings.h"
James Cook3bd49ab2020-04-02 23:47:5517#include "components/prefs/pref_service.h"
Yann Dagod2c2d5192025-06-18 14:32:0418#include "components/signin/public/identity_manager/account_capabilities_test_mutator.h"
Aga Wronskae4822652023-05-22 16:45:2319#include "components/signin/public/identity_manager/account_info.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1920#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
Tomasz Jurkiewicz83b093802025-05-27 17:00:3121#include "components/supervised_user/core/browser/supervised_user_test_environment.h"
Tomasz Jurkiewicze66d4882025-05-15 10:57:5122#include "components/supervised_user/core/browser/supervised_user_utils.h"
[email protected]f1a9899c2023-01-26 16:48:2423#include "components/supervised_user/core/common/pref_names.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1924#include "components/supervised_user/core/common/supervised_user_constants.h"
James Cook3bd49ab2020-04-02 23:47:5525
26namespace supervised_user_test_util {
27
28void AddCustodians(Profile* profile) {
Nohemi Fernandez1ee7df12021-12-06 10:34:0529 DCHECK(profile->IsChild());
James Cook3bd49ab2020-04-02 23:47:5530 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 Fernandezdfc72df62023-03-22 13:08:1942void SetSupervisedUserExtensionsMayRequestPermissionsPref(Profile* profile,
43 bool enabled) {
Nohemi Fernandezdfc72df62023-03-22 13:08:1944 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]0ae8f39e2024-07-11 13:51:3151
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 Fernandezdfc72df62023-03-22 13:08:1962}
63
[email protected]447cb86c2024-02-15 19:12:5464void 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]0ae8f39e2024-07-11 13:51:3179void 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 Wronskae4822652023-05-22 16:45:2395void 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 Dagod2c2d5192025-06-18 14:32:04102 AccountCapabilitiesTestMutator(&info.capabilities)
103 .set_is_subject_to_enterprise_policies(true);
Aga Wronskae4822652023-05-22 16:45:23104
105 CHECK(info.IsValid());
106}
107
Liza Bipin1302f072023-09-19 11:37:50108void SetManualFilterForHost(Profile* profile,
Tomasz Jurkiewicz50e6dec2025-05-23 07:59:27109 std::string_view host,
Liza Bipin1302f072023-09-19 11:37:50110 bool allowlist) {
Tomasz Jurkiewicz83b093802025-05-27 17:00:31111 supervised_user::SupervisedUserTestEnvironment::SetManualFilterForHost(
112 host, allowlist,
113 *SupervisedUserSettingsServiceFactory::GetForKey(
114 profile->GetProfileKey()));
Tomasz Jurkiewicz50e6dec2025-05-23 07:59:27115}
Liza Bipin1302f072023-09-19 11:37:50116
Tomasz Jurkiewicz50e6dec2025-05-23 07:59:27117void SetManualFilterForUrl(Profile* profile,
118 std::string_view url,
119 bool allowlist) {
Tomasz Jurkiewicz83b093802025-05-27 17:00:31120 supervised_user::SupervisedUserTestEnvironment::SetManualFilterForUrl(
121 url, allowlist,
122 *SupervisedUserSettingsServiceFactory::GetForKey(
123 profile->GetProfileKey()));
Liza Bipin1302f072023-09-19 11:37:50124}
125
Tomasz Jurkiewicze66d4882025-05-15 10:57:51126void 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 Jurkiewicz83b093802025-05-27 17:00:31135 supervised_user::SupervisedUserTestEnvironment::SetWebFilterType(
136 web_filter_type, *service);
Tomasz Jurkiewicze66d4882025-05-15 10:57:51137}
138
James Cook3bd49ab2020-04-02 23:47:55139} // namespace supervised_user_test_util