xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/screen_capturer_mac_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 <ApplicationServices/ApplicationServices.h>
12 
13 #include <memory>
14 #include <ostream>
15 
16 #include "modules/desktop_capture/desktop_capture_options.h"
17 #include "modules/desktop_capture/desktop_capturer.h"
18 #include "modules/desktop_capture/desktop_frame.h"
19 #include "modules/desktop_capture/desktop_geometry.h"
20 #include "modules/desktop_capture/desktop_region.h"
21 #include "modules/desktop_capture/mac/desktop_configuration.h"
22 #include "modules/desktop_capture/mock_desktop_capturer_callback.h"
23 #include "test/gtest.h"
24 
25 using ::testing::_;
26 using ::testing::AnyNumber;
27 using ::testing::Return;
28 
29 namespace webrtc {
30 
31 class ScreenCapturerMacTest : public ::testing::Test {
32  public:
33   // Verifies that the whole screen is initially dirty.
34   void CaptureDoneCallback1(DesktopCapturer::Result result,
35                             std::unique_ptr<DesktopFrame>* frame);
36 
37   // Verifies that a rectangle explicitly marked as dirty is propagated
38   // correctly.
39   void CaptureDoneCallback2(DesktopCapturer::Result result,
40                             std::unique_ptr<DesktopFrame>* frame);
41 
42  protected:
SetUp()43   void SetUp() override {
44     capturer_ = DesktopCapturer::CreateScreenCapturer(
45         DesktopCaptureOptions::CreateDefault());
46   }
47 
48   std::unique_ptr<DesktopCapturer> capturer_;
49   MockDesktopCapturerCallback callback_;
50 };
51 
CaptureDoneCallback1(DesktopCapturer::Result result,std::unique_ptr<DesktopFrame> * frame)52 void ScreenCapturerMacTest::CaptureDoneCallback1(
53     DesktopCapturer::Result result,
54     std::unique_ptr<DesktopFrame>* frame) {
55   EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS);
56 
57   MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
58       MacDesktopConfiguration::BottomLeftOrigin);
59 
60   // Verify that the region contains full frame.
61   DesktopRegion::Iterator it((*frame)->updated_region());
62   EXPECT_TRUE(!it.IsAtEnd() && it.rect().equals(config.pixel_bounds));
63 }
64 
CaptureDoneCallback2(DesktopCapturer::Result result,std::unique_ptr<DesktopFrame> * frame)65 void ScreenCapturerMacTest::CaptureDoneCallback2(
66     DesktopCapturer::Result result,
67     std::unique_ptr<DesktopFrame>* frame) {
68   EXPECT_EQ(result, DesktopCapturer::Result::SUCCESS);
69 
70   MacDesktopConfiguration config = MacDesktopConfiguration::GetCurrent(
71       MacDesktopConfiguration::BottomLeftOrigin);
72   int width = config.pixel_bounds.width();
73   int height = config.pixel_bounds.height();
74 
75   EXPECT_EQ(width, (*frame)->size().width());
76   EXPECT_EQ(height, (*frame)->size().height());
77   EXPECT_TRUE((*frame)->data() != NULL);
78   // Depending on the capture method, the screen may be flipped or not, so
79   // the stride may be positive or negative.
80   EXPECT_EQ(static_cast<int>(sizeof(uint32_t) * width),
81             abs((*frame)->stride()));
82 }
83 
TEST_F(ScreenCapturerMacTest,Capture)84 TEST_F(ScreenCapturerMacTest, Capture) {
85   EXPECT_CALL(callback_,
86               OnCaptureResultPtr(DesktopCapturer::Result::SUCCESS, _))
87       .Times(2)
88       .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback1))
89       .WillOnce(Invoke(this, &ScreenCapturerMacTest::CaptureDoneCallback2));
90 
91   SCOPED_TRACE("");
92   capturer_->Start(&callback_);
93 
94   // Check that we get an initial full-screen updated.
95   capturer_->CaptureFrame();
96 
97   // Check that subsequent dirty rects are propagated correctly.
98   capturer_->CaptureFrame();
99 }
100 
101 }  // namespace webrtc
102