OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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_controller_client.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <string> |
| 9 |
| 10 #include "ash/common/login_status.h" |
| 11 #include "ash/common/session/session_controller.h" |
| 12 #include "ash/common/wm_shell.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/strings/stringprintf.h" |
| 15 #include "components/session_manager/session_manager_types.h" |
| 16 #include "components/signin/core/account_id/account_id.h" |
| 17 #include "components/user_manager/user_type.h" |
| 18 |
| 19 namespace ash { |
| 20 namespace test { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Returns the "canonicalized" email from a given |email| address. Note |
| 25 // production code should use gaia::CanonicalizeEmail. This is used in tests |
| 26 // without introducing dependency on google_api. |
| 27 std::string GetUserIdFromEmail(const std::string& email) { |
| 28 std::string user_id = email; |
| 29 std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower); |
| 30 return user_id; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 TestSessionControllerClient::TestSessionControllerClient( |
| 36 SessionController* controller) |
| 37 : controller_(controller), binding_(this) { |
| 38 DCHECK(controller_); |
| 39 Reset(); |
| 40 } |
| 41 |
| 42 TestSessionControllerClient::~TestSessionControllerClient() = default; |
| 43 |
| 44 void TestSessionControllerClient::InitializeAndBind() { |
| 45 session_info_->can_lock_screen = controller_->CanLockScreen(); |
| 46 session_info_->should_lock_screen_automatically = |
| 47 controller_->ShouldLockScreenAutomatically(); |
| 48 session_info_->add_user_session_policy = controller_->GetAddUserPolicy(); |
| 49 session_info_->state = controller_->GetSessionState(); |
| 50 |
| 51 controller_->SetClient(binding_.CreateInterfacePtrAndBind()); |
| 52 } |
| 53 |
| 54 void TestSessionControllerClient::Reset() { |
| 55 session_info_ = mojom::SessionInfo::New(); |
| 56 session_info_->can_lock_screen = true; |
| 57 session_info_->should_lock_screen_automatically = false; |
| 58 session_info_->add_user_session_policy = AddUserSessionPolicy::ALLOWED; |
| 59 session_info_->state = session_manager::SessionState::LOGIN_PRIMARY; |
| 60 |
| 61 controller_->ClearUserSessionsForTest(); |
| 62 controller_->SetSessionInfo(session_info_->Clone()); |
| 63 } |
| 64 |
| 65 void TestSessionControllerClient::SetCanLockScreen(bool can_lock) { |
| 66 session_info_->can_lock_screen = can_lock; |
| 67 controller_->SetSessionInfo(session_info_->Clone()); |
| 68 } |
| 69 |
| 70 void TestSessionControllerClient::SetShouldLockScreenAutomatically( |
| 71 bool should_lock) { |
| 72 session_info_->should_lock_screen_automatically = should_lock; |
| 73 controller_->SetSessionInfo(session_info_->Clone()); |
| 74 } |
| 75 |
| 76 void TestSessionControllerClient::SetSessionState( |
| 77 session_manager::SessionState state) { |
| 78 session_info_->state = state; |
| 79 controller_->SetSessionInfo(session_info_->Clone()); |
| 80 |
| 81 // TODO(xiyuan): Remove after LoginStatus becomes part of SessionController. |
| 82 WmShell::Get()->UpdateAfterLoginStatusChange( |
| 83 state == session_manager::SessionState::ACTIVE |
| 84 ? LoginStatus::USER |
| 85 : LoginStatus::NOT_LOGGED_IN); |
| 86 } |
| 87 |
| 88 void TestSessionControllerClient::CreatePredefinedUserSessions(int count) { |
| 89 DCHECK_GT(count, 0); |
| 90 |
| 91 // Resets the controller's state. |
| 92 Reset(); |
| 93 |
| 94 // Adds user sessions with numbered emails if more are needed. |
| 95 for (int numbered_user_index = 0; numbered_user_index < count; |
| 96 ++numbered_user_index) { |
| 97 AddUserSession(base::StringPrintf("user%d@tray", numbered_user_index)); |
| 98 } |
| 99 |
| 100 // Updates session state after adding user sessions. |
| 101 SetSessionState(session_manager::SessionState::ACTIVE); |
| 102 } |
| 103 |
| 104 void TestSessionControllerClient::AddUserSession( |
| 105 const std::string& display_email) { |
| 106 mojom::UserSessionPtr session = mojom::UserSession::New(); |
| 107 session->session_id = ++fake_session_id_; |
| 108 session->type = user_manager::USER_TYPE_REGULAR; |
| 109 session->account_id = |
| 110 AccountId::FromUserEmail(GetUserIdFromEmail(display_email)); |
| 111 session->display_name = "Über tray Über tray Über tray Über tray"; |
| 112 session->display_email = display_email; |
| 113 controller_->UpdateUserSession(std::move(session)); |
| 114 } |
| 115 |
| 116 void TestSessionControllerClient::UnlockScreen() { |
| 117 SetSessionState(session_manager::SessionState::ACTIVE); |
| 118 } |
| 119 |
| 120 void TestSessionControllerClient::RequestLockScreen() { |
| 121 SetSessionState(session_manager::SessionState::LOCKED); |
| 122 } |
| 123 |
| 124 void TestSessionControllerClient::SwitchActiveUser( |
| 125 const AccountId& account_id) { |
| 126 std::vector<uint32_t> session_order; |
| 127 session_order.reserve(controller_->GetUserSessions().size()); |
| 128 |
| 129 for (const auto& user_session : controller_->GetUserSessions()) { |
| 130 if (user_session->account_id == account_id) { |
| 131 session_order.insert(session_order.begin(), user_session->session_id); |
| 132 } else { |
| 133 session_order.push_back(user_session->session_id); |
| 134 } |
| 135 } |
| 136 |
| 137 controller_->SetUserSessionOrder(session_order); |
| 138 } |
| 139 |
| 140 void TestSessionControllerClient::CycleActiveUser( |
| 141 CycleUserDirection direction) { |
| 142 DCHECK_GT(controller_->NumberOfLoggedInUsers(), 0); |
| 143 |
| 144 const std::vector<mojom::UserSessionPtr>& sessions = |
| 145 controller_->GetUserSessions(); |
| 146 const size_t session_count = sessions.size(); |
| 147 |
| 148 // The following code depends on that |fake_session_id_| is used to generate |
| 149 // session ids. |
| 150 uint32_t session_id = sessions[0]->session_id; |
| 151 if (direction == CycleUserDirection::NEXT) { |
| 152 ++session_id; |
| 153 } else if (direction == CycleUserDirection::PREVIOUS) { |
| 154 DCHECK_GT(session_id, 0u); |
| 155 --session_id; |
| 156 } else { |
| 157 LOG(ERROR) << "Invalid cycle user direction=" |
| 158 << static_cast<int>(direction); |
| 159 return; |
| 160 } |
| 161 |
| 162 // Valid range of an session id is [1, session_count]. |
| 163 if (session_id < 1u) |
| 164 session_id = session_count; |
| 165 if (session_id > session_count) |
| 166 session_id = 1u; |
| 167 |
| 168 // Maps session id to AccountId and call SwitchActiveUser. |
| 169 auto it = std::find_if(sessions.begin(), sessions.end(), |
| 170 [session_id](const mojom::UserSessionPtr& session) { |
| 171 return session && session->session_id == session_id; |
| 172 }); |
| 173 if (it == sessions.end()) { |
| 174 NOTREACHED(); |
| 175 return; |
| 176 } |
| 177 |
| 178 SwitchActiveUser((*it)->account_id); |
| 179 } |
| 180 |
| 181 } // namespace test |
| 182 } // namespace ash |
OLD | NEW |