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.
5 #include "ash/system/chromeos/screen_security/screen_tray_item.h"
8 #include "ash/system/chromeos/screen_security/screen_capture_tray_item.h"
9 #include "ash/system/chromeos/screen_security/screen_share_tray_item.h"
10 #include "ash/system/tray/tray_item_view.h"
11 #include "ash/test/ash_test_base.h"
12 #include "base/callback.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/events/event.h"
15 #include "ui/gfx/point.h"
16 #include "ui/message_center/message_center.h"
17 #include "ui/views/view.h"
21 // Test with unicode strings.
22 const char kTestScreenCaptureAppName
[] =
23 "\xE0\xB2\xA0\x5F\xE0\xB2\xA0 (Screen Capture Test)";
24 const char kTestScreenShareHelperName
[] =
25 "\xE5\xAE\x8B\xE8\x85\xBE (Screen Share Test)";
27 SystemTray
* GetSystemTray() {
28 return Shell::GetInstance()->GetPrimarySystemTray();
31 SystemTrayNotifier
* GetSystemTrayNotifier() {
32 return Shell::GetInstance()->system_tray_notifier();
35 void ClickViewCenter(views::View
* view
) {
36 gfx::Point click_location_in_local
=
37 gfx::Point(view
->width() / 2, view
->height() / 2);
38 view
->OnMousePressed(ui::MouseEvent(ui::ET_MOUSE_PRESSED
,
39 click_location_in_local
,
40 click_location_in_local
,
45 class ScreenTrayItemTest
: public ash::test::AshTestBase
{
48 : tray_item_(NULL
), stop_callback_hit_count_(0) {}
49 virtual ~ScreenTrayItemTest() {}
51 ScreenTrayItem
* tray_item() { return tray_item_
; }
52 void set_tray_item(ScreenTrayItem
* tray_item
) { tray_item_
= tray_item
; }
54 int stop_callback_hit_count() const { return stop_callback_hit_count_
; }
56 virtual void SetUp() OVERRIDE
{
57 test::AshTestBase::SetUp();
58 TrayItemView::DisableAnimationsForTest();
63 base::Bind(&ScreenTrayItemTest::StopCallback
, base::Unretained(this)));
71 stop_callback_hit_count_
++;
75 ScreenTrayItem
* tray_item_
;
76 int stop_callback_hit_count_
;
78 DISALLOW_COPY_AND_ASSIGN(ScreenTrayItemTest
);
81 class ScreenCaptureTest
: public ScreenTrayItemTest
{
83 ScreenCaptureTest() {}
84 virtual ~ScreenCaptureTest() {}
86 virtual void SetUp() OVERRIDE
{
87 ScreenTrayItemTest::SetUp();
88 // This tray item is owned by its parent system tray view and will
89 // be deleted automatically when its parent is destroyed in AshTestBase.
90 ScreenTrayItem
* tray_item
= new ScreenCaptureTrayItem(GetSystemTray());
91 GetSystemTray()->AddTrayItem(tray_item
);
92 set_tray_item(tray_item
);
95 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureTest
);
98 class ScreenShareTest
: public ScreenTrayItemTest
{
101 virtual ~ScreenShareTest() {}
103 virtual void SetUp() OVERRIDE
{
104 ScreenTrayItemTest::SetUp();
105 // This tray item is owned by its parent system tray view and will
106 // be deleted automatically when its parent is destroyed in AshTestBase.
107 ScreenTrayItem
* tray_item
= new ScreenShareTrayItem(GetSystemTray());
108 GetSystemTray()->AddTrayItem(tray_item
);
109 set_tray_item(tray_item
);
112 DISALLOW_COPY_AND_ASSIGN(ScreenShareTest
);
115 void TestStartAndStop(ScreenTrayItemTest
* test
) {
116 ScreenTrayItem
* tray_item
= test
->tray_item();
118 EXPECT_FALSE(tray_item
->is_started());
119 EXPECT_EQ(0, test
->stop_callback_hit_count());
121 test
->StartSession();
122 EXPECT_TRUE(tray_item
->is_started());
125 EXPECT_FALSE(tray_item
->is_started());
126 EXPECT_EQ(1, test
->stop_callback_hit_count());
129 TEST_F(ScreenCaptureTest
, StartAndStop
) { TestStartAndStop(this); }
130 TEST_F(ScreenShareTest
, StartAndStop
) { TestStartAndStop(this); }
132 void TestNotificationStartAndStop(ScreenTrayItemTest
* test
,
133 const base::Closure
& start_function
,
134 const base::Closure
& stop_function
) {
135 ScreenTrayItem
* tray_item
= test
->tray_item();
136 EXPECT_FALSE(tray_item
->is_started());
138 start_function
.Run();
139 EXPECT_TRUE(tray_item
->is_started());
141 // The stop callback shouldn't be called because we stopped
142 // through the notification system.
144 EXPECT_FALSE(tray_item
->is_started());
145 EXPECT_EQ(0, test
->stop_callback_hit_count());
148 TEST_F(ScreenCaptureTest
, NotificationStartAndStop
) {
149 base::Closure start_function
=
150 base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStart
,
151 base::Unretained(GetSystemTrayNotifier()),
152 base::Bind(&ScreenTrayItemTest::StopCallback
,
153 base::Unretained(this)),
154 base::UTF8ToUTF16(kTestScreenCaptureAppName
));
156 base::Closure stop_function
=
157 base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStop
,
158 base::Unretained(GetSystemTrayNotifier()));
160 TestNotificationStartAndStop(this, start_function
, stop_function
);
163 TEST_F(ScreenShareTest
, NotificationStartAndStop
) {
164 base::Closure start_func
=
165 base::Bind(&SystemTrayNotifier::NotifyScreenShareStart
,
166 base::Unretained(GetSystemTrayNotifier()),
167 base::Bind(&ScreenTrayItemTest::StopCallback
,
168 base::Unretained(this)),
169 base::UTF8ToUTF16(kTestScreenShareHelperName
));
171 base::Closure stop_func
=
172 base::Bind(&SystemTrayNotifier::NotifyScreenShareStop
,
173 base::Unretained(GetSystemTrayNotifier()));
175 TestNotificationStartAndStop(this, start_func
, stop_func
);
178 void TestNotificationView(ScreenTrayItemTest
* test
) {
179 ScreenTrayItem
* tray_item
= test
->tray_item();
181 test
->StartSession();
182 message_center::MessageCenter
* message_center
=
183 message_center::MessageCenter::Get();
184 EXPECT_TRUE(message_center
->FindVisibleNotificationById(
185 tray_item
->GetNotificationId()));
189 TEST_F(ScreenCaptureTest
, NotificationView
) { TestNotificationView(this); }
190 TEST_F(ScreenShareTest
, NotificationView
) { TestNotificationView(this); }
192 void TestSystemTrayInteraction(ScreenTrayItemTest
* test
) {
193 ScreenTrayItem
* tray_item
= test
->tray_item();
194 EXPECT_FALSE(tray_item
->tray_view()->visible());
196 const std::vector
<SystemTrayItem
*>& tray_items
=
197 GetSystemTray()->GetTrayItems();
198 EXPECT_NE(std::find(tray_items
.begin(), tray_items
.end(), tray_item
),
201 test
->StartSession();
202 EXPECT_TRUE(tray_item
->tray_view()->visible());
204 // The default view should be created in a new bubble.
205 GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW
);
206 EXPECT_TRUE(tray_item
->default_view());
207 GetSystemTray()->CloseSystemBubble();
208 EXPECT_FALSE(tray_item
->default_view());
211 EXPECT_FALSE(tray_item
->tray_view()->visible());
213 // The default view should not be visible because session is stopped.
214 GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW
);
215 EXPECT_FALSE(tray_item
->default_view()->visible());
218 TEST_F(ScreenCaptureTest
, SystemTrayInteraction
) {
219 TestSystemTrayInteraction(this);
222 TEST_F(ScreenShareTest
, SystemTrayInteraction
) {
223 TestSystemTrayInteraction(this);