blob: f8887e20fec59be46b5bd01877e83d7e3308fe24 [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>
8
9#include "base/check.h"
[email protected]0ae8f39e2024-07-11 13:51:3110#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
James Cook3bd49ab2020-04-02 23:47:5511#include "chrome/browser/profiles/profile.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1912#include "chrome/browser/profiles/profile_key.h"
13#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
[email protected]0ae8f39e2024-07-11 13:51:3114#include "components/content_settings/core/browser/host_content_settings_map.h"
15#include "components/content_settings/core/common/content_settings.h"
James Cook3bd49ab2020-04-02 23:47:5516#include "components/prefs/pref_service.h"
Aga Wronskae4822652023-05-22 16:45:2317#include "components/signin/public/identity_manager/account_info.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1918#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
Tomasz Jurkiewicze66d4882025-05-15 10:57:5119#include "components/supervised_user/core/browser/supervised_user_utils.h"
[email protected]f1a9899c2023-01-26 16:48:2420#include "components/supervised_user/core/common/pref_names.h"
Nohemi Fernandezdfc72df62023-03-22 13:08:1921#include "components/supervised_user/core/common/supervised_user_constants.h"
James Cook3bd49ab2020-04-02 23:47:5522
23namespace supervised_user_test_util {
24
25void AddCustodians(Profile* profile) {
Nohemi Fernandez1ee7df12021-12-06 10:34:0526 DCHECK(profile->IsChild());
James Cook3bd49ab2020-04-02 23:47:5527 PrefService* prefs = profile->GetPrefs();
28 prefs->SetString(prefs::kSupervisedUserCustodianEmail,
29 "[email protected]");
30 prefs->SetString(prefs::kSupervisedUserCustodianObfuscatedGaiaId,
31 "239029320");
32
33 prefs->SetString(prefs::kSupervisedUserSecondCustodianEmail,
34 "[email protected]");
35 prefs->SetString(prefs::kSupervisedUserSecondCustodianObfuscatedGaiaId,
36 "85948533");
37}
38
Nohemi Fernandezdfc72df62023-03-22 13:08:1939void SetSupervisedUserExtensionsMayRequestPermissionsPref(Profile* profile,
40 bool enabled) {
Nohemi Fernandezdfc72df62023-03-22 13:08:1941 supervised_user::SupervisedUserSettingsService* settings_service =
42 SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey(
43 profile->GetProfileKey());
44 settings_service->SetLocalSetting(supervised_user::kGeolocationDisabled,
45 base::Value(!enabled));
46 profile->GetPrefs()->SetBoolean(
47 prefs::kSupervisedUserExtensionsMayRequestPermissions, enabled);
[email protected]0ae8f39e2024-07-11 13:51:3148
49 // Geolocation content setting is also set to the same value. See
50 // SupervisedUsePrefStore.
51 content_settings::ProviderType provider;
52 bool is_geolocation_allowed =
53 HostContentSettingsMapFactory::GetForProfile(profile)
54 ->GetDefaultContentSetting(ContentSettingsType::GEOLOCATION,
55 &provider) == CONTENT_SETTING_ALLOW;
56 if (is_geolocation_allowed != enabled) {
57 SetSupervisedUserGeolocationEnabledContentSetting(profile, enabled);
58 }
Nohemi Fernandezdfc72df62023-03-22 13:08:1959}
60
[email protected]447cb86c2024-02-15 19:12:5461void SetSkipParentApprovalToInstallExtensionsPref(Profile* profile,
62 bool enabled) {
63 // TODO(b/324898798): Once the new extension handling mode is releaded, this
64 // method replaces `SetSupervisedUserExtensionsMayRequestPermissionsPref` for
65 // handling the Extensions behaviour.
66 supervised_user::SupervisedUserSettingsService* settings_service =
67 SupervisedUserSettingsServiceFactory::GetInstance()->GetForKey(
68 profile->GetProfileKey());
69 settings_service->SetLocalSetting(
70 supervised_user::kSkipParentApprovalToInstallExtensions,
71 base::Value(enabled));
72 profile->GetPrefs()->SetBoolean(prefs::kSkipParentApprovalToInstallExtensions,
73 enabled);
74}
75
[email protected]0ae8f39e2024-07-11 13:51:3176void SetSupervisedUserGeolocationEnabledContentSetting(Profile* profile,
77 bool enabled) {
78 HostContentSettingsMapFactory::GetForProfile(profile)
79 ->SetDefaultContentSetting(
80 ContentSettingsType::GEOLOCATION,
81 enabled ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
82#if BUILDFLAG(ENABLE_EXTENSIONS)
83 if (profile->GetPrefs()->GetBoolean(
84 prefs::kSupervisedUserExtensionsMayRequestPermissions) != enabled) {
85 // Permissions preference is also set to the same value. See
86 // SupervisedUsePrefStore.
87 SetSupervisedUserExtensionsMayRequestPermissionsPref(profile, enabled);
88 }
89#endif // BUILDFLAG(ENABLE_EXTENSIONS)
90}
91
Aga Wronskae4822652023-05-22 16:45:2392void PopulateAccountInfoWithName(AccountInfo& info,
93 const std::string& given_name) {
94 info.given_name = given_name;
95 info.full_name = "fullname";
96 info.hosted_domain = "example.com";
97 info.locale = "en";
98 info.picture_url = "https://example.com";
99
100 CHECK(info.IsValid());
101}
102
Liza Bipin1302f072023-09-19 11:37:50103void SetManualFilterForHost(Profile* profile,
104 const std::string& host,
105 bool allowlist) {
106 supervised_user::SupervisedUserSettingsService* settings_service =
107 SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey());
108
109 const base::Value::Dict& local_settings =
110 settings_service->LocalSettingsForTest();
111 base::Value::Dict dict_to_insert;
112
113 if (const base::Value::Dict* dict_value = local_settings.FindDict(
114 supervised_user::kContentPackManualBehaviorHosts)) {
115 dict_to_insert = dict_value->Clone();
116 }
117
118 dict_to_insert.Set(host, allowlist);
119 settings_service->SetLocalSetting(
120 supervised_user::kContentPackManualBehaviorHosts,
121 std::move(dict_to_insert));
122}
123
Tomasz Jurkiewicze66d4882025-05-15 10:57:51124void SetWebFilterType(const Profile* profile,
125 supervised_user::WebFilterType web_filter_type) {
126 supervised_user::SupervisedUserSettingsService* service =
127 SupervisedUserSettingsServiceFactory::GetForKey(profile->GetProfileKey());
128 CHECK(service) << "Missing settings service might indicate misconfigured "
129 "test environment. If this is a unittest, consider using "
130 "SupervisedUserSyncDataFake";
131 CHECK(service->IsReady())
132 << "If settings service is not ready, the change will not be successful";
133
134 switch (web_filter_type) {
135 case supervised_user::WebFilterType::kAllowAllSites:
136 service->SetLocalSetting(
137 supervised_user::kContentPackDefaultFilteringBehavior,
138 base::Value(
139 static_cast<int>(supervised_user::FilteringBehavior::kAllow)));
140 service->SetLocalSetting(supervised_user::kSafeSitesEnabled,
141 base::Value(false));
142 break;
143 case supervised_user::WebFilterType::kTryToBlockMatureSites:
144 service->SetLocalSetting(
145 supervised_user::kContentPackDefaultFilteringBehavior,
146 base::Value(
147 static_cast<int>(supervised_user::FilteringBehavior::kAllow)));
148 service->SetLocalSetting(supervised_user::kSafeSitesEnabled,
149 base::Value(true));
150 break;
151 case supervised_user::WebFilterType::kCertainSites:
152 service->SetLocalSetting(
153 supervised_user::kContentPackDefaultFilteringBehavior,
154 base::Value(
155 static_cast<int>(supervised_user::FilteringBehavior::kBlock)));
156
157 // Value of kSupervisedUserSafeSites is not important here.
158 break;
159 case supervised_user::WebFilterType::kMixed:
160 NOTREACHED() << "That value is not intended to be set, but is rather "
161 "used to indicate multiple settings used in profiles "
162 "in metrics.";
163 }
164}
165
James Cook3bd49ab2020-04-02 23:47:55166} // namespace supervised_user_test_util