OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 #include "ui/gfx/rect.h" | 7 #include "ui/gfx/rect.h" |
8 #include "ui/gfx/rect_conversions.h" | 8 #include "ui/gfx/rect_conversions.h" |
9 #include "ui/gfx/skia_util.h" | 9 #include "ui/gfx/skia_util.h" |
10 | 10 |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); | 158 gfx::Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3); |
159 gfx::Rect u = r1.Union(r2); | 159 gfx::Rect u = r1.Union(r2); |
160 EXPECT_EQ(r3.x(), u.x()); | 160 EXPECT_EQ(r3.x(), u.x()); |
161 EXPECT_EQ(r3.y(), u.y()); | 161 EXPECT_EQ(r3.y(), u.y()); |
162 EXPECT_EQ(r3.width(), u.width()); | 162 EXPECT_EQ(r3.width(), u.width()); |
163 EXPECT_EQ(r3.height(), u.height()); | 163 EXPECT_EQ(r3.height(), u.height()); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
167 TEST(RectTest, Equals) { | 167 TEST(RectTest, Equals) { |
168 ASSERT_TRUE(gfx::Rect(0, 0, 0, 0).Equals(gfx::Rect(0, 0, 0, 0))); | 168 ASSERT_TRUE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 0, 0, 0)); |
169 ASSERT_TRUE(gfx::Rect(1, 2, 3, 4).Equals(gfx::Rect(1, 2, 3, 4))); | 169 ASSERT_TRUE(gfx::Rect(1, 2, 3, 4) == gfx::Rect(1, 2, 3, 4)); |
170 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0).Equals(gfx::Rect(0, 0, 0, 1))); | 170 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 0, 0, 1)); |
171 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0).Equals(gfx::Rect(0, 0, 1, 0))); | 171 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 0, 1, 0)); |
172 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0).Equals(gfx::Rect(0, 1, 0, 0))); | 172 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(0, 1, 0, 0)); |
173 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0).Equals(gfx::Rect(1, 0, 0, 0))); | 173 ASSERT_FALSE(gfx::Rect(0, 0, 0, 0) == gfx::Rect(1, 0, 0, 0)); |
174 } | 174 } |
175 | 175 |
176 TEST(RectTest, AdjustToFit) { | 176 TEST(RectTest, AdjustToFit) { |
177 static const struct Test { | 177 static const struct Test { |
178 int x1; // source | 178 int x1; // source |
179 int y1; | 179 int y1; |
180 int w1; | 180 int w1; |
181 int h1; | 181 int h1; |
182 int x2; // target | 182 int x2; // target |
183 int y2; | 183 int y2; |
(...skipping 29 matching lines...) Expand all Loading... |
213 EXPECT_EQ(r3.y(), u.y()); | 213 EXPECT_EQ(r3.y(), u.y()); |
214 EXPECT_EQ(r3.width(), u.width()); | 214 EXPECT_EQ(r3.width(), u.width()); |
215 EXPECT_EQ(r3.height(), u.height()); | 215 EXPECT_EQ(r3.height(), u.height()); |
216 } | 216 } |
217 } | 217 } |
218 | 218 |
219 TEST(RectTest, Subtract) { | 219 TEST(RectTest, Subtract) { |
220 // Matching | 220 // Matching |
221 EXPECT_TRUE( | 221 EXPECT_TRUE( |
222 gfx::Rect(10, 10, 20, 20).Subtract( | 222 gfx::Rect(10, 10, 20, 20).Subtract( |
223 gfx::Rect(10, 10, 20, 20)).Equals( | 223 gfx::Rect(10, 10, 20, 20)) == |
224 gfx::Rect(0, 0, 0, 0))); | 224 gfx::Rect(0, 0, 0, 0)); |
225 | 225 |
226 // Contains | 226 // Contains |
227 EXPECT_TRUE( | 227 EXPECT_TRUE( |
228 gfx::Rect(10, 10, 20, 20).Subtract( | 228 gfx::Rect(10, 10, 20, 20).Subtract( |
229 gfx::Rect(5, 5, 30, 30)).Equals( | 229 gfx::Rect(5, 5, 30, 30)) == |
230 gfx::Rect(0, 0, 0, 0))); | 230 gfx::Rect(0, 0, 0, 0)); |
231 | 231 |
232 // No intersection | 232 // No intersection |
233 EXPECT_TRUE( | 233 EXPECT_TRUE( |
234 gfx::Rect(10, 10, 20, 20).Subtract( | 234 gfx::Rect(10, 10, 20, 20).Subtract( |
235 gfx::Rect(30, 30, 20, 20)).Equals( | 235 gfx::Rect(30, 30, 20, 20)) == |
236 gfx::Rect(10, 10, 20, 20))); | 236 gfx::Rect(10, 10, 20, 20)); |
237 | 237 |
238 // Not a complete intersection in either direction | 238 // Not a complete intersection in either direction |
239 EXPECT_TRUE( | 239 EXPECT_TRUE( |
240 gfx::Rect(10, 10, 20, 20).Subtract( | 240 gfx::Rect(10, 10, 20, 20).Subtract( |
241 gfx::Rect(15, 15, 20, 20)).Equals( | 241 gfx::Rect(15, 15, 20, 20)) == |
242 gfx::Rect(10, 10, 20, 20))); | 242 gfx::Rect(10, 10, 20, 20)); |
243 | 243 |
244 // Complete intersection in the x-direction | 244 // Complete intersection in the x-direction |
245 EXPECT_TRUE( | 245 EXPECT_TRUE( |
246 gfx::Rect(10, 10, 20, 20).Subtract( | 246 gfx::Rect(10, 10, 20, 20).Subtract( |
247 gfx::Rect(10, 15, 20, 20)).Equals( | 247 gfx::Rect(10, 15, 20, 20)) == |
248 gfx::Rect(10, 10, 20, 5))); | 248 gfx::Rect(10, 10, 20, 5)); |
249 | 249 |
250 // Complete intersection in the x-direction | 250 // Complete intersection in the x-direction |
251 EXPECT_TRUE( | 251 EXPECT_TRUE( |
252 gfx::Rect(10, 10, 20, 20).Subtract( | 252 gfx::Rect(10, 10, 20, 20).Subtract( |
253 gfx::Rect(5, 15, 30, 20)).Equals( | 253 gfx::Rect(5, 15, 30, 20)) == |
254 gfx::Rect(10, 10, 20, 5))); | 254 gfx::Rect(10, 10, 20, 5)); |
255 | 255 |
256 // Complete intersection in the x-direction | 256 // Complete intersection in the x-direction |
257 EXPECT_TRUE( | 257 EXPECT_TRUE( |
258 gfx::Rect(10, 10, 20, 20).Subtract( | 258 gfx::Rect(10, 10, 20, 20).Subtract( |
259 gfx::Rect(5, 5, 30, 20)).Equals( | 259 gfx::Rect(5, 5, 30, 20)) == |
260 gfx::Rect(10, 25, 20, 5))); | 260 gfx::Rect(10, 25, 20, 5)); |
261 | 261 |
262 // Complete intersection in the y-direction | 262 // Complete intersection in the y-direction |
263 EXPECT_TRUE( | 263 EXPECT_TRUE( |
264 gfx::Rect(10, 10, 20, 20).Subtract( | 264 gfx::Rect(10, 10, 20, 20).Subtract( |
265 gfx::Rect(10, 10, 10, 30)).Equals( | 265 gfx::Rect(10, 10, 10, 30)) == |
266 gfx::Rect(20, 10, 10, 20))); | 266 gfx::Rect(20, 10, 10, 20)); |
267 | 267 |
268 // Complete intersection in the y-direction | 268 // Complete intersection in the y-direction |
269 EXPECT_TRUE( | 269 EXPECT_TRUE( |
270 gfx::Rect(10, 10, 20, 20).Subtract( | 270 gfx::Rect(10, 10, 20, 20).Subtract( |
271 gfx::Rect(5, 5, 20, 30)).Equals( | 271 gfx::Rect(5, 5, 20, 30)) == |
272 gfx::Rect(25, 10, 5, 20))); | 272 gfx::Rect(25, 10, 5, 20)); |
273 } | 273 } |
274 | 274 |
275 TEST(RectTest, IsEmpty) { | 275 TEST(RectTest, IsEmpty) { |
276 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).IsEmpty()); | 276 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).IsEmpty()); |
277 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).size().IsEmpty()); | 277 EXPECT_TRUE(gfx::Rect(0, 0, 0, 0).size().IsEmpty()); |
278 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).IsEmpty()); | 278 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).IsEmpty()); |
279 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).size().IsEmpty()); | 279 EXPECT_TRUE(gfx::Rect(0, 0, 10, 0).size().IsEmpty()); |
280 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).IsEmpty()); | 280 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).IsEmpty()); |
281 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).size().IsEmpty()); | 281 EXPECT_TRUE(gfx::Rect(0, 0, 0, 10).size().IsEmpty()); |
282 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).IsEmpty()); | 282 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).IsEmpty()); |
283 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).size().IsEmpty()); | 283 EXPECT_FALSE(gfx::Rect(0, 0, 10, 10).size().IsEmpty()); |
284 } | 284 } |
285 | 285 |
286 TEST(RectTest, SplitVertically) { | 286 TEST(RectTest, SplitVertically) { |
287 gfx::Rect left_half, right_half; | 287 gfx::Rect left_half, right_half; |
288 | 288 |
289 // Splitting when origin is (0, 0). | 289 // Splitting when origin is (0, 0). |
290 gfx::Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half); | 290 gfx::Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half); |
291 EXPECT_TRUE(left_half.Equals(gfx::Rect(0, 0, 10, 20))); | 291 EXPECT_TRUE(left_half == gfx::Rect(0, 0, 10, 20)); |
292 EXPECT_TRUE(right_half.Equals(gfx::Rect(10, 0, 10, 20))); | 292 EXPECT_TRUE(right_half == gfx::Rect(10, 0, 10, 20)); |
293 | 293 |
294 // Splitting when origin is arbitrary. | 294 // Splitting when origin is arbitrary. |
295 gfx::Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half); | 295 gfx::Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half); |
296 EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 10, 10))); | 296 EXPECT_TRUE(left_half == gfx::Rect(10, 10, 10, 10)); |
297 EXPECT_TRUE(right_half.Equals(gfx::Rect(20, 10, 10, 10))); | 297 EXPECT_TRUE(right_half == gfx::Rect(20, 10, 10, 10)); |
298 | 298 |
299 // Splitting a rectangle of zero width. | 299 // Splitting a rectangle of zero width. |
300 gfx::Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half); | 300 gfx::Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half); |
301 EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 0, 10))); | 301 EXPECT_TRUE(left_half == gfx::Rect(10, 10, 0, 10)); |
302 EXPECT_TRUE(right_half.Equals(gfx::Rect(10, 10, 0, 10))); | 302 EXPECT_TRUE(right_half == gfx::Rect(10, 10, 0, 10)); |
303 | 303 |
304 // Splitting a rectangle of odd width. | 304 // Splitting a rectangle of odd width. |
305 gfx::Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half); | 305 gfx::Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half); |
306 EXPECT_TRUE(left_half.Equals(gfx::Rect(10, 10, 2, 10))); | 306 EXPECT_TRUE(left_half == gfx::Rect(10, 10, 2, 10)); |
307 EXPECT_TRUE(right_half.Equals(gfx::Rect(12, 10, 3, 10))); | 307 EXPECT_TRUE(right_half == gfx::Rect(12, 10, 3, 10)); |
308 } | 308 } |
309 | 309 |
310 TEST(RectTest, CenterPoint) { | 310 TEST(RectTest, CenterPoint) { |
311 gfx::Point center; | 311 gfx::Point center; |
312 | 312 |
313 // When origin is (0, 0). | 313 // When origin is (0, 0). |
314 center = gfx::Rect(0, 0, 20, 20).CenterPoint(); | 314 center = gfx::Rect(0, 0, 20, 20).CenterPoint(); |
315 EXPECT_TRUE(center == gfx::Point(10, 10)); | 315 EXPECT_TRUE(center == gfx::Point(10, 10)); |
316 | 316 |
317 // When origin is even. | 317 // When origin is even. |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 560 |
561 #if defined(OS_WIN) | 561 #if defined(OS_WIN) |
562 TEST(RectTest, ConstructAndAssign) { | 562 TEST(RectTest, ConstructAndAssign) { |
563 const RECT rect_1 = { 0, 0, 10, 10 }; | 563 const RECT rect_1 = { 0, 0, 10, 10 }; |
564 const RECT rect_2 = { 0, 0, -10, -10 }; | 564 const RECT rect_2 = { 0, 0, -10, -10 }; |
565 gfx::Rect test1(rect_1); | 565 gfx::Rect test1(rect_1); |
566 gfx::Rect test2(rect_2); | 566 gfx::Rect test2(rect_2); |
567 } | 567 } |
568 #endif | 568 #endif |
569 | 569 |
| 570 TEST(RectTest, ToRectF) { |
| 571 // Check that implicit conversion from integer to float compiles. |
| 572 gfx::Rect a(10, 20, 30, 40); |
| 573 gfx::RectF b(10, 20, 30, 40); |
| 574 |
| 575 gfx::RectF intersect = b.Intersect(a); |
| 576 EXPECT_EQ(b.ToString(), intersect.ToString()); |
| 577 |
| 578 bool equals = a == b; |
| 579 EXPECT_EQ(true, equals); |
| 580 |
| 581 equals = b == a; |
| 582 EXPECT_EQ(true, equals); |
| 583 } |
| 584 |
570 } // namespace ui | 585 } // namespace ui |
OLD | NEW |