Move gn build file for sfntly from secondary tree into the main tree.
[chromium-blink-merge.git] / ash / test / test_session_state_delegate.cc
blobc28c49737c20b8612169bb097dcbf9f699cd2bf2
1 // Copyright (c) 2013 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 "ash/test/test_session_state_delegate.h"
7 #include <algorithm>
8 #include <string>
10 #include "ash/shell.h"
11 #include "ash/system/user/login_status.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string16.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "components/user_manager/user_info.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace ash {
19 namespace test {
21 namespace {
23 // The the "canonicalized" user ID from a given |email| address.
24 std::string GetUserIDFromEmail(const std::string& email) {
25 std::string user_id = email;
26 std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower);
27 return user_id;
30 } // namespace
32 class MockUserInfo : public user_manager::UserInfo {
33 public:
34 explicit MockUserInfo(const std::string& id) : email_(id) {}
35 ~MockUserInfo() override {}
37 void SetUserImage(const gfx::ImageSkia& user_image) {
38 user_image_ = user_image;
41 base::string16 GetDisplayName() const override {
42 return base::UTF8ToUTF16("Über tray Über tray Über tray Über tray");
45 base::string16 GetGivenName() const override {
46 return base::UTF8ToUTF16("Über Über Über Über");
49 std::string GetEmail() const override { return email_; }
51 std::string GetUserID() const override {
52 return GetUserIDFromEmail(GetEmail());
55 const gfx::ImageSkia& GetImage() const override { return user_image_; }
57 // A test user image.
58 gfx::ImageSkia user_image_;
60 std::string email_;
62 DISALLOW_COPY_AND_ASSIGN(MockUserInfo);
65 TestSessionStateDelegate::TestSessionStateDelegate()
66 : has_active_user_(false),
67 active_user_session_started_(false),
68 can_lock_screen_(true),
69 should_lock_screen_before_suspending_(false),
70 screen_locked_(false),
71 user_adding_screen_running_(false),
72 logged_in_users_(1),
73 active_user_index_(0),
74 session_state_(SESSION_STATE_LOGIN_PRIMARY) {
75 user_list_.push_back(
76 new MockUserInfo("First@tray")); // This is intended to be capitalized.
77 user_list_.push_back(
78 new MockUserInfo("Second@tray")); // This is intended to be capitalized.
79 user_list_.push_back(new MockUserInfo("third@tray"));
80 user_list_.push_back(new MockUserInfo("someone@tray"));
83 TestSessionStateDelegate::~TestSessionStateDelegate() {
84 STLDeleteElements(&user_list_);
87 void TestSessionStateDelegate::AddUser(const std::string user_id) {
88 user_list_.push_back(new MockUserInfo(user_id));
91 const user_manager::UserInfo* TestSessionStateDelegate::GetActiveUserInfo()
92 const {
93 return user_list_[active_user_index_];
96 content::BrowserContext*
97 TestSessionStateDelegate::GetBrowserContextByIndex(
98 MultiProfileIndex index) {
99 return NULL;
102 content::BrowserContext* TestSessionStateDelegate::GetBrowserContextForWindow(
103 aura::Window* window) {
104 return NULL;
107 content::BrowserContext*
108 TestSessionStateDelegate::GetUserPresentingBrowserContextForWindow(
109 aura::Window* window) {
110 return NULL;
113 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const {
114 return 3;
117 int TestSessionStateDelegate::NumberOfLoggedInUsers() const {
118 // TODO(skuhne): Add better test framework to test multiple profiles.
119 return has_active_user_ ? logged_in_users_ : 0;
122 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const {
123 return active_user_session_started_;
126 bool TestSessionStateDelegate::CanLockScreen() const {
127 return has_active_user_ && can_lock_screen_;
130 bool TestSessionStateDelegate::IsScreenLocked() const {
131 return screen_locked_;
134 bool TestSessionStateDelegate::ShouldLockScreenBeforeSuspending() const {
135 return should_lock_screen_before_suspending_;
138 void TestSessionStateDelegate::LockScreen() {
139 if (CanLockScreen())
140 screen_locked_ = true;
143 void TestSessionStateDelegate::UnlockScreen() {
144 screen_locked_ = false;
147 bool TestSessionStateDelegate::IsUserSessionBlocked() const {
148 return !IsActiveUserSessionStarted() || IsScreenLocked() ||
149 user_adding_screen_running_;
152 SessionStateDelegate::SessionState TestSessionStateDelegate::GetSessionState()
153 const {
154 return session_state_;
157 void TestSessionStateDelegate::SetHasActiveUser(bool has_active_user) {
158 has_active_user_ = has_active_user;
159 if (!has_active_user) {
160 active_user_session_started_ = false;
161 session_state_ = SESSION_STATE_LOGIN_PRIMARY;
162 } else {
163 Shell::GetInstance()->ShowShelf();
167 void TestSessionStateDelegate::SetActiveUserSessionStarted(
168 bool active_user_session_started) {
169 active_user_session_started_ = active_user_session_started;
170 if (active_user_session_started) {
171 session_state_ = SESSION_STATE_ACTIVE;
172 has_active_user_ = true;
173 Shell::GetInstance()->CreateShelf();
174 Shell::GetInstance()->UpdateAfterLoginStatusChange(
175 user::LOGGED_IN_USER);
176 } else {
177 session_state_ = SESSION_STATE_LOGIN_PRIMARY;
181 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) {
182 can_lock_screen_ = can_lock_screen;
185 void TestSessionStateDelegate::SetShouldLockScreenBeforeSuspending(
186 bool should_lock) {
187 should_lock_screen_before_suspending_ = should_lock;
190 void TestSessionStateDelegate::SetUserAddingScreenRunning(
191 bool user_adding_screen_running) {
192 user_adding_screen_running_ = user_adding_screen_running;
193 if (user_adding_screen_running_)
194 session_state_ = SESSION_STATE_LOGIN_SECONDARY;
197 void TestSessionStateDelegate::SetUserImage(
198 const gfx::ImageSkia& user_image) {
199 user_list_[active_user_index_]->SetUserImage(user_image);
202 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
203 MultiProfileIndex index) const {
204 int max = static_cast<int>(user_list_.size());
205 return user_list_[index < max ? index : max - 1];
208 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
209 content::BrowserContext* context) const {
210 return user_list_[active_user_index_];
213 bool TestSessionStateDelegate::ShouldShowAvatar(aura::Window* window) const {
214 return !GetActiveUserInfo()->GetImage().isNull();
217 void TestSessionStateDelegate::SwitchActiveUser(const std::string& user_id) {
218 // Make sure this is a user id and not an email address.
219 EXPECT_EQ(user_id, GetUserIDFromEmail(user_id));
220 active_user_index_ = 0;
221 for (std::vector<MockUserInfo*>::iterator iter = user_list_.begin();
222 iter != user_list_.end();
223 ++iter) {
224 if ((*iter)->GetUserID() == user_id) {
225 active_user_index_ = iter - user_list_.begin();
226 return;
229 NOTREACHED() << "Unknown user:" << user_id;
232 void TestSessionStateDelegate::CycleActiveUser(CycleUser cycle_user) {
233 SwitchActiveUser("someone@tray");
236 bool TestSessionStateDelegate::IsMultiProfileAllowedByPrimaryUserPolicy()
237 const {
238 return true;
241 void TestSessionStateDelegate::AddSessionStateObserver(
242 SessionStateObserver* observer) {
245 void TestSessionStateDelegate::RemoveSessionStateObserver(
246 SessionStateObserver* observer) {
249 } // namespace test
250 } // namespace ash