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. 4 5 #include "ash/test/test_session_state_delegate.h" 6 7 #include <algorithm> 8 #include <string> 9 10 #include "ash/session/user_info.h" 11 #include "ash/shell.h" 12 #include "ash/system/user/login_status.h" 13 #include "base/stl_util.h" 14 #include "base/strings/string16.h" 15 #include "base/strings/utf_string_conversions.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace ash { 19 namespace test { 20 21 namespace { 22 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; 28 } 29 30 } // namespace 31 32 class MockUserInfo : public UserInfo { 33 public: 34 explicit MockUserInfo(const std::string& id) : email_(id) {} 35 virtual ~MockUserInfo() {} 36 37 void SetUserImage(const gfx::ImageSkia& user_image) { 38 user_image_ = user_image; 39 } 40 41 virtual base::string16 GetDisplayName() const OVERRIDE { 42 return base::UTF8ToUTF16("ber tray ber tray ber tray ber tray"); 43 } 44 45 virtual base::string16 GetGivenName() const OVERRIDE { 46 return base::UTF8ToUTF16("ber ber ber ber"); 47 } 48 49 virtual std::string GetEmail() const OVERRIDE { return email_; } 50 51 virtual std::string GetUserID() const OVERRIDE { 52 return GetUserIDFromEmail(GetEmail()); 53 } 54 55 virtual const gfx::ImageSkia& GetImage() const OVERRIDE { 56 return user_image_; 57 } 58 59 // A test user image. 60 gfx::ImageSkia user_image_; 61 62 std::string email_; 63 64 DISALLOW_COPY_AND_ASSIGN(MockUserInfo); 65 }; 66 67 TestSessionStateDelegate::TestSessionStateDelegate() 68 : has_active_user_(false), 69 active_user_session_started_(false), 70 can_lock_screen_(true), 71 should_lock_screen_before_suspending_(false), 72 screen_locked_(false), 73 user_adding_screen_running_(false), 74 logged_in_users_(1), 75 active_user_index_(0) { 76 user_list_.push_back( 77 new MockUserInfo("First@tray")); // This is intended to be capitalized. 78 user_list_.push_back( 79 new MockUserInfo("Second@tray")); // This is intended to be capitalized. 80 user_list_.push_back(new MockUserInfo("third@tray")); 81 user_list_.push_back(new MockUserInfo("someone@tray")); 82 } 83 84 TestSessionStateDelegate::~TestSessionStateDelegate() { 85 STLDeleteElements(&user_list_); 86 } 87 88 void TestSessionStateDelegate::AddUser(const std::string user_id) { 89 user_list_.push_back(new MockUserInfo(user_id)); 90 } 91 92 const UserInfo* TestSessionStateDelegate::GetActiveUserInfo() const { 93 return user_list_[active_user_index_]; 94 } 95 96 content::BrowserContext* 97 TestSessionStateDelegate::GetBrowserContextByIndex( 98 MultiProfileIndex index) { 99 return NULL; 100 } 101 102 content::BrowserContext* 103 TestSessionStateDelegate::GetBrowserContextForWindow( 104 aura::Window* window) { 105 return NULL; 106 } 107 108 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const { 109 return 3; 110 } 111 112 int TestSessionStateDelegate::NumberOfLoggedInUsers() const { 113 // TODO(skuhne): Add better test framework to test multiple profiles. 114 return has_active_user_ ? logged_in_users_ : 0; 115 } 116 117 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const { 118 return active_user_session_started_; 119 } 120 121 bool TestSessionStateDelegate::CanLockScreen() const { 122 return has_active_user_ && can_lock_screen_; 123 } 124 125 bool TestSessionStateDelegate::IsScreenLocked() const { 126 return screen_locked_; 127 } 128 129 bool TestSessionStateDelegate::ShouldLockScreenBeforeSuspending() const { 130 return should_lock_screen_before_suspending_; 131 } 132 133 void TestSessionStateDelegate::LockScreen() { 134 if (CanLockScreen()) 135 screen_locked_ = true; 136 } 137 138 void TestSessionStateDelegate::UnlockScreen() { 139 screen_locked_ = false; 140 } 141 142 bool TestSessionStateDelegate::IsUserSessionBlocked() const { 143 return !IsActiveUserSessionStarted() || IsScreenLocked() || 144 user_adding_screen_running_; 145 } 146 147 SessionStateDelegate::SessionState TestSessionStateDelegate::GetSessionState() 148 const { 149 if (user_adding_screen_running_) 150 return SESSION_STATE_LOGIN_SECONDARY; 151 152 // Assuming that if session is not active we're at login. 153 return IsActiveUserSessionStarted() ? 154 SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY; 155 } 156 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 else 162 Shell::GetInstance()->ShowShelf(); 163 } 164 165 void TestSessionStateDelegate::SetActiveUserSessionStarted( 166 bool active_user_session_started) { 167 active_user_session_started_ = active_user_session_started; 168 if (active_user_session_started) { 169 has_active_user_ = true; 170 Shell::GetInstance()->CreateShelf(); 171 Shell::GetInstance()->UpdateAfterLoginStatusChange( 172 user::LOGGED_IN_USER); 173 } 174 } 175 176 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) { 177 can_lock_screen_ = can_lock_screen; 178 } 179 180 void TestSessionStateDelegate::SetShouldLockScreenBeforeSuspending( 181 bool should_lock) { 182 should_lock_screen_before_suspending_ = should_lock; 183 } 184 185 void TestSessionStateDelegate::SetUserAddingScreenRunning( 186 bool user_adding_screen_running) { 187 user_adding_screen_running_ = user_adding_screen_running; 188 } 189 190 void TestSessionStateDelegate::SetUserImage( 191 const gfx::ImageSkia& user_image) { 192 user_list_[active_user_index_]->SetUserImage(user_image); 193 } 194 195 const UserInfo* TestSessionStateDelegate::GetUserInfo( 196 MultiProfileIndex index) const { 197 int max = static_cast<int>(user_list_.size()); 198 return user_list_[index < max ? index : max - 1]; 199 } 200 201 const UserInfo* TestSessionStateDelegate::GetUserInfo( 202 content::BrowserContext* context) const { 203 return user_list_[active_user_index_]; 204 } 205 206 bool TestSessionStateDelegate::ShouldShowAvatar(aura::Window* window) const { 207 return !GetActiveUserInfo()->GetImage().isNull(); 208 } 209 210 void TestSessionStateDelegate::SwitchActiveUser(const std::string& user_id) { 211 // Make sure this is a user id and not an email address. 212 EXPECT_EQ(user_id, GetUserIDFromEmail(user_id)); 213 active_user_index_ = 0; 214 for (std::vector<MockUserInfo*>::iterator iter = user_list_.begin(); 215 iter != user_list_.end(); 216 ++iter) { 217 if ((*iter)->GetUserID() == user_id) { 218 active_user_index_ = iter - user_list_.begin(); 219 return; 220 } 221 } 222 NOTREACHED() << "Unknown user:" << user_id; 223 } 224 225 void TestSessionStateDelegate::CycleActiveUser(CycleUser cycle_user) { 226 SwitchActiveUser("someone@tray"); 227 } 228 229 void TestSessionStateDelegate::AddSessionStateObserver( 230 SessionStateObserver* observer) { 231 } 232 233 void TestSessionStateDelegate::RemoveSessionStateObserver( 234 SessionStateObserver* observer) { 235 } 236 237 } // namespace test 238 } // namespace ash 239