Chromium Code Reviews| Index: ash/common/session/session_controller_unittest.cc |
| diff --git a/ash/common/session/session_controller_unittest.cc b/ash/common/session/session_controller_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f483413108e66a5c8d8a254997da8188f745e330 |
| --- /dev/null |
| +++ b/ash/common/session/session_controller_unittest.cc |
| @@ -0,0 +1,236 @@ |
| +// Copyright 2016 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/session/session_controller.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "ash/common/session/session_controller.h" |
| +#include "ash/common/session/session_state_observer.h" |
| +#include "ash/public/interfaces/session_controller.mojom.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "components/user_manager/user_type.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace ash { |
| +namespace test { |
|
James Cook
2016/12/06 18:08:25
nit: Remove. I only use "namespace test" for test
xiyuan
2016/12/06 19:24:21
Done.
|
| + |
| +namespace { |
| + |
| +class TestSessionStateObserver : public SessionStateObserver { |
| + public: |
| + TestSessionStateObserver() : active_account_id_(EmptyAccountId()) {} |
| + ~TestSessionStateObserver() override {} |
| + |
| + // SessionStateObserver: |
| + void ActiveUserChanged(const AccountId& account_id) override { |
| + active_account_id_ = account_id; |
| + } |
| + |
| + void UserAddedToSession(const AccountId& account_id) override { |
| + user_session_account_ids_.push_back(account_id); |
| + } |
| + |
| + void SessionStateChanged(session_manager::SessionState state) override { |
| + state_ = state; |
| + } |
| + |
| + std::string GetUserSessionEmails() const { |
| + std::string emails; |
| + for (const auto& account_id : user_session_account_ids_) { |
| + emails += account_id.GetUserEmail() + ","; |
| + } |
| + return emails; |
| + } |
| + |
| + session_manager::SessionState state() const { return state_; } |
| + const AccountId& active_account_id() const { return active_account_id_; } |
| + const std::vector<AccountId>& user_session_account_ids() const { |
| + return user_session_account_ids_; |
| + } |
| + |
| + private: |
| + session_manager::SessionState state_ = session_manager::SessionState::UNKNOWN; |
| + AccountId active_account_id_; |
| + std::vector<AccountId> user_session_account_ids_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestSessionStateObserver); |
| +}; |
| + |
| +void FillDefaultSessionInfo(mojom::SessionInfo* info) { |
| + info->max_users = 123; |
| + info->can_lock_screen = true; |
| + info->should_lock_screen_automatically = true; |
| + info->add_user_session_policy = AddUserSessionPolicy::ALLOWED; |
| + info->state = session_manager::SessionState::LOGIN_PRIMARY; |
| +} |
| + |
| +} // namespace |
| + |
| +class SessionControllerTest : public testing::Test { |
| + public: |
| + SessionControllerTest() {} |
| + ~SessionControllerTest() override {} |
| + |
| + // testing::Test: |
| + void SetUp() override { |
| + controller_ = base::MakeUnique<SessionController>(); |
| + controller_->AddSessionStateObserver(&observer_); |
| + } |
| + |
| + void TearDown() override { |
| + controller_->RemoveSessionStateObserver(&observer_); |
| + } |
| + |
| + void SetSessionInfo(const mojom::SessionInfo& info) { |
| + mojom::SessionInfoPtr info_ptr = mojom::SessionInfo::New(); |
| + *info_ptr = info; |
| + controller_->SetSessionInfo(std::move(info_ptr)); |
| + } |
| + |
| + void UpdateSession(uint32_t session_id, const std::string& email) { |
| + mojom::UserSessionPtr session = mojom::UserSession::New(); |
| + session->session_id = session_id; |
| + session->type = user_manager::USER_TYPE_REGULAR; |
| + session->account_id = AccountId::FromUserEmail(email).Serialize(); |
| + session->display_name = email; |
| + session->display_email = email; |
| + |
| + controller_->UpdateUserSession(std::move(session)); |
| + } |
| + |
| + std::string GetUserSessionEmails() const { |
| + std::string emails; |
| + for (const auto& session : controller_->GetUserSessions()) { |
| + emails += session->display_email + ","; |
| + } |
| + return emails; |
| + } |
| + |
| + SessionController* controller() { return controller_.get(); } |
| + const TestSessionStateObserver* observer() const { return &observer_; } |
| + |
| + private: |
| + std::unique_ptr<SessionController> controller_; |
| + TestSessionStateObserver observer_; |
| + DISALLOW_COPY_AND_ASSIGN(SessionControllerTest); |
|
James Cook
2016/12/06 18:08:25
nit: blank line above
xiyuan
2016/12/06 19:24:21
Done.
|
| +}; |
| + |
| +TEST_F(SessionControllerTest, SimpleSessionInfo) { |
|
James Cook
2016/12/06 18:08:25
nit: Document what the test is testing (and below)
xiyuan
2016/12/06 19:24:21
Done.
|
| + mojom::SessionInfo info; |
| + FillDefaultSessionInfo(&info); |
| + SetSessionInfo(info); |
| + |
| + EXPECT_EQ(123, controller()->GetMaximumNumberOfLoggedInUsers()); |
| + EXPECT_TRUE(controller()->CanLockScreen()); |
| + EXPECT_TRUE(controller()->ShouldLockScreenAutomatically()); |
| + EXPECT_EQ(AddUserSessionPolicy::ALLOWED, controller()->GetAddUserPolicy()); |
| + EXPECT_EQ(session_manager::SessionState::LOGIN_PRIMARY, |
| + controller()->GetSessionState()); |
| + EXPECT_EQ(observer()->state(), controller()->GetSessionState()); |
| + |
| + info.can_lock_screen = false; |
| + SetSessionInfo(info); |
| + EXPECT_FALSE(controller()->CanLockScreen()); |
| + |
| + info.should_lock_screen_automatically = false; |
| + SetSessionInfo(info); |
| + EXPECT_FALSE(controller()->ShouldLockScreenAutomatically()); |
| +} |
| + |
| +TEST_F(SessionControllerTest, AddUserPolicy) { |
| + const AddUserSessionPolicy kTestCases[] = { |
| + AddUserSessionPolicy::ALLOWED, |
| + AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER, |
| + AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS, |
| + AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED, |
| + }; |
| + |
| + mojom::SessionInfo info; |
| + FillDefaultSessionInfo(&info); |
| + for (auto policy : kTestCases) { |
|
James Cook
2016/12/06 18:08:25
auto& or use the underlying type
xiyuan
2016/12/06 19:24:21
Done.
|
| + info.add_user_session_policy = policy; |
| + SetSessionInfo(info); |
| + EXPECT_EQ(policy, controller()->GetAddUserPolicy()) |
| + << "Test case policy=" << static_cast<int>(policy); |
| + } |
| +} |
| + |
| +TEST_F(SessionControllerTest, SessionState) { |
| + const struct { |
| + session_manager::SessionState state; |
| + bool expected_is_screen_locked; |
| + bool expected_is_user_session_blocked; |
| + } kTestCases[] = { |
| + {session_manager::SessionState::OOBE, false, true}, |
| + {session_manager::SessionState::LOGIN_PRIMARY, false, true}, |
| + {session_manager::SessionState::LOGGED_IN_NOT_ACTIVE, false, true}, |
| + {session_manager::SessionState::ACTIVE, false, false}, |
| + {session_manager::SessionState::LOCKED, true, true}, |
| + {session_manager::SessionState::LOGIN_SECONDARY, false, true}, |
| + }; |
| + |
| + mojom::SessionInfo info; |
| + FillDefaultSessionInfo(&info); |
| + for (const auto& test_case : kTestCases) { |
| + info.state = test_case.state; |
| + SetSessionInfo(info); |
| + |
| + EXPECT_EQ(test_case.state, controller()->GetSessionState()) |
| + << "Test case state=" << static_cast<int>(test_case.state); |
| + EXPECT_EQ(observer()->state(), controller()->GetSessionState()) |
| + << "Test case state=" << static_cast<int>(test_case.state); |
| + EXPECT_EQ(test_case.expected_is_screen_locked, |
| + controller()->IsScreenLocked()) |
| + << "Test case state=" << static_cast<int>(test_case.state); |
| + EXPECT_EQ(test_case.expected_is_user_session_blocked, |
| + controller()->IsUserSessionBlocked()) |
| + << "Test case state=" << static_cast<int>(test_case.state); |
| + } |
| +} |
| + |
| +TEST_F(SessionControllerTest, UserSessions) { |
| + EXPECT_FALSE(controller()->IsActiveUserSessionStarted()); |
| + |
| + UpdateSession(1u, "user1@test.com"); |
| + EXPECT_TRUE(controller()->IsActiveUserSessionStarted()); |
| + EXPECT_EQ("user1@test.com,", GetUserSessionEmails()); |
| + EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails()); |
| + |
| + UpdateSession(2u, "user2@test.com"); |
| + EXPECT_TRUE(controller()->IsActiveUserSessionStarted()); |
| + EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails()); |
| + EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails()); |
| + |
| + UpdateSession(1u, "user1_changed@test.com"); |
| + EXPECT_EQ("user1_changed@test.com,user2@test.com,", GetUserSessionEmails()); |
| + // TODO(xiyuan): Verify observer gets the updated user session info. |
| +} |
| + |
| +TEST_F(SessionControllerTest, ActiveSession) { |
| + UpdateSession(1u, "user1@test.com"); |
| + UpdateSession(2u, "user2@test.com"); |
| + |
| + std::vector<uint32_t> order = {1u, 2u}; |
| + controller()->SetUserSessionOrder(order); |
| + EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails()); |
| + EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail()); |
| + |
| + order = {2u, 1u}; |
| + controller()->SetUserSessionOrder(order); |
| + EXPECT_EQ("user2@test.com,user1@test.com,", GetUserSessionEmails()); |
| + EXPECT_EQ("user2@test.com", observer()->active_account_id().GetUserEmail()); |
| + |
| + order = {1u, 2u}; |
| + controller()->SetUserSessionOrder(order); |
| + EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails()); |
| + EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail()); |
| +} |
| + |
| +} // namespace test |
| +} // namespace ash |
|
James Cook
2016/12/06 18:08:25
Nice tests. Good coverage and easy to read.
xiyuan
2016/12/06 19:24:21
:)
|