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

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

Issue 2736573002: chromeos: Move files in //ash/common to //ash, part 2 (Closed)
Patch Set: Created 3 years, 9 months 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
« no previous file with comments | « ash/common/session/session_controller.cc ('k') | ash/common/session/session_state_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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->can_lock_screen = true;
65 info->should_lock_screen_automatically = true;
66 info->add_user_session_policy = AddUserSessionPolicy::ALLOWED;
67 info->state = session_manager::SessionState::LOGIN_PRIMARY;
68 }
69
70 class SessionControllerTest : public testing::Test {
71 public:
72 SessionControllerTest() {}
73 ~SessionControllerTest() override {}
74
75 // testing::Test:
76 void SetUp() override {
77 controller_ = base::MakeUnique<SessionController>();
78 controller_->AddSessionStateObserver(&observer_);
79 }
80
81 void TearDown() override {
82 controller_->RemoveSessionStateObserver(&observer_);
83 }
84
85 void SetSessionInfo(const mojom::SessionInfo& info) {
86 mojom::SessionInfoPtr info_ptr = mojom::SessionInfo::New();
87 *info_ptr = info;
88 controller_->SetSessionInfo(std::move(info_ptr));
89 }
90
91 void UpdateSession(uint32_t session_id, const std::string& email) {
92 mojom::UserSessionPtr session = mojom::UserSession::New();
93 session->session_id = session_id;
94 session->type = user_manager::USER_TYPE_REGULAR;
95 session->account_id = AccountId::FromUserEmail(email);
96 session->display_name = email;
97 session->display_email = email;
98
99 controller_->UpdateUserSession(std::move(session));
100 }
101
102 std::string GetUserSessionEmails() const {
103 std::string emails;
104 for (const auto& session : controller_->GetUserSessions()) {
105 emails += session->display_email + ",";
106 }
107 return emails;
108 }
109
110 SessionController* controller() { return controller_.get(); }
111 const TestSessionStateObserver* observer() const { return &observer_; }
112
113 private:
114 std::unique_ptr<SessionController> controller_;
115 TestSessionStateObserver observer_;
116
117 DISALLOW_COPY_AND_ASSIGN(SessionControllerTest);
118 };
119
120 // Tests that the simple session info is reflected properly.
121 TEST_F(SessionControllerTest, SimpleSessionInfo) {
122 mojom::SessionInfo info;
123 FillDefaultSessionInfo(&info);
124 SetSessionInfo(info);
125
126 EXPECT_EQ(session_manager::kMaxmiumNumberOfUserSessions,
127 controller()->GetMaximumNumberOfLoggedInUsers());
128 EXPECT_TRUE(controller()->CanLockScreen());
129 EXPECT_TRUE(controller()->ShouldLockScreenAutomatically());
130
131 info.can_lock_screen = false;
132 SetSessionInfo(info);
133 EXPECT_FALSE(controller()->CanLockScreen());
134 EXPECT_TRUE(controller()->ShouldLockScreenAutomatically());
135
136 info.should_lock_screen_automatically = false;
137 SetSessionInfo(info);
138 EXPECT_FALSE(controller()->CanLockScreen());
139 EXPECT_FALSE(controller()->ShouldLockScreenAutomatically());
140 }
141
142 // Tests that AddUserSessionPolicy is set properly.
143 TEST_F(SessionControllerTest, AddUserPolicy) {
144 const AddUserSessionPolicy kTestCases[] = {
145 AddUserSessionPolicy::ALLOWED,
146 AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER,
147 AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS,
148 AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED,
149 };
150
151 mojom::SessionInfo info;
152 FillDefaultSessionInfo(&info);
153 for (const auto& policy : kTestCases) {
154 info.add_user_session_policy = policy;
155 SetSessionInfo(info);
156 EXPECT_EQ(policy, controller()->GetAddUserPolicy())
157 << "Test case policy=" << static_cast<int>(policy);
158 }
159 }
160
161 // Tests that session state can be set and reflected properly.
162 TEST_F(SessionControllerTest, SessionState) {
163 const struct {
164 session_manager::SessionState state;
165 bool expected_is_screen_locked;
166 bool expected_is_user_session_blocked;
167 } kTestCases[] = {
168 {session_manager::SessionState::OOBE, false, true},
169 {session_manager::SessionState::LOGIN_PRIMARY, false, true},
170 {session_manager::SessionState::LOGGED_IN_NOT_ACTIVE, false, true},
171 {session_manager::SessionState::ACTIVE, false, false},
172 {session_manager::SessionState::LOCKED, true, true},
173 {session_manager::SessionState::LOGIN_SECONDARY, false, true},
174 };
175
176 mojom::SessionInfo info;
177 FillDefaultSessionInfo(&info);
178 for (const auto& test_case : kTestCases) {
179 info.state = test_case.state;
180 SetSessionInfo(info);
181
182 EXPECT_EQ(test_case.state, controller()->GetSessionState())
183 << "Test case state=" << static_cast<int>(test_case.state);
184 EXPECT_EQ(observer()->state(), controller()->GetSessionState())
185 << "Test case state=" << static_cast<int>(test_case.state);
186 EXPECT_EQ(test_case.expected_is_screen_locked,
187 controller()->IsScreenLocked())
188 << "Test case state=" << static_cast<int>(test_case.state);
189 EXPECT_EQ(test_case.expected_is_user_session_blocked,
190 controller()->IsUserSessionBlocked())
191 << "Test case state=" << static_cast<int>(test_case.state);
192 }
193 }
194
195 // Tests that user sessions can be set and updated.
196 TEST_F(SessionControllerTest, UserSessions) {
197 EXPECT_FALSE(controller()->IsActiveUserSessionStarted());
198
199 UpdateSession(1u, "user1@test.com");
200 EXPECT_TRUE(controller()->IsActiveUserSessionStarted());
201 EXPECT_EQ("user1@test.com,", GetUserSessionEmails());
202 EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails());
203
204 UpdateSession(2u, "user2@test.com");
205 EXPECT_TRUE(controller()->IsActiveUserSessionStarted());
206 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
207 EXPECT_EQ(GetUserSessionEmails(), observer()->GetUserSessionEmails());
208
209 UpdateSession(1u, "user1_changed@test.com");
210 EXPECT_EQ("user1_changed@test.com,user2@test.com,", GetUserSessionEmails());
211 // TODO(xiyuan): Verify observer gets the updated user session info.
212 }
213
214 // Tests that user sessions can be ordered.
215 TEST_F(SessionControllerTest, ActiveSession) {
216 UpdateSession(1u, "user1@test.com");
217 UpdateSession(2u, "user2@test.com");
218
219 std::vector<uint32_t> order = {1u, 2u};
220 controller()->SetUserSessionOrder(order);
221 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
222 EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail());
223
224 order = {2u, 1u};
225 controller()->SetUserSessionOrder(order);
226 EXPECT_EQ("user2@test.com,user1@test.com,", GetUserSessionEmails());
227 EXPECT_EQ("user2@test.com", observer()->active_account_id().GetUserEmail());
228
229 order = {1u, 2u};
230 controller()->SetUserSessionOrder(order);
231 EXPECT_EQ("user1@test.com,user2@test.com,", GetUserSessionEmails());
232 EXPECT_EQ("user1@test.com", observer()->active_account_id().GetUserEmail());
233 }
234
235 } // namespace
236 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/session/session_controller.cc ('k') | ash/common/session/session_state_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698