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
|
// Copyright 2022 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/ui/ash/projector/projector_utils.h"
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/test/base/fake_gaia_mixin.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/account_id/account_id.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_names.h"
#include "components/user_manager/user_type.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_task_environment.h"
#include "google_apis/gaia/gaia_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/events/devices/device_data_manager.h"
namespace ash {
namespace {
constexpr GaiaId::Literal kTestGaiaId("1234567890");
class ScopedLogIn {
public:
ScopedLogIn(
ash::FakeChromeUserManager* fake_user_manager,
const AccountId& account_id,
user_manager::UserType user_type = user_manager::UserType::kRegular)
: fake_user_manager_(fake_user_manager), account_id_(account_id) {
// Prevent access to DBus. This switch is reset in case set from test SetUp
// due massive usage of InitFromArgv.
base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kTestType)) {
command_line.AppendSwitch(switches::kTestType);
}
switch (user_type) {
case user_manager::UserType::kRegular: // fallthrough
LogIn();
break;
case user_manager::UserType::kPublicAccount:
LogInAsPublicAccount();
break;
case user_manager::UserType::kWebKioskApp:
LogInWebKioskApp();
break;
case user_manager::UserType::kChild:
LogInChildUser();
return;
case user_manager::UserType::kGuest:
LogInGuestUser();
return;
default:
NOTREACHED();
}
}
ScopedLogIn(const ScopedLogIn&) = delete;
ScopedLogIn& operator=(const ScopedLogIn&) = delete;
~ScopedLogIn() { fake_user_manager_->RemoveUserFromList(account_id_); }
private:
void LogIn() {
fake_user_manager_->AddUser(account_id_);
fake_user_manager_->LoginUser(account_id_);
}
void LogInAsPublicAccount() {
fake_user_manager_->AddPublicAccountUser(account_id_);
fake_user_manager_->LoginUser(account_id_);
}
void LogInWebKioskApp() {
fake_user_manager_->AddWebKioskAppUser(account_id_);
fake_user_manager_->LoginUser(account_id_);
}
void LogInChildUser() {
fake_user_manager_->AddChildUser(account_id_);
fake_user_manager_->LoginUser(account_id_);
}
void LogInGuestUser() {
fake_user_manager_->AddGuestUser();
fake_user_manager_->LoginUser(account_id_);
}
raw_ptr<ash::FakeChromeUserManager> fake_user_manager_;
const AccountId account_id_;
};
} // namespace
class ProjectorUtilsTest : public testing::Test {
public:
ProjectorUtilsTest() = default;
ProjectorUtilsTest(const ProjectorUtilsTest&) = delete;
ProjectorUtilsTest& operator=(const ProjectorUtilsTest&) = delete;
~ProjectorUtilsTest() override = default;
void SetUp() override {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
user_manager_.Reset(std::make_unique<ash::FakeChromeUserManager>());
std::unique_ptr<sync_preferences::TestingPrefServiceSyncable> prefs =
std::make_unique<sync_preferences::TestingPrefServiceSyncable>();
RegisterUserProfilePrefs(prefs->registry());
TestingProfile::Builder builder;
builder.SetPrefService(std::move(prefs));
if (is_child()) {
builder.SetIsSupervisedProfile();
}
builder.OverridePolicyConnectorIsManagedForTesting(is_managed());
profile_ = builder.Build();
}
void TearDown() override {
ui::DeviceDataManager::DeleteInstance();
user_manager_.Reset();
profile_.reset();
}
TestingProfile* profile() { return profile_.get(); }
PrefService* GetPrefs() { return profile_->GetPrefs(); }
ash::FakeChromeUserManager* GetFakeUserManager() const {
return user_manager_.Get();
}
virtual bool is_child() const { return false; }
virtual bool is_managed() const { return false; }
private:
ScopedTestingLocalState local_state_{TestingBrowserProcess::GetGlobal()};
content::BrowserTaskEnvironment task_environment_;
base::ScopedTempDir data_dir_;
user_manager::TypedScopedUserManager<ash::FakeChromeUserManager>
user_manager_;
std::unique_ptr<TestingProfile> profile_;
};
class ProjectorUtilsChildTest : public ProjectorUtilsTest {
public:
// ProjectorUtilsTest:
void SetUp() override {
ProjectorUtilsTest::SetUp();
GetPrefs()->SetBoolean(prefs::kProjectorDogfoodForFamilyLinkEnabled, true);
}
bool is_child() const override { return true; }
bool is_managed() const override { return true; }
};
class ProjectorUtilsManagedTest : public ProjectorUtilsTest {
public:
// ProjectorUtilsTest:
void SetUp() override {
ProjectorUtilsTest::SetUp();
GetPrefs()->SetBoolean(prefs::kProjectorAllowByPolicy, true);
}
bool is_managed() const override { return true; }
};
TEST_F(ProjectorUtilsTest, IsProjectorAllowedForProfile_RegularAccount) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId));
EXPECT_TRUE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsManagedTest, IsProjectorAllowedForProfile_ManagedAccount) {
ScopedLogIn login(
GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(FakeGaiaMixin::kEnterpriseUser1,
FakeGaiaMixin::kEnterpriseUser1GaiaId));
EXPECT_TRUE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsChildTest, IsProjectorAllowedForProfile_ChildUser) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId),
user_manager::UserType::kChild);
EXPECT_TRUE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAllowedForProfile_GuestAccount) {
ScopedLogIn login(GetFakeUserManager(), user_manager::GuestAccountId(),
user_manager::UserType::kGuest);
EXPECT_FALSE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAllowedForProfile_DemoAccount) {
ScopedLogIn login(GetFakeUserManager(), user_manager::DemoAccountId(),
user_manager::UserType::kPublicAccount);
EXPECT_FALSE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAllowedForProfile_KioskAppAccount) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmail(profile()->GetProfileUserName()),
user_manager::UserType::kWebKioskApp);
EXPECT_FALSE(IsProjectorAllowedForProfile(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAppEnabled_RegularAccount) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId));
EXPECT_TRUE(IsProjectorAppEnabled(profile()));
}
TEST_F(ProjectorUtilsManagedTest, IsProjectorAppEnabled_ManagedAccount) {
ScopedLogIn login(
GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(FakeGaiaMixin::kEnterpriseUser1,
FakeGaiaMixin::kEnterpriseUser1GaiaId));
EXPECT_TRUE(IsProjectorAppEnabled(profile()));
}
TEST_F(ProjectorUtilsChildTest, IsProjectorAppEnabled_ChildUser) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmailGaiaId(
profile()->GetProfileUserName(), kTestGaiaId),
user_manager::UserType::kChild);
EXPECT_TRUE(IsProjectorAppEnabled(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAppEnabled_GuestAccount) {
ScopedLogIn login(GetFakeUserManager(), user_manager::GuestAccountId(),
user_manager::UserType::kGuest);
EXPECT_FALSE(IsProjectorAppEnabled(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAppEnabled_DemoAccount) {
ScopedLogIn login(GetFakeUserManager(), user_manager::DemoAccountId(),
user_manager::UserType::kPublicAccount);
EXPECT_FALSE(IsProjectorAppEnabled(profile()));
}
TEST_F(ProjectorUtilsTest, IsProjectorAppEnabled_KioskAppAccount) {
ScopedLogIn login(GetFakeUserManager(),
AccountId::FromUserEmail(profile()->GetProfileUserName()),
user_manager::UserType::kWebKioskApp);
EXPECT_FALSE(IsProjectorAppEnabled(profile()));
}
} // namespace ash
|