Home | History | Annotate | Download | only in desktop_capture
      1 /*
      2  *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/modules/desktop_capture/mouse_cursor_monitor.h"
     12 
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "webrtc/modules/desktop_capture/desktop_capture_options.h"
     15 #include "webrtc/modules/desktop_capture/desktop_frame.h"
     16 #include "webrtc/modules/desktop_capture/mouse_cursor.h"
     17 #include "webrtc/modules/desktop_capture/window_capturer.h"
     18 #include "webrtc/system_wrappers/interface/logging.h"
     19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
     20 
     21 namespace webrtc {
     22 
     23 class MouseCursorMonitorTest : public testing::Test,
     24                                public MouseCursorMonitor::Callback {
     25  public:
     26   MouseCursorMonitorTest()
     27       : position_received_(false) {
     28   }
     29 
     30   // MouseCursorMonitor::Callback interface
     31   virtual void OnMouseCursor(MouseCursor* cursor_image) OVERRIDE {
     32     cursor_image_.reset(cursor_image);
     33   }
     34 
     35   virtual void OnMouseCursorPosition(MouseCursorMonitor::CursorState state,
     36                                      const DesktopVector& position) OVERRIDE {
     37     state_ = state;
     38     position_ = position;
     39     position_received_ = true;
     40   }
     41 
     42  protected:
     43   scoped_ptr<MouseCursor> cursor_image_;
     44   MouseCursorMonitor::CursorState state_;
     45   DesktopVector position_;
     46   bool position_received_;
     47 };
     48 
     49 // TODO(sergeyu): On Mac we need to initialize NSApplication before running the
     50 // tests. Figure out how to do that without breaking other tests in
     51 // modules_unittests and enable these tests on Mac.
     52 // https://code.google.com/p/webrtc/issues/detail?id=2532
     53 //
     54 // Disabled on Windows due to flake, see:
     55 // https://code.google.com/p/webrtc/issues/detail?id=3408
     56 // Disabled on Linux due to flake, see:
     57 // https://code.google.com/p/webrtc/issues/detail?id=3245
     58 #if !defined(WEBRTC_MAC) && !defined(WEBRTC_WIN) && !defined(WEBRTC_LINUX)
     59 #define MAYBE(x) x
     60 #else
     61 #define MAYBE(x) DISABLED_##x
     62 #endif
     63 
     64 TEST_F(MouseCursorMonitorTest, MAYBE(FromScreen)) {
     65   scoped_ptr<MouseCursorMonitor> capturer(MouseCursorMonitor::CreateForScreen(
     66       DesktopCaptureOptions::CreateDefault(), webrtc::kFullDesktopScreenId));
     67   assert(capturer.get());
     68   capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION);
     69   capturer->Capture();
     70 
     71   EXPECT_TRUE(cursor_image_.get());
     72   EXPECT_GE(cursor_image_->hotspot().x(), 0);
     73   EXPECT_LE(cursor_image_->hotspot().x(),
     74             cursor_image_->image()->size().width());
     75   EXPECT_GE(cursor_image_->hotspot().y(), 0);
     76   EXPECT_LE(cursor_image_->hotspot().y(),
     77             cursor_image_->image()->size().height());
     78 
     79   EXPECT_TRUE(position_received_);
     80   EXPECT_EQ(MouseCursorMonitor::INSIDE, state_);
     81 }
     82 
     83 TEST_F(MouseCursorMonitorTest, MAYBE(FromWindow)) {
     84   DesktopCaptureOptions options = DesktopCaptureOptions::CreateDefault();
     85 
     86   // First get list of windows.
     87   scoped_ptr<WindowCapturer> window_capturer(WindowCapturer::Create(options));
     88 
     89   // If window capturing is not supported then skip this test.
     90   if (!window_capturer.get())
     91     return;
     92 
     93   WindowCapturer::WindowList windows;
     94   EXPECT_TRUE(window_capturer->GetWindowList(&windows));
     95 
     96   // Iterate over all windows and try capturing mouse cursor for each of them.
     97   for (size_t i = 0; i < windows.size(); ++i) {
     98     cursor_image_.reset();
     99     position_received_ = false;
    100 
    101     scoped_ptr<MouseCursorMonitor> capturer(
    102         MouseCursorMonitor::CreateForWindow(
    103             DesktopCaptureOptions::CreateDefault(), windows[i].id));
    104     assert(capturer.get());
    105 
    106     capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION);
    107     capturer->Capture();
    108 
    109     EXPECT_TRUE(cursor_image_.get());
    110     EXPECT_TRUE(position_received_);
    111   }
    112 }
    113 
    114 // Make sure that OnMouseCursorPosition() is not called in the SHAPE_ONLY mode.
    115 TEST_F(MouseCursorMonitorTest, MAYBE(ShapeOnly)) {
    116   scoped_ptr<MouseCursorMonitor> capturer(MouseCursorMonitor::CreateForScreen(
    117       DesktopCaptureOptions::CreateDefault(), webrtc::kFullDesktopScreenId));
    118   assert(capturer.get());
    119   capturer->Init(this, MouseCursorMonitor::SHAPE_ONLY);
    120   capturer->Capture();
    121 
    122   EXPECT_TRUE(cursor_image_.get());
    123   EXPECT_FALSE(position_received_);
    124 }
    125 
    126 }  // namespace webrtc
    127