sergeyu@chromium.org | af54d4b | 2013-10-16 02:42:38 +0000 | [diff] [blame] | 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 "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 | |
sergeyu@chromium.org | 2873c4c | 2013-10-17 19:47:18 +0000 | [diff] [blame^] | 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 | #if !defined(WEBRTC_MAC) |
sergeyu@chromium.org | af54d4b | 2013-10-16 02:42:38 +0000 | [diff] [blame] | 54 | #define MAYBE(x) x |
| 55 | #else |
| 56 | #define MAYBE(x) DISABLED_##x |
| 57 | #endif |
| 58 | |
| 59 | TEST_F(MouseCursorMonitorTest, MAYBE(FromScreen)) { |
| 60 | scoped_ptr<MouseCursorMonitor> capturer(MouseCursorMonitor::CreateForScreen( |
| 61 | DesktopCaptureOptions::CreateDefault())); |
| 62 | assert(capturer.get()); |
| 63 | capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION); |
| 64 | capturer->Capture(); |
| 65 | |
| 66 | EXPECT_TRUE(cursor_image_.get()); |
| 67 | EXPECT_GE(cursor_image_->hotspot().x(), 0); |
| 68 | EXPECT_LE(cursor_image_->hotspot().x(), |
| 69 | cursor_image_->image().size().width()); |
| 70 | EXPECT_GE(cursor_image_->hotspot().y(), 0); |
| 71 | EXPECT_LE(cursor_image_->hotspot().y(), |
| 72 | cursor_image_->image().size().height()); |
| 73 | |
| 74 | EXPECT_TRUE(position_received_); |
| 75 | EXPECT_EQ(MouseCursorMonitor::INSIDE, state_); |
| 76 | } |
| 77 | |
| 78 | TEST_F(MouseCursorMonitorTest, MAYBE(FromWindow)) { |
| 79 | DesktopCaptureOptions options = DesktopCaptureOptions::CreateDefault(); |
| 80 | |
| 81 | // First get list of windows. |
| 82 | scoped_ptr<WindowCapturer> window_capturer(WindowCapturer::Create(options)); |
| 83 | |
| 84 | // If window capturing is not supported then skip this test. |
| 85 | if (!window_capturer.get()) |
| 86 | return; |
| 87 | |
| 88 | WindowCapturer::WindowList windows; |
| 89 | EXPECT_TRUE(window_capturer->GetWindowList(&windows)); |
| 90 | |
| 91 | // Iterate over all windows and try capturing mouse cursor for each of them. |
| 92 | for (size_t i = 0; i < windows.size(); ++i) { |
| 93 | cursor_image_.reset(); |
| 94 | position_received_ = false; |
| 95 | |
| 96 | scoped_ptr<MouseCursorMonitor> capturer( |
| 97 | MouseCursorMonitor::CreateForWindow( |
| 98 | DesktopCaptureOptions::CreateDefault(), windows[i].id)); |
| 99 | assert(capturer.get()); |
| 100 | |
| 101 | capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION); |
| 102 | capturer->Capture(); |
| 103 | |
| 104 | EXPECT_TRUE(cursor_image_.get()); |
| 105 | EXPECT_TRUE(position_received_); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Make sure that OnMouseCursorPosition() is not called in the SHAPE_ONLY mode. |
| 110 | TEST_F(MouseCursorMonitorTest, MAYBE(ShapeOnly)) { |
| 111 | scoped_ptr<MouseCursorMonitor> capturer(MouseCursorMonitor::CreateForScreen( |
| 112 | DesktopCaptureOptions::CreateDefault())); |
| 113 | assert(capturer.get()); |
| 114 | capturer->Init(this, MouseCursorMonitor::SHAPE_ONLY); |
| 115 | capturer->Capture(); |
| 116 | |
| 117 | EXPECT_TRUE(cursor_image_.get()); |
| 118 | EXPECT_FALSE(position_received_); |
| 119 | } |
| 120 | |
| 121 | } // namespace webrtc |