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.
4
5#include "chrome/browser/chromeos/login/users/mock_user_manager.h"
6
7#include "base/task_runner.h"
8#include "chrome/browser/chromeos/login/users/fake_supervised_user_manager.h"
9#include "chrome/browser/chromeos/profiles/profile_helper.h"
10
11namespace {
12
13class FakeTaskRunner : public base::TaskRunner {
14 public:
15  virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
16                               const base::Closure& task,
17                               base::TimeDelta delay) OVERRIDE {
18    task.Run();
19    return true;
20  }
21  virtual bool RunsTasksOnCurrentThread() const OVERRIDE { return true; }
22
23 protected:
24  virtual ~FakeTaskRunner() {}
25};
26
27}  // namespace
28
29namespace chromeos {
30
31MockUserManager::MockUserManager()
32    : ChromeUserManager(new FakeTaskRunner(), new FakeTaskRunner()),
33      user_flow_(new DefaultUserFlow()),
34      supervised_user_manager_(new FakeSupervisedUserManager()) {
35  ProfileHelper::SetProfileToUserForTestingEnabled(true);
36}
37
38MockUserManager::~MockUserManager() {
39  ProfileHelper::SetProfileToUserForTestingEnabled(false);
40  ClearUserList();
41}
42
43const user_manager::UserList& MockUserManager::GetUsers() const {
44  return user_list_;
45}
46
47const user_manager::User* MockUserManager::GetLoggedInUser() const {
48  return user_list_.empty() ? NULL : user_list_.front();
49}
50
51user_manager::User* MockUserManager::GetLoggedInUser() {
52  return user_list_.empty() ? NULL : user_list_.front();
53}
54
55user_manager::UserList MockUserManager::GetUnlockUsers() const {
56  return user_list_;
57}
58
59const std::string& MockUserManager::GetOwnerEmail() const {
60  return GetLoggedInUser()->email();
61}
62
63const user_manager::User* MockUserManager::GetActiveUser() const {
64  return GetLoggedInUser();
65}
66
67user_manager::User* MockUserManager::GetActiveUser() {
68  return GetLoggedInUser();
69}
70
71const user_manager::User* MockUserManager::GetPrimaryUser() const {
72  return GetLoggedInUser();
73}
74
75MultiProfileUserController* MockUserManager::GetMultiProfileUserController() {
76  return NULL;
77}
78
79UserImageManager* MockUserManager::GetUserImageManager(
80    const std::string& user_id) {
81  return NULL;
82}
83
84SupervisedUserManager* MockUserManager::GetSupervisedUserManager() {
85  return supervised_user_manager_.get();
86}
87
88// Creates a new User instance.
89void MockUserManager::SetActiveUser(const std::string& email) {
90  ClearUserList();
91  AddUser(email);
92}
93
94UserFlow* MockUserManager::GetCurrentUserFlow() const {
95  return user_flow_.get();
96}
97
98UserFlow* MockUserManager::GetUserFlow(const std::string&) const {
99  return user_flow_.get();
100}
101
102user_manager::User* MockUserManager::CreatePublicAccountUser(
103    const std::string& email) {
104  ClearUserList();
105  user_manager::User* user = user_manager::User::CreatePublicAccountUser(email);
106  user_list_.push_back(user);
107  ProfileHelper::Get()->SetProfileToUserMappingForTesting(user);
108  return user_list_.back();
109}
110
111void MockUserManager::AddUser(const std::string& email) {
112  user_manager::User* user = user_manager::User::CreateRegularUser(email);
113  user_list_.push_back(user);
114  ProfileHelper::Get()->SetProfileToUserMappingForTesting(user);
115}
116
117void MockUserManager::ClearUserList() {
118  // Can't use STLDeleteElements because of the protected destructor of User.
119  user_manager::UserList::iterator user;
120  for (user = user_list_.begin(); user != user_list_.end(); ++user)
121    delete *user;
122  user_list_.clear();
123}
124
125}  // namespace chromeos
126