[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestRegion.cs
1 //
2 // Region class testing unit
3 //
4 // Authors:
5 //   Jordi Mas, jordi@ximian.com
6 //   Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2008 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31 using System.Drawing.Imaging;
32 using System.Drawing;
33 using System.Drawing.Drawing2D;
34 using System.Security.Permissions;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Drawing
38 {
39
40         [TestFixture]
41         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class TestRegion
43         {
44                 /* For debugging */
45                 public static void DumpRegion (Region rgn)
46                 {
47                         Matrix matrix = new Matrix ();
48                         RectangleF [] rects = rgn.GetRegionScans (matrix);
49
50                         for (int i = 0; i < rects.Length; i++)
51                                 Console.WriteLine ( rects[i]);
52                 }
53
54                 private Bitmap bitmap;
55                 private Graphics graphic;
56
57                 [TestFixtureSetUp]
58                 public void FixtureSetUp ()
59                 {
60                         bitmap = new Bitmap (10, 10);
61                         graphic = Graphics.FromImage (bitmap);
62                 }
63
64                 [Test]
65                 public void TestBounds()
66                 {
67                         Bitmap bmp = new Bitmap (600, 800);
68                         Graphics dc = Graphics.FromImage (bmp);
69                         Rectangle rect1, rect2;
70                         Region rgn1, rgn2;
71                         RectangleF bounds;
72
73                         rect1 = new Rectangle (500, 30, 60, 80);
74                         rect2 = new Rectangle (520, 40, 60, 80);
75                         rgn1 = new Region(rect1);
76                         rgn2 = new Region(rect2);
77                         rgn1.Union(rgn2);
78
79                         bounds = rgn1.GetBounds (dc);
80
81                         Assert.AreEqual (500, bounds.X);
82                         Assert.AreEqual (30, bounds.Y);
83                         Assert.AreEqual (80, bounds.Width);
84                         Assert.AreEqual (90, bounds.Height);
85                 }
86
87                 [Test]
88                 public void TestCloneAndEquals()
89                 {
90                         Bitmap bmp = new Bitmap (600, 800);
91                         Graphics dc = Graphics.FromImage (bmp);
92                         Rectangle rect1, rect2;
93                         Region rgn1, rgn2;
94                         RectangleF [] rects;
95                         RectangleF [] rects2;
96                         Matrix matrix = new Matrix ();
97
98                         rect1 = new Rectangle (500, 30, 60, 80);
99                         rect2 = new Rectangle (520, 40, 60, 80);
100                         rgn1 = new Region (rect1);
101                         rgn1.Union (rect2);
102                         rgn2 = rgn1.Clone ();
103
104                         rects = rgn1.GetRegionScans (matrix);
105                         rects2 = rgn2.GetRegionScans (matrix);
106
107                         Assert.AreEqual (rects.Length, rects2.Length);
108
109                         for (int i = 0; i < rects.Length; i++) {
110
111                                 Assert.AreEqual (rects[i].X, rects[i].X);
112                                 Assert.AreEqual (rects[i].Y, rects[i].Y);
113                                 Assert.AreEqual (rects[i].Width, rects[i].Width);
114                                 Assert.AreEqual (rects[i].Height, rects[i].Height);
115                         }
116
117                         Assert.AreEqual (true, rgn1.Equals (rgn2, dc));
118                 }
119
120                  /*Tests infinite, empty, etc*/
121                 [Test]
122                 public void TestInfiniteAndEmpty()
123                 {
124                         Bitmap bmp = new Bitmap (600, 800);
125                         Graphics dc = Graphics.FromImage (bmp);
126                         Rectangle rect1, rect2;
127                         Region rgn1;
128                         RectangleF [] rects;
129                         Matrix matrix = new Matrix ();
130
131                         rect1 = new Rectangle (500, 30, 60, 80);
132                         rect2 = new Rectangle (520, 40, 60, 80);
133                         rgn1 = new Region (rect1);
134                         rgn1.Union (rect2);
135
136                         Assert.AreEqual (false, rgn1.IsEmpty (dc));
137                         Assert.AreEqual (false, rgn1.IsInfinite (dc));
138
139                         rgn1.MakeEmpty();
140                         Assert.AreEqual (true, rgn1.IsEmpty (dc));
141
142                         rgn1 = new Region (rect1);
143                         rgn1.Union (rect2);
144                         rgn1.MakeInfinite ();
145                         rects = rgn1.GetRegionScans (matrix);
146
147                         Assert.AreEqual (1, rects.Length);
148                         Assert.AreEqual (-4194304, rects[0].X);
149                         Assert.AreEqual (-4194304, rects[0].Y);
150                         Assert.AreEqual (8388608, rects[0].Width);
151                         Assert.AreEqual (8388608, rects[0].Height);
152                         Assert.AreEqual (true, rgn1.IsInfinite (dc));
153                 }
154
155
156                 [Test]
157                 public void TestUnionGroup1 ()
158                 {
159                         Bitmap bmp = new Bitmap (600, 800);
160                         Graphics dc = Graphics.FromImage (bmp);
161                         Matrix matrix = new Matrix ();
162                         Rectangle rect1, rect2, rect3, rect4;
163                         Region rgn1, rgn2, rgn3, rgn4;
164                         RectangleF [] rects;
165
166                         rect1 = new Rectangle (500, 30, 60, 80);
167                         rect2 = new Rectangle (520, 40, 60, 80);
168                         rgn1 = new Region(rect1);
169                         rgn2 = new Region(rect2);
170                         rgn1.Union(rgn2);
171                         rects = rgn1.GetRegionScans (matrix);
172
173                         Assert.AreEqual (3, rects.Length);
174                         Assert.AreEqual (500, rects[0].X);
175                         Assert.AreEqual (30, rects[0].Y);
176                         Assert.AreEqual (60, rects[0].Width);
177                         Assert.AreEqual (10, rects[0].Height);
178
179                         Assert.AreEqual (500, rects[1].X);
180                         Assert.AreEqual (40, rects[1].Y);
181                         Assert.AreEqual (80, rects[1].Width);
182                         Assert.AreEqual (70, rects[1].Height);
183
184                         Assert.AreEqual (520, rects[2].X);
185                         Assert.AreEqual (110, rects[2].Y);
186                         Assert.AreEqual (60, rects[2].Width);
187                         Assert.AreEqual (10, rects[2].Height);
188
189                         rect1 = new Rectangle (20, 180, 40, 50);
190                         rect2 = new Rectangle (50, 190, 40, 50);
191                         rect3 = new Rectangle (70, 210, 30, 50);
192                         rgn1 = new Region (rect1);
193                         rgn2 = new Region (rect2);
194                         rgn3 = new Region (rect3);
195
196                         rgn1.Union (rgn2);
197                         rgn1.Union (rgn3);
198                         rects = rgn1.GetRegionScans (matrix);
199                         Assert.AreEqual (5, rects.Length);
200
201                         Assert.AreEqual (20, rects[0].X);
202                         Assert.AreEqual (180, rects[0].Y);
203                         Assert.AreEqual (40, rects[0].Width);
204                         Assert.AreEqual (10, rects[0].Height);
205
206                         Assert.AreEqual (20, rects[1].X);
207                         Assert.AreEqual (190, rects[1].Y);
208                         Assert.AreEqual (70, rects[1].Width);
209                         Assert.AreEqual (20, rects[1].Height);
210
211                         Assert.AreEqual (20, rects[2].X);
212                         Assert.AreEqual (210, rects[2].Y);
213                         Assert.AreEqual (80, rects[2].Width);
214                         Assert.AreEqual (20, rects[2].Height);
215
216                         Assert.AreEqual (50, rects[3].X);
217                         Assert.AreEqual (230, rects[3].Y);
218                         Assert.AreEqual (50, rects[3].Width);
219                         Assert.AreEqual (10, rects[3].Height);
220
221                         Assert.AreEqual (70, rects[4].X);
222                         Assert.AreEqual (240, rects[4].Y);
223                         Assert.AreEqual (30, rects[4].Width);
224                         Assert.AreEqual (20, rects[4].Height);
225
226                         rect1 = new Rectangle (20, 330, 40, 50);
227                         rect2 = new Rectangle (50, 340, 40, 50);
228                         rect3 = new Rectangle (70, 360, 30, 50);
229                         rect4 = new Rectangle (80, 400, 30, 10);
230                         rgn1 = new Region (rect1);
231                         rgn2 = new Region (rect2);
232                         rgn3 = new Region (rect3);
233                         rgn4 = new Region (rect4);
234
235                         rgn1.Union (rgn2);
236                         rgn1.Union (rgn3);
237                         rgn1.Union (rgn4);
238
239                         rects = rgn1.GetRegionScans (matrix);
240
241                         Assert.AreEqual (6, rects.Length);
242
243                         Assert.AreEqual (20, rects[0].X);
244                         Assert.AreEqual (330, rects[0].Y);
245                         Assert.AreEqual (40, rects[0].Width);
246                         Assert.AreEqual (10, rects[0].Height);
247
248                         Assert.AreEqual (20, rects[1].X);
249                         Assert.AreEqual (340, rects[1].Y);
250                         Assert.AreEqual (70, rects[1].Width);
251                         Assert.AreEqual (20, rects[1].Height);
252
253                         Assert.AreEqual (20, rects[2].X);
254                         Assert.AreEqual (360, rects[2].Y);
255                         Assert.AreEqual (80, rects[2].Width);
256                         Assert.AreEqual (20, rects[2].Height);
257
258                         Assert.AreEqual (50, rects[3].X);
259                         Assert.AreEqual (380, rects[3].Y);
260                         Assert.AreEqual (50, rects[3].Width);
261                         Assert.AreEqual (10, rects[3].Height);
262
263                         Assert.AreEqual (70, rects[4].X);
264                         Assert.AreEqual (390, rects[4].Y);
265                         Assert.AreEqual (30, rects[4].Width);
266                         Assert.AreEqual (10, rects[4].Height);
267
268                         Assert.AreEqual (70, rects[5].X);
269                         Assert.AreEqual (400, rects[5].Y);
270                         Assert.AreEqual (40, rects[5].Width);
271                         Assert.AreEqual (10, rects[5].Height);
272
273                         rect1 = new Rectangle (10, 20, 50, 50);
274                         rect2 = new Rectangle (100, 100, 60, 60);
275                         rect3 = new Rectangle (200, 200, 80, 80);
276
277                         rgn1 = new Region (rect1);
278                         rgn1.Union (rect2);
279                         rgn1.Union (rect3);
280
281                         rects = rgn1.GetRegionScans (matrix);
282
283                         Assert.AreEqual (3, rects.Length);
284
285                         Assert.AreEqual (10, rects[0].X);
286                         Assert.AreEqual (20, rects[0].Y);
287                         Assert.AreEqual (50, rects[0].Width);
288                         Assert.AreEqual (50, rects[0].Height);
289
290                         Assert.AreEqual (100, rects[1].X);
291                         Assert.AreEqual (100, rects[1].Y);
292                         Assert.AreEqual (60, rects[1].Width);
293                         Assert.AreEqual (60, rects[1].Height);
294
295                         Assert.AreEqual (200, rects[2].X);
296                         Assert.AreEqual (200, rects[2].Y);
297                         Assert.AreEqual (80, rects[2].Width);
298                         Assert.AreEqual (80, rects[2].Height);
299                 }
300
301                 void AssertEqualRectangles (RectangleF rect1, RectangleF rect2, string text)
302                 {
303                         Assert.AreEqual (rect1.X, rect2.X, text + ".X");
304                         Assert.AreEqual (rect1.Y, rect2.Y, text + ".Y");
305                         Assert.AreEqual (rect1.Width, rect2.Width, text + ".Width");
306                         Assert.AreEqual (rect1.Height, rect2.Height, text + ".Height");
307                 }
308
309                 [Test]
310                 public void TestUnionGroup2 ()
311                 {
312                         RectangleF[] rects;
313                         Region r1  = new Region ();
314                         Rectangle rect2 = Rectangle.Empty;
315                         Rectangle rect1 = Rectangle.Empty;
316                         Rectangle rect3 = Rectangle.Empty;
317                         Rectangle rect4 = Rectangle.Empty;
318
319                         { // TEST1: Not intersecting rects. Union just adds them
320
321                                 rect1 = new Rectangle (20, 20, 20, 20);
322                                 rect2 = new Rectangle (20, 80, 20, 10);
323                                 rect3 = new Rectangle (60, 60, 30, 10);
324
325                                 r1 = new Region (rect1);
326                                 r1.Union (rect2);
327                                 r1.Union (rect3);
328
329                                 rects = r1.GetRegionScans (new Matrix ());
330                                 Assert.AreEqual (3, rects.Length, "TUG1Test1");
331                                 AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TUG1Test2");
332                                 AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[1], "TUG1Test3");
333                                 AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[2], "TUG1Test4");
334                         }
335
336                         { // TEST2: Intersecting from the right
337                                  /*
338                                  *  -----------
339                                  *  |         |
340                                  *  |     |-------- |
341                                  *  |     |         |
342                                  *  |     |-------- |
343                                  *  |         |
344                                  *  ----------|
345                                  *
346                                  */
347
348                                 rect1 = new Rectangle (10, 10, 100, 100);
349                                 rect2 = new Rectangle (40, 60, 100, 20);
350                                 r1 = new Region (rect1);
351                                 r1.Union (rect2);
352
353                                 rects = r1.GetRegionScans (new Matrix ());
354                                 Assert.AreEqual  (3, rects.Length, "TUG2Test1");
355                                 AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TUG2Test2");
356                                 AssertEqualRectangles (new RectangleF (10, 60, 130, 20), rects[1], "TUG2Test3");
357                                 AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TUG2Test4");
358                         }
359
360                         { // TEST3: Intersecting from the right
361                                  /*
362                                  *      -----------
363                                  *      |         |
364                                  * |-------- |    |
365                                  * |         |    |
366                                  * |-------- |    |
367                                  *      |         |
368                                  *      ----------|
369                                  *
370                                  */
371
372                                 rect1 = new Rectangle (70, 10, 100, 100);
373                                 rect2 = new Rectangle (40, 60, 100, 20);
374
375                                 r1 = new Region (rect1);
376                                 r1.Union (rect2);
377
378                                 rects = r1.GetRegionScans (new Matrix ());
379                                 Assert.AreEqual  (3, rects.Length, "TUG3Test1");
380                                 AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TUG3Test2");
381                                 AssertEqualRectangles (new RectangleF (40, 60, 130, 20), rects[1], "TUG3Test3");
382                                 AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TUG3Test4");
383                         }
384
385                         { // TEST4: Intersecting from the top
386                                  /*
387                                  *         -----
388                                  *         |   |
389                                  *      -----------
390                                  *      |  |   |  |
391                                  *      |  -----  |
392                                  *      |         |
393                                  *      |         |
394                                  *      ----------|
395                                  *
396                                  */
397
398                                 rect1 = new Rectangle (40, 100, 100, 100);
399                                 rect2 = new Rectangle (70, 80, 50, 40);
400                                 r1 = new Region (rect1);
401                                 r1.Union (rect2);
402
403                                 rects = r1.GetRegionScans (new Matrix ());
404                                 Assert.AreEqual  (2, rects.Length, "TUG4Test1");
405                                 AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TUG4Test2");
406                                 AssertEqualRectangles (new RectangleF (40, 100, 100, 100), rects[1], "TUG4Test3");
407                         }
408
409                         { // TEST5: Intersecting from the bottom
410                                  /*
411
412                                  *      -----------
413                                  *      |         |
414                                  *      |         |
415                                  *      |         |
416                                  *      |  |   |  |
417                                  *      |--|   |--|
418                                  *         |   |
419                                  *         -----
420                                  */
421
422                                 rect1 = new Rectangle (40, 10, 100, 100);
423                                 rect2 = new Rectangle (70, 80, 50, 40);
424
425                                 r1 = new Region (rect1);
426                                 r1.Union (rect2);
427
428                                 rects = r1.GetRegionScans (new Matrix ());
429                                 Assert.AreEqual  (2, rects.Length, "TUG5Test1");
430                                 AssertEqualRectangles (new RectangleF (40, 10, 100, 100), rects[0], "TUG5Test2");
431                                 AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[1], "TUG5Test3");
432                         }
433
434                         { // TEST6: Multiple regions, two separted by zero pixels
435
436                                 rect1 = new Rectangle (30, 30, 80, 80);
437                                 rect2 = new Rectangle (45, 45, 200, 200);
438                                 rect3 = new Rectangle (160, 260, 10, 10);
439                                 rect4 = new Rectangle (170, 260, 10, 10);
440
441                                 r1 = new Region (rect1);
442                                 r1.Union (rect2);
443                                 r1.Union (rect3);
444                                 r1.Union (rect4);
445
446                                 rects = r1.GetRegionScans (new Matrix ());
447                                 Assert.AreEqual  (4, rects.Length, "TUG6Test1");
448                                 AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TUG6Test2");
449                                 AssertEqualRectangles (new RectangleF (30, 45, 215, 65), rects[1], "TUG6Test3");
450                                 AssertEqualRectangles (new RectangleF (45, 110, 200, 135), rects[2], "TUG6Test4");
451                                 AssertEqualRectangles (new RectangleF (160, 260, 20, 10), rects[3], "TUG6Test5");
452                         }
453                 }
454
455
456                 [Test]
457                 public void TestComplementGroup1 ()
458                 {
459                         RectangleF[] rects;
460                         Region r1  = new Region ();
461                         Region r2 = new Region ();
462                         Rectangle rect1 = Rectangle.Empty;
463                         Rectangle rect2 = Rectangle.Empty;
464                         Rectangle rect3 = Rectangle.Empty;
465                         Rectangle rect4 = Rectangle.Empty;
466                         Rectangle rect5 = Rectangle.Empty;
467                         Rectangle rect6 = Rectangle.Empty;
468                         Rectangle rect7 = Rectangle.Empty;
469
470
471                         { // TEST1
472
473                                 rect1 = new Rectangle (20, 20, 20, 20);
474                                 rect2 = new Rectangle (20, 80, 20, 10);
475                                 rect3 = new Rectangle (60, 60, 30, 10);
476
477                                 r1 = new Region (rect1);
478                                 r2 = new Region (rect2);
479                                 r2.Union (rect3);
480                                 r1.Complement (r2);
481
482                                 rects = r1.GetRegionScans (new Matrix ());
483                                 Assert.AreEqual  (2, rects.Length, "TCG1Test1");
484                                 AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[0], "TCG1Test2");
485                                 AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[1], "TCG1Test3");
486                         }
487
488
489                         { // TEST2
490
491                                 rect1 = new Rectangle (10, 10, 100, 100);
492                                 rect2 = new Rectangle (40, 60, 100, 20);
493
494                                 r1 = new Region (rect1);
495                                 r1.Complement (rect2);
496
497                                 rects = r1.GetRegionScans (new Matrix ());
498                                 Assert.AreEqual  (1, rects.Length, "TCG2Test1");
499                                 AssertEqualRectangles (new RectangleF (110, 60, 30, 20), rects[0], "TCG2Test2");
500                         }
501
502                         { // TEST3
503
504                                 rect1 = new Rectangle (70, 10, 100, 100);
505                                 rect2 = new Rectangle (40, 60, 100, 20);
506
507                                 r1 = new Region (rect1);
508                                 r1.Complement (rect2);
509
510                                 rects = r1.GetRegionScans (new Matrix ());
511                                 Assert.AreEqual  (1, rects.Length, "TCG3Test1");
512                                 AssertEqualRectangles (new RectangleF (40, 60, 30, 20), rects[0], "TCG3Test2");
513                         }
514
515                         { // TEST4
516
517                                 rect1 = new Rectangle (40, 100, 100, 100);
518                                 rect2 = new Rectangle (70, 80, 50, 40);
519
520                                 r1 = new Region (rect1);
521                                 r1.Complement (rect2);
522
523                                 rects = r1.GetRegionScans (new Matrix ());
524                                 Assert.AreEqual  (1, rects.Length, "TCG4Test1");
525                                 AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TCG4Test2");
526                         }
527
528                         { // TEST5
529
530                                 rect1 = new Rectangle (40, 10, 100, 100);
531                                 rect2 = new Rectangle (70, 80, 50, 40);
532
533                                 r1 = new Region (rect1);
534                                 r1.Complement (rect2);
535
536                                 rects = r1.GetRegionScans (new Matrix ());
537                                 Assert.AreEqual  (1, rects.Length, "TCG5Test1");
538                                 AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[0], "TCG5Test2");
539                         }
540
541                         { // TEST6: Multiple regions
542
543                                 rect1 = new Rectangle (30, 30, 80, 80);
544                                 rect2 = new Rectangle (45, 45, 200, 200);
545                                 rect3 = new Rectangle (160, 260, 10, 10);
546                                 rect4 = new Rectangle (170, 260, 10, 10);
547
548                                 r1 = new Region (rect1);
549                                 r1.Complement (rect2);
550                                 r1.Complement (rect3);
551                                 r1.Complement (rect4);
552
553                                 rects = r1.GetRegionScans (new Matrix ());
554                                 Assert.AreEqual  (1, rects.Length, "TCG6Test1");
555                                 AssertEqualRectangles (new RectangleF (170, 260, 10, 10), rects[0], "TCG6Test2");
556                         }
557
558                 }
559
560                 [Test]
561                 public void TestComplementGroup2 ()
562                 {
563
564                         Bitmap bmp = new Bitmap (600, 800);
565                         Graphics dc = Graphics.FromImage (bmp);
566                         Matrix matrix = new Matrix ();
567                         Rectangle rect1, rect2;
568                         Region rgn1, rgn2;
569                         RectangleF [] rects;
570
571                         rect1 = new Rectangle (20, 30, 60, 80);
572                         rect2 = new Rectangle (50, 40, 60, 80);
573                         rgn1 = new Region (rect1);
574                         rgn2 = new Region (rect2);
575                         dc.DrawRectangle (Pens.Green, rect1);
576                         dc.DrawRectangle (Pens.Red, rect2);
577                         rgn1.Complement (rgn2);
578                         dc.FillRegion (Brushes.Blue, rgn1);
579                         dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));
580
581                         rects = rgn1.GetRegionScans (matrix);
582
583                         Assert.AreEqual (2, rects.Length);
584
585                         Assert.AreEqual (80, rects[0].X);
586                         Assert.AreEqual (40, rects[0].Y);
587                         Assert.AreEqual (30, rects[0].Width);
588                         Assert.AreEqual (70, rects[0].Height);
589
590                         Assert.AreEqual (50, rects[1].X);
591                         Assert.AreEqual (110, rects[1].Y);
592                         Assert.AreEqual (60, rects[1].Width);
593                         Assert.AreEqual (10, rects[1].Height);
594
595                 }
596
597
598                 [Test]
599                 public void TestExcludeGroup1 ()
600                 {
601                         RectangleF[] rects;
602                         Region r1  = new Region ();
603                         Region r2 = new Region ();
604                         Rectangle rect1 = Rectangle.Empty;
605                         Rectangle rect2 = Rectangle.Empty;
606                         Rectangle rect3 = Rectangle.Empty;
607                         Rectangle rect4 = Rectangle.Empty;
608                         Rectangle rect5 = Rectangle.Empty;
609                         Rectangle rect6 = Rectangle.Empty;
610                         Rectangle rect7 = Rectangle.Empty;
611
612
613                         { // TEST1: Not intersecting rects. Exclude just adds them
614
615                                 rect1 = new Rectangle (20, 20, 20, 20);
616                                 rect2 = new Rectangle (20, 80, 20, 10);
617                                 rect3 = new Rectangle (60, 60, 30, 10);
618
619                                 r1 = new Region (rect1);
620                                 r2 = new Region (rect2);
621                                 r2.Union (rect3);
622                                 r1.Exclude (r2);
623
624                                 rects = r1.GetRegionScans (new Matrix ());
625                                 Assert.AreEqual  (1, rects.Length, "TEG1Test1");
626                                 AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TEG1Test2");
627                         }
628
629                         { // TEST2: Excluding from the right
630                                  /*
631                                  *  -----------
632                                  *  |         |
633                                  *  |     |-------- |
634                                  *  |     |         |
635                                  *  |     |-------- |
636                                  *  |         |
637                                  *  ----------|
638                                  *
639                                  */
640
641                                 rect1 = new Rectangle (10, 10, 100, 100);
642                                 rect2 = new Rectangle (40, 60, 100, 20);
643                                 r1 = new Region (rect1);
644                                 r1.Exclude (rect2);
645
646                                 rects = r1.GetRegionScans (new Matrix ());
647                                 Assert.AreEqual  (3, rects.Length, "TEG2Test1");
648                                 AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TEG2Test2");
649                                 AssertEqualRectangles (new RectangleF (10, 60, 30, 20), rects[1], "TEG2Test3");
650                                 AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TEG2Test4");
651                         }
652
653
654                         { // TEST3: Intersecting from the right
655                                  /*
656                                  *      -----------
657                                  *      |         |
658                                  * |-------- |    |
659                                  * |         |    |
660                                  * |-------- |    |
661                                  *      |         |
662                                  *      ----------|
663                                  *
664                                  */
665
666                                 rect1 = new Rectangle (70, 10, 100, 100);
667                                 rect2 = new Rectangle (40, 60, 100, 20);
668
669                                 r1 = new Region (rect1);
670                                 r1.Exclude (rect2);
671
672                                 rects = r1.GetRegionScans (new Matrix ());
673                                 Assert.AreEqual  (3, rects.Length, "TEG3Test1");
674                                 AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TEG3Test2");
675                                 AssertEqualRectangles (new RectangleF (140, 60, 30, 20), rects[1], "TEG3Test3");
676                                 AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TEG3Test4");
677                         }
678
679
680                         { // TEST4: Intersecting from the top
681                                  /*
682                                  *         -----
683                                  *         |   |
684                                  *      -----------
685                                  *      |  |   |  |
686                                  *      |  -----  |
687                                  *      |         |
688                                  *      |         |
689                                  *      ----------|
690                                  *
691                                  */
692
693                                 rect1 = new Rectangle (40, 100, 100, 100);
694                                 rect2 = new Rectangle (70, 80, 50, 40);
695
696                                 r1 = new Region (rect1);
697                                 r1.Exclude (rect2);
698
699                                 rects = r1.GetRegionScans (new Matrix ());
700                                 Assert.AreEqual  (3, rects.Length, "TEG4Test1");
701                                 AssertEqualRectangles (new RectangleF (40, 100, 30, 20), rects[0], "TEG4Test2");
702                                 AssertEqualRectangles (new RectangleF (120, 100, 20, 20), rects[1], "TEG4Test3");
703                                 AssertEqualRectangles (new RectangleF (40, 120, 100, 80), rects[2], "TEG4Test4");
704                         }
705
706
707                         { // TEST5: Intersecting from the bottom
708                                  /*
709                                  *      -----------
710                                  *      |         |
711                                  *      |         |
712                                  *      |         |
713                                  *      |  |   |  |
714                                  *      |--|   |--|
715                                  *         |   |
716                                  *         -----
717                                  *
718                                  */
719
720                                 rect1 = new Rectangle (40, 10, 100, 100);
721                                 rect2 = new Rectangle (70, 80, 50, 40);
722
723                                 r1 = new Region (rect1);
724                                 r1.Exclude (rect2);
725
726                                 rects = r1.GetRegionScans (new Matrix ());
727                                 Assert.AreEqual (3, rects.Length, "TEG5Test1");
728                                 AssertEqualRectangles (new RectangleF (40, 10, 100, 70), rects[0], "TEG5Test2");
729                                 AssertEqualRectangles (new RectangleF (40, 80, 30, 30), rects[1], "TEG5Test3");
730                                 AssertEqualRectangles (new RectangleF (120, 80, 20, 30), rects[2], "TEG5Test4");
731                         }
732
733
734                         { // TEST6: Multiple regions
735
736                                 rect1 = new Rectangle (30, 30, 80, 80);
737                                 rect2 = new Rectangle (45, 45, 200, 200);
738                                 rect3 = new Rectangle (160, 260, 10, 10);
739                                 rect4 = new Rectangle (170, 260, 10, 10);
740
741                                 r1 = new Region (rect1);
742                                 r1.Exclude (rect2);
743                                 r1.Exclude (rect3);
744                                 r1.Exclude (rect4);
745
746                                 rects = r1.GetRegionScans (new Matrix ());
747                                 Assert.AreEqual (2, rects.Length, "TEG6Test1");
748                                 AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TEG6Test2");
749                                 AssertEqualRectangles (new RectangleF (30, 45, 15, 65), rects[1], "TEG6Test3");
750                         }
751
752
753                         { // TEST7: Intersecting from the top with a larger rect
754                                  /*
755                                  *    -----------------
756                                  *    |               |
757                                  *    | -----------   |
758                                  *      |  |   |  |
759                                  *      |  -----  |
760                                  *      |         |
761                                  *      |         |
762                                  *      ----------|
763                                  *
764                                  */
765
766                                 rect1 = new Rectangle (50, 100, 100, 100);
767                                 rect2 = new Rectangle (30, 70, 150, 40);
768
769                                 r1 = new Region (rect1);
770                                 r1.Exclude (rect2);
771
772                                 rects = r1.GetRegionScans (new Matrix ());
773                                 Assert.AreEqual (1, rects.Length, "TEG7Test1");
774                                 AssertEqualRectangles (new RectangleF (50, 110, 100, 90), rects[0], "TEG7Test2");
775                         }
776
777                         { // TEST8: Intersecting from the right with a larger rect
778                                  /*
779                                  *
780                                  * |--------|
781                                  * |        |
782                                  * |    -----------
783                                  * |    |         |
784                                  * |    |         |
785                                  * |    |         |
786                                  * |    |         |
787                                  * |    |         |
788                                  * |    ----------|
789                                  * |-------|
790                                  */
791
792                                 rect1 = new Rectangle (70, 60, 100, 70);
793                                 rect2 = new Rectangle (40, 10, 100, 150);
794
795                                 r1 = new Region (rect1);
796                                 r1.Exclude (rect2);
797
798                                 rects = r1.GetRegionScans (new Matrix ());
799                                 Assert.AreEqual (1, rects.Length, "TEG8Test1");
800                                 AssertEqualRectangles (new RectangleF (140, 60, 30, 70), rects[0], "TEG8Test2");
801
802                         }
803
804                         { // TEST9: Intersecting from the left with a larger rect
805                                  /*
806                                  *
807                                  *              |--------|
808                                  *              |        |
809                                  *      -----------      |
810                                  *      |         |      |
811                                  *      |         |      |
812                                  *      |         |      |
813                                  *      |         |      |
814                                  *      |         |      |
815                                  *      ----------|      |
816                                  *              |--------|
817                                  *
818                                  */
819
820
821                                 rect1 = new Rectangle (70, 60, 100, 70);
822                                 rect2 = new Rectangle (100, 10, 100, 150);
823
824                                 r1 = new Region (rect1);
825                                 r1.Exclude (rect2);
826
827                                 rects = r1.GetRegionScans (new Matrix ());
828                                 Assert.AreEqual (1, rects.Length, "TEG9Test1");
829                                 AssertEqualRectangles (new RectangleF (70, 60, 30, 70), rects[0], "TEG9Test2");
830                         }
831
832
833                         { // TEST10: Intersecting from the bottom with a larger rect
834                                  /*
835                                  * *
836                                  *              |--------|
837                                  *              |        |
838                                  *              |        |
839                                  *              |        |
840                                  *        --------------------
841                                  *        |                  |
842                                  *        |                  |
843                                  *        |------------------|
844                                  */
845
846
847                                 rect1 = new Rectangle (20, 20, 100, 100);
848                                 rect2 = new Rectangle (10, 80, 140, 150);
849
850                                 r1 = new Region (rect1);
851                                 r1.Exclude (rect2);
852
853                                 rects = r1.GetRegionScans (new Matrix ());
854                                 Assert.AreEqual (1, rects.Length, "TEG10Test1");
855                                 AssertEqualRectangles (new RectangleF (20, 20, 100, 60), rects[0], "TEG10Test2");
856                         }
857
858
859                 }
860
861                 [Test]
862                 public void TestExcludeGroup2 ()
863                 {
864                         Bitmap bmp = new Bitmap (600, 800);
865                         Graphics dc = Graphics.FromImage (bmp);
866                         Matrix matrix = new Matrix ();
867                         Rectangle rect1, rect2;
868                         Region rgn1;
869                         RectangleF [] rects;
870
871                         rect1 = new Rectangle (130, 30, 60, 80);
872                         rect2 = new Rectangle (170, 40, 60, 80);
873                         rgn1 = new Region (rect1);
874                         rgn1.Exclude (rect2);
875                         rects = rgn1.GetRegionScans (matrix);
876
877                         Assert.AreEqual (2, rects.Length);
878
879                         Assert.AreEqual (130, rects[0].X);
880                         Assert.AreEqual (30, rects[0].Y);
881                         Assert.AreEqual (60, rects[0].Width);
882                         Assert.AreEqual (10, rects[0].Height);
883
884                         Assert.AreEqual (130, rects[1].X);
885                         Assert.AreEqual (40, rects[1].Y);
886                         Assert.AreEqual (40, rects[1].Width);
887                         Assert.AreEqual (70, rects[1].Height);
888                 }
889
890                 [Test]
891                 public void ExcludeBug402613 ()
892                 {
893                         Region r = new Region();
894                         r.MakeInfinite ();
895                         r.Exclude (new Rectangle (387,292,189,133));
896                         r.Exclude (new Rectangle (387,66,189,133));
897                         Assert.IsTrue (r.IsVisible (new Rectangle (66,292,189,133)));
898                 }
899
900                 [Test]
901                 public void TestIntersect()
902                 {
903
904
905                         Bitmap bmp = new Bitmap (600, 800);
906                         Graphics dc = Graphics.FromImage (bmp);
907                         Matrix matrix = new Matrix ();
908                         RectangleF [] rects;
909                         RectangleF rect3, rect4;
910                         Region rgn3, rgn4;
911
912                         /* Two simple areas */
913                         Rectangle rect1 = new Rectangle (260, 30, 60, 80);
914                         Rectangle rect2 = new Rectangle (290, 40, 60, 80);
915                         Region rgn1 = new Region (rect1);
916                         Region rgn2 = new Region (rect2);
917                         rgn1.Intersect (rgn2);
918
919                         rects = rgn1.GetRegionScans (matrix);
920                         Assert.AreEqual (1, rects.Length);
921
922                         Assert.AreEqual (290, rects[0].X);
923                         Assert.AreEqual (40, rects[0].Y);
924                         Assert.AreEqual (30, rects[0].Width);
925                         Assert.AreEqual (70, rects[0].Height);                                                  
926
927                         /* No intersect */
928                         rect1 = new Rectangle (20, 330, 40, 50);
929                         rect2 = new Rectangle (50, 340, 40, 50);
930                         rect3 = new Rectangle (70, 360, 30, 50);
931                         rect4 = new Rectangle (80, 400, 30, 10);
932                         rgn1 = new Region (rect1);
933                         rgn2 = new Region (rect2);
934                         rgn3 = new Region (rect3);
935                         rgn4 = new Region (rect4);
936
937                         rgn1.Intersect (rgn2);
938                         rgn1.Intersect (rgn3);
939                         rgn1.Intersect (rgn4);
940                         rects = rgn1.GetRegionScans (matrix);
941                         Assert.AreEqual (0, rects.Length);
942                 }
943
944                 [Test]
945                 public void TestXor()
946                 {
947                         Bitmap bmp = new Bitmap (600, 800);
948                         Graphics dc = Graphics.FromImage (bmp);
949                         Matrix matrix = new Matrix ();
950                         RectangleF [] rects;
951
952                         Rectangle rect1 = new Rectangle (380, 30, 60, 80);
953                         Rectangle rect2 = new Rectangle (410, 40, 60, 80);
954                         Region rgn1 = new Region (rect1);
955                         Region rgn2 = new Region (rect2);
956                         rgn1.Xor (rgn2);
957
958
959                         rects = rgn1.GetRegionScans (matrix);
960
961                         Assert.AreEqual (4, rects.Length);
962
963                         Assert.AreEqual (380, rects[0].X);
964                         Assert.AreEqual (30, rects[0].Y);
965                         Assert.AreEqual (60, rects[0].Width);
966                         Assert.AreEqual (10, rects[0].Height);
967
968                         Assert.AreEqual (380, rects[1].X);
969                         Assert.AreEqual (40, rects[1].Y);
970                         Assert.AreEqual (30, rects[1].Width);
971                         Assert.AreEqual (70, rects[1].Height);
972
973                         Assert.AreEqual (440, rects[2].X);
974                         Assert.AreEqual (40, rects[2].Y);
975                         Assert.AreEqual (30, rects[2].Width);
976                         Assert.AreEqual (70, rects[2].Height);
977
978                         Assert.AreEqual (410, rects[3].X);
979                         Assert.AreEqual (110, rects[3].Y);
980                         Assert.AreEqual (60, rects[3].Width);
981                         Assert.AreEqual (10, rects[3].Height);
982                 }
983
984
985                 [Test]
986                 public void TestIsVisible()
987                 {
988                         Bitmap bmp = new Bitmap (600, 800);
989                         Graphics dc = Graphics.FromImage (bmp);
990                         Rectangle rect1, rect2;
991                         Region rgn1, rgn2;
992                         Matrix matrix = new Matrix ();
993
994                         rect1 = new Rectangle (500, 30, 60, 80);
995                         rect2 = new Rectangle (520, 40, 60, 80);
996
997                         rgn1 = new Region (new RectangleF (0, 0, 10,10));
998                         Assert.AreEqual (false, rgn1.IsVisible (0,0,0,1));
999
1000                         rgn1 = new Region (rect1);
1001                         Assert.AreEqual (false, rgn1.IsVisible (500,29));
1002                         Assert.AreEqual (true, rgn1.IsVisible (500,30));
1003                         Assert.AreEqual (true, rgn1.IsVisible (rect1));
1004                         Assert.AreEqual (true, rgn1.IsVisible (rect2));
1005                         Assert.AreEqual (false, rgn1.IsVisible (new Rectangle (50,50,2,5)));
1006
1007                         Rectangle r = new Rectangle (1,1, 2,1);
1008                         rgn2 = new Region (r);
1009                         Assert.AreEqual (true, rgn2.IsVisible (r));
1010                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 2,2)));
1011                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 10,10)));
1012                         Assert.AreEqual (true, rgn2.IsVisible (new Rectangle (1,1, 1,1)));
1013                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (2,2, 1,1)));
1014                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (0,0, 1,1)));
1015                         Assert.AreEqual (false, rgn2.IsVisible (new Rectangle (3,3, 1,1)));
1016
1017                         Assert.AreEqual (false, rgn2.IsVisible (0,0));
1018                         Assert.AreEqual (false, rgn2.IsVisible (1,0));
1019                         Assert.AreEqual (false, rgn2.IsVisible (2,0));
1020                         Assert.AreEqual (false, rgn2.IsVisible (3,0));
1021                         Assert.AreEqual (false, rgn2.IsVisible (0,1));
1022                         Assert.AreEqual (true, rgn2.IsVisible (1,1));
1023                         Assert.AreEqual (true, rgn2.IsVisible (2,1));
1024                         Assert.AreEqual (false, rgn2.IsVisible (3,1));
1025                         Assert.AreEqual (false, rgn2.IsVisible (0,2));
1026                         Assert.AreEqual (false, rgn2.IsVisible (1,2));
1027                         Assert.AreEqual (false, rgn2.IsVisible (2,2));
1028                         Assert.AreEqual (false, rgn2.IsVisible (3,2));
1029
1030
1031                 }
1032
1033                 [Test]
1034                 public void TestTranslate()
1035                 {
1036                         Region rgn1 = new Region (new RectangleF (10, 10, 120,120));
1037                         rgn1.Translate (30,20);
1038                         Matrix matrix = new Matrix ();
1039
1040                         RectangleF [] rects = rgn1.GetRegionScans (matrix);
1041
1042                         Assert.AreEqual (1, rects.Length);
1043
1044                         Assert.AreEqual (40, rects[0].X);
1045                         Assert.AreEqual (30, rects[0].Y);
1046                         Assert.AreEqual (120, rects[0].Width);
1047                         Assert.AreEqual (120, rects[0].Height);
1048                 }
1049
1050                 [Test]
1051                 [ExpectedException (typeof (ArgumentNullException))]
1052                 public void Constructor_GraphicsPath_Null ()
1053                 {
1054                         GraphicsPath gp = null;
1055                         Region r = new Region (gp);
1056                 }
1057
1058                 [Test]
1059                 [ExpectedException (typeof (ArgumentNullException))]
1060                 public void Constructor_RegionData_Null ()
1061                 {
1062                         RegionData rd = null;
1063                         Region r = new Region (rd);
1064                 }
1065
1066                 [Test]
1067                 [ExpectedException (typeof (ArgumentNullException))]
1068                 public void Union_GraphicsPath_Null ()
1069                 {
1070                         GraphicsPath gp = null;
1071                         new Region ().Union (gp);
1072                 }
1073
1074                 [Test]
1075                 [ExpectedException (typeof (ArgumentNullException))]
1076                 public void Union_Region_Null ()
1077                 {
1078                         Region r = null;
1079                         new Region ().Union (r);
1080                 }
1081
1082                 [Test]
1083                 public void Union_Region_Infinite ()
1084                 {
1085                         // default ctor creates an infinite region
1086                         Region r = new Region ();
1087                         CheckEmpty ("default .ctor", r);
1088                         // union-ing to infinity doesn't change the results
1089                         r.Union (new Rectangle (10, 10, 100, 100));
1090                         CheckEmpty ("U infinity", r);
1091                 }
1092
1093                 [Test]
1094                 [ExpectedException (typeof (ArgumentNullException))]
1095                 public void Intersect_GraphicsPath_Null ()
1096                 {
1097                         GraphicsPath gp = null;
1098                         new Region ().Intersect (gp);
1099                 }
1100
1101                 [Test]
1102                 [ExpectedException (typeof (ArgumentNullException))]
1103                 public void Intersect_Region_Null ()
1104                 {
1105                         Region r = null;
1106                         new Region ().Intersect (r);
1107                 }
1108
1109                 [Test]
1110                 [ExpectedException (typeof (ArgumentNullException))]
1111                 public void Complement_GraphicsPath_Null ()
1112                 {
1113                         GraphicsPath gp = null;
1114                         new Region ().Complement (gp);
1115                 }
1116
1117                 [Test]
1118                 [ExpectedException (typeof (ArgumentNullException))]
1119                 public void Complement_Region_Null ()
1120                 {
1121                         Region r = null;
1122                         new Region ().Complement (r);
1123                 }
1124
1125                 [Test]
1126                 [ExpectedException (typeof (ArgumentNullException))]
1127                 public void Exclude_GraphicsPath_Null ()
1128                 {
1129                         GraphicsPath gp = null;
1130                         new Region ().Exclude (gp);
1131                 }
1132
1133                 [Test]
1134                 [ExpectedException (typeof (ArgumentNullException))]
1135                 public void Exclude_Region_Null ()
1136                 {
1137                         Region r = null;
1138                         new Region ().Exclude (r);
1139                 }
1140
1141                 [Test]
1142                 [ExpectedException (typeof (ArgumentNullException))]
1143                 public void Xor_GraphicsPath_Null ()
1144                 {
1145                         GraphicsPath gp = null;
1146                         new Region ().Xor (gp);
1147                 }
1148
1149                 [Test]
1150                 [ExpectedException (typeof (ArgumentNullException))]
1151                 public void Xor_Region_Null ()
1152                 {
1153                         Region r = null;
1154                         new Region ().Xor (r);
1155                 }
1156
1157                 [Test]
1158                 [ExpectedException (typeof (ArgumentNullException))]
1159                 public void GetBounds_Null ()
1160                 {
1161                         new Region ().GetBounds (null);
1162                 }
1163
1164                 [Test]
1165                 public void IsVisible_IntIntNull ()
1166                 {
1167                         Assert.IsTrue (new Region ().IsVisible (0, 0, null));
1168                 }
1169
1170                 [Test]
1171                 public void IsVisible_IntIntIntIntNull ()
1172                 {
1173                         Assert.IsFalse (new Region ().IsVisible (0, 0, 0, 0, null));
1174                 }
1175
1176                 [Test]
1177                 public void IsVisible_PointNull ()
1178                 {
1179                         Point p = new Point ();
1180                         Assert.IsTrue (new Region ().IsVisible (p, null));
1181                 }
1182
1183                 [Test]
1184                 public void IsVisible_PointFNull ()
1185                 {
1186                         PointF p = new PointF ();
1187                         Assert.IsTrue (new Region ().IsVisible (p, null));
1188                 }
1189
1190                 [Test]
1191                 public void IsVisible_RectangleNull ()
1192                 {
1193                         Rectangle r = new Rectangle ();
1194                         Assert.IsFalse (new Region ().IsVisible (r, null));
1195                 }
1196
1197                 [Test]
1198                 public void IsVisible_RectangleFNull ()
1199                 {
1200                         RectangleF r = new RectangleF ();
1201                         Assert.IsFalse (new Region ().IsVisible (r, null));
1202                 }
1203
1204                 [Test]
1205                 public void IsVisible_SingleSingleNull ()
1206                 {
1207                         Assert.IsTrue (new Region ().IsVisible (0f, 0f, null));
1208                 }
1209
1210                 [Test]
1211                 public void IsVisible_SingleSingleSingleSingleNull ()
1212                 {
1213                         Assert.IsFalse (new Region ().IsVisible (0f, 0f, 0f, 0f, null));
1214                 }
1215
1216                 [Test]
1217                 [ExpectedException (typeof (ArgumentNullException))]
1218                 public void IsEmpty_Null ()
1219                 {
1220                         new Region ().IsEmpty (null);
1221                 }
1222
1223                 [Test]
1224                 [ExpectedException (typeof (ArgumentNullException))]
1225                 public void IsInfinite_Null ()
1226                 {
1227                         new Region ().IsInfinite (null);
1228                 }
1229
1230                 [Test]
1231                 [ExpectedException (typeof (ArgumentNullException))]
1232                 public void Equals_NullGraphics ()
1233                 {
1234                         new Region ().Equals (null, Graphics.FromImage (new Bitmap (10, 10)));
1235                 }
1236
1237                 [Test]
1238                 [ExpectedException (typeof (ArgumentNullException))]
1239                 public void Equals_RegionNull ()
1240                 {
1241                         new Region ().Equals (new Region (), null);
1242                 }
1243
1244                 [Test]
1245                 [ExpectedException (typeof (ArgumentNullException))]
1246                 [Category ("NotWorking")] // caused regression in SWF
1247                 public void GetHrgn_Null ()
1248                 {
1249                         new Region ().GetHrgn (null);
1250                 }
1251
1252                 [Test]
1253                 [ExpectedException (typeof (ArgumentNullException))]
1254                 public void GetRegionScans_Null ()
1255                 {
1256                         new Region ().GetRegionScans (null);
1257                 }
1258
1259                 [Test]
1260                 [ExpectedException (typeof (ArgumentNullException))]
1261                 public void Transform_Null ()
1262                 {
1263                         new Region ().Transform (null);
1264                 }
1265
1266                 // an "empty ctor" Region is infinite
1267                 private void CheckEmpty (string prefix, Region region)
1268                 {
1269                         Assert.IsFalse (region.IsEmpty (graphic), prefix + "IsEmpty");
1270                         Assert.IsTrue (region.IsInfinite (graphic), prefix + "graphic");
1271
1272                         RectangleF rect = region.GetBounds (graphic);
1273                         Assert.AreEqual (-4194304f, rect.X, prefix + "GetBounds.X");
1274                         Assert.AreEqual (-4194304f, rect.Y, prefix + "GetBounds.Y");
1275                         Assert.AreEqual (8388608f, rect.Width, prefix + "GetBounds.Width");
1276                         Assert.AreEqual (8388608f, rect.Height, prefix + "GetBounds.Height");
1277                 }
1278
1279                 [Test]
1280                 public void Region_Empty ()
1281                 {
1282                         Region region = new Region ();
1283                         CheckEmpty ("Empty.", region);
1284
1285                         Region clone = region.Clone ();
1286                         CheckEmpty ("Clone.", region);
1287
1288                         RegionData data = region.GetRegionData ();
1289                         Region r2 = new Region (data);
1290                         CheckEmpty ("RegionData.", region);
1291                 }
1292
1293                 [Test]
1294                 [Category ("NotWorking")]
1295                 public void Region_Infinite_MultipleRectangles ()
1296                 {
1297                         Region region = new Region ();
1298                         Assert.IsTrue (region.IsInfinite (graphic), "Empty.IsInfinite");
1299
1300                         GraphicsPath gp = new GraphicsPath ();
1301                         gp.AddRectangle (new Rectangle (-4194304, -4194304, 8388608, 8388608));
1302                         region = new Region (gp);
1303                         Assert.IsTrue (region.IsInfinite (graphic), "OneRectangle.IsInfinite");
1304
1305                         gp.AddRectangle (new Rectangle (1, 1, 2, 2));
1306                         region = new Region (gp);
1307                         Assert.IsFalse (region.IsInfinite (graphic), "TwoOverlappingRectangle.IsInfinite");
1308
1309                         gp = new GraphicsPath ();
1310                         gp.AddRectangle (new Rectangle (-4194304, -4194304, 4194304, 8388608));
1311                         gp.AddRectangle (new Rectangle (0, -4194304, 4194304, 8388608));
1312                         Assert.IsFalse (region.IsInfinite (graphic), "TwoSideBySideRectangle.IsInfinite");
1313                 }
1314
1315                 [Test]
1316                 public void Rectangle_GetRegionScans ()
1317                 {
1318                         Matrix matrix = new Matrix ();
1319                         GraphicsPath gp = new GraphicsPath ();
1320                         gp.AddRectangle (new Rectangle (10, 10, 10, 10));
1321                         Region region = new Region (gp);
1322                         Assert.AreEqual (1, region.GetRegionScans (matrix).Length, "1");
1323
1324                         gp.AddRectangle (new Rectangle (20, 20, 20, 20));
1325                         region = new Region (gp);
1326                         Assert.AreEqual (2, region.GetRegionScans (matrix).Length, "2");
1327                 }
1328
1329                 [Test]
1330                 public void InfinityExclude ()
1331                 {
1332                         using (Region r = new Region ()) {
1333                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1334                                 r.Exclude (new Rectangle (5, 5, 10, 10));
1335                                 Assert.IsFalse (r.IsInfinite (graphic), "after");
1336                                 RectangleF bounds = r.GetBounds (graphic);
1337                                 Assert.AreEqual (-4194304, bounds.X, "X");
1338                                 Assert.AreEqual (-4194304, bounds.Y, "Y");
1339                                 Assert.AreEqual (8388608, bounds.Width, "Width");
1340                                 Assert.AreEqual (8388608, bounds.Height, "Height");
1341                         }
1342                 }
1343
1344                 [Test]
1345                 public void InfinityIntersect ()
1346                 {
1347                         using (Region r = new Region ()) {
1348                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1349                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1350                                 Assert.IsFalse (r.IsInfinite (graphic), "after");
1351                                 RectangleF bounds = r.GetBounds (graphic);
1352                                 Assert.AreEqual (-10, bounds.X, "X");
1353                                 Assert.AreEqual (-10, bounds.Y, "Y");
1354                                 Assert.AreEqual (20, bounds.Width, "Width");
1355                                 Assert.AreEqual (20, bounds.Height, "Height");
1356                         }
1357                 }
1358
1359                 [Test]
1360                 public void InfinityIntersectTranslate ()
1361                 {
1362                         using (Region r = new Region ()) {
1363                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1364                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1365                                 r.Translate (10, 10);
1366                                 RectangleF bounds = r.GetBounds (graphic);
1367                                 Assert.AreEqual (0, bounds.X, "X");
1368                                 Assert.AreEqual (0, bounds.Y, "Y");
1369                                 Assert.AreEqual (20, bounds.Width, "Width");
1370                                 Assert.AreEqual (20, bounds.Height, "Height");
1371                         }
1372                 }
1373
1374                 [Test]
1375                 public void InfinityIntersectScale ()
1376                 {
1377                         using (Region r = new Region ()) {
1378                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1379                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1380                                 using (Matrix m = new Matrix ()) {
1381                                         m.Scale (2, 0.5f);
1382                                         r.Transform (m);
1383                                 }
1384                                 RectangleF bounds = r.GetBounds (graphic);
1385                                 Assert.AreEqual (-20, bounds.X, "X");
1386                                 Assert.AreEqual (-5, bounds.Y, "Y");
1387                                 Assert.AreEqual (40, bounds.Width, "Width");
1388                                 Assert.AreEqual (10, bounds.Height, "Height");
1389                         }
1390                 }
1391
1392                 [Test]
1393                 public void InfinityIntersectTransform ()
1394                 {
1395                         using (Region r = new Region ()) {
1396                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1397                                 r.Intersect (new Rectangle (-10, -10, 20, 20));
1398                                 using (Matrix m = new Matrix (2, 0, 0, 0.5f, 10, 10)) {
1399                                         r.Transform (m);
1400                                 }
1401                                 RectangleF bounds = r.GetBounds (graphic);
1402                                 Assert.AreEqual (-10, bounds.X, "X");
1403                                 Assert.AreEqual (5, bounds.Y, "Y");
1404                                 Assert.AreEqual (40, bounds.Width, "Width");
1405                                 Assert.AreEqual (10, bounds.Height, "Height");
1406                         }
1407                 }
1408
1409                 [Test]
1410                 public void InfinityTranslate ()
1411                 {
1412                         using (Region r = new Region ()) {
1413                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1414                                 r.Translate (10, 10);
1415                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1416                                 CheckEmpty ("InfinityTranslate", r);
1417                         }
1418                 }
1419
1420                 [Test]
1421                 public void InfinityScaleUp ()
1422                 {
1423                         using (Region r = new Region ()) {
1424                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1425                                 using (Matrix m = new Matrix ()) {
1426                                         m.Scale (2, 2);
1427                                         r.Transform (m);
1428                                 }
1429                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1430                                 CheckEmpty ("InfinityScaleUp", r);
1431                         }
1432                 }
1433
1434                 [Test]
1435                 public void InfinityScaleDown ()
1436                 {
1437                         using (Region r = new Region ()) {
1438                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1439                                 using (Matrix m = new Matrix ()) {
1440                                         m.Scale (0.5f, 0.5f);
1441                                         r.Transform (m);
1442                                 }
1443                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1444                                 CheckEmpty ("InfinityScaleDown", r);
1445                         }
1446                 }
1447
1448                 [Test]
1449                 public void InfinityRotate ()
1450                 {
1451                         using (Region r = new Region ()) {
1452                                 Assert.IsTrue (r.IsInfinite (graphic), "before");
1453                                 using (Matrix m = new Matrix ()) {
1454                                         m.Rotate (45);
1455                                         r.Transform (m);
1456                                 }
1457                                 Assert.IsTrue (r.IsInfinite (graphic), "after");
1458                                 CheckEmpty ("InfinityRotate", r);
1459                         }
1460                 }
1461
1462                 [Test]
1463                 public void Intersect_383878 ()
1464                 {
1465                         using (Region clipRegion = new Region ()) {
1466                                 clipRegion.MakeInfinite ();
1467
1468                                 Rectangle smaller = new Rectangle (5, 5, -10, -10);
1469
1470                                 clipRegion.Intersect (smaller);
1471                                 Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
1472                                 Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");
1473
1474                                 RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
1475                                 Assert.AreEqual (1, rects.Length, "Length");
1476                                 Assert.AreEqual (new RectangleF (-5, -5, 10, 10), rects [0], "0");
1477                         }
1478                 }
1479
1480                 [Test]
1481                 public void Complement_383878 ()
1482                 {
1483                         using (Region clipRegion = new Region ()) {
1484                                 clipRegion.MakeInfinite ();
1485
1486                                 Rectangle smaller = new Rectangle (5, 5, -10, -10);
1487                                 Rectangle bigger = new Rectangle (-5, -5, 12, 12);
1488
1489                                 clipRegion.Intersect (smaller);
1490                                 clipRegion.Complement (bigger);
1491
1492                                 Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
1493                                 Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");
1494
1495                                 RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
1496                                 Assert.AreEqual (2, rects.Length, "Length");
1497                                 Assert.AreEqual (new RectangleF (5, -5, 2, 10), rects [0], "0");
1498                                 Assert.AreEqual (new RectangleF (-5, 5, 12, 2), rects [1], "1");
1499                         }
1500                 }
1501         }
1502
1503         [TestFixture]
1504         // the test cases in this fixture aren't restricted wrt running unmanaged code
1505         public class RegionTestUnmanaged {
1506                 private Bitmap bitmap;
1507                 private Graphics graphic;
1508
1509                 [TestFixtureSetUp]
1510                 public void FixtureSetUp ()
1511                 {
1512                         bitmap = new Bitmap (10, 10);
1513                         graphic = Graphics.FromImage (bitmap);
1514                 }
1515
1516                 // Note: Test cases calling GetHrng will leak memory unless ReleaseHrgn
1517                 // (which only exists in 2.0) is called.
1518
1519                 [Test]
1520                 public void GetHrgn_Infinite_MakeEmpty ()
1521                 {
1522                         Region r = new Region ();
1523                         Assert.IsFalse (r.IsEmpty (graphic), "!Empty");
1524                         Assert.IsTrue (r.IsInfinite (graphic), "Infinite");
1525                         Assert.AreEqual (IntPtr.Zero, r.GetHrgn (graphic), "Handle==0");
1526
1527                         r.MakeEmpty ();
1528                         Assert.IsTrue (r.IsEmpty (graphic), "Empty");
1529                         Assert.IsFalse (r.IsInfinite (graphic), "!Infinite");
1530                         IntPtr h = r.GetHrgn (graphic);
1531                         Assert.IsFalse (h == IntPtr.Zero, "Handle!=0");
1532 #if NET_2_0
1533                         r.ReleaseHrgn (h);
1534 #endif
1535                 }
1536
1537                 [Test]
1538                 public void GetHrgn_Empty_MakeInfinite ()
1539                 {
1540                         Region r = new Region (new GraphicsPath ());
1541                         Assert.IsTrue (r.IsEmpty (graphic), "Empty");
1542                         Assert.IsFalse (r.IsInfinite (graphic), "!Infinite");
1543                         IntPtr h = r.GetHrgn (graphic);
1544                         Assert.IsFalse (h == IntPtr.Zero, "Handle!=0");
1545
1546                         r.MakeInfinite ();
1547                         Assert.IsFalse (r.IsEmpty (graphic), "!Empty");
1548                         Assert.IsTrue (r.IsInfinite (graphic), "Infinite");
1549                         Assert.AreEqual (IntPtr.Zero, r.GetHrgn (graphic), "Handle==0");
1550 #if NET_2_0
1551                         r.ReleaseHrgn (h);
1552 #endif
1553                 }
1554
1555                 [Test]
1556                 public void GetHrgn_TwiceFromSameRegionInstance ()
1557                 {
1558                         Region r = new Region (new GraphicsPath ());
1559                         IntPtr h1 = r.GetHrgn (graphic);
1560                         IntPtr h2 = r.GetHrgn (graphic);
1561                         Assert.IsFalse (h1 == h2, "Handle_1!=Handle_2");
1562 #if NET_2_0
1563                         r.ReleaseHrgn (h1);
1564                         r.ReleaseHrgn (h2);
1565 #endif
1566                 }
1567
1568                 [Test]
1569                 public void GetHrgn_FromHrgn ()
1570                 {
1571                         Region r1 = new Region (new GraphicsPath ());
1572                         IntPtr h1 = r1.GetHrgn (graphic);
1573                         Assert.IsFalse (h1 == IntPtr.Zero, "Handle_1!=0");
1574
1575                         Region r2 = Region.FromHrgn (h1);
1576                         IntPtr h2 = r2.GetHrgn (graphic);
1577                         Assert.IsFalse (h2 == IntPtr.Zero, "Handle_2!=0");
1578                         Assert.IsFalse (h1 == h2, "Handle_1!=Handle_2");
1579 #if NET_2_0
1580                         r1.ReleaseHrgn (h1);
1581                         r2.ReleaseHrgn (h2);
1582 #endif
1583                 }
1584
1585                 [Test]
1586                 [ExpectedException (typeof (ArgumentException))]
1587                 public void FromHrgn_Zero ()
1588                 {
1589                         Region.FromHrgn (IntPtr.Zero);
1590                 }
1591 #if NET_2_0
1592                 [Test]
1593                 [ExpectedException (typeof (ArgumentNullException))]
1594                 public void ReleaseHrng_Zero ()
1595                 {
1596                         Region r = new Region (new GraphicsPath ());
1597                         r.ReleaseHrgn (IntPtr.Zero);
1598                 }
1599
1600                 [Test]
1601                 public void ReleaseHrng ()
1602                 {
1603                         Region r = new Region (new GraphicsPath ());
1604                         IntPtr ptr = r.GetHrgn (graphic);
1605                         Assert.IsFalse (IntPtr.Zero == ptr, "ptr");
1606                         r.ReleaseHrgn (ptr);
1607                 }
1608 #endif
1609         }
1610 }