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

Side by Side Diff: ash/system/user/tray_user_unittest.cc

Issue 2125373002: mash: Migrate ash/system/user to ash/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and rebase. Created 4 years, 5 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 2013 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 <vector>
6
7 #include "ash/common/shell_delegate.h"
8 #include "ash/common/system/tray/tray_constants.h"
9 #include "ash/common/system/user/tray_user_separator.h"
10 #include "ash/common/wm_shell.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/shell.h"
13 #include "ash/system/tray/system_tray.h"
14 #include "ash/system/user/tray_user.h"
15 #include "ash/system/user/user_view.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/ash_test_helper.h"
18 #include "ash/test/test_session_state_delegate.h"
19 #include "ash/test/test_shell_delegate.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "components/signin/core/account_id/account_id.h"
22 #include "components/user_manager/user_info.h"
23 #include "ui/accessibility/ax_view_state.h"
24 #include "ui/events/test/event_generator.h"
25 #include "ui/gfx/animation/animation_container_element.h"
26 #include "ui/views/view.h"
27 #include "ui/views/widget/widget.h"
28
29 namespace ash {
30
31 class TrayUserTest : public test::AshTestBase {
32 public:
33 TrayUserTest() = default;
34
35 // testing::Test:
36 void SetUp() override;
37
38 // This has to be called prior to first use with the proper configuration.
39 void InitializeParameters(int users_logged_in, bool multiprofile);
40
41 // Show the system tray menu using the provided event generator.
42 void ShowTrayMenu(ui::test::EventGenerator* generator);
43
44 // Move the mouse over the user item.
45 void MoveOverUserItem(ui::test::EventGenerator* generator, int index);
46
47 // Click on the user item. Note that the tray menu needs to be shown.
48 void ClickUserItem(ui::test::EventGenerator* generator, int index);
49
50 // Accessors to various system components.
51 SystemTray* tray() { return tray_; }
52 test::TestSessionStateDelegate* delegate() { return delegate_; }
53 TrayUser* tray_user(int index) { return tray_user_[index]; }
54 TrayUserSeparator* tray_user_separator() { return tray_user_separator_; }
55
56 private:
57 SystemTray* tray_ = nullptr;
58 test::TestSessionStateDelegate* delegate_ = nullptr;
59
60 // Note that the ownership of these items is on the shelf.
61 std::vector<TrayUser*> tray_user_;
62
63 // The separator between the tray users and the rest of the menu.
64 // Note: The item will get owned by the shelf.
65 TrayUserSeparator* tray_user_separator_ = nullptr;
66
67 DISALLOW_COPY_AND_ASSIGN(TrayUserTest);
68 };
69
70 void TrayUserTest::SetUp() {
71 test::AshTestBase::SetUp();
72 tray_ = Shell::GetPrimaryRootWindowController()->GetSystemTray();
73 delegate_ = test::AshTestHelper::GetTestSessionStateDelegate();
74 }
75
76 void TrayUserTest::InitializeParameters(int users_logged_in,
77 bool multiprofile) {
78 // Set our default assumptions. Note that it is sufficient to set these
79 // after everything was created.
80 delegate_->set_logged_in_users(users_logged_in);
81 test::TestShellDelegate* shell_delegate =
82 static_cast<test::TestShellDelegate*>(WmShell::Get()->delegate());
83 shell_delegate->set_multi_profiles_enabled(multiprofile);
84
85 // Instead of using the existing tray panels we create new ones which makes
86 // the access easier.
87 for (int i = 0; i < delegate_->GetMaximumNumberOfLoggedInUsers(); i++) {
88 tray_user_.push_back(new TrayUser(tray_, i));
89 tray_->AddTrayItem(tray_user_[i]);
90 }
91 // We then add also the separator.
92 tray_user_separator_ = new TrayUserSeparator(tray_);
93 tray_->AddTrayItem(tray_user_separator_);
94 }
95
96 void TrayUserTest::ShowTrayMenu(ui::test::EventGenerator* generator) {
97 gfx::Point center = tray()->GetBoundsInScreen().CenterPoint();
98
99 generator->MoveMouseTo(center.x(), center.y());
100 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
101 generator->ClickLeftButton();
102 }
103
104 void TrayUserTest::MoveOverUserItem(ui::test::EventGenerator* generator,
105 int index) {
106 gfx::Point center =
107 tray_user(index)->GetUserPanelBoundsInScreenForTest().CenterPoint();
108
109 generator->MoveMouseTo(center.x(), center.y());
110 }
111
112 void TrayUserTest::ClickUserItem(ui::test::EventGenerator* generator,
113 int index) {
114 MoveOverUserItem(generator, index);
115 generator->ClickLeftButton();
116 }
117
118 // Make sure that we show items for all users in the tray accordingly.
119 TEST_F(TrayUserTest, CheckTrayItemSize) {
120 InitializeParameters(1, false);
121 tray_user(0)->UpdateAfterLoginStatusChangeForTest(LoginStatus::GUEST);
122 gfx::Size size = tray_user(0)->GetLayoutSizeForTest();
123 EXPECT_EQ(kTrayItemSize, size.height());
124 tray_user(0)->UpdateAfterLoginStatusChangeForTest(LoginStatus::USER);
125 size = tray_user(0)->GetLayoutSizeForTest();
126 EXPECT_EQ(kTrayItemSize, size.height());
127 }
128
129 // Make sure that in single user mode the user panel cannot be activated and no
130 // separators are being created.
131 TEST_F(TrayUserTest, SingleUserModeDoesNotAllowAddingUser) {
132 InitializeParameters(1, false);
133
134 // Move the mouse over the status area and click to open the status menu.
135 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
136
137 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
138
139 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++)
140 EXPECT_EQ(TrayUser::HIDDEN, tray_user(i)->GetStateForTest());
141 EXPECT_FALSE(tray_user_separator()->separator_shown());
142
143 ShowTrayMenu(&generator);
144
145 EXPECT_TRUE(tray()->HasSystemBubble());
146 EXPECT_TRUE(tray()->IsAnyBubbleVisible());
147
148 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++)
149 EXPECT_EQ(i == 0 ? TrayUser::SHOWN : TrayUser::HIDDEN,
150 tray_user(i)->GetStateForTest());
151 EXPECT_FALSE(tray_user_separator()->separator_shown());
152 tray()->CloseSystemBubble();
153 }
154
155 TEST_F(TrayUserTest, AccessibleLabelContainsSingleUserInfo) {
156 InitializeParameters(1, false);
157 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
158 ShowTrayMenu(&generator);
159
160 views::View* view =
161 tray_user(0)->user_view_for_test()->user_card_view_for_test();
162 ui::AXViewState state;
163 view->GetAccessibleState(&state);
164 EXPECT_EQ(
165 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"),
166 state.name);
167 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, state.role);
168 }
169
170 TEST_F(TrayUserTest, AccessibleLabelContainsMultiUserInfo) {
171 InitializeParameters(1, true);
172 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
173 ShowTrayMenu(&generator);
174
175 views::View* view =
176 tray_user(0)->user_view_for_test()->user_card_view_for_test();
177 ui::AXViewState state;
178 view->GetAccessibleState(&state);
179 EXPECT_EQ(
180 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"),
181 state.name);
182 EXPECT_EQ(ui::AX_ROLE_BUTTON, state.role);
183 }
184
185 #if defined(OS_CHROMEOS)
186 // Make sure that in multi user mode the user panel can be activated and there
187 // will be one panel for each user plus one additional separator at the end.
188 // Note: the mouse watcher (for automatic closing upon leave) cannot be tested
189 // here since it does not work with the event system in unit tests.
190 TEST_F(TrayUserTest, MutiUserModeDoesNotAllowToAddUser) {
191 InitializeParameters(1, true);
192
193 // Move the mouse over the status area and click to open the status menu.
194 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
195 generator.set_async(false);
196
197 int max_users = delegate()->GetMaximumNumberOfLoggedInUsers();
198 // Checking now for each amount of users that the correct is done.
199 for (int j = 1; j < max_users; j++) {
200 // Set the number of logged in users.
201 delegate()->set_logged_in_users(j);
202
203 // Verify that nothing is shown.
204 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
205 for (int i = 0; i < max_users; i++)
206 EXPECT_FALSE(tray_user(i)->GetStateForTest());
207 EXPECT_FALSE(tray_user_separator()->separator_shown());
208 // After clicking on the tray the menu should get shown and for each logged
209 // in user we should get a visible item. In addition, the separator should
210 // show up when we reach more then one user.
211 ShowTrayMenu(&generator);
212
213 EXPECT_TRUE(tray()->HasSystemBubble());
214 EXPECT_TRUE(tray()->IsAnyBubbleVisible());
215 for (int i = 0; i < max_users; i++) {
216 EXPECT_EQ(i < j ? TrayUser::SHOWN : TrayUser::HIDDEN,
217 tray_user(i)->GetStateForTest());
218 }
219
220 // Check the visibility of the separator.
221 EXPECT_EQ(j > 1 ? true : false, tray_user_separator()->separator_shown());
222
223 // Move the mouse over the user item and it should hover.
224 MoveOverUserItem(&generator, 0);
225 EXPECT_EQ(TrayUser::HOVERED, tray_user(0)->GetStateForTest());
226 for (int i = 1; i < max_users; i++) {
227 EXPECT_EQ(i < j ? TrayUser::SHOWN : TrayUser::HIDDEN,
228 tray_user(i)->GetStateForTest());
229 }
230
231 // Check that clicking the button allows to add item if we have still room
232 // for one more user.
233 ClickUserItem(&generator, 0);
234 EXPECT_EQ(j == max_users ? TrayUser::ACTIVE_BUT_DISABLED : TrayUser::ACTIVE,
235 tray_user(0)->GetStateForTest());
236
237 // Click the button again to see that the menu goes away.
238 ClickUserItem(&generator, 0);
239 EXPECT_EQ(TrayUser::HOVERED, tray_user(0)->GetStateForTest());
240
241 // Close and check that everything is deleted.
242 tray()->CloseSystemBubble();
243 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
244 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++)
245 EXPECT_EQ(TrayUser::HIDDEN, tray_user(i)->GetStateForTest());
246 }
247 }
248
249 // Make sure that user changing gets properly executed.
250 TEST_F(TrayUserTest, MutiUserModeButtonClicks) {
251 // Have two users.
252 InitializeParameters(2, true);
253 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
254 ShowTrayMenu(&generator);
255
256 // Switch to a new user - which has a capitalized name.
257 ClickUserItem(&generator, 1);
258 const user_manager::UserInfo* active_user = delegate()->GetActiveUserInfo();
259 const user_manager::UserInfo* second_user = delegate()->GetUserInfo(1);
260 EXPECT_EQ(active_user->GetAccountId(), second_user->GetAccountId());
261 // Since the name is capitalized, the email should be different then the
262 // user_id.
263 EXPECT_NE(active_user->GetAccountId().GetUserEmail(),
264 second_user->GetEmail());
265 tray()->CloseSystemBubble();
266 }
267
268 #endif
269
270 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698