sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "modules/desktop_capture/screen_capturer_helper.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 13 | #include "test/gtest.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 13:11:12 | [diff] [blame] | 17 | class ScreenCapturerHelperTest : public ::testing::Test { |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 18 | protected: |
| 19 | ScreenCapturerHelper capturer_helper_; |
| 20 | }; |
| 21 | |
| 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(®ion); |
| 27 | EXPECT_TRUE(region.is_empty()); |
| 28 | } |
| 29 | |
| 30 | TEST_F(ScreenCapturerHelperTest, InvalidateRegion) { |
| 31 | DesktopRegion region; |
| 32 | capturer_helper_.TakeInvalidRegion(®ion); |
| 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(®ion); |
| 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(®ion); |
| 45 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(1, 2, 6, 4)).Equals(region)); |
| 46 | } |
| 47 | |
| 48 | TEST_F(ScreenCapturerHelperTest, InvalidateScreen) { |
| 49 | DesktopRegion region; |
| 50 | capturer_helper_.InvalidateScreen(DesktopSize(12, 34)); |
| 51 | capturer_helper_.TakeInvalidRegion(®ion); |
| 52 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeWH(12, 34)).Equals(region)); |
| 53 | } |
| 54 | |
| 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)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 58 | EXPECT_TRUE(DesktopSize(12, 34).equals(capturer_helper_.size_most_recent())); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST_F(ScreenCapturerHelperTest, SetLogGridSize) { |
| 62 | capturer_helper_.set_size_most_recent(DesktopSize(10, 10)); |
| 63 | |
| 64 | DesktopRegion region; |
| 65 | capturer_helper_.TakeInvalidRegion(®ion); |
| 66 | EXPECT_TRUE(DesktopRegion().Equals(region)); |
| 67 | |
| 68 | capturer_helper_.InvalidateRegion( |
| 69 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 70 | capturer_helper_.TakeInvalidRegion(®ion); |
| 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(®ion); |
| 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(®ion); |
| 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(®ion); |
| 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(®ion); |
| 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(®ion); |
| 102 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region)); |
| 103 | } |
| 104 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 105 | void TestExpandRegionToGrid(const DesktopRegion& region, |
| 106 | int log_grid_size, |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 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) { |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 127 | TestExpandRegionToGrid(DesktopRegion(DesktopRect::MakeLTRB(l, t, r, b)), |
| 128 | log_grid_size, |
| 129 | DesktopRegion(DesktopRect::MakeLTRB( |
| 130 | lExpanded, tExpanded, rExpanded, bExpanded))); |
| 131 | } |
| 132 | |
| 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; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 140 | TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, kLogGridSize, x + 0, |
| 141 | y + 0, x + kGridSize, y + kGridSize); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 142 | TestExpandRectToGrid(x + 0, y + kGridSize - 1, x + 1, y + kGridSize, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 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); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 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); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 155 | TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, kLogGridSize, x, |
| 156 | y - kGridSize, x + kGridSize, y + kGridSize); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 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)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 173 | expanded_region_expected.AddRect( |
| 174 | DesktopRect::MakeXYWH(x, y - kGridSize, kGridSize, kGridSize)); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 175 | } |
| 176 | if (q != 2) { |
| 177 | region.AddRect(DesktopRect::MakeXYWH(x - 1, y, 1, 1)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 178 | expanded_region_expected.AddRect( |
| 179 | DesktopRect::MakeXYWH(x - kGridSize, y, kGridSize, kGridSize)); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 180 | } |
| 181 | if (q != 3) { |
| 182 | region.AddRect(DesktopRect::MakeXYWH(x, y, 1, 1)); |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 183 | expanded_region_expected.AddRect( |
| 184 | DesktopRect::MakeXYWH(x, y, kGridSize, kGridSize)); |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } // namespace webrtc |