| 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 | } | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | TEST(RectTest, makeInvalid) { | 
|  | 105 | Rect rect(10, 20, 60, 60); | 
|  | 106 | EXPECT_TRUE(rect.isValid()); | 
|  | 107 | rect.makeInvalid(); | 
|  | 108 | EXPECT_FALSE(rect.isValid()); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | TEST(RectTest, clear) { | 
|  | 112 | Rect rect(10, 20, 60, 60); | 
|  | 113 | EXPECT_FALSE(rect.isEmpty()); | 
|  | 114 | rect.clear(); | 
|  | 115 | EXPECT_TRUE(rect.isEmpty()); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | TEST(RectTest, getSize) { | 
|  | 119 | const Rect rect(10, 20, 60, 60); | 
|  | 120 | EXPECT_EQ(Size(50, 40), rect.getSize()); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | TEST(RectTest, getBounds) { | 
|  | 124 | const Rect rect(10, 20, 60, 60); | 
|  | 125 | const Rect bounds = rect.getBounds(); | 
|  | 126 | EXPECT_EQ(0, bounds.left); | 
|  | 127 | EXPECT_EQ(0, bounds.top); | 
|  | 128 | EXPECT_EQ(50, bounds.right); | 
|  | 129 | EXPECT_EQ(40, bounds.bottom); | 
|  | 130 | EXPECT_EQ(rect.getSize(), bounds.getSize()); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | TEST(RectTest, getCornerPoints) { | 
|  | 134 | const Rect rect(10, 20, 50, 60); | 
|  | 135 | EXPECT_EQ(Point(10, 20), rect.leftTop()); | 
|  | 136 | EXPECT_EQ(Point(10, 60), rect.leftBottom()); | 
|  | 137 | EXPECT_EQ(Point(50, 20), rect.rightTop()); | 
|  | 138 | EXPECT_EQ(Point(50, 60), rect.rightBottom()); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | TEST(RectTest, operatorEquals) { | 
|  | 142 | const Rect rect(10, 20, 50, 60); | 
|  | 143 | EXPECT_EQ(rect, rect); | 
|  | 144 | EXPECT_NE(Rect(0, 20, 50, 60), rect); | 
|  | 145 | EXPECT_NE(Rect(10, 0, 50, 60), rect); | 
|  | 146 | EXPECT_NE(Rect(10, 20, 0, 60), rect); | 
|  | 147 | EXPECT_NE(Rect(10, 20, 50, 0), rect); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | TEST(RectTest, operatorsPlusMinus) { | 
|  | 151 | Rect rect = Rect(10, 20, 50, 60) + Point(1, 2); | 
|  | 152 | EXPECT_EQ(Rect(11, 22, 51, 62), rect); | 
|  | 153 | rect -= Point(1, 2); | 
|  | 154 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); | 
|  | 155 |  | 
|  | 156 | rect = Rect(10, 20, 50, 60) - Point(1, 2); | 
|  | 157 | EXPECT_EQ(Rect(9, 18, 49, 58), rect); | 
|  | 158 | rect += Point(1, 2); | 
|  | 159 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | TEST(RectTest, scale) { | 
|  | 163 | Rect rect(10, 20, 50, 60); | 
|  | 164 | EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f, 3.f)); | 
|  | 165 | rect.scaleSelf(2.f, 3.f); | 
|  | 166 | EXPECT_EQ(Rect(20, 60, 100, 180), rect); | 
|  | 167 |  | 
|  | 168 | rect = Rect(10, 20, 50, 60); | 
|  | 169 | constexpr float kError = 1e-3; | 
|  | 170 | EXPECT_EQ(Rect(20, 60, 100, 180), rect.scale(2.f - kError, 3.f - kError)); | 
|  | 171 | rect.scaleSelf(2.f - kError, 3.f - kError); | 
|  | 172 | EXPECT_EQ(Rect(20, 60, 100, 180), rect); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | TEST(RectTest, inset) { | 
|  | 176 | Rect rect(10, 20, 50, 60); | 
|  | 177 | rect.inset(0, 0, 0, 0); | 
|  | 178 | EXPECT_EQ(Rect(10, 20, 50, 60), rect); | 
|  | 179 | rect.inset(1, 2, 3, 4); | 
|  | 180 | EXPECT_EQ(Rect(11, 22, 47, 56), rect); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | TEST(RectTest, intersect) { | 
|  | 184 | const Rect rect(10, 20, 50, 60); | 
|  | 185 | Rect intersection; | 
|  | 186 |  | 
|  | 187 | // Intersect with self is self | 
|  | 188 | intersection.makeInvalid(); | 
|  | 189 | EXPECT_TRUE(rect.intersect(rect, &intersection)); | 
|  | 190 | EXPECT_EQ(Rect(10, 20, 50, 60), intersection); | 
|  | 191 |  | 
|  | 192 | // Intersect with rect contained in us | 
|  | 193 | const Rect insideRect(11, 21, 45, 55); | 
|  | 194 | intersection.makeInvalid(); | 
|  | 195 | EXPECT_TRUE(rect.intersect(insideRect, &intersection)); | 
|  | 196 | EXPECT_EQ(insideRect, intersection); | 
|  | 197 |  | 
|  | 198 | // Intersect with rect we are contained in | 
|  | 199 | intersection.makeInvalid(); | 
|  | 200 | EXPECT_TRUE(insideRect.intersect(rect, &intersection)); | 
|  | 201 | EXPECT_EQ(insideRect, intersection); | 
|  | 202 |  | 
|  | 203 | // Empty intersection | 
|  | 204 | intersection.makeInvalid(); | 
|  | 205 | EXPECT_FALSE(rect.intersect(Rect(100, 202, 150, 260), &intersection)); | 
|  | 206 | EXPECT_TRUE(intersection.isEmpty()); | 
|  | 207 |  | 
|  | 208 | // Partial intersection | 
|  | 209 | const Rect other(30, 40, 70, 80); | 
|  | 210 | intersection.makeInvalid(); | 
|  | 211 | EXPECT_TRUE(rect.intersect(other, &intersection)); | 
|  | 212 | EXPECT_EQ(Rect(30, 40, 50, 60), intersection); | 
|  | 213 |  | 
|  | 214 | // Intersetion is commutative | 
|  | 215 | intersection.makeInvalid(); | 
|  | 216 | EXPECT_TRUE(other.intersect(rect, &intersection)); | 
|  | 217 | EXPECT_EQ(Rect(30, 40, 50, 60), intersection); | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | TEST(RectTest, reduce) { | 
|  | 221 | const Rect rect(10, 20, 50, 60); | 
|  | 222 |  | 
|  | 223 | // Reduce with self is empty | 
|  | 224 | EXPECT_TRUE(rect.reduce(rect).isEmpty()); | 
|  | 225 |  | 
|  | 226 | // Reduce with rect entirely inside is a noop | 
|  | 227 | const Rect insideRect(11, 21, 45, 55); | 
|  | 228 | EXPECT_EQ(rect, rect.reduce(insideRect)); | 
|  | 229 |  | 
|  | 230 | // Reduce with rect entirely outside is empty | 
|  | 231 | EXPECT_TRUE(insideRect.reduce(rect).isEmpty()); | 
|  | 232 |  | 
|  | 233 | // Reduce with rect on the right | 
|  | 234 | EXPECT_EQ(Rect(10, 20, 20, 60), rect.reduce(Rect(20, 0, 60, 70))); | 
|  | 235 |  | 
|  | 236 | // Reduce with rect on the left | 
|  | 237 | EXPECT_EQ(Rect(40, 20, 50, 60), rect.reduce(Rect(0, 0, 40, 70))); | 
|  | 238 |  | 
|  | 239 | // Reduce with rect at the top | 
|  | 240 | EXPECT_EQ(Rect(10, 40, 50, 60), rect.reduce(Rect(0, 0, 70, 40))); | 
|  | 241 |  | 
|  | 242 | // Reduce with rect at the bottom | 
|  | 243 | EXPECT_EQ(Rect(10, 20, 50, 40), rect.reduce(Rect(0, 40, 70, 70))); | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | TEST(RectTest, transform) { | 
|  | 247 | const int32_t width = 100, height = 200; | 
|  | 248 | const Rect rect(1, 1, 2, 3); | 
|  | 249 | EXPECT_EQ(Rect(98, 1, 99, 3), rect.transform(HAL_TRANSFORM_FLIP_H, width, height)); | 
|  | 250 | EXPECT_EQ(Rect(1, 197, 2, 199), rect.transform(HAL_TRANSFORM_FLIP_V, width, height)); | 
|  | 251 | EXPECT_EQ(Rect(197, 1, 199, 2), rect.transform(HAL_TRANSFORM_ROT_90, width, height)); | 
|  | 252 | EXPECT_EQ(Rect(98, 197, 99, 199), rect.transform(HAL_TRANSFORM_ROT_180, width, height)); | 
|  | 253 | EXPECT_EQ(Rect(1, 98, 3, 99), rect.transform(HAL_TRANSFORM_ROT_270, width, height)); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | TEST(RectTest, toFloatRect) { | 
|  | 257 | const Rect rect(10, 20, 50, 60); | 
|  | 258 | const FloatRect floatRect = rect.toFloatRect(); | 
|  | 259 | EXPECT_EQ(FloatRect(10.f, 20.f, 50.f, 60.f), floatRect); | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | } // namespace android::ui |