1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
23 // Chris Toshok (toshok@ximian.com)
27 using System.Globalization;
29 using System.Windows.Media;
30 using NUnit.Framework;
32 namespace MonoTests.System.Windows {
38 public void Ctor_Accessor ()
42 r = new Rect (new Size (40, 50));
43 Assert.AreEqual (0, r.X);
44 Assert.AreEqual (0, r.Y);
45 Assert.AreEqual (40, r.Width);
46 Assert.AreEqual (50, r.Height);
47 Assert.AreEqual (0, r.Left);
48 Assert.AreEqual (0, r.Top);
49 Assert.AreEqual (40, r.Right);
50 Assert.AreEqual (50, r.Bottom);
51 Assert.AreEqual (new Point (0, 0), r.TopLeft);
52 Assert.AreEqual (new Point (40, 0), r.TopRight);
53 Assert.AreEqual (new Point (0, 50), r.BottomLeft);
54 Assert.AreEqual (new Point (40, 50), r.BottomRight);
55 Assert.AreEqual (new Point (0, 0), r.Location);
56 Assert.AreEqual (new Size (40, 50), r.Size);
58 r = new Rect (new Point (4, 5), new Vector (20, 30));
59 Assert.AreEqual (4, r.X);
60 Assert.AreEqual (5, r.Y);
61 Assert.AreEqual (20, r.Width);
62 Assert.AreEqual (30, r.Height);
63 Assert.AreEqual (4, r.Left);
64 Assert.AreEqual (5, r.Top);
65 Assert.AreEqual (24, r.Right);
66 Assert.AreEqual (35, r.Bottom);
67 Assert.AreEqual (new Point (4, 5), r.TopLeft);
68 Assert.AreEqual (new Point (24, 5), r.TopRight);
69 Assert.AreEqual (new Point (4, 35), r.BottomLeft);
70 Assert.AreEqual (new Point (24, 35), r.BottomRight);
71 Assert.AreEqual (new Point (4, 5), r.Location);
72 Assert.AreEqual (new Size (20, 30), r.Size);
74 r = new Rect (new Point (4, 5), new Point (20, 30));
75 Assert.AreEqual (4, r.X);
76 Assert.AreEqual (5, r.Y);
77 Assert.AreEqual (16, r.Width);
78 Assert.AreEqual (25, r.Height);
79 Assert.AreEqual (4, r.Left);
80 Assert.AreEqual (5, r.Top);
81 Assert.AreEqual (20, r.Right);
82 Assert.AreEqual (30, r.Bottom);
83 Assert.AreEqual (new Point (4, 5), r.TopLeft);
84 Assert.AreEqual (new Point (20, 5), r.TopRight);
85 Assert.AreEqual (new Point (4, 30), r.BottomLeft);
86 Assert.AreEqual (new Point (20, 30), r.BottomRight);
87 Assert.AreEqual (new Point (4, 5), r.Location);
88 Assert.AreEqual (new Size (16,25), r.Size);
90 r = new Rect (new Point (20, 30), new Point (4, 5));
91 Assert.AreEqual (4, r.X);
92 Assert.AreEqual (5, r.Y);
93 Assert.AreEqual (16, r.Width);
94 Assert.AreEqual (25, r.Height);
95 Assert.AreEqual (4, r.Left);
96 Assert.AreEqual (5, r.Top);
97 Assert.AreEqual (20, r.Right);
98 Assert.AreEqual (30, r.Bottom);
99 Assert.AreEqual (new Point (4, 5), r.TopLeft);
100 Assert.AreEqual (new Point (20, 5), r.TopRight);
101 Assert.AreEqual (new Point (4, 30), r.BottomLeft);
102 Assert.AreEqual (new Point (20, 30), r.BottomRight);
103 Assert.AreEqual (new Point (4, 5), r.Location);
104 Assert.AreEqual (new Size (16,25), r.Size);
106 r = new Rect (10, 15, 20, 30);
107 Assert.AreEqual (10, r.X);
108 Assert.AreEqual (15, r.Y);
109 Assert.AreEqual (20, r.Width);
110 Assert.AreEqual (30, r.Height);
111 Assert.AreEqual (10, r.Left);
112 Assert.AreEqual (15, r.Top);
113 Assert.AreEqual (30, r.Right);
114 Assert.AreEqual (45, r.Bottom);
115 Assert.AreEqual (new Point (10, 15), r.TopLeft);
116 Assert.AreEqual (new Point (30, 15), r.TopRight);
117 Assert.AreEqual (new Point (10, 45), r.BottomLeft);
118 Assert.AreEqual (new Point (30, 45), r.BottomRight);
119 Assert.AreEqual (new Point (10, 15), r.Location);
120 Assert.AreEqual (new Size (20, 30), r.Size);
122 r = new Rect (new Point (10, 15), new Size (20, 30));
123 Assert.AreEqual (10, r.X);
124 Assert.AreEqual (15, r.Y);
125 Assert.AreEqual (20, r.Width);
126 Assert.AreEqual (30, r.Height);
127 Assert.AreEqual (10, r.Left);
128 Assert.AreEqual (15, r.Top);
129 Assert.AreEqual (30, r.Right);
130 Assert.AreEqual (45, r.Bottom);
131 Assert.AreEqual (new Point (10, 15), r.TopLeft);
132 Assert.AreEqual (new Point (30, 15), r.TopRight);
133 Assert.AreEqual (new Point (10, 45), r.BottomLeft);
134 Assert.AreEqual (new Point (30, 45), r.BottomRight);
135 Assert.AreEqual (new Point (10, 15), r.Location);
136 Assert.AreEqual (new Size (20, 30), r.Size);
140 [ExpectedException (typeof (ArgumentException))]
141 public void Ctor_NegativeWidth ()
143 new Rect (10, 10, -10, 10);
147 [ExpectedException (typeof (ArgumentException))]
148 public void Ctor_NegativeHeight ()
150 new Rect (10, 10, 10, -10);
157 Assert.AreEqual (Double.PositiveInfinity, r.X);
158 Assert.AreEqual (Double.PositiveInfinity, r.Y);
159 Assert.AreEqual (Double.NegativeInfinity, r.Width);
160 Assert.AreEqual (Double.NegativeInfinity, r.Height);
164 [ExpectedException (typeof (InvalidOperationException))]
165 public void ModifyEmpty_x ()
172 [ExpectedException (typeof (InvalidOperationException))]
173 public void ModifyEmpty_y ()
180 [ExpectedException (typeof (InvalidOperationException))]
181 public void ModifyEmpty_width ()
188 [ExpectedException (typeof (InvalidOperationException))]
189 public void ModifyEmpty_height ()
196 [ExpectedException (typeof (InvalidOperationException))]
197 public void ModifyEmpty_negative_width ()
204 [ExpectedException (typeof (InvalidOperationException))]
205 public void ModifyEmpty_negative_height ()
213 [ExpectedException (typeof (ArgumentException))]
214 public void Modify_negative_width ()
216 Rect r = new Rect (0, 0, 10, 10);
221 [ExpectedException (typeof (ArgumentException))]
222 public void Modify_negative_height ()
224 Rect r = new Rect (0, 0, 10, 10);
229 public void Empty_Size ()
231 Assert.AreEqual (Size.Empty, Rect.Empty.Size);
235 public void IsEmpty ()
237 Assert.IsTrue (Rect.Empty.IsEmpty);
238 Assert.IsFalse ((new Rect (5, 5, 5, 5)).IsEmpty);
242 public void Location ()
244 Rect r = new Rect (0, 0, 0, 0);
246 r.Location = new Point (10, 15);
247 Assert.AreEqual (10, r.X);
248 Assert.AreEqual (15, r.Y);
252 public void RectSize ()
254 Rect r = new Rect (0, 0, 5, 5);
256 r.Size = new Size (10, 15);
257 Assert.AreEqual (10, r.Width);
258 Assert.AreEqual (15, r.Height);
262 [SetCulture ("en-us")]
263 public void ToStringTest ()
265 Rect r = new Rect (1.0, 2.5, 3, 4);
267 string expectedStringOutput = "1,2.5,3,4";
268 Assert.AreEqual (expectedStringOutput, r.ToString ());
269 Assert.AreEqual (expectedStringOutput, r.ToString (null));
270 Assert.AreEqual ("Empty", Rect.Empty.ToString ());
272 // IFormattable.ToString
273 IFormattable rFormattable = r;
274 Assert.AreEqual (expectedStringOutput,
275 rFormattable.ToString (null, null),
276 "IFormattable.ToString with null format");
277 Assert.AreEqual (expectedStringOutput,
278 rFormattable.ToString (string.Empty, null),
279 "IFormattable.ToString with empty format");
280 Assert.AreEqual ("1.00,2.50,3.00,4.00",
281 rFormattable.ToString ("N2", null),
282 "IFormattable.ToString with N2 format");
283 Assert.AreEqual ("blah,blah,blah,blah",
284 rFormattable.ToString ("blah", null),
285 "IFormattable.ToString with blah format");
286 Assert.AreEqual (":,:,:,:",
287 rFormattable.ToString (":", null),
288 "IFormattable.ToString with : format");
289 Assert.AreEqual ("Empty",
290 ((IFormattable) Rect.Empty).ToString ("blah", null),
291 "IFormattable.ToString on Rect.Empty with blah format");
293 foreach (CultureInfo culture in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
294 if (culture.IsNeutralCulture)
296 string separator = ",";
297 if (culture.NumberFormat.NumberDecimalSeparator == separator)
299 expectedStringOutput =
300 1.ToString (culture) + separator +
301 2.5.ToString (culture) + separator +
302 3.ToString (culture) + separator +
303 4.ToString (culture);
304 Assert.AreEqual (expectedStringOutput,
305 r.ToString (culture),
306 "ToString with Culture: " + culture.Name);
307 Assert.AreEqual ("Empty",
308 Rect.Empty.ToString (culture),
309 "ToString on Empty with Culture: " + culture.Name);
311 // IFormattable.ToString
312 Assert.AreEqual (expectedStringOutput,
313 rFormattable.ToString (null, culture),
314 "IFormattable.ToString with null format with Culture: " + culture.Name);
315 Assert.AreEqual (expectedStringOutput,
316 rFormattable.ToString (string.Empty, culture),
317 "IFormattable.ToString with empty format with Culture: " + culture.Name);
318 expectedStringOutput =
319 1.ToString ("N2", culture) + separator +
320 2.5.ToString ("N2", culture) + separator +
321 3.ToString ("N2", culture) + separator +
322 4.ToString ("N2", culture);
323 Assert.AreEqual (expectedStringOutput,
324 rFormattable.ToString ("N2", culture),
325 "IFormattable.ToString with N2 format with Culture: " + culture.Name);
330 public void ToString_FormatException ()
332 // This test does not currently work because
333 // String.Format does not throw all necessary exceptions
334 IFormattable rFormattable = new Rect (1.0, 2.5, 3, 4);
335 bool exceptionRaised = false;
337 rFormattable.ToString ("{", null);
338 } catch (FormatException) {
339 exceptionRaised = true;
341 Assert.IsTrue (exceptionRaised, "Expected FormatException with IFormattable.ToString (\"{\", null)");
347 Rect r = Rect.Parse ("1 , 2, 3, 4");
348 Assert.AreEqual (new Rect (1, 2, 3, 4), r);
352 public void Parse2 ()
354 Rect r = Rect.Parse ("1 2 3 4");
355 Assert.AreEqual (new Rect (1, 2, 3, 4), r);
359 public void Parse3 ()
361 Rect r = Rect.Parse (" 1 2 3 4 ");
362 Assert.AreEqual (new Rect (1, 2, 3, 4), r);
366 public void ParseWithBothSeparators ()
368 Rect.Parse ("1.0, 3 2.0, 5.0");
372 [ExpectedException (typeof (ArgumentException))]
373 public void ParseNegative ()
375 Rect.Parse ("1, 2, -3, -4");
379 [ExpectedException (typeof (InvalidOperationException))] // "Premature string termination encountered."
380 public void Parse3Doubles ()
382 Rect.Parse ("1.0, 3, -5");
386 [ExpectedException (typeof (FormatException))]
387 public void ParseInvalidString1 ()
389 Rect.Parse ("1.0, 3, -x, 5.0");
393 [ExpectedException (typeof (InvalidOperationException))]
394 public void ParseInvalidString3 ()
396 Rect.Parse ("1.0, 3, 2.0, 5.0, 2");
400 [ExpectedException (typeof (FormatException))]
401 public void ParseInvalidString4 ()
403 Rect.Parse ("1.0-3, 2.0, 5.0, 2");
407 [ExpectedException (typeof (InvalidOperationException))]
408 public void ParseInvalidString5 ()
410 Rect.Parse ("1.0, 2.0, 5.0, 2,");
414 public void ParseInvalidString6 ()
416 Rect.Parse ("\n1.0, 2.0, 5.0, 2");
420 [ExpectedException (typeof (InvalidOperationException))]
421 public void ParseInvalidString7 ()
423 Rect r = Rect.Parse ("1,, 2, 3, 4");
424 Assert.AreEqual (new Rect (1, 2, 3, 4), r);
428 public void Equals ()
430 Rect r1 = new Rect (1, 2, 3, 4);
433 Assert.IsTrue (r1.Equals (r1));
436 Assert.IsFalse (r1.Equals (r2));
440 Assert.IsFalse (r1.Equals (r2));
444 Assert.IsFalse (r1.Equals (r2));
448 Assert.IsFalse (r1.Equals (r2));
449 r2.Height = r1.Height;
451 Assert.IsFalse (r1.Equals (new object ()));
456 Assert.AreEqual (true, r1.Equals (r2));
457 Assert.AreEqual (true, r2.Equals (r1));
461 public void ContainsRect ()
463 Rect r = new Rect (0, 0, 50, 50);
466 Assert.IsTrue (r.Contains (new Rect (10, 10, 10, 10)));
469 Assert.IsFalse (r.Contains (new Rect (5, -5, 10, 10)));
471 // crosses right side
472 Assert.IsFalse (r.Contains (new Rect (5, 5, 50, 10)));
474 // crosses bottom side
475 Assert.IsFalse (r.Contains (new Rect (5, 5, 10, 50)));
478 Assert.IsFalse (r.Contains (new Rect (-5, 5, 10, 10)));
480 // completely outside (top)
481 Assert.IsFalse (r.Contains (new Rect (5, -5, 1, 1)));
483 // completely outside (right)
484 Assert.IsFalse (r.Contains (new Rect (75, 5, 1, 1)));
486 // completely outside (bottom)
487 Assert.IsFalse (r.Contains (new Rect (5, 75, 1, 1)));
489 // completely outside (left)
490 Assert.IsFalse (r.Contains (new Rect (-25, 5, 1, 1)));
494 public void ContainsDoubles ()
496 Rect r = new Rect (0, 0, 50, 50);
498 Assert.IsTrue (r.Contains (10, 10));
499 Assert.IsFalse (r.Contains (-5, -5));
503 public void ContainsPoint ()
505 Rect r = new Rect (0, 0, 50, 50);
507 Assert.IsTrue (r.Contains (new Point (10, 10)));
508 Assert.IsFalse (r.Contains (new Point (-5, -5)));
512 public void Inflate ()
514 Rect r = Rect.Inflate (new Rect (0, 0, 20, 20), 10, 15);
515 Assert.AreEqual (new Rect (-10, -15, 40, 50), r);
517 r = Rect.Inflate (new Rect (0, 0, 20, 20), new Size (10, 15));
518 Assert.AreEqual (new Rect (-10, -15, 40, 50), r);
520 r = new Rect (0, 0, 20, 20);
522 Assert.AreEqual (new Rect (-10, -15, 40, 50), r);
524 r = new Rect (0, 0, 20, 20);
525 r.Inflate (new Size (10, 15));
526 Assert.AreEqual (new Rect (-10, -15, 40, 50), r);
530 public void IntersectsWith ()
532 Rect r = new Rect (0, 0, 50, 50);
535 Assert.IsTrue (r.IntersectsWith (new Rect (10, 10, 10, 10)));
538 Assert.IsTrue (r.IntersectsWith (new Rect (5, -5, 10, 10)));
540 // crosses right side
541 Assert.IsTrue (r.IntersectsWith (new Rect (5, 5, 50, 10)));
543 // crosses bottom side
544 Assert.IsTrue (r.IntersectsWith (new Rect (5, 5, 10, 50)));
547 Assert.IsTrue (r.IntersectsWith (new Rect (-5, 5, 10, 10)));
549 // completely outside (top)
550 Assert.IsFalse (r.IntersectsWith (new Rect (5, -5, 1, 1)));
552 // completely outside (right)
553 Assert.IsFalse (r.IntersectsWith (new Rect (75, 5, 1, 1)));
555 // completely outside (bottom)
556 Assert.IsFalse (r.IntersectsWith (new Rect (5, 75, 1, 1)));
558 // completely outside (left)
559 Assert.IsFalse (r.IntersectsWith (new Rect (-25, 5, 1, 1)));
563 public void Intersect ()
568 r = new Rect (0, 0, 50, 50);
569 r.Intersect (new Rect (10, 10, 10, 10));
570 Assert.AreEqual (new Rect (10, 10, 10, 10), r);
573 r = new Rect (0, 0, 50, 50);
574 r.Intersect (new Rect (5, -5, 10, 10));
575 Assert.AreEqual (new Rect (5, 0, 10, 5), r);
577 // crosses right side
578 r = new Rect (0, 0, 50, 50);
579 r.Intersect (new Rect (5, 5, 50, 10));
580 Assert.AreEqual (new Rect (5, 5, 45, 10), r);
582 // crosses bottom side
583 r = new Rect (0, 0, 50, 50);
584 r.Intersect (new Rect (5, 5, 10, 50));
585 Assert.AreEqual(new Rect(5, 5, 10, 45), r);
588 r = new Rect (0, 0, 50, 50);
589 r.Intersect (new Rect (-5, 5, 10, 10));
590 Assert.AreEqual(new Rect(0, 5, 5, 10), r);
592 // completely outside (top)
593 r = new Rect (0, 0, 50, 50);
594 r.Intersect (new Rect (5, -5, 1, 1));
595 Assert.AreEqual (Rect.Empty, r);
597 // completely outside (right)
598 r = new Rect (0, 0, 50, 50);
599 r.Intersect (new Rect (75, 5, 1, 1));
600 Assert.AreEqual (Rect.Empty, r);
602 // completely outside (bottom)
603 r = new Rect (0, 0, 50, 50);
604 r.Intersect (new Rect (5, 75, 1, 1));
605 Assert.AreEqual (Rect.Empty, r);
607 // completely outside (left)
608 r = new Rect (0, 0, 50, 50);
609 r.Intersect (new Rect (-25, 5, 1, 1));
610 Assert.AreEqual (Rect.Empty, r);
619 r = new Rect(0, 0, 50, 50);
620 r.Union(new Rect(10, 10, 10, 10));
621 Assert.AreEqual(new Rect(0, 0, 50, 50), r);
624 r = new Rect(0, 0, 50, 50);
625 r.Union(new Rect(5, -5, 10, 10));
626 Assert.AreEqual(new Rect(0, -5, 50, 55), r);
628 // crosses right side
629 r = new Rect(0, 0, 50, 50);
630 r.Union(new Rect(5, 5, 50, 10));
631 Assert.AreEqual(new Rect(0, 0, 55, 50), r);
633 // crosses bottom side
634 r = new Rect(0, 0, 50, 50);
635 r.Union(new Rect(5, 5, 10, 50));
636 Assert.AreEqual(new Rect(0, 0, 50, 55), r);
639 r = new Rect(0, 0, 50, 50);
640 r.Union(new Rect(-5, 5, 10, 10));
641 Assert.AreEqual(new Rect(-5, 0, 55, 50), r);
643 // completely outside (top)
644 r = new Rect(0, 0, 50, 50);
645 r.Union(new Rect(5, -5, 1, 1));
646 Assert.AreEqual(new Rect(0, -5, 50, 55), r);
648 // completely outside (right)
649 r = new Rect(0, 0, 50, 50);
650 r.Union(new Rect(75, 5, 1, 1));
651 Assert.AreEqual(new Rect(0, 0, 76, 50), r);
653 // completely outside (bottom)
654 r = new Rect(0, 0, 50, 50);
655 r.Union(new Rect(5, 75, 1, 1));
656 Assert.AreEqual(new Rect(0,0, 50, 76), r);
658 // completely outside (left)
659 r = new Rect(0, 0, 50, 50);
660 r.Union(new Rect(-25, 5, 1, 1));
661 Assert.AreEqual(new Rect(-25, 0, 75, 50), r);
665 public void Equals_Operator ()
667 Rect r1 = new Rect (1, 2, 30, 30);
668 Rect r2 = new Rect (1, 2, 30, 30);
670 Assert.AreEqual (true, r1 == r2);
671 Assert.AreEqual (false, r1 != r2);
673 r2 = new Rect (10, 20, 30, 30);
675 Assert.AreEqual (false, r1 == r2);
676 Assert.AreEqual (true, r1 != r2);
681 Assert.AreEqual (true, r1 == r2);
682 Assert.AreEqual (false, r1 != r2);