1 // Copyright 2014 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.
6 #include "ash/system/chromeos/multi_user/user_switch_util.h"
7 #include "ash/system/chromeos/screen_security/screen_tray_item.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/test/ash_test_base.h"
13 class TrySwitchingUserTest
: public ash::test::AshTestBase
{
15 // The action type to perform / check for upon user switching.
17 NO_DIALOG
, // No dialog should be shown.
18 ACCEPT_DIALOG
, // A dialog should be shown and we should accept it.
19 DECLINE_DIALOG
, // A dialog should be shown and we do not accept it.
21 TrySwitchingUserTest()
22 : capture_item_(NULL
),
24 stop_capture_callback_hit_count_(0),
25 stop_share_callback_hit_count_(0),
26 switch_callback_hit_count_(0) {}
27 ~TrySwitchingUserTest() override
{}
29 void SetUp() override
{
30 test::AshTestBase::SetUp();
31 TrayItemView::DisableAnimationsForTest();
32 SystemTray
* system_tray
= Shell::GetInstance()->GetPrimarySystemTray();
33 share_item_
= system_tray
->GetScreenShareItem();
34 capture_item_
= system_tray
->GetScreenCaptureItem();
35 EXPECT_TRUE(share_item_
);
36 EXPECT_TRUE(capture_item_
);
39 // Accessing the capture session functionality.
40 // Simulates a screen capture session start.
41 void StartCaptureSession() {
43 base::Bind(&TrySwitchingUserTest::StopCaptureCallback
,
44 base::Unretained(this)));
47 // The callback which gets called when the screen capture gets stopped.
48 void StopCaptureSession() {
49 capture_item_
->Stop();
52 // Simulates a screen capture session stop.
53 void StopCaptureCallback() {
54 stop_capture_callback_hit_count_
++;
57 // Accessing the share session functionality.
58 // Simulate a Screen share session start.
59 void StartShareSession() {
61 base::Bind(&TrySwitchingUserTest::StopShareCallback
,
62 base::Unretained(this)));
65 // Simulates a screen share session stop.
66 void StopShareSession() {
70 // The callback which gets called when the screen share gets stopped.
71 void StopShareCallback() {
72 stop_share_callback_hit_count_
++;
75 // Issuing a switch user call which might or might not create a dialog.
76 // The passed |action| type parameter defines the outcome (which will be
77 // checked) and the action the user will choose.
78 void SwitchUser(ActionType action
) {
79 TrySwitchingActiveUser(
80 base::Bind(&TrySwitchingUserTest::SwitchCallback
,
81 base::Unretained(this)));
84 EXPECT_TRUE(!TestAndTerminateDesktopCastingWarningForTest(true));
87 EXPECT_TRUE(TestAndTerminateDesktopCastingWarningForTest(true));
90 EXPECT_TRUE(TestAndTerminateDesktopCastingWarningForTest(false));
95 // Called when the user will get actually switched.
96 void SwitchCallback() {
97 switch_callback_hit_count_
++;
100 // Various counter accessors.
101 int stop_capture_callback_hit_count() const {
102 return stop_capture_callback_hit_count_
;
104 int stop_share_callback_hit_count() const {
105 return stop_share_callback_hit_count_
;
107 int switch_callback_hit_count() const { return switch_callback_hit_count_
; }
110 // The two items from the SystemTray for the screen capture / share
112 ScreenTrayItem
* capture_item_
;
113 ScreenTrayItem
* share_item_
;
115 // Various counters to query for.
116 int stop_capture_callback_hit_count_
;
117 int stop_share_callback_hit_count_
;
118 int switch_callback_hit_count_
;
120 DISALLOW_COPY_AND_ASSIGN(TrySwitchingUserTest
);
123 // Test that when there is no screen operation going on the user switch will be
124 // performed as planned.
125 TEST_F(TrySwitchingUserTest
, NoLock
) {
126 EXPECT_EQ(0, switch_callback_hit_count());
127 SwitchUser(TrySwitchingUserTest::NO_DIALOG
);
128 EXPECT_EQ(1, switch_callback_hit_count());
131 // Test that with a screen capture operation going on, the user will need to
132 // confirm. Declining will neither change the running state or switch users.
133 TEST_F(TrySwitchingUserTest
, CaptureActiveDeclined
) {
134 EXPECT_EQ(0, switch_callback_hit_count());
135 StartCaptureSession();
136 SwitchUser(TrySwitchingUserTest::DECLINE_DIALOG
);
137 EXPECT_EQ(0, switch_callback_hit_count());
138 EXPECT_EQ(0, stop_capture_callback_hit_count());
139 EXPECT_EQ(0, stop_share_callback_hit_count());
140 StopCaptureSession();
141 EXPECT_EQ(0, switch_callback_hit_count());
142 EXPECT_EQ(1, stop_capture_callback_hit_count());
143 EXPECT_EQ(0, stop_share_callback_hit_count());
146 // Test that with a screen share operation going on, the user will need to
147 // confirm. Declining will neither change the running state or switch users.
148 TEST_F(TrySwitchingUserTest
, ShareActiveDeclined
) {
149 EXPECT_EQ(0, switch_callback_hit_count());
151 SwitchUser(TrySwitchingUserTest::DECLINE_DIALOG
);
152 EXPECT_EQ(0, switch_callback_hit_count());
153 EXPECT_EQ(0, stop_capture_callback_hit_count());
154 EXPECT_EQ(0, stop_share_callback_hit_count());
156 EXPECT_EQ(0, switch_callback_hit_count());
157 EXPECT_EQ(0, stop_capture_callback_hit_count());
158 EXPECT_EQ(1, stop_share_callback_hit_count());
161 // Test that with both operations going on, the user will need to confirm.
162 // Declining will neither change the running state or switch users.
163 TEST_F(TrySwitchingUserTest
, BothActiveDeclined
) {
164 EXPECT_EQ(0, switch_callback_hit_count());
166 StartCaptureSession();
167 SwitchUser(TrySwitchingUserTest::DECLINE_DIALOG
);
168 EXPECT_EQ(0, switch_callback_hit_count());
169 EXPECT_EQ(0, stop_capture_callback_hit_count());
170 EXPECT_EQ(0, stop_share_callback_hit_count());
172 StopCaptureSession();
173 EXPECT_EQ(0, switch_callback_hit_count());
174 EXPECT_EQ(1, stop_capture_callback_hit_count());
175 EXPECT_EQ(1, stop_share_callback_hit_count());
178 // Test that with a screen capture operation going on, the user will need to
179 // confirm. Accepting will change to stopped state and switch users.
180 TEST_F(TrySwitchingUserTest
, CaptureActiveAccepted
) {
181 EXPECT_EQ(0, switch_callback_hit_count());
182 StartCaptureSession();
183 SwitchUser(TrySwitchingUserTest::ACCEPT_DIALOG
);
184 EXPECT_EQ(1, switch_callback_hit_count());
185 EXPECT_EQ(1, stop_capture_callback_hit_count());
186 EXPECT_EQ(0, stop_share_callback_hit_count());
187 // Another stop should have no effect.
188 StopCaptureSession();
189 EXPECT_EQ(1, switch_callback_hit_count());
190 EXPECT_EQ(1, stop_capture_callback_hit_count());
191 EXPECT_EQ(0, stop_share_callback_hit_count());
194 // Test that with a screen share operation going on, the user will need to
195 // confirm. Accepting will change to stopped state and switch users.
196 TEST_F(TrySwitchingUserTest
, ShareActiveAccepted
) {
197 EXPECT_EQ(0, switch_callback_hit_count());
199 SwitchUser(TrySwitchingUserTest::ACCEPT_DIALOG
);
200 EXPECT_EQ(1, switch_callback_hit_count());
201 EXPECT_EQ(0, stop_capture_callback_hit_count());
202 EXPECT_EQ(1, stop_share_callback_hit_count());
203 // Another stop should have no effect.
205 EXPECT_EQ(1, switch_callback_hit_count());
206 EXPECT_EQ(0, stop_capture_callback_hit_count());
207 EXPECT_EQ(1, stop_share_callback_hit_count());
210 // Test that with both operations going on, the user will need to confirm.
211 // Accepting will change to stopped state and switch users.
212 TEST_F(TrySwitchingUserTest
, BothActiveAccepted
) {
213 EXPECT_EQ(0, switch_callback_hit_count());
215 StartCaptureSession();
216 SwitchUser(TrySwitchingUserTest::ACCEPT_DIALOG
);
217 EXPECT_EQ(1, switch_callback_hit_count());
218 EXPECT_EQ(1, stop_capture_callback_hit_count());
219 EXPECT_EQ(1, stop_share_callback_hit_count());
220 // Another stop should have no effect.
222 StopCaptureSession();
223 EXPECT_EQ(1, switch_callback_hit_count());
224 EXPECT_EQ(1, stop_capture_callback_hit_count());
225 EXPECT_EQ(1, stop_share_callback_hit_count());