1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/enterprise/util/managed_browser_utils.h"
#include <vector>
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/enterprise/signals/user_permission_service_factory.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/pref_names.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/device_signals/core/browser/user_permission_service.h"
#include "components/policy/core/common/management/management_service.h"
#include "components/policy/core/common/management/scoped_management_service_override_for_testing.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test.h"
namespace enterprise_util {
namespace {
class ManagedBrowserUtilsBrowserTest
: public policy::PolicyTest,
public testing::WithParamInterface<bool> {
public:
ManagedBrowserUtilsBrowserTest() = default;
~ManagedBrowserUtilsBrowserTest() override = default;
bool managed_policy() { return GetParam(); }
base::Value policy_value() {
constexpr char kAutoSelectCertificateValue[] = R"({
"pattern": "https://foo.com",
"filter": {
"ISSUER": {
"O": "Chrome",
"OU": "Chrome Org Unit",
"CN": "Chrome Common Name"
}
}
})";
base::Value::List list;
list.Append(kAutoSelectCertificateValue);
return base::Value(std::move(list));
}
};
INSTANTIATE_TEST_SUITE_P(, ManagedBrowserUtilsBrowserTest, testing::Bool());
} // namespace
IN_PROC_BROWSER_TEST_P(ManagedBrowserUtilsBrowserTest, LocalState) {
EXPECT_FALSE(
IsMachinePolicyPref(prefs::kManagedAutoSelectCertificateForUrls));
policy::PolicyMap policies;
policies.Set(policy::key::kAutoSelectCertificateForUrls,
managed_policy() ? policy::POLICY_LEVEL_MANDATORY
: policy::POLICY_LEVEL_RECOMMENDED,
policy::POLICY_SCOPE_MACHINE, policy::POLICY_SOURCE_CLOUD,
policy_value(), nullptr);
UpdateProviderPolicy(policies);
EXPECT_EQ(managed_policy(),
IsMachinePolicyPref(prefs::kManagedAutoSelectCertificateForUrls));
}
#if !BUILDFLAG(IS_CHROMEOS)
class EnterpriseProfileBadgingTest
: public InProcessBrowserTest,
public testing::WithParamInterface<std::tuple<bool, bool, bool, bool>> {
public:
void SetUp() override {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
if (avatar_feature_enabled()) {
enabled_features.emplace_back(
features::kEnterpriseProfileBadgingForAvatar);
} else {
disabled_features.emplace_back(
features::kEnterpriseProfileBadgingForAvatar);
}
if (profile_menu_feature_enabled()) {
enabled_features.emplace_back(features::kEnterpriseProfileBadgingForMenu);
} else {
disabled_features.emplace_back(
features::kEnterpriseProfileBadgingForMenu);
}
if (policies_feature_enabled()) {
enabled_features.emplace_back(
features::kEnterpriseProfileBadgingPolicies);
} else {
disabled_features.emplace_back(
features::kEnterpriseProfileBadgingPolicies);
}
scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
InProcessBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
SetUserAcceptedAccountManagement(browser()->profile(), managed_profile());
if (managed_profile()) {
scoped_browser_management_ =
std::make_unique<policy::ScopedManagementServiceOverrideForTesting>(
policy::ManagementServiceFactory::GetForProfile(
browser()->profile()),
policy::EnterpriseManagementAuthority::CLOUD);
}
InProcessBrowserTest::SetUpOnMainThread();
}
void TearDownOnMainThread() override { scoped_browser_management_.reset(); }
bool avatar_feature_enabled() { return std::get<0>(GetParam()); }
bool profile_menu_feature_enabled() { return std::get<1>(GetParam()); }
bool policies_feature_enabled() { return std::get<2>(GetParam()); }
bool managed_profile() { return std::get<3>(GetParam()); }
private:
std::unique_ptr<policy::ScopedManagementServiceOverrideForTesting>
scoped_browser_management_;
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(EnterpriseProfileBadgingTest, CanShowEnterpriseBadging) {
Profile* profile = browser()->profile();
// When no custom policy is set, the visibility of each of the the avatar
// badging and profile menu badging depends on whether the profile is managed
// and if each feature controlling the default behaviour is enabled.
EXPECT_EQ(CanShowEnterpriseBadgingForAvatar(profile),
avatar_feature_enabled() && managed_profile());
EXPECT_EQ(CanShowEnterpriseBadgingForMenu(profile),
profile_menu_feature_enabled() && managed_profile());
profile->GetPrefs()->SetString(prefs::kEnterpriseCustomLabelForProfile,
"some_label");
EXPECT_EQ(CanShowEnterpriseBadgingForAvatar(profile),
(avatar_feature_enabled() || policies_feature_enabled()) &&
managed_profile());
profile->GetPrefs()->SetString(prefs::kEnterpriseLogoUrlForProfile,
"some_url");
EXPECT_EQ(CanShowEnterpriseBadgingForMenu(profile),
((profile_menu_feature_enabled() || policies_feature_enabled()) &&
managed_profile()));
}
IN_PROC_BROWSER_TEST_P(EnterpriseProfileBadgingTest,
CanNotShowEnterpriseBadgingForPrimaryOTRProfile) {
Browser* incognito_browser = Browser::Create(Browser::CreateParams(
browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true),
true));
// Profile badging should always return false in incognito.
EXPECT_FALSE(CanShowEnterpriseBadgingForAvatar(incognito_browser->profile()));
EXPECT_FALSE(CanShowEnterpriseBadgingForMenu(incognito_browser->profile()));
}
IN_PROC_BROWSER_TEST_P(EnterpriseProfileBadgingTest,
CanNotShowEnterpriseBadgingForNonPrimaryOTRProfile) {
browser()->profile()->GetPrimaryOTRProfile(/*create_if_needed=*/true);
Profile* secondary_incognito = browser()->profile()->GetOffTheRecordProfile(
Profile::OTRProfileID::CreateUnique("Test:NonPrimaryOTRProfile"),
/*create_if_needed=*/true);
// Profile badging should always return false in incognito.
EXPECT_FALSE(CanShowEnterpriseBadgingForAvatar(secondary_incognito));
EXPECT_FALSE(CanShowEnterpriseBadgingForMenu(secondary_incognito));
}
INSTANTIATE_TEST_SUITE_P(,
EnterpriseProfileBadgingTest,
testing::Combine(testing::Bool(),
testing::Bool(),
testing::Bool(),
testing::Bool()));
class EnterpriseBrowserBadgingTest
: public InProcessBrowserTest,
public testing::WithParamInterface<std::tuple<bool, bool, bool>> {
public:
void SetUp() override {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
if (footer_management_notice_feature_enabled()) {
enabled_features.emplace_back(features::kEnterpriseBadgingForNtpFooter);
} else {
disabled_features.emplace_back(features::kEnterpriseBadgingForNtpFooter);
}
if (policies_feature_enabled()) {
enabled_features.emplace_back(features::kNTPFooterBadgingPolicies);
} else {
disabled_features.emplace_back(features::kNTPFooterBadgingPolicies);
}
scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
InProcessBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
if (managed_browser()) {
scoped_browser_management_ =
std::make_unique<policy::ScopedManagementServiceOverrideForTesting>(
policy::ManagementServiceFactory::GetForProfile(
browser()->profile()),
policy::EnterpriseManagementAuthority::CLOUD_DOMAIN);
} else {
scoped_browser_management_ =
std::make_unique<policy::ScopedManagementServiceOverrideForTesting>(
policy::ManagementServiceFactory::GetForProfile(
browser()->profile()),
policy::EnterpriseManagementAuthority::NONE);
}
InProcessBrowserTest::SetUpOnMainThread();
}
void TearDownOnMainThread() override { scoped_browser_management_.reset(); }
bool footer_management_notice_feature_enabled() {
return std::get<0>(GetParam());
}
bool policies_feature_enabled() { return std::get<1>(GetParam()); }
bool managed_browser() { return std::get<2>(GetParam()); }
private:
std::unique_ptr<policy::ScopedManagementServiceOverrideForTesting>
scoped_browser_management_;
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(EnterpriseBrowserBadgingTest,
CanShowEnterpriseBadgingForNTPFooter) {
Profile* profile = browser()->profile();
// When no custom policy is set, the visibility of the management notice in
// the NTP footer depends on whether the browser is managed and
// if the feature controlling the default behaviour is enabled.
EXPECT_EQ(CanShowEnterpriseBadgingForNTPFooter(profile),
footer_management_notice_feature_enabled() && managed_browser());
g_browser_process->local_state()->SetString(
prefs::kEnterpriseCustomLabelForBrowser, "some_label");
EXPECT_EQ(CanShowEnterpriseBadgingForNTPFooter(profile),
(footer_management_notice_feature_enabled() ||
policies_feature_enabled()) &&
managed_browser());
g_browser_process->local_state()->SetString(
prefs::kEnterpriseCustomLabelForBrowser, "");
g_browser_process->local_state()->SetString(
prefs::kEnterpriseLogoUrlForBrowser, "some_url");
EXPECT_EQ(CanShowEnterpriseBadgingForNTPFooter(profile),
((footer_management_notice_feature_enabled() ||
policies_feature_enabled()) &&
managed_browser()));
// Presence of a customization policy overrides `kNtpFooterVisible` user
// setting.
profile->GetPrefs()->SetBoolean(prefs::kNtpFooterVisible, false);
EXPECT_EQ(CanShowEnterpriseBadgingForNTPFooter(profile),
policies_feature_enabled() && managed_browser());
// Toggling the footer off when the the policies are not set should disable
// the notice.
g_browser_process->local_state()->SetString(
prefs::kEnterpriseLogoUrlForBrowser, "");
profile->GetPrefs()->SetBoolean(prefs::kNtpFooterVisible, false);
EXPECT_FALSE(CanShowEnterpriseBadgingForNTPFooter(profile));
}
INSTANTIATE_TEST_SUITE_P(,
EnterpriseBrowserBadgingTest,
testing::Combine(testing::Bool(),
testing::Bool(),
testing::Bool()));
#endif // !BUILDFLAG(IS_CHROMEOS)
class ManagedBrowserUtilsDeviceSignalsBrowserTest
: public InProcessBrowserTest,
public testing::WithParamInterface<bool> {
public:
void SetUp() override {
scoped_feature_list_.InitWithFeatureState(
features::kEnterpriseUpdatedProfileCreationScreen, GetParam());
InProcessBrowserTest::SetUp();
}
bool EnterpriseUpdatedProfileCreationScreenEnabled() { return GetParam(); }
protected:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(ManagedBrowserUtilsDeviceSignalsBrowserTest,
UserAcceptedAccountManagementSharesDeviceSignals) {
Profile* profile = browser()->profile();
auto* user_permission_service =
enterprise_signals::UserPermissionServiceFactory::GetForProfile(profile);
ASSERT_FALSE(user_permission_service->HasUserConsented());
ASSERT_FALSE(UserAcceptedAccountManagement(profile));
// User has not consented to anything.
SetUserAcceptedAccountManagement(profile, false);
ASSERT_FALSE(UserAcceptedAccountManagement(profile));
ASSERT_FALSE(user_permission_service->HasUserConsented());
// User has consented to sharing signals for the lifetime of the profile.
SetUserAcceptedAccountManagement(profile, true);
ASSERT_TRUE(UserAcceptedAccountManagement(profile));
ASSERT_EQ(user_permission_service->HasUserConsented(),
EnterpriseUpdatedProfileCreationScreenEnabled());
}
INSTANTIATE_TEST_SUITE_P(,
ManagedBrowserUtilsDeviceSignalsBrowserTest,
testing::Bool());
} // namespace enterprise_util
|