1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/user_manager/fake_user_manager.h"
7 #include "base/callback.h"
8 #include "base/task_runner.h"
9 #include "components/user_manager/user_type.h"
10 #include "ui/base/resource/resource_bundle.h"
14 class FakeTaskRunner
: public base::TaskRunner
{
16 bool PostDelayedTask(const tracked_objects::Location
& from_here
,
17 const base::Closure
& task
,
18 base::TimeDelta delay
) override
{
22 bool RunsTasksOnCurrentThread() const override
{ return true; }
25 ~FakeTaskRunner() override
{}
30 namespace user_manager
{
32 FakeUserManager::FakeUserManager()
33 : UserManagerBase(new FakeTaskRunner(), new FakeTaskRunner()),
35 owner_email_(std::string()) {
38 FakeUserManager::~FakeUserManager() {
41 const user_manager::User
* FakeUserManager::AddUser(const std::string
& email
) {
42 return AddUserWithAffiliation(email
, false);
45 const user_manager::User
* FakeUserManager::AddUserWithAffiliation(
46 const std::string
& email
, bool is_affiliated
) {
47 user_manager::User
* user
= user_manager::User::CreateRegularUser(email
);
48 user
->set_affiliation(is_affiliated
);
49 users_
.push_back(user
);
53 void FakeUserManager::RemoveUserFromList(const std::string
& email
) {
54 user_manager::UserList::iterator it
= users_
.begin();
55 while (it
!= users_
.end() && (*it
)->email() != email
)
57 if (it
!= users_
.end()) {
63 const user_manager::UserList
& FakeUserManager::GetUsers() const {
67 user_manager::UserList
FakeUserManager::GetUsersAllowedForMultiProfile() const {
68 user_manager::UserList result
;
69 for (user_manager::UserList::const_iterator it
= users_
.begin();
70 it
!= users_
.end(); ++it
) {
71 if ((*it
)->GetType() == user_manager::USER_TYPE_REGULAR
&&
72 !(*it
)->is_logged_in())
73 result
.push_back(*it
);
78 const user_manager::UserList
& FakeUserManager::GetLoggedInUsers() const {
79 return logged_in_users_
;
82 void FakeUserManager::UserLoggedIn(const std::string
& email
,
83 const std::string
& username_hash
,
84 bool browser_restart
) {
85 for (user_manager::UserList::const_iterator it
= users_
.begin();
86 it
!= users_
.end(); ++it
) {
87 if ((*it
)->username_hash() == username_hash
) {
88 (*it
)->set_is_logged_in(true);
89 (*it
)->set_profile_is_created();
90 logged_in_users_
.push_back(*it
);
99 user_manager::User
* FakeUserManager::GetActiveUserInternal() const {
101 if (!active_user_id_
.empty()) {
102 for (user_manager::UserList::const_iterator it
= users_
.begin();
103 it
!= users_
.end(); ++it
) {
104 if ((*it
)->email() == active_user_id_
)
113 const user_manager::User
* FakeUserManager::GetActiveUser() const {
114 return GetActiveUserInternal();
117 user_manager::User
* FakeUserManager::GetActiveUser() {
118 return GetActiveUserInternal();
121 void FakeUserManager::SwitchActiveUser(const std::string
& email
) {
124 void FakeUserManager::SaveUserDisplayName(const std::string
& username
,
125 const base::string16
& display_name
) {
126 for (user_manager::UserList::iterator it
= users_
.begin(); it
!= users_
.end();
128 if ((*it
)->email() == username
) {
129 (*it
)->set_display_name(display_name
);
135 const user_manager::UserList
& FakeUserManager::GetLRULoggedInUsers() const {
139 user_manager::UserList
FakeUserManager::GetUnlockUsers() const {
143 const std::string
& FakeUserManager::GetOwnerEmail() const {
147 bool FakeUserManager::IsKnownUser(const std::string
& email
) const {
151 const user_manager::User
* FakeUserManager::FindUser(
152 const std::string
& email
) const {
153 const user_manager::UserList
& users
= GetUsers();
154 for (user_manager::UserList::const_iterator it
= users
.begin();
155 it
!= users
.end(); ++it
) {
156 if ((*it
)->email() == email
)
162 user_manager::User
* FakeUserManager::FindUserAndModify(
163 const std::string
& email
) {
167 const user_manager::User
* FakeUserManager::GetLoggedInUser() const {
171 user_manager::User
* FakeUserManager::GetLoggedInUser() {
175 const user_manager::User
* FakeUserManager::GetPrimaryUser() const {
176 return primary_user_
;
179 base::string16
FakeUserManager::GetUserDisplayName(
180 const std::string
& username
) const {
181 return base::string16();
184 std::string
FakeUserManager::GetUserDisplayEmail(
185 const std::string
& username
) const {
186 return std::string();
189 bool FakeUserManager::IsCurrentUserOwner() const {
193 bool FakeUserManager::IsCurrentUserNew() const {
197 bool FakeUserManager::IsCurrentUserNonCryptohomeDataEphemeral() const {
201 bool FakeUserManager::CanCurrentUserLock() const {
205 bool FakeUserManager::IsUserLoggedIn() const {
206 return logged_in_users_
.size() > 0;
209 bool FakeUserManager::IsLoggedInAsUserWithGaiaAccount() const {
213 bool FakeUserManager::IsLoggedInAsPublicAccount() const {
217 bool FakeUserManager::IsLoggedInAsGuest() const {
221 bool FakeUserManager::IsLoggedInAsSupervisedUser() const {
225 bool FakeUserManager::IsLoggedInAsKioskApp() const {
226 const user_manager::User
* active_user
= GetActiveUser();
228 ? active_user
->GetType() == user_manager::USER_TYPE_KIOSK_APP
232 bool FakeUserManager::IsLoggedInAsStub() const {
236 bool FakeUserManager::IsSessionStarted() const {
240 bool FakeUserManager::IsUserNonCryptohomeDataEphemeral(
241 const std::string
& email
) const {
245 bool FakeUserManager::AreSupervisedUsersAllowed() const {
249 bool FakeUserManager::AreEphemeralUsersEnabled() const {
253 const std::string
& FakeUserManager::GetApplicationLocale() const {
254 static const std::string
default_locale("en-US");
255 return default_locale
;
258 PrefService
* FakeUserManager::GetLocalState() const {
262 bool FakeUserManager::IsEnterpriseManaged() const {
266 bool FakeUserManager::IsDemoApp(const std::string
& user_id
) const {
270 bool FakeUserManager::IsKioskApp(const std::string
& user_id
) const {
274 bool FakeUserManager::IsPublicAccountMarkedForRemoval(
275 const std::string
& user_id
) const {
279 } // namespace user_manager