blob: 165bbe42de44ad42637acf4b2108613521d9e65f [file] [log] [blame]
sergeyu@chromium.org3d34f662013-06-04 18:51:231/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "modules/desktop_capture/screen_capturer_helper.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:2312
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "test/gtest.h"
sergeyu@chromium.org3d34f662013-06-04 18:51:2314
15namespace webrtc {
16
Mirko Bonadei6a489f22019-04-09 13:11:1217class ScreenCapturerHelperTest : public ::testing::Test {
sergeyu@chromium.org3d34f662013-06-04 18:51:2318 protected:
19 ScreenCapturerHelper capturer_helper_;
20};
21
22TEST_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
30TEST_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
48TEST_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
55TEST_F(ScreenCapturerHelperTest, SizeMostRecent) {
56 EXPECT_TRUE(capturer_helper_.size_most_recent().is_empty());
57 capturer_helper_.set_size_most_recent(DesktopSize(12, 34));
Yves Gerey665174f2018-06-19 13:03:0558 EXPECT_TRUE(DesktopSize(12, 34).equals(capturer_helper_.size_most_recent()));
sergeyu@chromium.org3d34f662013-06-04 18:51:2359}
60
61TEST_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
Yves Gerey665174f2018-06-19 13:03:05105void TestExpandRegionToGrid(const DesktopRegion& region,
106 int log_grid_size,
sergeyu@chromium.org3d34f662013-06-04 18:51:23107 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
Yves Gerey665174f2018-06-19 13:03:05118void 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) {
sergeyu@chromium.org3d34f662013-06-04 18:51:23127 TestExpandRegionToGrid(DesktopRegion(DesktopRect::MakeLTRB(l, t, r, b)),
128 log_grid_size,
129 DesktopRegion(DesktopRect::MakeLTRB(
130 lExpanded, tExpanded, rExpanded, bExpanded)));
131}
132
133TEST_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;
Yves Gerey665174f2018-06-19 13:03:05140 TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, kLogGridSize, x + 0,
141 y + 0, x + kGridSize, y + kGridSize);
sergeyu@chromium.org3d34f662013-06-04 18:51:23142 TestExpandRectToGrid(x + 0, y + kGridSize - 1, x + 1, y + kGridSize,
Yves Gerey665174f2018-06-19 13:03:05143 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);
sergeyu@chromium.org3d34f662013-06-04 18:51:23151 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);
Yves Gerey665174f2018-06-19 13:03:05155 TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, kLogGridSize, x,
156 y - kGridSize, x + kGridSize, y + kGridSize);
sergeyu@chromium.org3d34f662013-06-04 18:51:23157 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));
Yves Gerey665174f2018-06-19 13:03:05173 expanded_region_expected.AddRect(
174 DesktopRect::MakeXYWH(x, y - kGridSize, kGridSize, kGridSize));
sergeyu@chromium.org3d34f662013-06-04 18:51:23175 }
176 if (q != 2) {
177 region.AddRect(DesktopRect::MakeXYWH(x - 1, y, 1, 1));
Yves Gerey665174f2018-06-19 13:03:05178 expanded_region_expected.AddRect(
179 DesktopRect::MakeXYWH(x - kGridSize, y, kGridSize, kGridSize));
sergeyu@chromium.org3d34f662013-06-04 18:51:23180 }
181 if (q != 3) {
182 region.AddRect(DesktopRect::MakeXYWH(x, y, 1, 1));
Yves Gerey665174f2018-06-19 13:03:05183 expanded_region_expected.AddRect(
184 DesktopRect::MakeXYWH(x, y, kGridSize, kGridSize));
sergeyu@chromium.org3d34f662013-06-04 18:51:23185 }
186
187 TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected);
188 }
189 }
190 }
191}
192
193} // namespace webrtc