OLD | NEW |
| (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/system_tray.h" | |
9 #include "ash/common/system/tray/tray_constants.h" | |
10 #include "ash/common/system/user/tray_user.h" | |
11 #include "ash/common/system/user/user_view.h" | |
12 #include "ash/common/test/test_session_state_delegate.h" | |
13 #include "ash/common/wm_shell.h" | |
14 #include "ash/test/ash_test_base.h" | |
15 #include "ash/test/ash_test_helper.h" | |
16 #include "ash/test/test_shell_delegate.h" | |
17 #include "base/memory/ptr_util.h" | |
18 #include "base/strings/utf_string_conversions.h" | |
19 #include "components/signin/core/account_id/account_id.h" | |
20 #include "components/user_manager/user_info.h" | |
21 #include "ui/accessibility/ax_node_data.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" | |
26 | |
27 namespace ash { | |
28 | |
29 namespace { | |
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 | |
55 private: | |
56 SystemTray* tray_ = nullptr; | |
57 test::TestSessionStateDelegate* delegate_ = nullptr; | |
58 | |
59 // Note that the ownership of these items is on the shelf. | |
60 std::vector<TrayUser*> tray_user_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(TrayUserTest); | |
63 }; | |
64 | |
65 void TrayUserTest::SetUp() { | |
66 test::AshTestBase::SetUp(); | |
67 tray_ = GetPrimarySystemTray(); | |
68 delegate_ = test::AshTestHelper::GetTestSessionStateDelegate(); | |
69 } | |
70 | |
71 void TrayUserTest::InitializeParameters(int users_logged_in, | |
72 bool multiprofile) { | |
73 // Set our default assumptions. Note that it is sufficient to set these | |
74 // after everything was created. | |
75 delegate_->set_logged_in_users(users_logged_in); | |
76 test::TestShellDelegate* shell_delegate = | |
77 static_cast<test::TestShellDelegate*>(WmShell::Get()->delegate()); | |
78 shell_delegate->set_multi_profiles_enabled(multiprofile); | |
79 | |
80 // Instead of using the existing tray panels we create new ones which makes | |
81 // the access easier. | |
82 for (int i = 0; i < delegate_->GetMaximumNumberOfLoggedInUsers(); i++) { | |
83 tray_user_.push_back(new TrayUser(tray_, i)); | |
84 tray_->AddTrayItem(base::WrapUnique(tray_user_[i])); | |
85 } | |
86 } | |
87 | |
88 void TrayUserTest::ShowTrayMenu(ui::test::EventGenerator* generator) { | |
89 gfx::Point center = tray()->GetBoundsInScreen().CenterPoint(); | |
90 | |
91 generator->MoveMouseTo(center.x(), center.y()); | |
92 EXPECT_FALSE(tray()->IsSystemBubbleVisible()); | |
93 generator->ClickLeftButton(); | |
94 } | |
95 | |
96 void TrayUserTest::MoveOverUserItem(ui::test::EventGenerator* generator, | |
97 int index) { | |
98 gfx::Point center = | |
99 tray_user(index)->GetUserPanelBoundsInScreenForTest().CenterPoint(); | |
100 | |
101 generator->MoveMouseTo(center.x(), center.y()); | |
102 } | |
103 | |
104 void TrayUserTest::ClickUserItem(ui::test::EventGenerator* generator, | |
105 int index) { | |
106 MoveOverUserItem(generator, index); | |
107 generator->ClickLeftButton(); | |
108 } | |
109 | |
110 } // namespace | |
111 | |
112 // Make sure that we show items for all users in the tray accordingly. | |
113 TEST_F(TrayUserTest, CheckTrayItemSize) { | |
114 InitializeParameters(1, false); | |
115 tray_user(0)->UpdateAfterLoginStatusChangeForTest(LoginStatus::GUEST); | |
116 gfx::Size size = tray_user(0)->GetLayoutSizeForTest(); | |
117 EXPECT_EQ(kTrayItemSize, size.height()); | |
118 tray_user(0)->UpdateAfterLoginStatusChangeForTest(LoginStatus::USER); | |
119 size = tray_user(0)->GetLayoutSizeForTest(); | |
120 EXPECT_EQ(kTrayItemSize, size.height()); | |
121 } | |
122 | |
123 // Make sure that in single user mode the user panel cannot be activated. | |
124 TEST_F(TrayUserTest, SingleUserModeDoesNotAllowAddingUser) { | |
125 InitializeParameters(1, false); | |
126 | |
127 // Move the mouse over the status area and click to open the status menu. | |
128 ui::test::EventGenerator& generator = GetEventGenerator(); | |
129 | |
130 EXPECT_FALSE(tray()->IsSystemBubbleVisible()); | |
131 | |
132 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++) | |
133 EXPECT_EQ(TrayUser::HIDDEN, tray_user(i)->GetStateForTest()); | |
134 | |
135 ShowTrayMenu(&generator); | |
136 | |
137 EXPECT_TRUE(tray()->HasSystemBubble()); | |
138 EXPECT_TRUE(tray()->IsSystemBubbleVisible()); | |
139 | |
140 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++) | |
141 EXPECT_EQ(i == 0 ? TrayUser::SHOWN : TrayUser::HIDDEN, | |
142 tray_user(i)->GetStateForTest()); | |
143 tray()->CloseSystemBubble(); | |
144 } | |
145 | |
146 TEST_F(TrayUserTest, AccessibleLabelContainsSingleUserInfo) { | |
147 InitializeParameters(1, false); | |
148 ui::test::EventGenerator& generator = GetEventGenerator(); | |
149 ShowTrayMenu(&generator); | |
150 | |
151 views::View* view = | |
152 tray_user(0)->user_view_for_test()->user_card_view_for_test(); | |
153 ui::AXNodeData node_data; | |
154 view->GetAccessibleNodeData(&node_data); | |
155 EXPECT_EQ( | |
156 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"), | |
157 node_data.GetString16Attribute(ui::AX_ATTR_NAME)); | |
158 EXPECT_EQ(ui::AX_ROLE_STATIC_TEXT, node_data.role); | |
159 } | |
160 | |
161 TEST_F(TrayUserTest, AccessibleLabelContainsMultiUserInfo) { | |
162 InitializeParameters(1, true); | |
163 ui::test::EventGenerator& generator = GetEventGenerator(); | |
164 ShowTrayMenu(&generator); | |
165 | |
166 views::View* view = | |
167 tray_user(0)->user_view_for_test()->user_card_view_for_test(); | |
168 ui::AXNodeData node_data; | |
169 view->GetAccessibleNodeData(&node_data); | |
170 EXPECT_EQ( | |
171 base::UTF8ToUTF16("Über tray Über tray Über tray Über tray First@tray"), | |
172 node_data.GetString16Attribute(ui::AX_ATTR_NAME)); | |
173 EXPECT_EQ(ui::AX_ROLE_BUTTON, node_data.role); | |
174 } | |
175 | |
176 // Make sure that in multi user mode the user panel can be activated and there | |
177 // will be one panel for each user. | |
178 // Note: the mouse watcher (for automatic closing upon leave) cannot be tested | |
179 // here since it does not work with the event system in unit tests. | |
180 TEST_F(TrayUserTest, MultiUserModeDoesNotAllowToAddUser) { | |
181 InitializeParameters(1, true); | |
182 | |
183 // Move the mouse over the status area and click to open the status menu. | |
184 ui::test::EventGenerator& generator = GetEventGenerator(); | |
185 generator.set_async(false); | |
186 | |
187 int max_users = delegate()->GetMaximumNumberOfLoggedInUsers(); | |
188 // Checking now for each amount of users that the correct is done. | |
189 for (int j = 1; j < max_users; j++) { | |
190 // Set the number of logged in users. | |
191 delegate()->set_logged_in_users(j); | |
192 | |
193 // Verify that nothing is shown. | |
194 EXPECT_FALSE(tray()->IsSystemBubbleVisible()); | |
195 for (int i = 0; i < max_users; i++) | |
196 EXPECT_FALSE(tray_user(i)->GetStateForTest()); | |
197 // After clicking on the tray the menu should get shown and for each logged | |
198 // in user we should get a visible item. | |
199 ShowTrayMenu(&generator); | |
200 | |
201 EXPECT_TRUE(tray()->HasSystemBubble()); | |
202 EXPECT_TRUE(tray()->IsSystemBubbleVisible()); | |
203 for (int i = 0; i < max_users; i++) { | |
204 EXPECT_EQ(i < j ? TrayUser::SHOWN : TrayUser::HIDDEN, | |
205 tray_user(i)->GetStateForTest()); | |
206 } | |
207 | |
208 // Move the mouse over the user item and it should hover. | |
209 MoveOverUserItem(&generator, 0); | |
210 EXPECT_EQ(TrayUser::HOVERED, tray_user(0)->GetStateForTest()); | |
211 for (int i = 1; i < max_users; i++) { | |
212 EXPECT_EQ(i < j ? TrayUser::SHOWN : TrayUser::HIDDEN, | |
213 tray_user(i)->GetStateForTest()); | |
214 } | |
215 | |
216 // Check that clicking the button allows to add item if we have still room | |
217 // for one more user. | |
218 ClickUserItem(&generator, 0); | |
219 EXPECT_EQ(j == max_users ? TrayUser::ACTIVE_BUT_DISABLED : TrayUser::ACTIVE, | |
220 tray_user(0)->GetStateForTest()); | |
221 | |
222 // Click the button again to see that the menu goes away. | |
223 ClickUserItem(&generator, 0); | |
224 MoveOverUserItem(&generator, 0); | |
225 EXPECT_EQ(TrayUser::HOVERED, tray_user(0)->GetStateForTest()); | |
226 | |
227 // Close and check that everything is deleted. | |
228 tray()->CloseSystemBubble(); | |
229 EXPECT_FALSE(tray()->IsSystemBubbleVisible()); | |
230 for (int i = 0; i < delegate()->GetMaximumNumberOfLoggedInUsers(); i++) | |
231 EXPECT_EQ(TrayUser::HIDDEN, tray_user(i)->GetStateForTest()); | |
232 } | |
233 } | |
234 | |
235 // Make sure that user changing gets properly executed. | |
236 TEST_F(TrayUserTest, MultiUserModeButtonClicks) { | |
237 // Have two users. | |
238 InitializeParameters(2, true); | |
239 ui::test::EventGenerator& generator = GetEventGenerator(); | |
240 ShowTrayMenu(&generator); | |
241 | |
242 // Switch to a new user - which has a capitalized name. | |
243 ClickUserItem(&generator, 1); | |
244 const user_manager::UserInfo* active_user = delegate()->GetActiveUserInfo(); | |
245 const user_manager::UserInfo* second_user = delegate()->GetUserInfo(1); | |
246 EXPECT_EQ(active_user->GetAccountId(), second_user->GetAccountId()); | |
247 // Since the name is capitalized, the email should be different than the | |
248 // user_id. | |
249 EXPECT_NE(active_user->GetAccountId().GetUserEmail(), | |
250 second_user->GetDisplayEmail()); | |
251 tray()->CloseSystemBubble(); | |
252 } | |
253 | |
254 } // namespace ash | |
OLD | NEW |