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.
4
5#include "ash/shell.h"
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"
10
11namespace ash {
12
13class TrySwitchingUserTest : public ash::test::AshTestBase {
14 public:
15  // The action type to perform / check for upon user switching.
16  enum ActionType {
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.
20  };
21  TrySwitchingUserTest()
22      : capture_item_(NULL),
23        share_item_(NULL),
24        stop_capture_callback_hit_count_(0),
25        stop_share_callback_hit_count_(0),
26        switch_callback_hit_count_(0) {}
27  virtual ~TrySwitchingUserTest() {}
28
29  virtual 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_);
37  }
38
39  // Accessing the capture session functionality.
40  // Simulates a screen capture session start.
41  void StartCaptureSession() {
42    capture_item_->Start(
43        base::Bind(&TrySwitchingUserTest::StopCaptureCallback,
44                   base::Unretained(this)));
45  }
46
47  // The callback which gets called when the screen capture gets stopped.
48  void StopCaptureSession() {
49    capture_item_->Stop();
50  }
51
52  // Simulates a screen capture session stop.
53  void StopCaptureCallback() {
54    stop_capture_callback_hit_count_++;
55  }
56
57  // Accessing the share session functionality.
58  // Simulate a Screen share session start.
59  void StartShareSession() {
60    share_item_->Start(
61        base::Bind(&TrySwitchingUserTest::StopShareCallback,
62                   base::Unretained(this)));
63  }
64
65  // Simulates a screen share session stop.
66  void StopShareSession() {
67    share_item_->Stop();
68  }
69
70  // The callback which gets called when the screen share gets stopped.
71  void StopShareCallback() {
72    stop_share_callback_hit_count_++;
73  }
74
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)));
82    switch(action) {
83      case NO_DIALOG:
84        EXPECT_TRUE(!TestAndTerminateDesktopCastingWarningForTest(true));
85        return;
86      case ACCEPT_DIALOG:
87        EXPECT_TRUE(TestAndTerminateDesktopCastingWarningForTest(true));
88        return;
89      case DECLINE_DIALOG:
90        EXPECT_TRUE(TestAndTerminateDesktopCastingWarningForTest(false));
91        return;
92    }
93  }
94
95  // Called when the user will get actually switched.
96  void SwitchCallback() {
97    switch_callback_hit_count_++;
98  }
99
100  // Various counter accessors.
101  int stop_capture_callback_hit_count() const {
102    return stop_capture_callback_hit_count_;
103  }
104  int stop_share_callback_hit_count() const {
105    return stop_share_callback_hit_count_;
106  }
107  int switch_callback_hit_count() const { return switch_callback_hit_count_; }
108
109 private:
110  // The two items from the SystemTray for the screen capture / share
111  // functionality.
112  ScreenTrayItem* capture_item_;
113  ScreenTrayItem* share_item_;
114
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_;
119
120  DISALLOW_COPY_AND_ASSIGN(TrySwitchingUserTest);
121};
122
123// Test that when there is no screen operation going on the user switch will be
124// performed as planned.
125TEST_F(TrySwitchingUserTest, NoLock) {
126  EXPECT_EQ(0, switch_callback_hit_count());
127  SwitchUser(TrySwitchingUserTest::NO_DIALOG);
128  EXPECT_EQ(1, switch_callback_hit_count());
129}
130
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.
133TEST_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());
144}
145
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.
148TEST_F(TrySwitchingUserTest, ShareActiveDeclined) {
149  EXPECT_EQ(0, switch_callback_hit_count());
150  StartShareSession();
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());
155  StopShareSession();
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());
159}
160
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.
163TEST_F(TrySwitchingUserTest, BothActiveDeclined) {
164  EXPECT_EQ(0, switch_callback_hit_count());
165  StartShareSession();
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());
171  StopShareSession();
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());
176}
177
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.
180TEST_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());
192}
193
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.
196TEST_F(TrySwitchingUserTest, ShareActiveAccepted) {
197  EXPECT_EQ(0, switch_callback_hit_count());
198  StartShareSession();
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.
204  StopShareSession();
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());
208}
209
210// Test that with both operations going on, the user will need to confirm.
211// Accepting will change to stopped state and switch users.
212TEST_F(TrySwitchingUserTest, BothActiveAccepted) {
213  EXPECT_EQ(0, switch_callback_hit_count());
214  StartShareSession();
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.
221  StopShareSession();
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());
226}
227
228}  // namespace ash
229