Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: ash/common/session/session_controller_unittest.cc

Issue 2545723003: ash: Add SessionController/Client mojo interfaces (Closed)
Patch Set: fix gn check and address comments in #6 Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/session/session_controller.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "ash/common/session/session_controller.h"
13 #include "ash/common/session/session_state_observer.h"
14 #include "ash/public/interfaces/session_controller.mojom.h"
15 #include "base/macros.h"
16 #include "base/memory/ptr_util.h"
17 #include "components/user_manager/user_type.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace ash {
21 namespace {
22
23 class TestSessionStateObserver : public SessionStateObserver {
24 public:
25 TestSessionStateObserver() : active_account_id_(EmptyAccountId()) {}
26 ~TestSessionStateObserver() override {}
27
28 // SessionStateObserver:
29 void ActiveUserChanged(const AccountId& account_id) override {
30 active_account_id_ = account_id;
31 }
32
33 void UserAddedToSession(const AccountId& account_id) override {
34 user_session_account_ids_.push_back(account_id);
35 }
36
37 void SessionStateChanged(session_manager::SessionState state) override {
38 state_ = state;
39 }
40
41 std::string GetUserSessionEmails() const {
42 std::string emails;
43 for (const auto& account_id : user_session_account_ids_) {
44 emails += account_id.GetUserEmail() + ",";
45 }
46 return emails;
47 }
48
49 session_manager::SessionState state() const { return state_; }
50 const AccountId& active_account_id() const { return active_account_id_; }
51 const std::vector<AccountId>& user_session_account_ids() const {
52 return user_session_account_ids_;
53 }
54
55 private:
56 session_manager::SessionState state_ = session_manager::SessionState::UNKNOWN;
57 AccountId active_account_id_;
58 std::vector<AccountId> user_session_account_ids_;
59
60 DISALLOW_COPY_AND_ASSIGN(TestSessionStateObserver);
61 };
62
63 void FillDefaultSessionInfo(mojom::SessionInfo* info) {
64 info->max_users = 123;
65 info->can_lock_screen = true;
66 info->should_lock_screen_automatically = true;
67 info->add_user_session_policy = AddUserSessionPolicy::ALLOWED;
68 info->state = session_manager::SessionState::LOGIN_PRIMARY;
69 }
70
71 class SessionControllerTest : public testing::Test {
72 public:
73 SessionControllerTest() {}
74 ~SessionControllerTest() override {}
75
76 // testing::Test:
77 void SetUp() override {
78 controller_ = base::MakeUnique<SessionController>();
79 controller_->AddSessionStateObserver(&observer_);
80 }
81
82 void TearDown() override {
83 controller_->RemoveSessionStateObserver(&observer_);
84 }
85
86 void SetSessionInfo(const mojom::SessionInfo& info) {
87 mojom::SessionInfoPtr info_ptr = mojom::SessionInfo::New();
88 *info_ptr = info;
89 controller_->SetSessionInfo(std::move(info_ptr));
90 }
91
92 void UpdateSession(uint32_t session_id, const std::string& email) {
93 mojom::UserSessionPtr session = mojom::UserSession::New();
94 session->session_id = session_id;
95 session->type = user_manager::USER_TYPE_REGULAR;
96 session->serialized_account_id =
97 AccountId::FromUserEmail(email).Serialize();
98 session->display_name = email;
99 session->display_email = email;
100
101 controller_->UpdateUserSession(std::move(session));
102 }
103
104 std::string GetUserSessionEmails() const {
105 std::string emails;
106 for (const auto& session : controller_->GetUserSessions()) {
107 emails += session->display_email + ",";
108 }
109 return emails;
110 }
111
112 SessionController* controller() { return controller_.get(); }
113 const TestSessionStateObserver* observer() const { return &observer_; }
114
115 private:
116 std::unique_ptr<SessionController> controller_;
117 TestSessionStateObserver observer_;
118
119 DISALLOW_COPY_AND_ASSIGN(SessionControllerTest);
120 };
121
122 // Tests that the simple session info is reflected properly.
123 TEST_F(SessionControllerTest, SimpleSessionInfo) {
124 mojom::SessionInfo info;
125 FillDefaultSessionInfo(&info);
126 SetSessionInfo(info);
127
128 EXPECT_EQ(123, controller()->GetMaximumNumberOfLoggedInUsers());
129 EXPECT_TRUE(controller()->CanLockScreen());
130 EXPECT_TRUE(controller()->ShouldLockScreenAutomatically());
131
132 info.can_lock_screen = false;
133 SetSessionInfo(info);
134 EXPECT_FALSE(controller()->CanLockScreen());
135 EXPECT_TRUE(controller()->ShouldLockScreenAutomatically());
136
137 info.should_lock_screen_automatically = false;
138 SetSessionInfo(info);
139 EXPECT_FALSE(controller()->CanLockScreen());
140 EXPECT_FALSE(controller()->ShouldLockScreenAutomatically());
141 }
142
143 // Tests that AddUserSessionPolicy is set properly.
144 TEST_F(SessionControllerTest, AddUserPolicy) {
145 const AddUserSessionPolicy kTestCases[] = {
146 AddUserSessionPolicy::ALLOWED,
147 AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER,
148 AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS,
149 AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED,
150 };
151
152 mojom::SessionInfo info;
153 FillDefaultSessionInfo(&info);
154 for (const auto& policy : kTestCases) {
155 info.add_user_session_policy = policy;
156 SetSessionInfo(info);
157 EXPECT_EQ(policy, controller()->GetAddUserPolicy())
158 << "Test case policy=" << static_cast<int>(policy);
159 }
160 }
161
162 // Tests that session state can be set and reflected properly.
163 TEST_F(SessionControllerTest, SessionState) {
164 const struct {
165 session_manager::SessionState state;
166 bool expected_is_screen_locked;
167 bool expected_is_user_session_blocked;
168 } kTestCases[] = {
169 {session_manager::SessionState::OOBE, false, true},
170 {session_manager::SessionState::LOGIN_PRIMARY, false, true},
171 {session_manager::SessionState::LOGGED_IN_NOT_ACTIVE, false, true},
172 {session_manager::SessionState::ACTIVE, false, false},
173 {session_manager::SessionState::LOCKED, true, true},
174 {session_manager::SessionState::LOGIN_SECONDARY, false, true},
175 };
176
177 mojom::SessionInfo info;
178 FillDefaultSessionInfo(&info);
179 for (const auto& test_case : kTestCases) {
180 info.state = test_case.state;
181 SetSessionInfo(info);
182
183 EXPECT_EQ(test_case.state, controller()->GetSessionState())
184 << "Test case state=" << static_cast<int>(test_case.state);
185 EXPECT_EQ(observer()->state(), controller()->GetSessionState())
186 << "Test case state=" << static_cast<int>(test_case.state);
187 EXPECT_EQ(test_case.expected_is_screen_locked,
188 controller()->IsScreenLocked())
189 << "Test case state=" << static_cast<int>(test_case.state);
190 EXPECT_EQ(test_case.expected_is_user_session_blocked,
191 controller()->IsUserSessionBlocked())
192 << "Test case state=" << static_cast<int>(test_case.state);
193 }
194 }
195
196 // Tests that user sessions can be set and updated.
197 TEST_F(SessionControllerTest, UserSessions) {
198 EXPECT_FALSE(controller()->IsActiveUserSessionStarted());
199
200 UpdateSession(1u, "user1@test.com");
201 EXPECT_TRUE(controller()->IsActiveUserSessionStarted());
202 EXPECT_EQ("user1@test.com,", GetUserSessionEmails());
203 EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails());
204
205 UpdateSession(2u, "user2@test.com");
206 EXPECT_TRUE(controller()->IsActiveUserSessionStarted());
207 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
208 EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails());
209
210 UpdateSession(1u, "user1_changed@test.com");
211 EXPECT_EQ("user1_changed@test.com,user2@test.com,", GetUserSessionEmails());
212 // TODO(xiyuan): Verify observer gets the updated user session info.
213 }
214
215 // Tests that user sessions can be ordered.
216 TEST_F(SessionControllerTest, ActiveSession) {
217 UpdateSession(1u, "user1@test.com");
218 UpdateSession(2u, "user2@test.com");
219
220 std::vector<uint32_t> order = {1u, 2u};
221 controller()->SetUserSessionOrder(order);
222 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
223 EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail());
224
225 order = {2u, 1u};
226 controller()->SetUserSessionOrder(order);
227 EXPECT_EQ("user2@test.com,user1@test.com,", GetUserSessionEmails());
228 EXPECT_EQ("user2@test.com", observer()->active_account_id().GetUserEmail());
229
230 order = {1u, 2u};
231 controller()->SetUserSessionOrder(order);
232 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
233 EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail());
234 }
235
236 } // namespace
237 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698