Home | History | Annotate | Download | only in screen_security
      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/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/gfx/point.h"
     16 #include "ui/message_center/message_center.h"
     17 #include "ui/views/view.h"
     18 
     19 namespace ash {
     20 
     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)";
     26 
     27 SystemTray* GetSystemTray() {
     28   return Shell::GetInstance()->GetPrimarySystemTray();
     29 }
     30 
     31 SystemTrayNotifier* GetSystemTrayNotifier() {
     32   return Shell::GetInstance()->system_tray_notifier();
     33 }
     34 
     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,
     41                                       ui::EF_NONE,
     42                                       ui::EF_NONE));
     43 }
     44 
     45 class ScreenTrayItemTest : public ash::test::AshTestBase {
     46  public:
     47   ScreenTrayItemTest()
     48       : tray_item_(NULL), stop_callback_hit_count_(0) {}
     49   virtual ~ScreenTrayItemTest() {}
     50 
     51   ScreenTrayItem* tray_item() { return tray_item_; }
     52   void set_tray_item(ScreenTrayItem* tray_item) { tray_item_ = tray_item; }
     53 
     54   int stop_callback_hit_count() const { return stop_callback_hit_count_; }
     55 
     56   virtual void SetUp() OVERRIDE {
     57     test::AshTestBase::SetUp();
     58     TrayItemView::DisableAnimationsForTest();
     59   }
     60 
     61   void StartSession() {
     62     tray_item_->Start(
     63         base::Bind(&ScreenTrayItemTest::StopCallback, base::Unretained(this)));
     64   }
     65 
     66   void StopSession() {
     67     tray_item_->Stop();
     68   }
     69 
     70   void StopCallback() {
     71     stop_callback_hit_count_++;
     72   }
     73 
     74  private:
     75   ScreenTrayItem* tray_item_;
     76   int stop_callback_hit_count_;
     77 
     78   DISALLOW_COPY_AND_ASSIGN(ScreenTrayItemTest);
     79 };
     80 
     81 class ScreenCaptureTest : public ScreenTrayItemTest {
     82  public:
     83   ScreenCaptureTest() {}
     84   virtual ~ScreenCaptureTest() {}
     85 
     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);
     93   }
     94 
     95   DISALLOW_COPY_AND_ASSIGN(ScreenCaptureTest);
     96 };
     97 
     98 class ScreenShareTest : public ScreenTrayItemTest {
     99  public:
    100   ScreenShareTest() {}
    101   virtual ~ScreenShareTest() {}
    102 
    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);
    110   }
    111 
    112   DISALLOW_COPY_AND_ASSIGN(ScreenShareTest);
    113 };
    114 
    115 void TestStartAndStop(ScreenTrayItemTest* test) {
    116   ScreenTrayItem* tray_item = test->tray_item();
    117 
    118   EXPECT_FALSE(tray_item->is_started());
    119   EXPECT_EQ(0, test->stop_callback_hit_count());
    120 
    121   test->StartSession();
    122   EXPECT_TRUE(tray_item->is_started());
    123 
    124   test->StopSession();
    125   EXPECT_FALSE(tray_item->is_started());
    126   EXPECT_EQ(1, test->stop_callback_hit_count());
    127 }
    128 
    129 TEST_F(ScreenCaptureTest, StartAndStop) { TestStartAndStop(this); }
    130 TEST_F(ScreenShareTest, StartAndStop) { TestStartAndStop(this); }
    131 
    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());
    137 
    138   start_function.Run();
    139   EXPECT_TRUE(tray_item->is_started());
    140 
    141   // The stop callback shouldn't be called because we stopped
    142   // through the notification system.
    143   stop_function.Run();
    144   EXPECT_FALSE(tray_item->is_started());
    145   EXPECT_EQ(0, test->stop_callback_hit_count());
    146 }
    147 
    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));
    155 
    156   base::Closure stop_function =
    157       base::Bind(&SystemTrayNotifier::NotifyScreenCaptureStop,
    158           base::Unretained(GetSystemTrayNotifier()));
    159 
    160   TestNotificationStartAndStop(this, start_function, stop_function);
    161 }
    162 
    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));
    170 
    171   base::Closure stop_func =
    172       base::Bind(&SystemTrayNotifier::NotifyScreenShareStop,
    173           base::Unretained(GetSystemTrayNotifier()));
    174 
    175   TestNotificationStartAndStop(this, start_func, stop_func);
    176 }
    177 
    178 void TestNotificationView(ScreenTrayItemTest* test) {
    179   ScreenTrayItem* tray_item = test->tray_item();
    180 
    181   test->StartSession();
    182   message_center::MessageCenter* message_center =
    183       message_center::MessageCenter::Get();
    184   EXPECT_TRUE(message_center->FindVisibleNotificationById(
    185       tray_item->GetNotificationId()));
    186   test->StopSession();
    187 }
    188 
    189 TEST_F(ScreenCaptureTest, NotificationView) { TestNotificationView(this); }
    190 TEST_F(ScreenShareTest, NotificationView) { TestNotificationView(this); }
    191 
    192 void TestSystemTrayInteraction(ScreenTrayItemTest* test) {
    193   ScreenTrayItem* tray_item = test->tray_item();
    194   EXPECT_FALSE(tray_item->tray_view()->visible());
    195 
    196   const std::vector<SystemTrayItem*>& tray_items =
    197       GetSystemTray()->GetTrayItems();
    198   EXPECT_NE(std::find(tray_items.begin(), tray_items.end(), tray_item),
    199             tray_items.end());
    200 
    201   test->StartSession();
    202   EXPECT_TRUE(tray_item->tray_view()->visible());
    203 
    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());
    209 
    210   test->StopSession();
    211   EXPECT_FALSE(tray_item->tray_view()->visible());
    212 
    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());
    216 }
    217 
    218 TEST_F(ScreenCaptureTest, SystemTrayInteraction) {
    219   TestSystemTrayInteraction(this);
    220 }
    221 
    222 TEST_F(ScreenShareTest, SystemTrayInteraction) {
    223   TestSystemTrayInteraction(this);
    224 }
    225 
    226 }  // namespace ash
    227