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