Chromium Code Reviews| Index: ash/common/test/test_session_controller_client.cc |
| diff --git a/ash/common/test/test_session_controller_client.cc b/ash/common/test/test_session_controller_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f75ec6016e8141f38516fc4eed4ad2fcd41ceb65 |
| --- /dev/null |
| +++ b/ash/common/test/test_session_controller_client.cc |
| @@ -0,0 +1,194 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/common/test/test_session_controller_client.h" |
| + |
| +#include <algorithm> |
| +#include <string> |
| + |
| +#include "ash/common/login_status.h" |
| +#include "ash/common/session/session_controller.h" |
| +#include "ash/common/wm_shell.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "components/session_manager/session_manager_types.h" |
| +#include "components/signin/core/account_id/account_id.h" |
| +#include "components/user_manager/user_type.h" |
| + |
| +namespace ash { |
| +namespace test { |
| + |
| +namespace { |
| + |
| +const char* kPredefinedUserEmails[] = { |
| + // This is intended to be capitalized. |
| + "First@tray", |
|
James Cook
2017/03/17 17:14:36
Yuck. Do these have to match particular tests in a
xiyuan
2017/03/17 22:52:02
Moved into tray_user_unittests.cc when following y
|
| + // This is intended to be capitalized. |
| + "Second@tray", "third@tray", "someone@tray"}; |
| + |
| +// Returns the "canonicalized" email from a given |email| address. Note |
| +// production code should use gaia::CanonicalizeEmail. This is used in tests |
| +// without introducing dependency on google_api. |
|
James Cook
2017/03/17 17:14:36
Nice comment.
|
| +std::string GetUserIdFromEmail(const std::string& email) { |
| + std::string user_id = email; |
| + std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower); |
| + return user_id; |
| +} |
| + |
| +} // namespace |
| + |
| +TestSessionControllerClient::TestSessionControllerClient( |
| + SessionController* controller) |
| + : controller_(controller), |
| + binding_(this), |
| + session_info_(mojom::SessionInfo::New()) {} |
|
James Cook
2017/03/17 17:14:36
nit: DCHECK(controller_)
xiyuan
2017/03/17 22:52:02
Done.
|
| + |
| +TestSessionControllerClient::~TestSessionControllerClient() = default; |
| + |
| +void TestSessionControllerClient::InitializeAndBind() { |
| + session_info_->can_lock_screen = controller_->CanLockScreen(); |
| + session_info_->should_lock_screen_automatically = |
| + controller_->ShouldLockScreenAutomatically(); |
| + session_info_->add_user_session_policy = controller_->GetAddUserPolicy(); |
| + session_info_->state = controller_->GetSessionState(); |
| + |
| + controller_->SetClient(binding_.CreateInterfacePtrAndBind()); |
| +} |
| + |
| +void TestSessionControllerClient::Reset() { |
|
James Cook
2017/03/17 17:14:36
Should the constructor call this?
Alternately, sh
xiyuan
2017/03/17 22:52:02
Made constructor to call this and also moved mojom
|
| + session_info_->can_lock_screen = true; |
| + session_info_->should_lock_screen_automatically = false; |
| + session_info_->add_user_session_policy = AddUserSessionPolicy::ALLOWED; |
| + session_info_->state = session_manager::SessionState::LOGIN_PRIMARY; |
| + |
| + controller_->ClearUserSessionsForTest(); |
| +} |
| + |
| +void TestSessionControllerClient::SetCanLockScreen(bool can_lock) { |
| + session_info_->can_lock_screen = can_lock; |
| + controller_->SetSessionInfo(session_info_->Clone()); |
| +} |
| + |
| +void TestSessionControllerClient::SetShouldLockScreenAutomatically( |
| + bool should_lock) { |
| + session_info_->should_lock_screen_automatically = should_lock; |
| + controller_->SetSessionInfo(session_info_->Clone()); |
| +} |
| + |
| +void TestSessionControllerClient::SetSessionState( |
| + session_manager::SessionState state) { |
| + session_info_->state = state; |
| + controller_->SetSessionInfo(session_info_->Clone()); |
| + |
| + // TODO(xiyuan): Remove after LoginStatus becomes part of SessionController. |
|
James Cook
2017/03/17 17:14:35
Is this still necessary after my CL?
xiyuan
2017/03/17 22:52:02
Still needs this for cash. Your CL covers the mash
James Cook
2017/03/18 18:40:05
Acknowledged.
|
| + WmShell::Get()->UpdateAfterLoginStatusChange( |
| + state == session_manager::SessionState::ACTIVE |
| + ? LoginStatus::USER |
| + : LoginStatus::NOT_LOGGED_IN); |
| +} |
| + |
| +void TestSessionControllerClient::SetPredefinedUserSessions(int count) { |
| + // Resets the controller's state. |
| + Reset(); |
| + |
| + // Adds user sessions with predefined emails. |
| + for (auto* user_email : kPredefinedUserEmails) { |
| + if (!count) |
| + break; |
| + AddUserSession(user_email); |
| + --count; |
| + } |
| + |
| + // Adds user sessions with numbered emails if more are needed. |
| + int numbered_user_index = 0; |
| + while (count) { |
| + AddUserSession(base::StringPrintf("user%d@tray", numbered_user_index)); |
| + ++numbered_user_index; |
| + --count; |
| + } |
| + |
| + // Updates session state after adding user sessions. |
| + SetSessionState(controller_->NumberOfLoggedInUsers() > 0 |
| + ? session_manager::SessionState::ACTIVE |
| + : session_manager::SessionState::LOGIN_PRIMARY); |
| +} |
| + |
| +void TestSessionControllerClient::AddUserSession( |
| + const std::string& display_email) { |
| + mojom::UserSessionPtr session = mojom::UserSession::New(); |
| + session->session_id = ++fake_session_id_; |
| + session->type = user_manager::USER_TYPE_REGULAR; |
| + session->account_id = |
| + AccountId::FromUserEmail(GetUserIdFromEmail(display_email)); |
| + session->display_name = "Über tray Über tray Über tray Über tray"; |
| + session->display_email = display_email; |
| + controller_->UpdateUserSession(std::move(session)); |
| +} |
| + |
| +void TestSessionControllerClient::UnlockScreen() { |
| + SetSessionState(session_manager::SessionState::ACTIVE); |
| +} |
| + |
| +void TestSessionControllerClient::RequestLockScreen() { |
| + SetSessionState(session_manager::SessionState::LOCKED); |
| +} |
| + |
| +void TestSessionControllerClient::SwitchActiveUser( |
| + const AccountId& account_id) { |
| + std::vector<uint32_t> session_order; |
| + session_order.reserve(controller_->GetUserSessions().size()); |
| + |
| + for (auto& user_session : controller_->GetUserSessions()) { |
|
James Cook
2017/03/17 17:14:35
const?
xiyuan
2017/03/17 22:52:02
Done.
|
| + if (user_session->account_id == account_id) { |
| + session_order.insert(session_order.begin(), user_session->session_id); |
| + } else { |
| + session_order.push_back(user_session->session_id); |
| + } |
| + } |
| + |
| + controller_->SetUserSessionOrder(session_order); |
| +} |
| + |
| +void TestSessionControllerClient::CycleActiveUser( |
| + CycleUserDirection direction) { |
| + DCHECK_GT(controller_->NumberOfLoggedInUsers(), 0); |
| + |
| + const std::vector<mojom::UserSessionPtr>& sessions = |
| + controller_->GetUserSessions(); |
| + const size_t session_count = sessions.size(); |
| + |
| + // The following code depends on that |fake_session_id_| is used to generate |
| + // session ids. |
| + uint32_t session_id = sessions[0]->session_id; |
| + if (direction == CycleUserDirection::NEXT) { |
| + ++session_id; |
| + } else if (direction == CycleUserDirection::PREVIOUS) { |
| + DCHECK_GT(session_id, 0u); |
| + --session_id; |
| + } else { |
| + LOG(ERROR) << "Invalid cycle user direction=" |
| + << static_cast<int>(direction); |
| + return; |
| + } |
| + |
| + // Valid range of an session id is [1, session_count]. |
| + if (session_id < 1u) |
| + session_id = session_count; |
| + if (session_id > session_count) |
| + session_id = 1u; |
| + |
| + // Maps session id to AccountId and call SwitchActiveUser. |
| + auto it = std::find_if(sessions.begin(), sessions.end(), |
|
James Cook
2017/03/17 17:14:36
+1 for using find_if(). I'm starting to like small
xiyuan
2017/03/17 22:52:02
:)
|
| + [session_id](const mojom::UserSessionPtr& session) { |
| + return session && session->session_id == session_id; |
| + }); |
| + if (it == sessions.end()) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + SwitchActiveUser((*it)->account_id); |
| +} |
| + |
| +} // namespace test |
| +} // namespace ash |