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

Side by Side Diff: ash/common/test/test_session_controller_client.cc

Issue 2734933004: ash: Use SessionController instead of SessionStateDelegate (Closed)
Patch Set: rebase 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
OLDNEW
(Empty)
1 // Copyright 2017 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/test/test_session_controller_client.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "ash/common/login_status.h"
11 #include "ash/common/session/session_controller.h"
12 #include "ash/common/wm_shell.h"
13 #include "base/strings/stringprintf.h"
14 #include "components/session_manager/session_manager_types.h"
15 #include "components/signin/core/account_id/account_id.h"
16 #include "components/user_manager/user_type.h"
17
18 namespace ash {
19 namespace test {
20
21 namespace {
22
23 const char* kPredefinedUserEmails[] = {
24 // This is intended to be capitalized.
25 "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
26 // This is intended to be capitalized.
27 "Second@tray", "third@tray", "someone@tray"};
28
29 // Returns the "canonicalized" email from a given |email| address. Note
30 // production code should use gaia::CanonicalizeEmail. This is used in tests
31 // without introducing dependency on google_api.
James Cook 2017/03/17 17:14:36 Nice comment.
32 std::string GetUserIdFromEmail(const std::string& email) {
33 std::string user_id = email;
34 std::transform(user_id.begin(), user_id.end(), user_id.begin(), ::tolower);
35 return user_id;
36 }
37
38 } // namespace
39
40 TestSessionControllerClient::TestSessionControllerClient(
41 SessionController* controller)
42 : controller_(controller),
43 binding_(this),
44 session_info_(mojom::SessionInfo::New()) {}
James Cook 2017/03/17 17:14:36 nit: DCHECK(controller_)
xiyuan 2017/03/17 22:52:02 Done.
45
46 TestSessionControllerClient::~TestSessionControllerClient() = default;
47
48 void TestSessionControllerClient::InitializeAndBind() {
49 session_info_->can_lock_screen = controller_->CanLockScreen();
50 session_info_->should_lock_screen_automatically =
51 controller_->ShouldLockScreenAutomatically();
52 session_info_->add_user_session_policy = controller_->GetAddUserPolicy();
53 session_info_->state = controller_->GetSessionState();
54
55 controller_->SetClient(binding_.CreateInterfacePtrAndBind());
56 }
57
58 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
59 session_info_->can_lock_screen = true;
60 session_info_->should_lock_screen_automatically = false;
61 session_info_->add_user_session_policy = AddUserSessionPolicy::ALLOWED;
62 session_info_->state = session_manager::SessionState::LOGIN_PRIMARY;
63
64 controller_->ClearUserSessionsForTest();
65 }
66
67 void TestSessionControllerClient::SetCanLockScreen(bool can_lock) {
68 session_info_->can_lock_screen = can_lock;
69 controller_->SetSessionInfo(session_info_->Clone());
70 }
71
72 void TestSessionControllerClient::SetShouldLockScreenAutomatically(
73 bool should_lock) {
74 session_info_->should_lock_screen_automatically = should_lock;
75 controller_->SetSessionInfo(session_info_->Clone());
76 }
77
78 void TestSessionControllerClient::SetSessionState(
79 session_manager::SessionState state) {
80 session_info_->state = state;
81 controller_->SetSessionInfo(session_info_->Clone());
82
83 // 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.
84 WmShell::Get()->UpdateAfterLoginStatusChange(
85 state == session_manager::SessionState::ACTIVE
86 ? LoginStatus::USER
87 : LoginStatus::NOT_LOGGED_IN);
88 }
89
90 void TestSessionControllerClient::SetPredefinedUserSessions(int count) {
91 // Resets the controller's state.
92 Reset();
93
94 // Adds user sessions with predefined emails.
95 for (auto* user_email : kPredefinedUserEmails) {
96 if (!count)
97 break;
98 AddUserSession(user_email);
99 --count;
100 }
101
102 // Adds user sessions with numbered emails if more are needed.
103 int numbered_user_index = 0;
104 while (count) {
105 AddUserSession(base::StringPrintf("user%d@tray", numbered_user_index));
106 ++numbered_user_index;
107 --count;
108 }
109
110 // Updates session state after adding user sessions.
111 SetSessionState(controller_->NumberOfLoggedInUsers() > 0
112 ? session_manager::SessionState::ACTIVE
113 : session_manager::SessionState::LOGIN_PRIMARY);
114 }
115
116 void TestSessionControllerClient::AddUserSession(
117 const std::string& display_email) {
118 mojom::UserSessionPtr session = mojom::UserSession::New();
119 session->session_id = ++fake_session_id_;
120 session->type = user_manager::USER_TYPE_REGULAR;
121 session->account_id =
122 AccountId::FromUserEmail(GetUserIdFromEmail(display_email));
123 session->display_name = "Über tray Über tray Über tray Über tray";
124 session->display_email = display_email;
125 controller_->UpdateUserSession(std::move(session));
126 }
127
128 void TestSessionControllerClient::UnlockScreen() {
129 SetSessionState(session_manager::SessionState::ACTIVE);
130 }
131
132 void TestSessionControllerClient::RequestLockScreen() {
133 SetSessionState(session_manager::SessionState::LOCKED);
134 }
135
136 void TestSessionControllerClient::SwitchActiveUser(
137 const AccountId& account_id) {
138 std::vector<uint32_t> session_order;
139 session_order.reserve(controller_->GetUserSessions().size());
140
141 for (auto& user_session : controller_->GetUserSessions()) {
James Cook 2017/03/17 17:14:35 const?
xiyuan 2017/03/17 22:52:02 Done.
142 if (user_session->account_id == account_id) {
143 session_order.insert(session_order.begin(), user_session->session_id);
144 } else {
145 session_order.push_back(user_session->session_id);
146 }
147 }
148
149 controller_->SetUserSessionOrder(session_order);
150 }
151
152 void TestSessionControllerClient::CycleActiveUser(
153 CycleUserDirection direction) {
154 DCHECK_GT(controller_->NumberOfLoggedInUsers(), 0);
155
156 const std::vector<mojom::UserSessionPtr>& sessions =
157 controller_->GetUserSessions();
158 const size_t session_count = sessions.size();
159
160 // The following code depends on that |fake_session_id_| is used to generate
161 // session ids.
162 uint32_t session_id = sessions[0]->session_id;
163 if (direction == CycleUserDirection::NEXT) {
164 ++session_id;
165 } else if (direction == CycleUserDirection::PREVIOUS) {
166 DCHECK_GT(session_id, 0u);
167 --session_id;
168 } else {
169 LOG(ERROR) << "Invalid cycle user direction="
170 << static_cast<int>(direction);
171 return;
172 }
173
174 // Valid range of an session id is [1, session_count].
175 if (session_id < 1u)
176 session_id = session_count;
177 if (session_id > session_count)
178 session_id = 1u;
179
180 // Maps session id to AccountId and call SwitchActiveUser.
181 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 :)
182 [session_id](const mojom::UserSessionPtr& session) {
183 return session && session->session_id == session_id;
184 });
185 if (it == sessions.end()) {
186 NOTREACHED();
187 return;
188 }
189
190 SwitchActiveUser((*it)->account_id);
191 }
192
193 } // namespace test
194 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698