bug 812562 - click-to-play: reshow notification for blocklisted plugins r=jaws
[gecko.git] / gfx / tests / TestRect.cpp
blob7577f3a3770fa3a02b68d0d9bd8da7d203ad9576
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TestHarness.h"
8 #include "nsRect.h"
9 #ifdef XP_WIN
10 #include <windows.h>
11 #endif
13 template <class RectType>
14 static bool
15 TestConstructors()
17 // Create a rectangle
18 RectType rect1(10, 20, 30, 40);
20 // Make sure the rectangle was properly initialized
21 if ((rect1.x != 10) || (rect1.y != 20) ||
22 (rect1.width != 30) || (rect1.height != 40)) {
23 fail("[1] Make sure the rectangle was properly initialized with constructor");
24 return false;
27 // Create a second rect using the copy constructor
28 RectType rect2(rect1);
30 // Make sure the rectangle was properly initialized
31 if ((rect2.x != rect1.x) || (rect2.y != rect1.y) ||
32 (rect2.width != rect1.width) || (rect2.height != rect1.height)) {
33 fail("[2] Make sure the rectangle was properly initialized with copy constructor");
34 return false;
37 passed("TestConstructors");
38 return true;
41 template <class RectType>
42 static bool
43 TestEqualityOperator()
45 RectType rect1(10, 20, 30, 40);
46 RectType rect2(rect1);
48 // Test the equality operator
49 if (!(rect1 == rect2)) {
50 fail("[1] Test the equality operator");
51 return false;
54 // Test the inequality operator
55 if (rect1 != rect2) {
56 fail("[2] Test the inequality operator");
57 return false;
60 // Make sure that two empty rects are equal
61 rect1.Empty();
62 rect2.Empty();
63 if (!(rect1 == rect2)) {
64 fail("[3] Make sure that two empty rects are equal");
65 return false;
68 passed("TestEqualityOperator");
69 return true;
72 template <class RectType>
73 static bool
74 TestContainment()
76 RectType rect1(10, 10, 50, 50);
78 // Test the point containment methods
81 // Basic test of a point in the middle of the rect
82 if (!rect1.Contains(rect1.x + rect1.width/2, rect1.y + rect1.height/2)) {
83 fail("[1] Basic test of a point in the middle of the rect");
84 return false;
87 // Test against a point at the left/top edges
88 if (!rect1.Contains(rect1.x, rect1.y)) {
89 fail("[2] Test against a point at the left/top edges");
90 return false;
93 // Test against a point at the right/bottom extents
94 if (rect1.Contains(rect1.XMost(), rect1.YMost())) {
95 fail("[3] Test against a point at the right/bottom extents");
96 return false;
99 // Test the rect containment methods
101 RectType rect2(rect1);
103 // Test against a rect that's the same as rect1
104 if (!rect1.Contains(rect2)) {
105 fail("[4] Test against a rect that's the same as rect1");
106 return false;
109 // Test against a rect whose left edge (only) is outside of rect1
110 rect2.x--;
111 if (rect1.Contains(rect2)) {
112 fail("[5] Test against a rect whose left edge (only) is outside of rect1");
113 return false;
115 rect2.x++;
117 // Test against a rect whose top edge (only) is outside of rect1
118 rect2.y--;
119 if (rect1.Contains(rect2)) {
120 fail("[6] Test against a rect whose top edge (only) is outside of rect1");
121 return false;
123 rect2.y++;
125 // Test against a rect whose right edge (only) is outside of rect1
126 rect2.x++;
127 if (rect1.Contains(rect2)) {
128 fail("[7] Test against a rect whose right edge (only) is outside of rect1");
129 return false;
131 rect2.x--;
133 // Test against a rect whose bottom edge (only) is outside of rect1
134 rect2.y++;
135 if (rect1.Contains(rect2)) {
136 fail("[8] Test against a rect whose bottom edge (only) is outside of rect1");
137 return false;
139 rect2.y--;
141 passed("TestContainment");
142 return true;
145 // Test the method that returns a boolean result but doesn't return a
146 // a rectangle
147 template <class RectType>
148 static bool
149 TestIntersects()
151 RectType rect1(10, 10, 50, 50);
152 RectType rect2(rect1);
154 // Test against a rect that's the same as rect1
155 if (!rect1.Intersects(rect2)) {
156 fail("[1] Test against a rect that's the same as rect1");
157 return false;
160 // Test against a rect that's enclosed by rect1
161 rect2.Inflate(-1, -1);
162 if (!rect1.Contains(rect2) || !rect1.Intersects(rect2)) {
163 fail("[2] Test against a rect that's enclosed by rect1");
164 return false;
166 rect2.Inflate(1, 1);
168 // Make sure inflate and deflate worked correctly
169 if (rect1 != rect2) {
170 fail("[3] Make sure inflate and deflate worked correctly");
171 return false;
174 // Test against a rect that overlaps the left edge of rect1
175 rect2.x--;
176 if (!rect1.Intersects(rect2)) {
177 fail("[4] Test against a rect that overlaps the left edge of rect1");
178 return false;
180 rect2.x++;
182 // Test against a rect that's outside of rect1 on the left
183 rect2.x -= rect2.width;
184 if (rect1.Intersects(rect2)) {
185 fail("[5] Test against a rect that's outside of rect1 on the left");
186 return false;
188 rect2.x += rect2.width;
190 // Test against a rect that overlaps the top edge of rect1
191 rect2.y--;
192 if (!rect1.Intersects(rect2)) {
193 fail("[6] Test against a rect that overlaps the top edge of rect1");
194 return false;
196 rect2.y++;
198 // Test against a rect that's outside of rect1 on the top
199 rect2.y -= rect2.height;
200 if (rect1.Intersects(rect2)) {
201 fail("[7] Test against a rect that's outside of rect1 on the top");
202 return false;
204 rect2.y += rect2.height;
206 // Test against a rect that overlaps the right edge of rect1
207 rect2.x++;
208 if (!rect1.Intersects(rect2)) {
209 fail("[8] Test against a rect that overlaps the right edge of rect1");
210 return false;
212 rect2.x--;
214 // Test against a rect that's outside of rect1 on the right
215 rect2.x += rect2.width;
216 if (rect1.Intersects(rect2)) {
217 fail("[9] Test against a rect that's outside of rect1 on the right");
218 return false;
220 rect2.x -= rect2.width;
222 // Test against a rect that overlaps the bottom edge of rect1
223 rect2.y++;
224 if (!rect1.Intersects(rect2)) {
225 fail("[10] Test against a rect that overlaps the bottom edge of rect1");
226 return false;
228 rect2.y--;
230 // Test against a rect that's outside of rect1 on the bottom
231 rect2.y += rect2.height;
232 if (rect1.Intersects(rect2)) {
233 fail("[11] Test against a rect that's outside of rect1 on the bottom");
234 return false;
236 rect2.y -= rect2.height;
238 passed("TestIntersects");
239 return true;
242 // Test the method that returns a boolean result and an intersection rect
243 template <class RectType>
244 static bool
245 TestIntersection()
247 RectType rect1(10, 10, 50, 50);
248 RectType rect2(rect1);
249 RectType dest;
251 // Test against a rect that's the same as rect1
252 if (!dest.IntersectRect(rect1, rect2) || (dest != rect1)) {
253 fail("[1] Test against a rect that's the same as rect1");
254 return false;
257 // Test against a rect that's enclosed by rect1
258 rect2.Inflate(-1, -1);
259 if (!dest.IntersectRect(rect1, rect2) || (dest != rect2)) {
260 fail("[2] Test against a rect that's enclosed by rect1");
261 return false;
263 rect2.Inflate(1, 1);
265 // Test against a rect that overlaps the left edge of rect1
266 rect2.x--;
267 if (!dest.IntersectRect(rect1, rect2) ||
268 (dest != RectType(rect1.x, rect1.y, rect1.width - 1, rect1.height))) {
269 fail("[3] Test against a rect that overlaps the left edge of rect1");
270 return false;
272 rect2.x++;
274 // Test against a rect that's outside of rect1 on the left
275 rect2.x -= rect2.width;
276 if (dest.IntersectRect(rect1, rect2)) {
277 fail("[4] Test against a rect that's outside of rect1 on the left");
278 return false;
280 // Make sure an empty rect is returned
281 if (!dest.IsEmpty()) {
282 fail("[4] Make sure an empty rect is returned");
283 return false;
285 rect2.x += rect2.width;
287 // Test against a rect that overlaps the top edge of rect1
288 rect2.y--;
289 if (!dest.IntersectRect(rect1, rect2) ||
290 (dest != RectType(rect1.x, rect1.y, rect1.width, rect1.height - 1))) {
291 fail("[5] Test against a rect that overlaps the top edge of rect1");
292 return false;
294 rect2.y++;
296 // Test against a rect that's outside of rect1 on the top
297 rect2.y -= rect2.height;
298 if (dest.IntersectRect(rect1, rect2)) {
299 fail("[6] Test against a rect that's outside of rect1 on the top");
300 return false;
302 // Make sure an empty rect is returned
303 if (!dest.IsEmpty()) {
304 fail("[6] Make sure an empty rect is returned");
305 return false;
307 rect2.y += rect2.height;
309 // Test against a rect that overlaps the right edge of rect1
310 rect2.x++;
311 if (!dest.IntersectRect(rect1, rect2) ||
312 (dest != RectType(rect1.x + 1, rect1.y, rect1.width - 1, rect1.height))) {
313 fail("[7] Test against a rect that overlaps the right edge of rect1");
314 return false;
316 rect2.x--;
318 // Test against a rect that's outside of rect1 on the right
319 rect2.x += rect2.width;
320 if (dest.IntersectRect(rect1, rect2)) {
321 fail("[8] Test against a rect that's outside of rect1 on the right");
322 return false;
324 // Make sure an empty rect is returned
325 if (!dest.IsEmpty()) {
326 fail("[8] Make sure an empty rect is returned");
327 return false;
329 rect2.x -= rect2.width;
331 // Test against a rect that overlaps the bottom edge of rect1
332 rect2.y++;
333 if (!dest.IntersectRect(rect1, rect2) ||
334 (dest != RectType(rect1.x, rect1.y + 1, rect1.width, rect1.height - 1))) {
335 fail("[9] Test against a rect that overlaps the bottom edge of rect1");
336 return false;
338 rect2.y--;
340 // Test against a rect that's outside of rect1 on the bottom
341 rect2.y += rect2.height;
342 if (dest.IntersectRect(rect1, rect2)) {
343 fail("[10] Test against a rect that's outside of rect1 on the bottom");
344 return false;
346 // Make sure an empty rect is returned
347 if (!dest.IsEmpty()) {
348 fail("[10] Make sure an empty rect is returned");
349 return false;
351 rect2.y -= rect2.height;
353 // Test against a rect with zero width or height
354 rect1.SetRect(100, 100, 100, 100);
355 rect2.SetRect(150, 100, 0, 100);
356 if (dest.IntersectRect(rect1, rect2) || !dest.IsEmpty()) {
357 fail("[11] Intersection of rects with zero width or height should be empty");
358 return false;
361 // Tests against a rect with negative width or height
364 // Test against a rect with negative width
365 rect1.SetRect(100, 100, 100, 100);
366 rect2.SetRect(100, 100, -100, 100);
367 if (dest.IntersectRect(rect1, rect2) || !dest.IsEmpty()) {
368 fail("[12] Intersection of rects with negative width or height should be empty");
369 return false;
372 // Those two rects exactly overlap in some way...
373 // but we still want to return an empty rect
374 rect1.SetRect(100, 100, 100, 100);
375 rect2.SetRect(200, 200, -100, -100);
376 if (dest.IntersectRect(rect1, rect2) || !dest.IsEmpty()) {
377 fail("[13] Intersection of rects with negative width or height should be empty");
378 return false;
381 // Test against two identical rects with negative height
382 rect1.SetRect(100, 100, 100, -100);
383 rect2.SetRect(100, 100, 100, -100);
384 if (dest.IntersectRect(rect1, rect2) || !dest.IsEmpty()) {
385 fail("[14] Intersection of rects with negative width or height should be empty");
386 return false;
389 passed("TestIntersection");
390 return true;
393 template <class RectType>
394 static bool
395 TestUnion()
397 RectType rect1;
398 RectType rect2(10, 10, 50, 50);
399 RectType dest;
401 // Check the case where the receiver is an empty rect
402 rect1.Empty();
403 if (!dest.UnionRect(rect1, rect2) || (dest != rect2)) {
404 fail("[1] Check the case where the receiver is an empty rect");
405 return false;
408 // Check the case where the source rect is an empty rect
409 rect1 = rect2;
410 rect2.Empty();
411 if (!dest.UnionRect(rect1, rect2) || (dest != rect1)) {
412 fail("[2] Check the case where the source rect is an empty rect");
413 return false;
416 // Test the case where both rects are empty
417 rect1.Empty();
418 rect2.Empty();
419 if (dest.UnionRect(rect1, rect2)) {
420 fail("[3] Test the case where both rects are empty");
421 return false;
424 // Test union case where the two rects don't overlap at all
425 rect1.SetRect(10, 10, 50, 50);
426 rect2.SetRect(100, 100, 50, 50);
427 if (!dest.UnionRect(rect1, rect2) ||
428 (dest != RectType(rect1.x, rect1.y, rect2.XMost() - rect1.x, rect2.YMost() - rect1.y))) {
429 fail("[4] Test union case where the two rects don't overlap at all");
430 return false;
433 // Test union case where the two rects overlap
434 rect1.SetRect(30, 30, 50, 50);
435 rect2.SetRect(10, 10, 50, 50);
436 if (!dest.UnionRect(rect1, rect2) ||
437 (dest != RectType(rect2.x, rect2.y, rect1.XMost() - rect2.x, rect1.YMost() - rect2.y))) {
438 fail("[5] Test union case where the two rects overlap");
439 return false;
442 passed("TestUnion");
443 return true;
446 int main(int argc, char** argv)
448 ScopedXPCOM xpcom("TestRect");
449 if (xpcom.failed())
450 return -1;
452 int rv = 0;
454 //-----------------------
455 // Test nsRect
457 printf("===== nsRect tests =====\n");
459 if (!TestConstructors<nsRect>())
460 rv = -1;
462 if (!TestEqualityOperator<nsRect>())
463 rv = -1;
465 if (!TestContainment<nsRect>())
466 rv = -1;
468 if (!TestIntersects<nsRect>())
469 rv = -1;
471 if (!TestIntersection<nsRect>())
472 rv = -1;
474 if (!TestUnion<nsRect>())
475 rv = -1;
477 //-----------------------
478 // Test nsIntRect
480 printf("===== nsIntRect tests =====\n");
482 if (!TestConstructors<nsIntRect>())
483 rv = -1;
485 if (!TestEqualityOperator<nsIntRect>())
486 rv = -1;
488 if (!TestContainment<nsIntRect>())
489 rv = -1;
491 if (!TestIntersects<nsIntRect>())
492 rv = -1;
494 if (!TestIntersection<nsIntRect>())
495 rv = -1;
497 if (!TestUnion<nsIntRect>())
498 rv = -1;
500 return rv;