xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/screen_capturer_helper_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/screen_capturer_helper.h"
12 
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 
17 class ScreenCapturerHelperTest : public ::testing::Test {
18  protected:
19   ScreenCapturerHelper capturer_helper_;
20 };
21 
TEST_F(ScreenCapturerHelperTest,ClearInvalidRegion)22 TEST_F(ScreenCapturerHelperTest, ClearInvalidRegion) {
23   DesktopRegion region(DesktopRect::MakeXYWH(1, 2, 3, 4));
24   capturer_helper_.InvalidateRegion(region);
25   capturer_helper_.ClearInvalidRegion();
26   capturer_helper_.TakeInvalidRegion(&region);
27   EXPECT_TRUE(region.is_empty());
28 }
29 
TEST_F(ScreenCapturerHelperTest,InvalidateRegion)30 TEST_F(ScreenCapturerHelperTest, InvalidateRegion) {
31   DesktopRegion region;
32   capturer_helper_.TakeInvalidRegion(&region);
33   EXPECT_TRUE(region.is_empty());
34 
35   region.SetRect(DesktopRect::MakeXYWH(1, 2, 3, 4));
36   capturer_helper_.InvalidateRegion(region);
37   capturer_helper_.TakeInvalidRegion(&region);
38   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(1, 2, 3, 4)).Equals(region));
39 
40   capturer_helper_.InvalidateRegion(
41       DesktopRegion(DesktopRect::MakeXYWH(1, 2, 3, 4)));
42   capturer_helper_.InvalidateRegion(
43       DesktopRegion(DesktopRect::MakeXYWH(4, 2, 3, 4)));
44   capturer_helper_.TakeInvalidRegion(&region);
45   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(1, 2, 6, 4)).Equals(region));
46 }
47 
TEST_F(ScreenCapturerHelperTest,InvalidateScreen)48 TEST_F(ScreenCapturerHelperTest, InvalidateScreen) {
49   DesktopRegion region;
50   capturer_helper_.InvalidateScreen(DesktopSize(12, 34));
51   capturer_helper_.TakeInvalidRegion(&region);
52   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeWH(12, 34)).Equals(region));
53 }
54 
TEST_F(ScreenCapturerHelperTest,SizeMostRecent)55 TEST_F(ScreenCapturerHelperTest, SizeMostRecent) {
56   EXPECT_TRUE(capturer_helper_.size_most_recent().is_empty());
57   capturer_helper_.set_size_most_recent(DesktopSize(12, 34));
58   EXPECT_TRUE(DesktopSize(12, 34).equals(capturer_helper_.size_most_recent()));
59 }
60 
TEST_F(ScreenCapturerHelperTest,SetLogGridSize)61 TEST_F(ScreenCapturerHelperTest, SetLogGridSize) {
62   capturer_helper_.set_size_most_recent(DesktopSize(10, 10));
63 
64   DesktopRegion region;
65   capturer_helper_.TakeInvalidRegion(&region);
66   EXPECT_TRUE(DesktopRegion().Equals(region));
67 
68   capturer_helper_.InvalidateRegion(
69       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
70   capturer_helper_.TakeInvalidRegion(&region);
71   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
72 
73   capturer_helper_.SetLogGridSize(-1);
74   capturer_helper_.InvalidateRegion(
75       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
76   capturer_helper_.TakeInvalidRegion(&region);
77   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
78 
79   capturer_helper_.SetLogGridSize(0);
80   capturer_helper_.InvalidateRegion(
81       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
82   capturer_helper_.TakeInvalidRegion(&region);
83   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
84 
85   capturer_helper_.SetLogGridSize(1);
86   capturer_helper_.InvalidateRegion(
87       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
88   capturer_helper_.TakeInvalidRegion(&region);
89 
90   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(6, 6, 2, 2)).Equals(region));
91 
92   capturer_helper_.SetLogGridSize(2);
93   capturer_helper_.InvalidateRegion(
94       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
95   capturer_helper_.TakeInvalidRegion(&region);
96   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(4, 4, 4, 4)).Equals(region));
97 
98   capturer_helper_.SetLogGridSize(0);
99   capturer_helper_.InvalidateRegion(
100       DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
101   capturer_helper_.TakeInvalidRegion(&region);
102   EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region));
103 }
104 
TestExpandRegionToGrid(const DesktopRegion & region,int log_grid_size,const DesktopRegion & expanded_region_expected)105 void TestExpandRegionToGrid(const DesktopRegion& region,
106                             int log_grid_size,
107                             const DesktopRegion& expanded_region_expected) {
108   DesktopRegion expanded_region1;
109   ScreenCapturerHelper::ExpandToGrid(region, log_grid_size, &expanded_region1);
110   EXPECT_TRUE(expanded_region_expected.Equals(expanded_region1));
111 
112   DesktopRegion expanded_region2;
113   ScreenCapturerHelper::ExpandToGrid(expanded_region1, log_grid_size,
114                                      &expanded_region2);
115   EXPECT_TRUE(expanded_region1.Equals(expanded_region2));
116 }
117 
TestExpandRectToGrid(int l,int t,int r,int b,int log_grid_size,int lExpanded,int tExpanded,int rExpanded,int bExpanded)118 void TestExpandRectToGrid(int l,
119                           int t,
120                           int r,
121                           int b,
122                           int log_grid_size,
123                           int lExpanded,
124                           int tExpanded,
125                           int rExpanded,
126                           int bExpanded) {
127   TestExpandRegionToGrid(DesktopRegion(DesktopRect::MakeLTRB(l, t, r, b)),
128                          log_grid_size,
129                          DesktopRegion(DesktopRect::MakeLTRB(
130                              lExpanded, tExpanded, rExpanded, bExpanded)));
131 }
132 
TEST_F(ScreenCapturerHelperTest,ExpandToGrid)133 TEST_F(ScreenCapturerHelperTest, ExpandToGrid) {
134   const int kLogGridSize = 4;
135   const int kGridSize = 1 << kLogGridSize;
136   for (int i = -2; i <= 2; i++) {
137     int x = i * kGridSize;
138     for (int j = -2; j <= 2; j++) {
139       int y = j * kGridSize;
140       TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, kLogGridSize, x + 0,
141                            y + 0, x + kGridSize, y + kGridSize);
142       TestExpandRectToGrid(x + 0, y + kGridSize - 1, x + 1, y + kGridSize,
143                            kLogGridSize, x + 0, y + 0, x + kGridSize,
144                            y + kGridSize);
145       TestExpandRectToGrid(x + kGridSize - 1, y + kGridSize - 1, x + kGridSize,
146                            y + kGridSize, kLogGridSize, x + 0, y + 0,
147                            x + kGridSize, y + kGridSize);
148       TestExpandRectToGrid(x + kGridSize - 1, y + 0, x + kGridSize, y + 1,
149                            kLogGridSize, x + 0, y + 0, x + kGridSize,
150                            y + kGridSize);
151       TestExpandRectToGrid(x - 1, y + 0, x + 1, y + 1, kLogGridSize,
152                            x - kGridSize, y + 0, x + kGridSize, y + kGridSize);
153       TestExpandRectToGrid(x - 1, y - 1, x + 1, y + 0, kLogGridSize,
154                            x - kGridSize, y - kGridSize, x + kGridSize, y);
155       TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, kLogGridSize, x,
156                            y - kGridSize, x + kGridSize, y + kGridSize);
157       TestExpandRectToGrid(x - 1, y - 1, x + 0, y + 1, kLogGridSize,
158                            x - kGridSize, y - kGridSize, x, y + kGridSize);
159 
160       // Construct a region consisting of 3 pixels and verify that it's expanded
161       // properly to 3 squares that are kGridSize by kGridSize.
162       for (int q = 0; q < 4; ++q) {
163         DesktopRegion region;
164         DesktopRegion expanded_region_expected;
165 
166         if (q != 0) {
167           region.AddRect(DesktopRect::MakeXYWH(x - 1, y - 1, 1, 1));
168           expanded_region_expected.AddRect(DesktopRect::MakeXYWH(
169               x - kGridSize, y - kGridSize, kGridSize, kGridSize));
170         }
171         if (q != 1) {
172           region.AddRect(DesktopRect::MakeXYWH(x, y - 1, 1, 1));
173           expanded_region_expected.AddRect(
174               DesktopRect::MakeXYWH(x, y - kGridSize, kGridSize, kGridSize));
175         }
176         if (q != 2) {
177           region.AddRect(DesktopRect::MakeXYWH(x - 1, y, 1, 1));
178           expanded_region_expected.AddRect(
179               DesktopRect::MakeXYWH(x - kGridSize, y, kGridSize, kGridSize));
180         }
181         if (q != 3) {
182           region.AddRect(DesktopRect::MakeXYWH(x, y, 1, 1));
183           expanded_region_expected.AddRect(
184               DesktopRect::MakeXYWH(x, y, kGridSize, kGridSize));
185         }
186 
187         TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected);
188       }
189     }
190   }
191 }
192 
193 }  // namespace webrtc
194