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 "modules/desktop_capture/mouse_cursor_monitor.h"
12
13 #include <assert.h>
14 #include <stddef.h>
15
16 #include <memory>
17
18 #include "modules/desktop_capture/desktop_capture_options.h"
19 #include "modules/desktop_capture/desktop_capturer.h"
20 #include "modules/desktop_capture/desktop_frame.h"
21 #include "modules/desktop_capture/mouse_cursor.h"
22 #include "rtc_base/checks.h"
23 #include "test/gtest.h"
24
25 namespace webrtc {
26
27 class MouseCursorMonitorTest : public ::testing::Test,
28 public MouseCursorMonitor::Callback {
29 public:
MouseCursorMonitorTest()30 MouseCursorMonitorTest() : position_received_(false) {}
31
32 // MouseCursorMonitor::Callback interface
OnMouseCursor(MouseCursor * cursor_image)33 void OnMouseCursor(MouseCursor* cursor_image) override {
34 cursor_image_.reset(cursor_image);
35 }
36
OnMouseCursorPosition(const DesktopVector & position)37 void OnMouseCursorPosition(const DesktopVector& position) override {
38 position_ = position;
39 position_received_ = true;
40 }
41
42 protected:
43 std::unique_ptr<MouseCursor> cursor_image_;
44 DesktopVector position_;
45 bool position_received_;
46 };
47
48 // TODO(sergeyu): On Mac we need to initialize NSApplication before running the
49 // tests. Figure out how to do that without breaking other tests in
50 // modules_unittests and enable these tests on Mac.
51 // https://code.google.com/p/webrtc/issues/detail?id=2532
52 //
53 // Disabled on Windows due to flake, see:
54 // https://code.google.com/p/webrtc/issues/detail?id=3408
55 // Disabled on Linux due to flake, see:
56 // https://code.google.com/p/webrtc/issues/detail?id=3245
57 #if !defined(WEBRTC_MAC) && !defined(WEBRTC_WIN) && !defined(WEBRTC_LINUX)
58 #define MAYBE(x) x
59 #else
60 #define MAYBE(x) DISABLED_##x
61 #endif
62
TEST_F(MouseCursorMonitorTest,MAYBE (FromScreen))63 TEST_F(MouseCursorMonitorTest, MAYBE(FromScreen)) {
64 std::unique_ptr<MouseCursorMonitor> capturer(
65 MouseCursorMonitor::CreateForScreen(
66 DesktopCaptureOptions::CreateDefault(),
67 webrtc::kFullDesktopScreenId));
68 assert(capturer.get());
69 capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION);
70 capturer->Capture();
71
72 EXPECT_TRUE(cursor_image_.get());
73 EXPECT_GE(cursor_image_->hotspot().x(), 0);
74 EXPECT_LE(cursor_image_->hotspot().x(),
75 cursor_image_->image()->size().width());
76 EXPECT_GE(cursor_image_->hotspot().y(), 0);
77 EXPECT_LE(cursor_image_->hotspot().y(),
78 cursor_image_->image()->size().height());
79
80 EXPECT_TRUE(position_received_);
81 }
82
TEST_F(MouseCursorMonitorTest,MAYBE (FromWindow))83 TEST_F(MouseCursorMonitorTest, MAYBE(FromWindow)) {
84 DesktopCaptureOptions options = DesktopCaptureOptions::CreateDefault();
85
86 // First get list of windows.
87 std::unique_ptr<DesktopCapturer> window_capturer(
88 DesktopCapturer::CreateWindowCapturer(options));
89
90 // If window capturing is not supported then skip this test.
91 if (!window_capturer.get())
92 return;
93
94 DesktopCapturer::SourceList sources;
95 EXPECT_TRUE(window_capturer->GetSourceList(&sources));
96
97 // Iterate over all windows and try capturing mouse cursor for each of them.
98 for (size_t i = 0; i < sources.size(); ++i) {
99 cursor_image_.reset();
100 position_received_ = false;
101
102 std::unique_ptr<MouseCursorMonitor> capturer(
103 MouseCursorMonitor::CreateForWindow(
104 DesktopCaptureOptions::CreateDefault(), sources[i].id));
105 assert(capturer.get());
106
107 capturer->Init(this, MouseCursorMonitor::SHAPE_AND_POSITION);
108 capturer->Capture();
109
110 EXPECT_TRUE(cursor_image_.get());
111 EXPECT_TRUE(position_received_);
112 }
113 }
114
115 // Make sure that OnMouseCursorPosition() is not called in the SHAPE_ONLY mode.
TEST_F(MouseCursorMonitorTest,MAYBE (ShapeOnly))116 TEST_F(MouseCursorMonitorTest, MAYBE(ShapeOnly)) {
117 std::unique_ptr<MouseCursorMonitor> capturer(
118 MouseCursorMonitor::CreateForScreen(
119 DesktopCaptureOptions::CreateDefault(),
120 webrtc::kFullDesktopScreenId));
121 assert(capturer.get());
122 capturer->Init(this, MouseCursorMonitor::SHAPE_ONLY);
123 capturer->Capture();
124
125 EXPECT_TRUE(cursor_image_.get());
126 EXPECT_FALSE(position_received_);
127 }
128
129 } // namespace webrtc
130