Roll src/third_party/WebKit c8d1660:ae3684d (svn 197337:197343)
[chromium-blink-merge.git] / ash / test / test_session_state_delegate.cc
bloba2b99c85521b6e4f2c5aab47eeff236d8d2d888d
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 // A test version of user_manager::UserManager which can be used for testing on
66 // non-ChromeOS builds.
67 class TestSessionStateDelegate::TestUserManager {
68 public:
69 TestUserManager() : session_started_(false) {}
71 void SessionStarted() { session_started_ = true; }
73 bool IsSessionStarted() const { return session_started_; }
75 private:
76 // True if SessionStarted() has been called.
77 bool session_started_;
78 DISALLOW_COPY_AND_ASSIGN(TestUserManager);
81 TestSessionStateDelegate::TestSessionStateDelegate()
82 : can_lock_screen_(true),
83 should_lock_screen_before_suspending_(false),
84 screen_locked_(false),
85 user_adding_screen_running_(false),
86 logged_in_users_(1),
87 active_user_index_(0),
88 user_manager_(new TestUserManager()),
89 session_state_(SESSION_STATE_LOGIN_PRIMARY) {
90 user_list_.push_back(
91 new MockUserInfo("First@tray")); // This is intended to be capitalized.
92 user_list_.push_back(
93 new MockUserInfo("Second@tray")); // This is intended to be capitalized.
94 user_list_.push_back(new MockUserInfo("third@tray"));
95 user_list_.push_back(new MockUserInfo("someone@tray"));
98 TestSessionStateDelegate::~TestSessionStateDelegate() {
99 STLDeleteElements(&user_list_);
102 void TestSessionStateDelegate::AddUser(const std::string user_id) {
103 user_list_.push_back(new MockUserInfo(user_id));
106 const user_manager::UserInfo* TestSessionStateDelegate::GetActiveUserInfo()
107 const {
108 return user_list_[active_user_index_];
111 content::BrowserContext*
112 TestSessionStateDelegate::GetBrowserContextByIndex(
113 MultiProfileIndex index) {
114 return NULL;
117 content::BrowserContext* TestSessionStateDelegate::GetBrowserContextForWindow(
118 aura::Window* window) {
119 return NULL;
122 content::BrowserContext*
123 TestSessionStateDelegate::GetUserPresentingBrowserContextForWindow(
124 aura::Window* window) {
125 return NULL;
128 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const {
129 return 3;
132 int TestSessionStateDelegate::NumberOfLoggedInUsers() const {
133 // TODO(skuhne): Add better test framework to test multiple profiles.
134 return IsActiveUserSessionStarted() ? logged_in_users_ : 0;
137 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const {
138 return user_manager_->IsSessionStarted() &&
139 session_state_ == SESSION_STATE_ACTIVE;
142 bool TestSessionStateDelegate::CanLockScreen() const {
143 return IsActiveUserSessionStarted() && can_lock_screen_;
146 bool TestSessionStateDelegate::IsScreenLocked() const {
147 return screen_locked_;
150 bool TestSessionStateDelegate::ShouldLockScreenBeforeSuspending() const {
151 return should_lock_screen_before_suspending_;
154 void TestSessionStateDelegate::LockScreen() {
155 if (CanLockScreen())
156 screen_locked_ = true;
159 void TestSessionStateDelegate::UnlockScreen() {
160 screen_locked_ = false;
163 bool TestSessionStateDelegate::IsUserSessionBlocked() const {
164 return !IsActiveUserSessionStarted() || IsScreenLocked() ||
165 user_adding_screen_running_ || session_state_ != SESSION_STATE_ACTIVE;
168 SessionStateDelegate::SessionState TestSessionStateDelegate::GetSessionState()
169 const {
170 return session_state_;
173 void TestSessionStateDelegate::SetHasActiveUser(bool has_active_user) {
174 if (!has_active_user) {
175 session_state_ = SESSION_STATE_LOGIN_PRIMARY;
176 } else {
177 session_state_ = SESSION_STATE_ACTIVE;
178 Shell::GetInstance()->ShowShelf();
182 void TestSessionStateDelegate::SetActiveUserSessionStarted(
183 bool active_user_session_started) {
184 if (active_user_session_started) {
185 user_manager_->SessionStarted();
186 session_state_ = SESSION_STATE_ACTIVE;
187 Shell::GetInstance()->CreateShelf();
188 Shell::GetInstance()->UpdateAfterLoginStatusChange(
189 user::LOGGED_IN_USER);
190 } else {
191 session_state_ = SESSION_STATE_LOGIN_PRIMARY;
192 user_manager_.reset(new TestUserManager());
196 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) {
197 can_lock_screen_ = can_lock_screen;
200 void TestSessionStateDelegate::SetShouldLockScreenBeforeSuspending(
201 bool should_lock) {
202 should_lock_screen_before_suspending_ = should_lock;
205 void TestSessionStateDelegate::SetUserAddingScreenRunning(
206 bool user_adding_screen_running) {
207 user_adding_screen_running_ = user_adding_screen_running;
208 if (user_adding_screen_running_)
209 session_state_ = SESSION_STATE_LOGIN_SECONDARY;
210 else
211 session_state_ = SESSION_STATE_ACTIVE;
214 void TestSessionStateDelegate::SetUserImage(
215 const gfx::ImageSkia& user_image) {
216 user_list_[active_user_index_]->SetUserImage(user_image);
219 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
220 MultiProfileIndex index) const {
221 int max = static_cast<int>(user_list_.size());
222 return user_list_[index < max ? index : max - 1];
225 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo(
226 content::BrowserContext* context) const {
227 return user_list_[active_user_index_];
230 bool TestSessionStateDelegate::ShouldShowAvatar(aura::Window* window) const {
231 return !GetActiveUserInfo()->GetImage().isNull();
234 void TestSessionStateDelegate::SwitchActiveUser(const std::string& user_id) {
235 // Make sure this is a user id and not an email address.
236 EXPECT_EQ(user_id, GetUserIDFromEmail(user_id));
237 active_user_index_ = 0;
238 for (std::vector<MockUserInfo*>::iterator iter = user_list_.begin();
239 iter != user_list_.end();
240 ++iter) {
241 if ((*iter)->GetUserID() == user_id) {
242 active_user_index_ = iter - user_list_.begin();
243 return;
246 NOTREACHED() << "Unknown user:" << user_id;
249 void TestSessionStateDelegate::CycleActiveUser(CycleUser cycle_user) {
250 SwitchActiveUser("someone@tray");
253 bool TestSessionStateDelegate::IsMultiProfileAllowedByPrimaryUserPolicy()
254 const {
255 return true;
258 void TestSessionStateDelegate::AddSessionStateObserver(
259 SessionStateObserver* observer) {
262 void TestSessionStateDelegate::RemoveSessionStateObserver(
263 SessionStateObserver* observer) {
266 } // namespace test
267 } // namespace ash