| 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 "ash/system/chromeos/screen_security/screen_tray_item.h" | |
| 6 | |
| 7 #include "ash/common/system/tray/system_tray_notifier.h" | |
| 8 #include "ash/common/system/tray/tray_item_view.h" | |
| 9 #include "ash/common/wm_shell.h" | |
| 10 #include "ash/system/chromeos/screen_security/screen_capture_tray_item.h" | |
| 11 #include "ash/system/chromeos/screen_security/screen_share_tray_item.h" | |
| 12 #include "ash/test/ash_test_base.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "ui/events/event.h" | |
| 16 #include "ui/events/event_utils.h" | |
| 17 #include "ui/gfx/geometry/point.h" | |
| 18 #include "ui/message_center/message_center.h" | |
| 19 #include "ui/views/view.h" | |
| 20 | |
| 21 namespace ash { | |
| 22 | |
| 23 // Test with unicode strings. | |
| 24 const char kTestScreenCaptureAppName[] = | |
| 25 "\xE0\xB2\xA0\x5F\xE0\xB2\xA0 (Screen Capture Test)"; | |
| 26 const char kTestScreenShareHelperName[] = | |
| 27 "\xE5\xAE\x8B\xE8\x85\xBE (Screen Share Test)"; | |
| 28 | |
| 29 void ClickViewCenter(views::View* view) { | |
| 30 gfx::Point click_location_in_local = | |
| 31 gfx::Point(view->width() / 2, view->height() / 2); | |
| 32 view->OnMousePressed(ui::MouseEvent( | |
| 33 ui::ET_MOUSE_PRESSED, click_location_in_local, click_location_in_local, | |
| 34 ui::EventTimeForNow(), ui::EF_NONE, ui::EF_NONE)); | |
| 35 } | |
| 36 | |
| 37 class ScreenTrayItemTest : public ash::test::AshTestBase { | |
| 38 public: | |
| 39 ScreenTrayItemTest() : tray_item_(NULL), stop_callback_hit_count_(0) {} | |
| 40 ~ScreenTrayItemTest() override {} | |
| 41 | |
| 42 ScreenTrayItem* tray_item() { return tray_item_; } | |
| 43 void set_tray_item(ScreenTrayItem* tray_item) { tray_item_ = tray_item; } | |
| 44 | |
| 45 int stop_callback_hit_count() const { return stop_callback_hit_count_; } | |
| 46 | |
| 47 void SetUp() override { | |
| 48 test::AshTestBase::SetUp(); | |
| 49 TrayItemView::DisableAnimationsForTest(); | |
| 50 } | |
| 51 | |
| 52 void StartSession() { | |
| 53 tray_item_->Start( | |
| 54 base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this))); | |
| 55 } | |
| 56 | |
| 57 void StopSession() { tray_item_->Stop(); } | |
| 58 | |
| 59 void StopCallback() { stop_callback_hit_count_++; } | |
| 60 | |
| 61 private: | |
| 62 ScreenTrayItem* tray_item_; | |
| 63 int stop_callback_hit_count_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(ScreenTrayItemTest); | |
| 66 }; | |
| 67 | |
| 68 class ScreenCaptureTest : public ScreenTrayItemTest { | |
| 69 public: | |
| 70 ScreenCaptureTest() {} | |
| 71 ~ScreenCaptureTest() override {} | |
| 72 | |
| 73 void SetUp() override { | |
| 74 ScreenTrayItemTest::SetUp(); | |
| 75 // This tray item is owned by its parent system tray view and will | |
| 76 // be deleted automatically when its parent is destroyed in AshTestBase. | |
| 77 ScreenTrayItem* item = new ScreenCaptureTrayItem(GetPrimarySystemTray()); | |
| 78 GetPrimarySystemTray()->AddTrayItem(item); | |
| 79 set_tray_item(item); | |
| 80 } | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(ScreenCaptureTest); | |
| 83 }; | |
| 84 | |
| 85 class ScreenShareTest : public ScreenTrayItemTest { | |
| 86 public: | |
| 87 ScreenShareTest() {} | |
| 88 ~ScreenShareTest() override {} | |
| 89 | |
| 90 void SetUp() override { | |
| 91 ScreenTrayItemTest::SetUp(); | |
| 92 // This tray item is owned by its parent system tray view and will | |
| 93 // be deleted automatically when its parent is destroyed in AshTestBase. | |
| 94 ScreenTrayItem* item = new ScreenShareTrayItem(GetPrimarySystemTray()); | |
| 95 GetPrimarySystemTray()->AddTrayItem(item); | |
| 96 set_tray_item(item); | |
| 97 } | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(ScreenShareTest); | |
| 100 }; | |
| 101 | |
| 102 void TestStartAndStop(ScreenTrayItemTest* test) { | |
| 103 ScreenTrayItem* tray_item = test->tray_item(); | |
| 104 | |
| 105 EXPECT_FALSE(tray_item->is_started()); | |
| 106 EXPECT_EQ(0, test->stop_callback_hit_count()); | |
| 107 | |
| 108 test->StartSession(); | |
| 109 EXPECT_TRUE(tray_item->is_started()); | |
| 110 | |
| 111 test->StopSession(); | |
| 112 EXPECT_FALSE(tray_item->is_started()); | |
| 113 EXPECT_EQ(1, test->stop_callback_hit_count()); | |
| 114 } | |
| 115 | |
| 116 TEST_F(ScreenCaptureTest, StartAndStop) { | |
| 117 TestStartAndStop(this); | |
| 118 } | |
| 119 TEST_F(ScreenShareTest, StartAndStop) { | |
| 120 TestStartAndStop(this); | |
| 121 } | |
| 122 | |
| 123 void TestNotificationStartAndStop(ScreenTrayItemTest* test, | |
| 124 const base::Closure& start_function, | |
| 125 const base::Closure& stop_function) { | |
| 126 ScreenTrayItem* tray_item = test->tray_item(); | |
| 127 EXPECT_FALSE(tray_item->is_started()); | |
| 128 | |
| 129 start_function.Run(); | |
| 130 EXPECT_TRUE(tray_item->is_started()); | |
| 131 | |
| 132 // The stop callback shouldn't be called because we stopped | |
| 133 // through the notification system. | |
| 134 stop_function.Run(); | |
| 135 EXPECT_FALSE(tray_item->is_started()); | |
| 136 EXPECT_EQ(0, test->stop_callback_hit_count()); | |
| 137 } | |
| 138 | |
| 139 TEST_F(ScreenCaptureTest, NotificationStartAndStop) { | |
| 140 base::Closure start_function = base::Bind( | |
| 141 &SystemTrayNotifier::NotifyScreenCaptureStart, | |
| 142 base::Unretained(WmShell::Get()->system_tray_notifier()), | |
| 143 base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this)), | |
| 144 base::UTF8ToUTF16(kTestScreenCaptureAppName)); | |
| 145 | |
| 146 base::Closure stop_function = | |
| 147 base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStop, | |
| 148 base::Unretained(WmShell::Get()->system_tray_notifier())); | |
| 149 | |
| 150 TestNotificationStartAndStop(this, start_function, stop_function); | |
| 151 } | |
| 152 | |
| 153 TEST_F(ScreenShareTest, NotificationStartAndStop) { | |
| 154 base::Closure start_func = base::Bind( | |
| 155 &SystemTrayNotifier::NotifyScreenShareStart, | |
| 156 base::Unretained(WmShell::Get()->system_tray_notifier()), | |
| 157 base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this)), | |
| 158 base::UTF8ToUTF16(kTestScreenShareHelperName)); | |
| 159 | |
| 160 base::Closure stop_func = | |
| 161 base::Bind(&SystemTrayNotifier::NotifyScreenShareStop, | |
| 162 base::Unretained(WmShell::Get()->system_tray_notifier())); | |
| 163 | |
| 164 TestNotificationStartAndStop(this, start_func, stop_func); | |
| 165 } | |
| 166 | |
| 167 void TestNotificationView(ScreenTrayItemTest* test) { | |
| 168 ScreenTrayItem* tray_item = test->tray_item(); | |
| 169 | |
| 170 test->StartSession(); | |
| 171 message_center::MessageCenter* message_center = | |
| 172 message_center::MessageCenter::Get(); | |
| 173 EXPECT_TRUE(message_center->FindVisibleNotificationById( | |
| 174 tray_item->GetNotificationId())); | |
| 175 test->StopSession(); | |
| 176 } | |
| 177 | |
| 178 TEST_F(ScreenCaptureTest, NotificationView) { | |
| 179 TestNotificationView(this); | |
| 180 } | |
| 181 TEST_F(ScreenShareTest, NotificationView) { | |
| 182 TestNotificationView(this); | |
| 183 } | |
| 184 | |
| 185 void TestSystemTrayInteraction(ScreenTrayItemTest* test) { | |
| 186 ScreenTrayItem* tray_item = test->tray_item(); | |
| 187 EXPECT_FALSE(tray_item->tray_view()->visible()); | |
| 188 | |
| 189 const std::vector<SystemTrayItem*>& tray_items = | |
| 190 test::AshTestBase::GetPrimarySystemTray()->GetTrayItems(); | |
| 191 EXPECT_NE(std::find(tray_items.begin(), tray_items.end(), tray_item), | |
| 192 tray_items.end()); | |
| 193 | |
| 194 test->StartSession(); | |
| 195 EXPECT_TRUE(tray_item->tray_view()->visible()); | |
| 196 | |
| 197 // The default view should be created in a new bubble. | |
| 198 test::AshTestBase::GetPrimarySystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW); | |
| 199 EXPECT_TRUE(tray_item->default_view()); | |
| 200 test::AshTestBase::GetPrimarySystemTray()->CloseSystemBubble(); | |
| 201 EXPECT_FALSE(tray_item->default_view()); | |
| 202 | |
| 203 test->StopSession(); | |
| 204 EXPECT_FALSE(tray_item->tray_view()->visible()); | |
| 205 | |
| 206 // The default view should not be visible because session is stopped. | |
| 207 test::AshTestBase::GetPrimarySystemTray()->ShowDefaultView(BUBBLE_CREATE_NEW); | |
| 208 EXPECT_FALSE(tray_item->default_view()->visible()); | |
| 209 } | |
| 210 | |
| 211 TEST_F(ScreenCaptureTest, SystemTrayInteraction) { | |
| 212 TestSystemTrayInteraction(this); | |
| 213 } | |
| 214 | |
| 215 TEST_F(ScreenShareTest, SystemTrayInteraction) { | |
| 216 TestSystemTrayInteraction(this); | |
| 217 } | |
| 218 | |
| 219 } // namespace ash | |
| OLD | NEW |