Backed out changeset e7acb4e12051 (bug 1893666) for causing xpcshell failures on...
[gecko.git] / third_party / libwebrtc / modules / desktop_capture / screen_capturer_helper_unittest.cc
blob165bbe42de44ad42637acf4b2108613521d9e65f
1 /*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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 */
11 #include "modules/desktop_capture/screen_capturer_helper.h"
13 #include "test/gtest.h"
15 namespace webrtc {
17 class ScreenCapturerHelperTest : public ::testing::Test {
18 protected:
19 ScreenCapturerHelper capturer_helper_;
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());
30 TEST_F(ScreenCapturerHelperTest, InvalidateRegion) {
31 DesktopRegion region;
32 capturer_helper_.TakeInvalidRegion(&region);
33 EXPECT_TRUE(region.is_empty());
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));
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));
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));
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()));
61 TEST_F(ScreenCapturerHelperTest, SetLogGridSize) {
62 capturer_helper_.set_size_most_recent(DesktopSize(10, 10));
64 DesktopRegion region;
65 capturer_helper_.TakeInvalidRegion(&region);
66 EXPECT_TRUE(DesktopRegion().Equals(region));
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));
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));
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));
85 capturer_helper_.SetLogGridSize(1);
86 capturer_helper_.InvalidateRegion(
87 DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)));
88 capturer_helper_.TakeInvalidRegion(&region);
90 EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(6, 6, 2, 2)).Equals(region));
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));
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));
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));
112 DesktopRegion expanded_region2;
113 ScreenCapturerHelper::ExpandToGrid(expanded_region1, log_grid_size,
114 &expanded_region2);
115 EXPECT_TRUE(expanded_region1.Equals(expanded_region2));
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)));
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);
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;
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));
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));
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));
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));
187 TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected);
193 } // namespace webrtc