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