xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/window_capturer_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 <memory>
12 #include <string>
13 #include <utility>
14 
15 #include "modules/desktop_capture/desktop_capture_options.h"
16 #include "modules/desktop_capture/desktop_capturer.h"
17 #include "modules/desktop_capture/desktop_frame.h"
18 #include "modules/desktop_capture/desktop_geometry.h"
19 #include "rtc_base/checks.h"
20 #include "test/gtest.h"
21 
22 namespace webrtc {
23 
24 class WindowCapturerTest : public ::testing::Test,
25                            public DesktopCapturer::Callback {
26  public:
SetUp()27   void SetUp() override {
28     capturer_ = DesktopCapturer::CreateWindowCapturer(
29         DesktopCaptureOptions::CreateDefault());
30     ASSERT_TRUE(capturer_);
31   }
32 
TearDown()33   void TearDown() override {}
34 
35   // DesktopCapturer::Callback interface
OnCaptureResult(DesktopCapturer::Result result,std::unique_ptr<DesktopFrame> frame)36   void OnCaptureResult(DesktopCapturer::Result result,
37                        std::unique_ptr<DesktopFrame> frame) override {
38     frame_ = std::move(frame);
39   }
40 
41  protected:
42   std::unique_ptr<DesktopCapturer> capturer_;
43   std::unique_ptr<DesktopFrame> frame_;
44 };
45 
46 // Verify that we can enumerate windows.
47 // TODO(bugs.webrtc.org/12950): Re-enable when libc++ issue is fixed
48 #if defined(WEBRTC_LINUX) && defined(MEMORY_SANITIZER)
49 #define MAYBE_Enumerate DISABLED_Enumerate
50 #else
51 #define MAYBE_Enumerate Enumerate
52 #endif
TEST_F(WindowCapturerTest,MAYBE_Enumerate)53 TEST_F(WindowCapturerTest, MAYBE_Enumerate) {
54   DesktopCapturer::SourceList sources;
55   EXPECT_TRUE(capturer_->GetSourceList(&sources));
56 
57   // Verify that window titles are set.
58   for (auto it = sources.begin(); it != sources.end(); ++it) {
59     EXPECT_FALSE(it->title.empty());
60   }
61 }
62 
63 // Flaky on Linux. See: crbug.com/webrtc/7830.
64 // Failing on macOS 11: See bugs.webrtc.org/12801
65 #if defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
66 #define MAYBE_Capture DISABLED_Capture
67 #else
68 #define MAYBE_Capture Capture
69 #endif
70 // Verify we can capture a window.
71 //
72 // TODO(sergeyu): Currently this test just looks at the windows that already
73 // exist. Ideally it should create a test window and capture from it, but there
74 // is no easy cross-platform way to create new windows (potentially we could
75 // have a python script showing Tk dialog, but launching code will differ
76 // between platforms).
TEST_F(WindowCapturerTest,MAYBE_Capture)77 TEST_F(WindowCapturerTest, MAYBE_Capture) {
78   DesktopCapturer::SourceList sources;
79   capturer_->Start(this);
80   EXPECT_TRUE(capturer_->GetSourceList(&sources));
81 
82   // Verify that we can select and capture each window.
83   for (auto it = sources.begin(); it != sources.end(); ++it) {
84     frame_.reset();
85     if (capturer_->SelectSource(it->id)) {
86       capturer_->CaptureFrame();
87     }
88 
89     // If we failed to capture a window make sure it no longer exists.
90     if (!frame_.get()) {
91       DesktopCapturer::SourceList new_list;
92       EXPECT_TRUE(capturer_->GetSourceList(&new_list));
93       for (auto new_list_it = new_list.begin(); new_list_it != new_list.end();
94            ++new_list_it) {
95         EXPECT_FALSE(it->id == new_list_it->id);
96       }
97       continue;
98     }
99 
100     EXPECT_GT(frame_->size().width(), 0);
101     EXPECT_GT(frame_->size().height(), 0);
102   }
103 }
104 
105 }  // namespace webrtc
106