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.
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shell_delegate.h"
11 #include "ash/system/tray/system_tray.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "ash/system/user/tray_user.h"
14 #include "ash/system/user/tray_user_separator.h"
15 #include "ash/system/user/user_view.h"
16 #include "ash/test/ash_test_base.h"
17 #include "ash/test/test_session_state_delegate.h"
18 #include "ash/test/test_shell_delegate.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "components/user_manager/user_info.h"
21 #include "ui/accessibility/ax_view_state.h"
22 #include "ui/events/test/event_generator.h"
23 #include "ui/gfx/animation/animation_container_element.h"
24 #include "ui/views/view.h"
25 #include "ui/views/widget/widget.h"
29 class TrayUserTest
: public ash::test::AshTestBase
{
34 void SetUp() override
;
36 // This has to be called prior to first use with the proper configuration.
37 void InitializeParameters(int users_logged_in
, bool multiprofile
);
39 // Show the system tray menu using the provided event generator.
40 void ShowTrayMenu(ui::test::EventGenerator
* generator
);
42 // Move the mouse over the user item.
43 void MoveOverUserItem(ui::test::EventGenerator
* generator
, int index
);
45 // Click on the user item. Note that the tray menu needs to be shown.
46 void ClickUserItem(ui::test::EventGenerator
* generator
, int index
);
48 // Accessors to various system components.
49 ShelfLayoutManager
* shelf() { return shelf_
; }
50 SystemTray
* tray() { return tray_
; }
51 ash::test::TestSessionStateDelegate
* delegate() { return delegate_
; }
52 ash::TrayUser
* tray_user(int index
) { return tray_user_
[index
]; }
53 ash::TrayUserSeparator
* tray_user_separator() { return tray_user_separator_
; }
56 ShelfLayoutManager
* shelf_
;
58 ash::test::TestSessionStateDelegate
* delegate_
;
60 // Note that the ownership of these items is on the shelf.
61 std::vector
<ash::TrayUser
*> tray_user_
;
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_
;
67 DISALLOW_COPY_AND_ASSIGN(TrayUserTest
);
70 TrayUserTest::TrayUserTest()
74 tray_user_separator_(NULL
) {
77 void TrayUserTest::SetUp() {
78 ash::test::AshTestBase::SetUp();
79 shelf_
= Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager();
80 tray_
= Shell::GetPrimaryRootWindowController()->GetSystemTray();
81 delegate_
= static_cast<ash::test::TestSessionStateDelegate
*>(
82 ash::Shell::GetInstance()->session_state_delegate());
85 void TrayUserTest::InitializeParameters(int users_logged_in
,
88 shelf()->LayoutShelf();
89 shelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER
);
91 // Set our default assumptions. Note that it is sufficient to set these
92 // after everything was created.
93 delegate_
->set_logged_in_users(users_logged_in
);
94 ash::test::TestShellDelegate
* shell_delegate
=
95 static_cast<ash::test::TestShellDelegate
*>(
96 ash::Shell::GetInstance()->delegate());
97 shell_delegate
->set_multi_profiles_enabled(multiprofile
);
99 // Instead of using the existing tray panels we create new ones which makes
100 // the access easier.
101 for (int i
= 0; i
< delegate_
->GetMaximumNumberOfLoggedInUsers(); i
++) {
102 tray_user_
.push_back(new ash::TrayUser(tray_
, i
));
103 tray_
->AddTrayItem(tray_user_
[i
]);
105 // We then add also the separator.
106 tray_user_separator_
= new ash::TrayUserSeparator(tray_
);
107 tray_
->AddTrayItem(tray_user_separator_
);
110 void TrayUserTest::ShowTrayMenu(ui::test::EventGenerator
* generator
) {
111 gfx::Point center
= tray()->GetBoundsInScreen().CenterPoint();
113 generator
->MoveMouseTo(center
.x(), center
.y());
114 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
115 generator
->ClickLeftButton();
118 void TrayUserTest::MoveOverUserItem(ui::test::EventGenerator
* generator
,
121 tray_user(index
)->GetUserPanelBoundsInScreenForTest().CenterPoint();
123 generator
->MoveMouseTo(center
.x(), center
.y());
126 void TrayUserTest::ClickUserItem(ui::test::EventGenerator
* generator
,
128 MoveOverUserItem(generator
, index
);
129 generator
->ClickLeftButton();
132 // Make sure that we show items for all users in the tray accordingly.
133 TEST_F(TrayUserTest
, CheckTrayItemSize
) {
134 InitializeParameters(1, false);
135 tray_user(0)->UpdateAfterLoginStatusChangeForTest(user::LOGGED_IN_GUEST
);
136 gfx::Size size
= tray_user(0)->GetLayoutSizeForTest();
137 EXPECT_EQ(kTrayItemSize
, size
.height());
138 tray_user(0)->UpdateAfterLoginStatusChangeForTest(user::LOGGED_IN_USER
);
139 size
= tray_user(0)->GetLayoutSizeForTest();
140 EXPECT_EQ(kTrayItemSize
, size
.height());
143 // Make sure that in single user mode the user panel cannot be activated and no
144 // separators are being created.
145 TEST_F(TrayUserTest
, SingleUserModeDoesNotAllowAddingUser
) {
146 InitializeParameters(1, false);
148 // Move the mouse over the status area and click to open the status menu.
149 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
151 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
153 for (int i
= 0; i
< delegate()->GetMaximumNumberOfLoggedInUsers(); i
++)
154 EXPECT_EQ(ash::TrayUser::HIDDEN
, tray_user(i
)->GetStateForTest());
155 EXPECT_FALSE(tray_user_separator()->separator_shown());
157 ShowTrayMenu(&generator
);
159 EXPECT_TRUE(tray()->HasSystemBubble());
160 EXPECT_TRUE(tray()->IsAnyBubbleVisible());
162 for (int i
= 0; i
< delegate()->GetMaximumNumberOfLoggedInUsers(); i
++)
163 EXPECT_EQ(i
== 0 ? ash::TrayUser::SHOWN
: ash::TrayUser::HIDDEN
,
164 tray_user(i
)->GetStateForTest());
165 EXPECT_FALSE(tray_user_separator()->separator_shown());
166 tray()->CloseSystemBubble();
169 TEST_F(TrayUserTest
, AccessibleLabelContainsSingleUserInfo
) {
170 InitializeParameters(1, false);
171 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
172 ShowTrayMenu(&generator
);
175 tray_user(0)->user_view_for_test()->user_card_view_for_test();
176 ui::AXViewState state
;
177 view
->GetAccessibleState(&state
);
179 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"),
181 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT
, state
.role
);
184 TEST_F(TrayUserTest
, AccessibleLabelContainsMultiUserInfo
) {
185 InitializeParameters(1, true);
186 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
187 ShowTrayMenu(&generator
);
190 tray_user(0)->user_view_for_test()->user_card_view_for_test();
191 ui::AXViewState state
;
192 view
->GetAccessibleState(&state
);
194 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"),
196 EXPECT_EQ(ui::AX_ROLE_BUTTON
, state
.role
);
199 #if defined(OS_CHROMEOS)
200 // Make sure that in multi user mode the user panel can be activated and there
201 // will be one panel for each user plus one additional separator at the end.
202 // Note: the mouse watcher (for automatic closing upon leave) cannot be tested
203 // here since it does not work with the event system in unit tests.
204 TEST_F(TrayUserTest
, MutiUserModeDoesNotAllowToAddUser
) {
205 InitializeParameters(1, true);
207 // Move the mouse over the status area and click to open the status menu.
208 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
209 generator
.set_async(false);
211 int max_users
= delegate()->GetMaximumNumberOfLoggedInUsers();
212 // Checking now for each amount of users that the correct is done.
213 for (int j
= 1; j
< max_users
; j
++) {
214 // Set the number of logged in users.
215 delegate()->set_logged_in_users(j
);
217 // Verify that nothing is shown.
218 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
219 for (int i
= 0; i
< max_users
; i
++)
220 EXPECT_FALSE(tray_user(i
)->GetStateForTest());
221 EXPECT_FALSE(tray_user_separator()->separator_shown());
222 // After clicking on the tray the menu should get shown and for each logged
223 // in user we should get a visible item. In addition, the separator should
224 // show up when we reach more then one user.
225 ShowTrayMenu(&generator
);
227 EXPECT_TRUE(tray()->HasSystemBubble());
228 EXPECT_TRUE(tray()->IsAnyBubbleVisible());
229 for (int i
= 0; i
< max_users
; i
++) {
230 EXPECT_EQ(i
< j
? ash::TrayUser::SHOWN
: ash::TrayUser::HIDDEN
,
231 tray_user(i
)->GetStateForTest());
234 // Check the visibility of the separator.
235 EXPECT_EQ(j
> 1 ? true : false, tray_user_separator()->separator_shown());
237 // Move the mouse over the user item and it should hover.
238 MoveOverUserItem(&generator
, 0);
239 EXPECT_EQ(ash::TrayUser::HOVERED
, tray_user(0)->GetStateForTest());
240 for (int i
= 1; i
< max_users
; i
++) {
241 EXPECT_EQ(i
< j
? ash::TrayUser::SHOWN
: ash::TrayUser::HIDDEN
,
242 tray_user(i
)->GetStateForTest());
245 // Check that clicking the button allows to add item if we have still room
246 // for one more user.
247 ClickUserItem(&generator
, 0);
248 EXPECT_EQ(j
== max_users
? ash::TrayUser::ACTIVE_BUT_DISABLED
249 : ash::TrayUser::ACTIVE
,
250 tray_user(0)->GetStateForTest());
252 // Click the button again to see that the menu goes away.
253 ClickUserItem(&generator
, 0);
254 EXPECT_EQ(ash::TrayUser::HOVERED
, tray_user(0)->GetStateForTest());
256 // Close and check that everything is deleted.
257 tray()->CloseSystemBubble();
258 EXPECT_FALSE(tray()->IsAnyBubbleVisible());
259 for (int i
= 0; i
< delegate()->GetMaximumNumberOfLoggedInUsers(); i
++)
260 EXPECT_EQ(ash::TrayUser::HIDDEN
, tray_user(i
)->GetStateForTest());
264 // Make sure that user changing gets properly executed.
265 TEST_F(TrayUserTest
, MutiUserModeButtonClicks
) {
267 InitializeParameters(2, true);
268 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
269 ShowTrayMenu(&generator
);
271 // Switch to a new user - which has a capitalized name.
272 ClickUserItem(&generator
, 1);
273 const user_manager::UserInfo
* active_user
= delegate()->GetActiveUserInfo();
274 const user_manager::UserInfo
* second_user
= delegate()->GetUserInfo(1);
275 EXPECT_EQ(active_user
->GetUserID(), second_user
->GetUserID());
276 // Since the name is capitalized, the email should be different then the
278 EXPECT_NE(active_user
->GetUserID(), second_user
->GetEmail());
279 tray()->CloseSystemBubble();