| OLD | NEW |
| (Empty) |
| 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/common/test/test_session_state_delegate.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "ash/common/login_status.h" | |
| 11 #include "ash/common/wm_shell.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "components/signin/core/account_id/account_id.h" | |
| 16 #include "components/user_manager/user_info.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 namespace test { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Returns the "canonicalized" email from a given |email| address. | |
| 25 std::string GetUserIdFromEmail(const std::string& email) { | |
| 26 std::string user_id = email; | |
| 27 std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower); | |
| 28 return user_id; | |
| 29 } | |
| 30 | |
| 31 // Returns Account ID from a given |email| address. | |
| 32 AccountId GetAccountIdFromEmail(const std::string& email) { | |
| 33 return AccountId::FromUserEmail(GetUserIdFromEmail(email)); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 class MockUserInfo : public user_manager::UserInfo { | |
| 39 public: | |
| 40 explicit MockUserInfo(const std::string& display_email) | |
| 41 : display_email_(display_email), | |
| 42 account_id_(GetAccountIdFromEmail(display_email)) {} | |
| 43 ~MockUserInfo() override {} | |
| 44 | |
| 45 void SetUserImage(const gfx::ImageSkia& user_image) { | |
| 46 user_image_ = user_image; | |
| 47 } | |
| 48 | |
| 49 base::string16 GetDisplayName() const override { | |
| 50 return base::UTF8ToUTF16("Über tray Über tray Über tray Über tray"); | |
| 51 } | |
| 52 | |
| 53 base::string16 GetGivenName() const override { | |
| 54 return base::UTF8ToUTF16("Über Über Über Über"); | |
| 55 } | |
| 56 | |
| 57 std::string GetDisplayEmail() const override { return display_email_; } | |
| 58 | |
| 59 const AccountId& GetAccountId() const override { return account_id_; } | |
| 60 | |
| 61 const gfx::ImageSkia& GetImage() const override { return user_image_; } | |
| 62 | |
| 63 // A test user image. | |
| 64 gfx::ImageSkia user_image_; | |
| 65 | |
| 66 std::string display_email_; | |
| 67 const AccountId account_id_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(MockUserInfo); | |
| 70 }; | |
| 71 | |
| 72 // A test version of user_manager::UserManager which can be used for testing on | |
| 73 // non-ChromeOS builds. | |
| 74 class TestSessionStateDelegate::TestUserManager { | |
| 75 public: | |
| 76 TestUserManager() : session_started_(false) {} | |
| 77 | |
| 78 void SessionStarted() { session_started_ = true; } | |
| 79 | |
| 80 bool IsSessionStarted() const { return session_started_; } | |
| 81 | |
| 82 private: | |
| 83 // True if SessionStarted() has been called. | |
| 84 bool session_started_; | |
| 85 DISALLOW_COPY_AND_ASSIGN(TestUserManager); | |
| 86 }; | |
| 87 | |
| 88 TestSessionStateDelegate::TestSessionStateDelegate() | |
| 89 : can_lock_screen_(true), | |
| 90 should_lock_screen_automatically_(false), | |
| 91 screen_locked_(false), | |
| 92 user_adding_screen_running_(false), | |
| 93 logged_in_users_(1), | |
| 94 active_user_index_(0), | |
| 95 user_manager_(new TestUserManager()), | |
| 96 session_state_(session_manager::SessionState::LOGIN_PRIMARY) { | |
| 97 // This is intended to be capitalized. | |
| 98 user_list_.push_back(base::MakeUnique<MockUserInfo>("First@tray")); | |
| 99 // This is intended to be capitalized. | |
| 100 user_list_.push_back(base::MakeUnique<MockUserInfo>("Second@tray")); | |
| 101 user_list_.push_back(base::MakeUnique<MockUserInfo>("third@tray")); | |
| 102 user_list_.push_back(base::MakeUnique<MockUserInfo>("someone@tray")); | |
| 103 } | |
| 104 | |
| 105 TestSessionStateDelegate::~TestSessionStateDelegate() {} | |
| 106 | |
| 107 void TestSessionStateDelegate::AddUser(const AccountId& account_id) { | |
| 108 user_list_.push_back( | |
| 109 base::MakeUnique<MockUserInfo>(account_id.GetUserEmail())); | |
| 110 } | |
| 111 | |
| 112 const user_manager::UserInfo* TestSessionStateDelegate::GetActiveUserInfo() | |
| 113 const { | |
| 114 return user_list_[active_user_index_].get(); | |
| 115 } | |
| 116 | |
| 117 int TestSessionStateDelegate::GetMaximumNumberOfLoggedInUsers() const { | |
| 118 return 3; | |
| 119 } | |
| 120 | |
| 121 int TestSessionStateDelegate::NumberOfLoggedInUsers() const { | |
| 122 // TODO(skuhne): Add better test framework to test multiple profiles. | |
| 123 return IsActiveUserSessionStarted() ? logged_in_users_ : 0; | |
| 124 } | |
| 125 | |
| 126 bool TestSessionStateDelegate::IsActiveUserSessionStarted() const { | |
| 127 return user_manager_->IsSessionStarted() && | |
| 128 session_state_ == session_manager::SessionState::ACTIVE; | |
| 129 } | |
| 130 | |
| 131 bool TestSessionStateDelegate::CanLockScreen() const { | |
| 132 return IsActiveUserSessionStarted() && can_lock_screen_; | |
| 133 } | |
| 134 | |
| 135 bool TestSessionStateDelegate::IsScreenLocked() const { | |
| 136 return screen_locked_; | |
| 137 } | |
| 138 | |
| 139 bool TestSessionStateDelegate::ShouldLockScreenAutomatically() const { | |
| 140 return should_lock_screen_automatically_; | |
| 141 } | |
| 142 | |
| 143 void TestSessionStateDelegate::LockScreen() { | |
| 144 if (CanLockScreen()) | |
| 145 screen_locked_ = true; | |
| 146 } | |
| 147 | |
| 148 void TestSessionStateDelegate::UnlockScreen() { | |
| 149 screen_locked_ = false; | |
| 150 } | |
| 151 | |
| 152 bool TestSessionStateDelegate::IsUserSessionBlocked() const { | |
| 153 return !IsActiveUserSessionStarted() || IsScreenLocked() || | |
| 154 user_adding_screen_running_ || | |
| 155 session_state_ != session_manager::SessionState::ACTIVE; | |
| 156 } | |
| 157 | |
| 158 session_manager::SessionState TestSessionStateDelegate::GetSessionState() | |
| 159 const { | |
| 160 return session_state_; | |
| 161 } | |
| 162 | |
| 163 void TestSessionStateDelegate::SetHasActiveUser(bool has_active_user) { | |
| 164 session_state_ = has_active_user | |
| 165 ? session_manager::SessionState::ACTIVE | |
| 166 : session_manager::SessionState::LOGIN_PRIMARY; | |
| 167 } | |
| 168 | |
| 169 void TestSessionStateDelegate::SetActiveUserSessionStarted( | |
| 170 bool active_user_session_started) { | |
| 171 if (active_user_session_started) { | |
| 172 user_manager_->SessionStarted(); | |
| 173 session_state_ = session_manager::SessionState::ACTIVE; | |
| 174 WmShell::Get()->CreateShelfView(); | |
| 175 WmShell::Get()->UpdateAfterLoginStatusChange(LoginStatus::USER); | |
| 176 } else { | |
| 177 session_state_ = session_manager::SessionState::LOGIN_PRIMARY; | |
| 178 user_manager_.reset(new TestUserManager()); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 void TestSessionStateDelegate::SetCanLockScreen(bool can_lock_screen) { | |
| 184 CHECK(WmShell::HasInstance()); | |
| 185 static_cast<ash::test::TestSessionStateDelegate*>( | |
| 186 WmShell::Get()->GetSessionStateDelegate()) | |
| 187 ->can_lock_screen_ = can_lock_screen; | |
| 188 } | |
| 189 | |
| 190 void TestSessionStateDelegate::SetShouldLockScreenAutomatically( | |
| 191 bool should_lock) { | |
| 192 should_lock_screen_automatically_ = should_lock; | |
| 193 } | |
| 194 | |
| 195 void TestSessionStateDelegate::SetUserAddingScreenRunning( | |
| 196 bool user_adding_screen_running) { | |
| 197 user_adding_screen_running_ = user_adding_screen_running; | |
| 198 if (user_adding_screen_running_) | |
| 199 session_state_ = session_manager::SessionState::LOGIN_SECONDARY; | |
| 200 else | |
| 201 session_state_ = session_manager::SessionState::ACTIVE; | |
| 202 } | |
| 203 | |
| 204 void TestSessionStateDelegate::SetUserImage(const gfx::ImageSkia& user_image) { | |
| 205 user_list_[active_user_index_]->SetUserImage(user_image); | |
| 206 } | |
| 207 | |
| 208 const user_manager::UserInfo* TestSessionStateDelegate::GetUserInfo( | |
| 209 UserIndex index) const { | |
| 210 int max = static_cast<int>(user_list_.size()); | |
| 211 return user_list_[index < max ? index : max - 1].get(); | |
| 212 } | |
| 213 | |
| 214 bool TestSessionStateDelegate::ShouldShowAvatar(WmWindow* window) const { | |
| 215 return !GetActiveUserInfo()->GetImage().isNull(); | |
| 216 } | |
| 217 | |
| 218 gfx::ImageSkia TestSessionStateDelegate::GetAvatarImageForWindow( | |
| 219 WmWindow* window) const { | |
| 220 return gfx::ImageSkia(); | |
| 221 } | |
| 222 | |
| 223 void TestSessionStateDelegate::SwitchActiveUser(const AccountId& account_id) { | |
| 224 // Make sure this is a user id and not an email address. | |
| 225 EXPECT_EQ(account_id.GetUserEmail(), | |
| 226 GetUserIdFromEmail(account_id.GetUserEmail())); | |
| 227 active_user_index_ = 0; | |
| 228 for (auto iter = user_list_.begin(); iter != user_list_.end(); ++iter) { | |
| 229 if ((*iter)->GetAccountId() == account_id) { | |
| 230 active_user_index_ = iter - user_list_.begin(); | |
| 231 return; | |
| 232 } | |
| 233 } | |
| 234 NOTREACHED() << "Unknown user:" << account_id.GetUserEmail(); | |
| 235 } | |
| 236 | |
| 237 void TestSessionStateDelegate::CycleActiveUser(CycleUserDirection direction) { | |
| 238 SwitchActiveUser(AccountId::FromUserEmail("someone@tray")); | |
| 239 } | |
| 240 | |
| 241 bool TestSessionStateDelegate::IsMultiProfileAllowedByPrimaryUserPolicy() | |
| 242 const { | |
| 243 return true; | |
| 244 } | |
| 245 | |
| 246 void TestSessionStateDelegate::AddSessionStateObserver( | |
| 247 SessionStateObserver* observer) {} | |
| 248 | |
| 249 void TestSessionStateDelegate::RemoveSessionStateObserver( | |
| 250 SessionStateObserver* observer) {} | |
| 251 | |
| 252 } // namespace test | |
| 253 } // namespace ash | |
| OLD | NEW |