Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / system / chromeos / screen_security / screen_tray_item_unittest.cc
blob63d63470fe803ca9676d9eaaaa52d3e6c0c86e19
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"
7 #include "ash/shell.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/events/event_utils.h"
16 #include "ui/gfx/geometry/point.h"
17 #include "ui/message_center/message_center.h"
18 #include "ui/views/view.h"
20 namespace ash {
22 // Test with unicode strings.
23 const char kTestScreenCaptureAppName[] =
24 "\xE0\xB2\xA0\x5F\xE0\xB2\xA0 (Screen Capture Test)";
25 const char kTestScreenShareHelperName[] =
26 "\xE5\xAE\x8B\xE8\x85\xBE (Screen Share Test)";
28 SystemTray* GetSystemTray() {
29 return Shell::GetInstance()->GetPrimarySystemTray();
32 SystemTrayNotifier* GetSystemTrayNotifier() {
33 return Shell::GetInstance()->system_tray_notifier();
36 void ClickViewCenter(views::View* view) {
37 gfx::Point click_location_in_local =
38 gfx::Point(view->width() / 2, view->height() / 2);
39 view->OnMousePressed(ui::MouseEvent(
40 ui::ET_MOUSE_PRESSED, click_location_in_local, click_location_in_local,
41 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE));
44 class ScreenTrayItemTest : public ash::test::AshTestBase {
45 public:
46 ScreenTrayItemTest()
47 : tray_item_(NULL), stop_callback_hit_count_(0) {}
48 ~ScreenTrayItemTest() override {}
50 ScreenTrayItem* tray_item() { return tray_item_; }
51 void set_tray_item(ScreenTrayItem* tray_item) { tray_item_ = tray_item; }
53 int stop_callback_hit_count() const { return stop_callback_hit_count_; }
55 void SetUp() override {
56 test::AshTestBase::SetUp();
57 TrayItemView::DisableAnimationsForTest();
60 void StartSession() {
61 tray_item_->Start(
62 base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this)));
65 void StopSession() {
66 tray_item_->Stop();
69 void StopCallback() {
70 stop_callback_hit_count_++;
73 private:
74 ScreenTrayItem* tray_item_;
75 int stop_callback_hit_count_;
77 DISALLOW_COPY_AND_ASSIGN(ScreenTrayItemTest);
80 class ScreenCaptureTest : public ScreenTrayItemTest {
81 public:
82 ScreenCaptureTest() {}
83 ~ScreenCaptureTest() override {}
85 void SetUp() override {
86 ScreenTrayItemTest::SetUp();
87 // This tray item is owned by its parent system tray view and will
88 // be deleted automatically when its parent is destroyed in AshTestBase.
89 ScreenTrayItem* tray_item = new ScreenCaptureTrayItem(GetSystemTray());
90 GetSystemTray()->AddTrayItem(tray_item);
91 set_tray_item(tray_item);
94 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureTest);
97 class ScreenShareTest : public ScreenTrayItemTest {
98 public:
99 ScreenShareTest() {}
100 ~ScreenShareTest() override {}
102 void SetUp() override {
103 ScreenTrayItemTest::SetUp();
104 // This tray item is owned by its parent system tray view and will
105 // be deleted automatically when its parent is destroyed in AshTestBase.
106 ScreenTrayItem* tray_item = new ScreenShareTrayItem(GetSystemTray());
107 GetSystemTray()->AddTrayItem(tray_item);
108 set_tray_item(tray_item);
111 DISALLOW_COPY_AND_ASSIGN(ScreenShareTest);
114 void TestStartAndStop(ScreenTrayItemTest* test) {
115 ScreenTrayItem* tray_item = test->tray_item();
117 EXPECT_FALSE(tray_item->is_started());
118 EXPECT_EQ(0, test->stop_callback_hit_count());
120 test->StartSession();
121 EXPECT_TRUE(tray_item->is_started());
123 test->StopSession();
124 EXPECT_FALSE(tray_item->is_started());
125 EXPECT_EQ(1, test->stop_callback_hit_count());
128 TEST_F(ScreenCaptureTest, StartAndStop) { TestStartAndStop(this); }
129 TEST_F(ScreenShareTest, StartAndStop) { TestStartAndStop(this); }
131 void TestNotificationStartAndStop(ScreenTrayItemTest* test,
132 const base::Closure& start_function,
133 const base::Closure& stop_function) {
134 ScreenTrayItem* tray_item = test->tray_item();
135 EXPECT_FALSE(tray_item->is_started());
137 start_function.Run();
138 EXPECT_TRUE(tray_item->is_started());
140 // The stop callback shouldn't be called because we stopped
141 // through the notification system.
142 stop_function.Run();
143 EXPECT_FALSE(tray_item->is_started());
144 EXPECT_EQ(0, test->stop_callback_hit_count());
147 TEST_F(ScreenCaptureTest, NotificationStartAndStop) {
148 base::Closure start_function =
149 base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStart,
150 base::Unretained(GetSystemTrayNotifier()),
151 base::Bind(&ScreenTrayItemTest::StopCallback,
152 base::Unretained(this)),
153 base::UTF8ToUTF16(kTestScreenCaptureAppName));
155 base::Closure stop_function =
156 base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStop,
157 base::Unretained(GetSystemTrayNotifier()));
159 TestNotificationStartAndStop(this, start_function, stop_function);
162 TEST_F(ScreenShareTest, NotificationStartAndStop) {
163 base::Closure start_func =
164 base::Bind(&SystemTrayNotifier::NotifyScreenShareStart,
165 base::Unretained(GetSystemTrayNotifier()),
166 base::Bind(&ScreenTrayItemTest::StopCallback,
167 base::Unretained(this)),
168 base::UTF8ToUTF16(kTestScreenShareHelperName));
170 base::Closure stop_func =
171 base::Bind(&SystemTrayNotifier::NotifyScreenShareStop,
172 base::Unretained(GetSystemTrayNotifier()));
174 TestNotificationStartAndStop(this, start_func, stop_func);
177 void TestNotificationView(ScreenTrayItemTest* test) {
178 ScreenTrayItem* tray_item = test->tray_item();
180 test->StartSession();
181 message_center::MessageCenter* message_center =
182 message_center::MessageCenter::Get();
183 EXPECT_TRUE(message_center->FindVisibleNotificationById(
184 tray_item->GetNotificationId()));
185 test->StopSession();
188 TEST_F(ScreenCaptureTest, NotificationView) { TestNotificationView(this); }
189 TEST_F(ScreenShareTest, NotificationView) { TestNotificationView(this); }
191 void TestSystemTrayInteraction(ScreenTrayItemTest* test) {
192 ScreenTrayItem* tray_item = test->tray_item();
193 EXPECT_FALSE(tray_item->tray_view()->visible());
195 const std::vector<SystemTrayItem*>& tray_items =
196 GetSystemTray()->GetTrayItems();
197 EXPECT_NE(std::find(tray_items.begin(), tray_items.end(), tray_item),
198 tray_items.end());
200 test->StartSession();
201 EXPECT_TRUE(tray_item->tray_view()->visible());
203 // The default view should be created in a new bubble.
204 GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW);
205 EXPECT_TRUE(tray_item->default_view());
206 GetSystemTray()->CloseSystemBubble();
207 EXPECT_FALSE(tray_item->default_view());
209 test->StopSession();
210 EXPECT_FALSE(tray_item->tray_view()->visible());
212 // The default view should not be visible because session is stopped.
213 GetSystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW);
214 EXPECT_FALSE(tray_item->default_view()->visible());
217 TEST_F(ScreenCaptureTest, SystemTrayInteraction) {
218 TestSystemTrayInteraction(this);
221 TEST_F(ScreenShareTest, SystemTrayInteraction) {
222 TestSystemTrayInteraction(this);
225 } // namespace ash