sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [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 | |
| 11 | #include "webrtc/modules/desktop_capture/screen_capturer_helper.h" |
| 12 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 13 | #include "webrtc/test/gtest.h" |
sergeyu@chromium.org | 3d34f66 | 2013-06-04 18:51:23 +0000 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | class ScreenCapturerHelperTest : public testing::Test { |
| 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)); |
| 58 | EXPECT_TRUE( |
| 59 | DesktopSize(12, 34).equals(capturer_helper_.size_most_recent())); |
| 60 | } |
| 61 | |
| 62 | TEST_F(ScreenCapturerHelperTest, SetLogGridSize) { |
| 63 | capturer_helper_.set_size_most_recent(DesktopSize(10, 10)); |
| 64 | |
| 65 | DesktopRegion region; |
| 66 | capturer_helper_.TakeInvalidRegion(®ion); |
| 67 | EXPECT_TRUE(DesktopRegion().Equals(region)); |
| 68 | |
| 69 | capturer_helper_.InvalidateRegion( |
| 70 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 71 | capturer_helper_.TakeInvalidRegion(®ion); |
| 72 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region)); |
| 73 | |
| 74 | capturer_helper_.SetLogGridSize(-1); |
| 75 | capturer_helper_.InvalidateRegion( |
| 76 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 77 | capturer_helper_.TakeInvalidRegion(®ion); |
| 78 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region)); |
| 79 | |
| 80 | capturer_helper_.SetLogGridSize(0); |
| 81 | capturer_helper_.InvalidateRegion( |
| 82 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 83 | capturer_helper_.TakeInvalidRegion(®ion); |
| 84 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region)); |
| 85 | |
| 86 | capturer_helper_.SetLogGridSize(1); |
| 87 | capturer_helper_.InvalidateRegion( |
| 88 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 89 | capturer_helper_.TakeInvalidRegion(®ion); |
| 90 | |
| 91 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(6, 6, 2, 2)).Equals(region)); |
| 92 | |
| 93 | capturer_helper_.SetLogGridSize(2); |
| 94 | capturer_helper_.InvalidateRegion( |
| 95 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 96 | capturer_helper_.TakeInvalidRegion(®ion); |
| 97 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(4, 4, 4, 4)).Equals(region)); |
| 98 | |
| 99 | capturer_helper_.SetLogGridSize(0); |
| 100 | capturer_helper_.InvalidateRegion( |
| 101 | DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); |
| 102 | capturer_helper_.TakeInvalidRegion(®ion); |
| 103 | EXPECT_TRUE(DesktopRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)).Equals(region)); |
| 104 | } |
| 105 | |
| 106 | void TestExpandRegionToGrid(const DesktopRegion& region, 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 | |
| 118 | void TestExpandRectToGrid(int l, int t, int r, int b, int log_grid_size, |
| 119 | int lExpanded, int tExpanded, |
| 120 | int rExpanded, int bExpanded) { |
| 121 | TestExpandRegionToGrid(DesktopRegion(DesktopRect::MakeLTRB(l, t, r, b)), |
| 122 | log_grid_size, |
| 123 | DesktopRegion(DesktopRect::MakeLTRB( |
| 124 | lExpanded, tExpanded, rExpanded, bExpanded))); |
| 125 | } |
| 126 | |
| 127 | TEST_F(ScreenCapturerHelperTest, ExpandToGrid) { |
| 128 | const int kLogGridSize = 4; |
| 129 | const int kGridSize = 1 << kLogGridSize; |
| 130 | for (int i = -2; i <= 2; i++) { |
| 131 | int x = i * kGridSize; |
| 132 | for (int j = -2; j <= 2; j++) { |
| 133 | int y = j * kGridSize; |
| 134 | TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, kLogGridSize, |
| 135 | x + 0, y + 0, x + kGridSize, y + kGridSize); |
| 136 | TestExpandRectToGrid(x + 0, y + kGridSize - 1, x + 1, y + kGridSize, |
| 137 | kLogGridSize, |
| 138 | x + 0, y + 0, x + kGridSize, y + kGridSize); |
| 139 | TestExpandRectToGrid(x + kGridSize - 1, y + kGridSize - 1, |
| 140 | x + kGridSize, y + kGridSize, kLogGridSize, |
| 141 | x + 0, y + 0, x + kGridSize, y + kGridSize); |
| 142 | TestExpandRectToGrid(x + kGridSize - 1, y + 0, |
| 143 | x + kGridSize, y + 1, kLogGridSize, |
| 144 | x + 0, y + 0, x + kGridSize, y + kGridSize); |
| 145 | TestExpandRectToGrid(x - 1, y + 0, x + 1, y + 1, kLogGridSize, |
| 146 | x - kGridSize, y + 0, x + kGridSize, y + kGridSize); |
| 147 | TestExpandRectToGrid(x - 1, y - 1, x + 1, y + 0, kLogGridSize, |
| 148 | x - kGridSize, y - kGridSize, x + kGridSize, y); |
| 149 | TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, kLogGridSize, |
| 150 | x, y - kGridSize, x + kGridSize, y + kGridSize); |
| 151 | TestExpandRectToGrid(x - 1, y - 1, x + 0, y + 1, kLogGridSize, |
| 152 | x - kGridSize, y - kGridSize, x, y + kGridSize); |
| 153 | |
| 154 | // Construct a region consisting of 3 pixels and verify that it's expanded |
| 155 | // properly to 3 squares that are kGridSize by kGridSize. |
| 156 | for (int q = 0; q < 4; ++q) { |
| 157 | DesktopRegion region; |
| 158 | DesktopRegion expanded_region_expected; |
| 159 | |
| 160 | if (q != 0) { |
| 161 | region.AddRect(DesktopRect::MakeXYWH(x - 1, y - 1, 1, 1)); |
| 162 | expanded_region_expected.AddRect(DesktopRect::MakeXYWH( |
| 163 | x - kGridSize, y - kGridSize, kGridSize, kGridSize)); |
| 164 | } |
| 165 | if (q != 1) { |
| 166 | region.AddRect(DesktopRect::MakeXYWH(x, y - 1, 1, 1)); |
| 167 | expanded_region_expected.AddRect(DesktopRect::MakeXYWH( |
| 168 | x, y - kGridSize, kGridSize, kGridSize)); |
| 169 | } |
| 170 | if (q != 2) { |
| 171 | region.AddRect(DesktopRect::MakeXYWH(x - 1, y, 1, 1)); |
| 172 | expanded_region_expected.AddRect(DesktopRect::MakeXYWH( |
| 173 | x - kGridSize, y, kGridSize, kGridSize)); |
| 174 | } |
| 175 | if (q != 3) { |
| 176 | region.AddRect(DesktopRect::MakeXYWH(x, y, 1, 1)); |
| 177 | expanded_region_expected.AddRect(DesktopRect::MakeXYWH( |
| 178 | x, y, kGridSize, kGridSize)); |
| 179 | } |
| 180 | |
| 181 | TestExpandRegionToGrid(region, kLogGridSize, expanded_region_expected); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | } // namespace webrtc |