| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/video/capture/screen/screen_capturer_helper.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 using webrtc::DesktopRect; | |
| 11 using webrtc::DesktopRegion; | |
| 12 using webrtc::DesktopSize; | |
| 13 | |
| 14 namespace media { | |
| 15 | |
| 16 class ScreenCapturerHelperTest : public testing::Test { | |
| 17 protected: | |
| 18 ScreenCapturerHelper capturer_helper_; | |
| 19 }; | |
| 20 | |
| 21 bool Equals(const DesktopRegion& region1, const DesktopRegion& region2) { | |
| 22 DesktopRegion::Iterator iter1(region1); | |
| 23 DesktopRegion::Iterator iter2(region2); | |
| 24 while (!iter1.IsAtEnd() && !iter1.IsAtEnd()) { | |
| 25 if (!iter1.rect().equals(iter2.rect())) { | |
| 26 return false; | |
| 27 } | |
| 28 iter1.Advance(); | |
| 29 iter2.Advance(); | |
| 30 } | |
| 31 return iter1.IsAtEnd() && iter2.IsAtEnd(); | |
| 32 } | |
| 33 | |
| 34 bool Equals(const SkRegion& region1, const SkRegion& region2) { | |
| 35 SkRegion::Iterator iter1(region1); | |
| 36 SkRegion::Iterator iter2(region2); | |
| 37 while (!iter1.done() && !iter2.done()) { | |
| 38 if (iter1.rect() != iter2.rect()) { | |
| 39 return false; | |
| 40 } | |
| 41 iter1.next(); | |
| 42 iter2.next(); | |
| 43 } | |
| 44 return iter1.done() && iter2.done(); | |
| 45 } | |
| 46 | |
| 47 DesktopRegion RectToRegion(const DesktopRect& rect) { | |
| 48 webrtc::DesktopRegion result; | |
| 49 result.SetRect(rect); | |
| 50 return result; | |
| 51 } | |
| 52 | |
| 53 TEST_F(ScreenCapturerHelperTest, ClearInvalidRegion) { | |
| 54 DesktopRegion region; | |
| 55 region.SetRect(DesktopRect::MakeXYWH(1, 2, 3, 4)); | |
| 56 capturer_helper_.InvalidateRegion(region); | |
| 57 capturer_helper_.ClearInvalidRegion(); | |
| 58 capturer_helper_.TakeInvalidRegion(®ion); | |
| 59 ASSERT_TRUE(region.is_empty()); | |
| 60 } | |
| 61 | |
| 62 TEST_F(ScreenCapturerHelperTest, InvalidateRegion) { | |
| 63 DesktopRegion region; | |
| 64 capturer_helper_.TakeInvalidRegion(®ion); | |
| 65 ASSERT_TRUE(region.is_empty()); | |
| 66 | |
| 67 region.SetRect(DesktopRect::MakeXYWH(1, 2, 3, 4)); | |
| 68 capturer_helper_.InvalidateRegion(region); | |
| 69 capturer_helper_.TakeInvalidRegion(®ion); | |
| 70 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(1, 2, 3, 4)), region)); | |
| 71 | |
| 72 capturer_helper_.InvalidateRegion( | |
| 73 RectToRegion(DesktopRect::MakeXYWH(1, 2, 3, 4))); | |
| 74 capturer_helper_.InvalidateRegion( | |
| 75 RectToRegion(DesktopRect::MakeXYWH(4, 2, 3, 4))); | |
| 76 capturer_helper_.TakeInvalidRegion(®ion); | |
| 77 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(1, 2, 6, 4)), region)); | |
| 78 } | |
| 79 | |
| 80 TEST_F(ScreenCapturerHelperTest, InvalidateScreen) { | |
| 81 DesktopRegion region; | |
| 82 capturer_helper_.InvalidateScreen(DesktopSize(12, 34)); | |
| 83 capturer_helper_.TakeInvalidRegion(®ion); | |
| 84 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeWH(12, 34)), region)); | |
| 85 } | |
| 86 | |
| 87 TEST_F(ScreenCapturerHelperTest, SizeMostRecent) { | |
| 88 ASSERT_TRUE(capturer_helper_.size_most_recent().is_empty()); | |
| 89 capturer_helper_.set_size_most_recent(DesktopSize(12, 34)); | |
| 90 ASSERT_TRUE( | |
| 91 DesktopSize(12, 34).equals(capturer_helper_.size_most_recent())); | |
| 92 } | |
| 93 | |
| 94 TEST_F(ScreenCapturerHelperTest, SetLogGridSize) { | |
| 95 capturer_helper_.set_size_most_recent(DesktopSize(10, 10)); | |
| 96 | |
| 97 DesktopRegion region; | |
| 98 capturer_helper_.TakeInvalidRegion(®ion); | |
| 99 ASSERT_TRUE(Equals(RectToRegion(DesktopRect()), region)); | |
| 100 | |
| 101 capturer_helper_.InvalidateRegion( | |
| 102 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 103 capturer_helper_.TakeInvalidRegion(®ion); | |
| 104 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)), region)); | |
| 105 | |
| 106 capturer_helper_.SetLogGridSize(-1); | |
| 107 capturer_helper_.InvalidateRegion( | |
| 108 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 109 capturer_helper_.TakeInvalidRegion(®ion); | |
| 110 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)), region)); | |
| 111 | |
| 112 capturer_helper_.SetLogGridSize(0); | |
| 113 capturer_helper_.InvalidateRegion( | |
| 114 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 115 capturer_helper_.TakeInvalidRegion(®ion); | |
| 116 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)), region)); | |
| 117 | |
| 118 capturer_helper_.SetLogGridSize(1); | |
| 119 capturer_helper_.InvalidateRegion( | |
| 120 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 121 capturer_helper_.TakeInvalidRegion(®ion); | |
| 122 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(6, 6, 2, 2)), region)); | |
| 123 | |
| 124 capturer_helper_.SetLogGridSize(2); | |
| 125 capturer_helper_.InvalidateRegion( | |
| 126 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 127 capturer_helper_.TakeInvalidRegion(®ion); | |
| 128 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(4, 4, 4, 4)), region)); | |
| 129 | |
| 130 capturer_helper_.SetLogGridSize(0); | |
| 131 capturer_helper_.InvalidateRegion( | |
| 132 RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1))); | |
| 133 capturer_helper_.TakeInvalidRegion(®ion); | |
| 134 ASSERT_TRUE(Equals(RectToRegion(DesktopRect::MakeXYWH(7, 7, 1, 1)), region)); | |
| 135 } | |
| 136 | |
| 137 void TestExpandRegionToGrid(const SkRegion& region, int log_grid_size, | |
| 138 const SkRegion& expandedRegionExpected) { | |
| 139 scoped_ptr<SkRegion> expandedRegion1( | |
| 140 ScreenCapturerHelper::ExpandToGrid(region, log_grid_size)); | |
| 141 ASSERT_TRUE(Equals(expandedRegionExpected, *expandedRegion1)); | |
| 142 scoped_ptr<SkRegion> expandedRegion2( | |
| 143 ScreenCapturerHelper::ExpandToGrid(*expandedRegion1, log_grid_size)); | |
| 144 ASSERT_TRUE(Equals(*expandedRegion1, *expandedRegion2)); | |
| 145 } | |
| 146 | |
| 147 void TestExpandRectToGrid(int l, int t, int r, int b, int log_grid_size, | |
| 148 int lExpanded, int tExpanded, | |
| 149 int rExpanded, int bExpanded) { | |
| 150 TestExpandRegionToGrid(SkRegion(SkIRect::MakeLTRB(l, t, r, b)), log_grid_size, | |
| 151 SkRegion(SkIRect::MakeLTRB(lExpanded, tExpanded, | |
| 152 rExpanded, bExpanded))); | |
| 153 } | |
| 154 | |
| 155 TEST_F(ScreenCapturerHelperTest, ExpandToGrid) { | |
| 156 const int LOG_GRID_SIZE = 4; | |
| 157 const int GRID_SIZE = 1 << LOG_GRID_SIZE; | |
| 158 for (int i = -2; i <= 2; i++) { | |
| 159 int x = i * GRID_SIZE; | |
| 160 for (int j = -2; j <= 2; j++) { | |
| 161 int y = j * GRID_SIZE; | |
| 162 TestExpandRectToGrid(x + 0, y + 0, x + 1, y + 1, LOG_GRID_SIZE, | |
| 163 x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE); | |
| 164 TestExpandRectToGrid(x + 0, y + GRID_SIZE - 1, x + 1, y + GRID_SIZE, | |
| 165 LOG_GRID_SIZE, | |
| 166 x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE); | |
| 167 TestExpandRectToGrid(x + GRID_SIZE - 1, y + GRID_SIZE - 1, | |
| 168 x + GRID_SIZE, y + GRID_SIZE, LOG_GRID_SIZE, | |
| 169 x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE); | |
| 170 TestExpandRectToGrid(x + GRID_SIZE - 1, y + 0, | |
| 171 x + GRID_SIZE, y + 1, LOG_GRID_SIZE, | |
| 172 x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE); | |
| 173 TestExpandRectToGrid(x - 1, y + 0, x + 1, y + 1, LOG_GRID_SIZE, | |
| 174 x - GRID_SIZE, y + 0, x + GRID_SIZE, y + GRID_SIZE); | |
| 175 TestExpandRectToGrid(x - 1, y - 1, x + 1, y + 0, LOG_GRID_SIZE, | |
| 176 x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y); | |
| 177 TestExpandRectToGrid(x + 0, y - 1, x + 1, y + 1, LOG_GRID_SIZE, | |
| 178 x, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE); | |
| 179 TestExpandRectToGrid(x - 1, y - 1, x + 0, y + 1, LOG_GRID_SIZE, | |
| 180 x - GRID_SIZE, y - GRID_SIZE, x, y + GRID_SIZE); | |
| 181 | |
| 182 SkRegion region(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1)); | |
| 183 region.op(SkIRect::MakeLTRB(x - 1, y - 1, x + 0, y + 0), | |
| 184 SkRegion::kDifference_Op); | |
| 185 SkRegion expandedRegionExpected(SkIRect::MakeLTRB( | |
| 186 x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE)); | |
| 187 expandedRegionExpected.op( | |
| 188 SkIRect::MakeLTRB(x - GRID_SIZE, y - GRID_SIZE, x + 0, y + 0), | |
| 189 SkRegion::kDifference_Op); | |
| 190 TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected); | |
| 191 | |
| 192 region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1)); | |
| 193 region.op(SkIRect::MakeLTRB(x - 1, y + 0, x + 0, y + 1), | |
| 194 SkRegion::kDifference_Op); | |
| 195 expandedRegionExpected.setRect(SkIRect::MakeLTRB( | |
| 196 x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE)); | |
| 197 expandedRegionExpected.op( | |
| 198 SkIRect::MakeLTRB(x - GRID_SIZE, y + 0, x + 0, y + GRID_SIZE), | |
| 199 SkRegion::kDifference_Op); | |
| 200 TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected); | |
| 201 | |
| 202 region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1)); | |
| 203 region.op(SkIRect::MakeLTRB(x + 0, y + 0, x + 1, y + 1), | |
| 204 SkRegion::kDifference_Op); | |
| 205 expandedRegionExpected.setRect(SkIRect::MakeLTRB( | |
| 206 x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE)); | |
| 207 expandedRegionExpected.op( | |
| 208 SkIRect::MakeLTRB(x + 0, y + 0, x + GRID_SIZE, y + GRID_SIZE), | |
| 209 SkRegion::kDifference_Op); | |
| 210 TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected); | |
| 211 | |
| 212 region.setRect(SkIRect::MakeLTRB(x - 1, y - 1, x + 1, y + 1)); | |
| 213 region.op(SkIRect::MakeLTRB(x + 0, y - 1, x + 1, y + 0), | |
| 214 SkRegion::kDifference_Op); | |
| 215 expandedRegionExpected.setRect(SkIRect::MakeLTRB( | |
| 216 x - GRID_SIZE, y - GRID_SIZE, x + GRID_SIZE, y + GRID_SIZE)); | |
| 217 expandedRegionExpected.op( | |
| 218 SkIRect::MakeLTRB(x + 0, y - GRID_SIZE, x + GRID_SIZE, y + 0), | |
| 219 SkRegion::kDifference_Op); | |
| 220 TestExpandRegionToGrid(region, LOG_GRID_SIZE, expandedRegionExpected); | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 } // namespace media | |
| OLD | NEW |