| 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..b4e86e2ccb2a19f5a560f61219562493eac3f43b
|
| --- /dev/null
|
| +++ b/ash/common/test/test_session_controller_client.cc
|
| @@ -0,0 +1,182 @@
|
| +// 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/logging.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 {
|
| +
|
| +// 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.
|
| +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) {
|
| + DCHECK(controller_);
|
| + Reset();
|
| +}
|
| +
|
| +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() {
|
| + session_info_ = mojom::SessionInfo::New();
|
| + 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();
|
| + controller_->SetSessionInfo(session_info_->Clone());
|
| +}
|
| +
|
| +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.
|
| + WmShell::Get()->UpdateAfterLoginStatusChange(
|
| + state == session_manager::SessionState::ACTIVE
|
| + ? LoginStatus::USER
|
| + : LoginStatus::NOT_LOGGED_IN);
|
| +}
|
| +
|
| +void TestSessionControllerClient::CreatePredefinedUserSessions(int count) {
|
| + DCHECK_GT(count, 0);
|
| +
|
| + // Resets the controller's state.
|
| + Reset();
|
| +
|
| + // Adds user sessions with numbered emails if more are needed.
|
| + for (int numbered_user_index = 0; numbered_user_index < count;
|
| + ++numbered_user_index) {
|
| + AddUserSession(base::StringPrintf("user%d@tray", numbered_user_index));
|
| + }
|
| +
|
| + // Updates session state after adding user sessions.
|
| + SetSessionState(session_manager::SessionState::ACTIVE);
|
| +}
|
| +
|
| +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 (const auto& user_session : controller_->GetUserSessions()) {
|
| + 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(),
|
| + [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
|
|
|