#include "chrome/browser/policy/developer_tools_policy_handler.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/policy/core/browser/configuration_policy_pref_store.h"
#include "components/policy/core/browser/configuration_policy_pref_store_test.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/user_manager/scoped_user_manager.h"
#include "content/public/test/browser_task_environment.h"
#endif
namespace policy {
Availability;
class DeveloperToolsPolicyHandlerTest
: public ConfigurationPolicyPrefStoreTest { … };
TEST_F(DeveloperToolsPolicyHandlerTest, NewPolicyOverridesLegacyPolicy) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, LegacyPolicyAppliesIfNewPolicyInvalid) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, NewPolicyAppliesIfLegacyPolicyInvalid) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, DisallowedForForceInstalledExtensions) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, Allowed) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, Disallowed) { … }
TEST_F(DeveloperToolsPolicyHandlerTest, InvalidValue) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
struct TestParam {
Availability primary_profile_availability;
Availability secondary_profile_availability;
Availability expected_result;
};
class DeveloperToolsPolicyHandlerWithProfileTest
: public testing::TestWithParam<TestParam> {
public:
DeveloperToolsPolicyHandlerWithProfileTest()
: profile_manager_(TestingBrowserProcess::GetGlobal()) {}
void SetUp() override {
ASSERT_TRUE(profile_manager_.SetUp());
auto user_manager = std::make_unique<ash::FakeChromeUserManager>();
{
constexpr char kPrimaryProfileName[] = "primary_profile";
const AccountId account_id(AccountId::FromUserEmail(kPrimaryProfileName));
primary_profile_ = profile_manager_.CreateTestingProfile(
kPrimaryProfileName, true);
user_manager->AddUserWithAffiliationAndTypeAndProfile(
account_id, true,
user_manager::UserType::kRegular, primary_profile_);
user_manager->LoginUser(account_id);
}
{
constexpr char kSecondaryProfileName[] = "secondary_profile";
const AccountId account_id(
AccountId::FromUserEmail(kSecondaryProfileName));
secondary_profile_ =
profile_manager_.CreateTestingProfile(kSecondaryProfileName);
user_manager->AddUserWithAffiliationAndTypeAndProfile(
account_id, true,
user_manager::UserType::kRegular, secondary_profile_);
}
scoped_user_manager_ = std::make_unique<user_manager::ScopedUserManager>(
std::move(user_manager));
EXPECT_TRUE(ash::ProfileHelper::IsPrimaryProfile(primary_profile_));
EXPECT_FALSE(ash::ProfileHelper::IsPrimaryProfile(secondary_profile_));
}
void UpdatePrimaryProfileAvailability() {
primary_profile_->GetTestingPrefService()->SetManagedPref(
prefs::kDevToolsAvailability,
base::Value(static_cast<int>(GetParam().primary_profile_availability)));
}
void UpdateSecondaryProfileAvailability() {
secondary_profile_->GetTestingPrefService()->SetManagedPref(
prefs::kDevToolsAvailability,
base::Value(
static_cast<int>(GetParam().secondary_profile_availability)));
}
Availability expected_result() const { return GetParam().expected_result; }
protected:
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_;
std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_;
raw_ptr<TestingProfile> primary_profile_;
raw_ptr<TestingProfile> secondary_profile_;
};
TEST_F(DeveloperToolsPolicyHandlerWithProfileTest,
GetEffectiveAvailabilityForced) {
EXPECT_EQ(
Availability::kDisallowedForForceInstalledExtensions,
DeveloperToolsPolicyHandler::GetEffectiveAvailability(primary_profile_));
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kForceDevToolsAvailable);
EXPECT_EQ(
Availability::kAllowed,
DeveloperToolsPolicyHandler::GetEffectiveAvailability(primary_profile_));
}
TEST_P(DeveloperToolsPolicyHandlerWithProfileTest,
GetEffectiveAvailabilityRestrictive) {
UpdatePrimaryProfileAvailability();
UpdateSecondaryProfileAvailability();
EXPECT_EQ(expected_result(),
DeveloperToolsPolicyHandler::GetEffectiveAvailability(
secondary_profile_));
}
INSTANTIATE_TEST_SUITE_P(
DeveloperToolsPolicyHandlerTest,
DeveloperToolsPolicyHandlerWithProfileTest,
::testing::Values(
TestParam(
Availability::kAllowed,
Availability::kAllowed,
Availability::kAllowed),
TestParam(
Availability::kDisallowed,
Availability::kAllowed,
Availability::kDisallowed),
TestParam(
Availability::kAllowed,
Availability::kDisallowed,
Availability::kDisallowed),
TestParam(
Availability::kAllowed,
Availability::kDisallowedForForceInstalledExtensions,
Availability::kDisallowedForForceInstalledExtensions),
TestParam(
Availability::
kDisallowedForForceInstalledExtensions,
Availability::kAllowed,
Availability::kDisallowedForForceInstalledExtensions),
TestParam(
Availability::kDisallowed,
Availability::kDisallowedForForceInstalledExtensions,
Availability::kDisallowed),
TestParam(
Availability::
kDisallowedForForceInstalledExtensions,
Availability::kDisallowed,
Availability::kDisallowed),
TestParam(
Availability::kDisallowed,
Availability::kDisallowed,
Availability::kDisallowed)));
#endif
}