Marin Shalamanov | 4aa3af1 | 2020-09-25 14:20:58 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <system/graphics.h> |
| 18 | #include <ui/FloatRect.h> |
| 19 | #include <ui/Point.h> |
| 20 | #include <ui/Rect.h> |
| 21 | #include <ui/Size.h> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | namespace android::ui { |
| 26 | |
| 27 | TEST(RectTest, constructDefault) { |
| 28 | const Rect rect; |
| 29 | EXPECT_FALSE(rect.isValid()); |
| 30 | EXPECT_TRUE(rect.isEmpty()); |
| 31 | } |
| 32 | |
| 33 | TEST(RectTest, constructFromWidthAndHeight) { |
| 34 | const Rect rect(100, 200); |
| 35 | EXPECT_TRUE(rect.isValid()); |
| 36 | EXPECT_FALSE(rect.isEmpty()); |
| 37 | EXPECT_EQ(0, rect.top); |
| 38 | EXPECT_EQ(0, rect.left); |
| 39 | EXPECT_EQ(100, rect.right); |
| 40 | EXPECT_EQ(200, rect.bottom); |
| 41 | EXPECT_EQ(100, rect.getWidth()); |
| 42 | EXPECT_EQ(200, rect.getHeight()); |
| 43 | } |
| 44 | |
| 45 | TEST(RectTest, constructFromSize) { |
| 46 | const Rect rect(Size(100, 200)); |
| 47 | EXPECT_TRUE(rect.isValid()); |
| 48 | EXPECT_FALSE(rect.isEmpty()); |
| 49 | EXPECT_EQ(0, rect.top); |
| 50 | EXPECT_EQ(0, rect.left); |
| 51 | EXPECT_EQ(100, rect.right); |
| 52 | EXPECT_EQ(200, rect.bottom); |
| 53 | EXPECT_EQ(100, rect.getWidth()); |
| 54 | EXPECT_EQ(200, rect.getHeight()); |
| 55 | } |
| 56 | |
| 57 | TEST(RectTest, constructFromLTRB) { |
| 58 | const Rect rect(11, 12, 14, 14); |
| 59 | EXPECT_TRUE(rect.isValid()); |
| 60 | EXPECT_FALSE(rect.isEmpty()); |
| 61 | EXPECT_EQ(11, rect.left); |
| 62 | EXPECT_EQ(12, rect.top); |
| 63 | EXPECT_EQ(14, rect.right); |
| 64 | EXPECT_EQ(14, rect.bottom); |
| 65 | EXPECT_EQ(3, rect.getWidth()); |
| 66 | EXPECT_EQ(2, rect.getHeight()); |
| 67 | } |
| 68 | |
| 69 | TEST(RectTest, constructFromPoints) { |
| 70 | const Rect rect(Point(11, 12), Point(14, 14)); |
| 71 | EXPECT_TRUE(rect.isValid()); |
| 72 | EXPECT_FALSE(rect.isEmpty()); |
| 73 | EXPECT_EQ(11, rect.left); |
| 74 | EXPECT_EQ(12, rect.top); |
| 75 | EXPECT_EQ(14, rect.right); |
| 76 | EXPECT_EQ(14, rect.bottom); |
| 77 | EXPECT_EQ(3, rect.getWidth()); |
| 78 | EXPECT_EQ(2, rect.getHeight()); |
| 79 | } |
| 80 | |
| 81 | TEST(RectTest, constructFromFloatRect) { |
| 82 | { |
| 83 | const Rect rect(FloatRect(10, 20, 30, 40)); |
| 84 | EXPECT_TRUE(rect.isValid()); |
| 85 | EXPECT_FALSE(rect.isEmpty()); |
| 86 | EXPECT_EQ(10, rect.left); |
| 87 | EXPECT_EQ(20, rect.top); |
| 88 | EXPECT_EQ(30, rect.right); |
| 89 | EXPECT_EQ(40, rect.bottom); |
| 90 | } |
| 91 | // Construct with floating point error |
| 92 | { |
| 93 | constexpr float kError = 1e-3; |
| 94 | const Rect rect(FloatRect(10 - kError, 20 - kError, 30 - kError, 40 - kError)); |
| 95 | EXPECT_TRUE(rect.isValid()); |
| 96 | EXPECT_FALSE(rect.isEmpty()); |
| 97 | EXPECT_EQ(10, rect.left); |
| 98 | EXPECT_EQ(20, rect.top); |
| 99 | EXPECT_EQ(30, rect.right); |
| 100 | EXPECT_EQ(40, rect.bottom); |
| 101 | } |
Vishnu Nair | dfee5a2 | 2024-10-03 03:42:49 +0000 | [diff] [blame^] | 102 | |
| 103 | EXPECT_EQ(Rect(0, 1, -1, 0), Rect(FloatRect(0.f, 1.f, -1.f, 0.f))); |
| 104 | EXPECT_EQ(Rect(100000, 100000, -100000, -100000), |
| 105 | Rect(FloatRect(100000.f, 100000.f, -100000.f, -100000.f))); |
| 106 | |
| 107 | // round down if < .5 |
| 108 | EXPECT_EQ(Rect(0, 1, -1, 0), Rect(FloatRect(0.4f, 1.1f, -1.499f, 0.1f))); |
| 109 | |
| 110 | // round up if >= .5 |
| 111 | EXPECT_EQ(Rect(20, 20, -20, -20), Rect(FloatRect(19.5f, 19.9f, -19.5f, -19.9f))); |
Marin Shalamanov | 4aa3af1 | 2020-09-25 14:20:58 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST(RectTest, makeInvalid) { |
| 115 | Rect rect(10, 20, 60, 60); |
| 116 | EXPECT_TRUE(rect.isValid()); |
| 117 | rect.makeInvalid(); |
| 118 | EXPECT_FALSE(rect.isValid()); |
| 119 | } |
| 120 | |
| 121 | TEST(RectTest, clear) { |
| 122 | Rect rect(10, 20, 60, 60); |
| 123 | EXPECT_FALSE(rect.isEmpty()); |
| 124 | rect.clear(); |
| 125 | EXPECT_TRUE(rect.isEmpty()); |
| 126 | } |
| 127 | |
| 128 | TEST(RectTest, getSize) { |
| 129 | const Rect rect(10, 20, 60, 60); |
| 130 | EXPECT_EQ(Size(50, 40), rect.getSize()); |
| 131 | } |
| 132 | |
| 133 | TEST(RectTest, getBounds) { |
| 134 | const Rect rect(10, 20, 60, 60); |
| 135 | const Rect bounds = rect.getBounds(); |
| 136 | EXPECT_EQ(0, bounds.left); |
| 137 | EXPECT_EQ(0, bounds.top); |
| 138 | EXPECT_EQ(50, bounds.right); |
| 139 | EXPECT_EQ(40, bounds.bottom); |
| 140 | EXPECT_EQ(rect.getSize(), bounds.getSize()); |
| 141 | } |
| 142 | |
| 143 | TEST(RectTest, getCornerPoints) { |
| 144 | const Rect rect(10, 20, 50, 60); |
| 145 | EXPECT_EQ(Point(10, 20), rect.leftTop()); |
| 146 | EXPECT_EQ(Point(10, 60), rect.leftBottom()); |
| 147 | EXPECT_EQ(Point(50, 20), rect.rightTop()); |
| 148 | EXPECT_EQ(Point(50, 60), rect.rightBottom()); |
| 149 | } |
| 150 | |
| 151 | TEST(RectTest, operatorEquals) { |
| 152 | const Rect rect(10, 20, 50, 60); |
| 153 | EXPECT_EQ(rect, rect); |
| 154 | EXPECT_NE(Rect(0, 20, 50, 60), rect); |
| 155 | EXPECT_NE(Rect(10, 0, 50, 60), rect); |
| 156 | EXPECT_NE(Rect(10, 20, 0, 60), rect); |
| 157 | EXPECT_NE(Rect(10, 20, 50, 0), rect); |
| 158 | } |
| 159 | |
| 160 | TEST(RectTest, operatorsPlusMinus) { |
| 161 | Rect rect = Rect(10, 20, 50, 60) + Point(1, 2); |
| 162 | EXPECT_EQ(Rect(11, 22, 51, 62), rect); |
| 163 | rect -= Point(1, 2); |
| 164 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); |
| 165 | |
| 166 | rect = Rect(10, 20, 50, 60) - Point(1, 2); |
| 167 | EXPECT_EQ(Rect(9, 18, 49, 58), rect); |
| 168 | rect += Point(1, 2); |
| 169 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); |
| 170 | } |
| 171 | |
| 172 | TEST(RectTest, scale) { |
| 173 | Rect rect(10, 20, 50, 60); |
| 174 | EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f, 3.f)); |
| 175 | rect.scaleSelf(2.f, 3.f); |
| 176 | EXPECT_EQ(Rect(20, 60, 100, 180), rect); |
| 177 | |
| 178 | rect = Rect(10, 20, 50, 60); |
| 179 | constexpr float kError = 1e-3; |
| 180 | EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f - kError, 3.f - kError)); |
| 181 | rect.scaleSelf(2.f - kError, 3.f - kError); |
| 182 | EXPECT_EQ(Rect(20, 60, 100, 180), rect); |
| 183 | } |
| 184 | |
| 185 | TEST(RectTest, inset) { |
| 186 | Rect rect(10, 20, 50, 60); |
| 187 | rect.inset(0, 0, 0, 0); |
| 188 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); |
| 189 | rect.inset(1, 2, 3, 4); |
| 190 | EXPECT_EQ(Rect(11, 22, 47, 56), rect); |
| 191 | } |
| 192 | |
| 193 | TEST(RectTest, intersect) { |
| 194 | const Rect rect(10, 20, 50, 60); |
| 195 | Rect intersection; |
| 196 | |
| 197 | // Intersect with self is self |
| 198 | intersection.makeInvalid(); |
| 199 | EXPECT_TRUE(rect.intersect(rect, &intersection)); |
| 200 | EXPECT_EQ(Rect(10, 20, 50, 60), intersection); |
| 201 | |
| 202 | // Intersect with rect contained in us |
| 203 | const Rect insideRect(11, 21, 45, 55); |
| 204 | intersection.makeInvalid(); |
| 205 | EXPECT_TRUE(rect.intersect(insideRect, &intersection)); |
| 206 | EXPECT_EQ(insideRect, intersection); |
| 207 | |
| 208 | // Intersect with rect we are contained in |
| 209 | intersection.makeInvalid(); |
| 210 | EXPECT_TRUE(insideRect.intersect(rect, &intersection)); |
| 211 | EXPECT_EQ(insideRect, intersection); |
| 212 | |
| 213 | // Empty intersection |
| 214 | intersection.makeInvalid(); |
| 215 | EXPECT_FALSE(rect.intersect(Rect(100, 202, 150, 260), &intersection)); |
| 216 | EXPECT_TRUE(intersection.isEmpty()); |
| 217 | |
| 218 | // Partial intersection |
| 219 | const Rect other(30, 40, 70, 80); |
| 220 | intersection.makeInvalid(); |
| 221 | EXPECT_TRUE(rect.intersect(other, &intersection)); |
| 222 | EXPECT_EQ(Rect(30, 40, 50, 60), intersection); |
| 223 | |
| 224 | // Intersetion is commutative |
| 225 | intersection.makeInvalid(); |
| 226 | EXPECT_TRUE(other.intersect(rect, &intersection)); |
| 227 | EXPECT_EQ(Rect(30, 40, 50, 60), intersection); |
| 228 | } |
| 229 | |
| 230 | TEST(RectTest, reduce) { |
| 231 | const Rect rect(10, 20, 50, 60); |
| 232 | |
| 233 | // Reduce with self is empty |
| 234 | EXPECT_TRUE(rect.reduce(rect).isEmpty()); |
| 235 | |
| 236 | // Reduce with rect entirely inside is a noop |
| 237 | const Rect insideRect(11, 21, 45, 55); |
| 238 | EXPECT_EQ(rect, rect.reduce(insideRect)); |
| 239 | |
| 240 | // Reduce with rect entirely outside is empty |
| 241 | EXPECT_TRUE(insideRect.reduce(rect).isEmpty()); |
| 242 | |
| 243 | // Reduce with rect on the right |
| 244 | EXPECT_EQ(Rect(10, 20, 20, 60), rect.reduce(Rect(20, 0, 60, 70))); |
| 245 | |
| 246 | // Reduce with rect on the left |
| 247 | EXPECT_EQ(Rect(40, 20, 50, 60), rect.reduce(Rect(0, 0, 40, 70))); |
| 248 | |
| 249 | // Reduce with rect at the top |
| 250 | EXPECT_EQ(Rect(10, 40, 50, 60), rect.reduce(Rect(0, 0, 70, 40))); |
| 251 | |
| 252 | // Reduce with rect at the bottom |
| 253 | EXPECT_EQ(Rect(10, 20, 50, 40), rect.reduce(Rect(0, 40, 70, 70))); |
| 254 | } |
| 255 | |
| 256 | TEST(RectTest, transform) { |
| 257 | const int32_t width = 100, height = 200; |
| 258 | const Rect rect(1, 1, 2, 3); |
| 259 | EXPECT_EQ(Rect(98, 1, 99, 3), rect.transform(HAL_TRANSFORM_FLIP_H, width, height)); |
| 260 | EXPECT_EQ(Rect(1, 197, 2, 199), rect.transform(HAL_TRANSFORM_FLIP_V, width, height)); |
| 261 | EXPECT_EQ(Rect(197, 1, 199, 2), rect.transform(HAL_TRANSFORM_ROT_90, width, height)); |
| 262 | EXPECT_EQ(Rect(98, 197, 99, 199), rect.transform(HAL_TRANSFORM_ROT_180, width, height)); |
| 263 | EXPECT_EQ(Rect(1, 98, 3, 99), rect.transform(HAL_TRANSFORM_ROT_270, width, height)); |
| 264 | } |
| 265 | |
| 266 | TEST(RectTest, toFloatRect) { |
| 267 | const Rect rect(10, 20, 50, 60); |
| 268 | const FloatRect floatRect = rect.toFloatRect(); |
| 269 | EXPECT_EQ(FloatRect(10.f, 20.f, 50.f, 60.f), floatRect); |
| 270 | } |
| 271 | |
Dan Stoza | ce58d5b | 2020-09-29 15:29:37 -0700 | [diff] [blame] | 272 | TEST(RectTest, RectHash) { |
| 273 | const std::vector<Rect> rects = { |
| 274 | Rect(10, 20, 50, 60), Rect(11, 20, 50, 60), Rect(11, 21, 50, 60), |
| 275 | Rect(11, 21, 51, 60), Rect(11, 21, 51, 61), |
| 276 | }; |
| 277 | |
| 278 | for (const auto& a : rects) { |
| 279 | for (const auto& b : rects) { |
| 280 | const bool hashEq = std::hash<Rect>{}(a) == std::hash<Rect>{}(b); |
| 281 | EXPECT_EQ(a == b, hashEq); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | TEST(RectTest, FloatRectHash) { |
| 287 | const std::vector<FloatRect> floatRects = { |
| 288 | Rect(10, 20, 50, 60).toFloatRect(), Rect(11, 20, 50, 60).toFloatRect(), |
| 289 | Rect(11, 21, 50, 60).toFloatRect(), Rect(11, 21, 51, 60).toFloatRect(), |
| 290 | Rect(11, 21, 51, 61).toFloatRect(), |
| 291 | }; |
| 292 | |
| 293 | for (const auto& a : floatRects) { |
| 294 | for (const auto& b : floatRects) { |
| 295 | const bool hashEq = std::hash<FloatRect>{}(a) == std::hash<FloatRect>{}(b); |
| 296 | EXPECT_EQ(a == b, hashEq); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
Marin Shalamanov | 4aa3af1 | 2020-09-25 14:20:58 +0200 | [diff] [blame] | 301 | } // namespace android::ui |