fda32858ec6709ebe341b8c5afc86ea93dbf7d7e
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / SolidBrushTest.cs
1 //
2 // System.Drawing.SolidBrush unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006-2007 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing;
31 using System.Security.Permissions;
32 using NUnit.Framework;
33
34 namespace MonoTests.System.Drawing {
35
36         [TestFixture]
37         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
38         public class SolidBrushTest {
39
40                 [Test]
41                 public void Transparent ()
42                 {
43                         SolidBrush sb = new SolidBrush (Color.Transparent);
44                         Assert.AreEqual (Color.Transparent, sb.Color, "Color");
45                         sb.Color = Color.Empty;
46                         SolidBrush clone = (SolidBrush) sb.Clone ();
47                         sb.Dispose ();
48                         Assert.AreEqual (Color.Empty.ToArgb (), clone.Color.ToArgb (), "Clone.Color");
49                 }
50
51                 [Test]
52                 public void Dispose_Color ()
53                 {
54                         SolidBrush sb = new SolidBrush (Color.Transparent);
55                         sb.Dispose ();
56                         Assert.AreEqual (Color.Transparent, sb.Color, "Color");
57                         // no exception - the call probably doesn't get to gdi+
58                 }
59
60                 [Test]
61                 [ExpectedException (typeof (ArgumentException))]
62                 public void Dispose_Clone ()
63                 {
64                         SolidBrush sb = new SolidBrush (Color.Transparent);
65                         sb.Dispose ();
66                         sb.Clone ();
67                 }
68
69                 [Test]
70                 public void Dispose_Dispose ()
71                 {
72                         SolidBrush sb = new SolidBrush (Color.Transparent);
73                         sb.Dispose ();
74                         sb.Dispose ();
75                 }
76
77                 [Test]
78                 public void FillRectangle ()
79                 {
80                         using (Bitmap bmp = new Bitmap (10, 10)) {
81                                 using (Graphics g = Graphics.FromImage (bmp)) {
82                                         SolidBrush sb = new SolidBrush (Color.Red);
83                                         g.FillRectangle (sb, 0, 0, 9, 9);
84                                         sb.Color = Color.Blue;
85                                         g.FillRectangle (sb, 4, 4, 5, 5);
86                                 }
87                                 Assert.AreEqual (Color.Red.ToArgb (), bmp.GetPixel (0, 0).ToArgb (), "0,0");
88                                 Assert.AreEqual (Color.Blue.ToArgb (), bmp.GetPixel (8, 8).ToArgb (), "8,8");
89                                 Assert.AreEqual (0, bmp.GetPixel (9, 9).ToArgb (), "9,9");
90                         }
91                 }
92
93                 [Test]
94                 public void DrawLine ()
95                 {
96                         using (Bitmap bmp = new Bitmap (10, 10)) {
97                                 using (Graphics g = Graphics.FromImage (bmp)) {
98                                         SolidBrush sb = new SolidBrush (Color.Red);
99                                         Pen p = new Pen (sb);
100                                         g.DrawLine (p, 0, 0, 9, 9);
101                                         sb.Color = Color.Blue;
102                                         g.DrawLine (p, 8, 8, 4, 4); // pen is still red
103                                 }
104                                 Assert.AreEqual (Color.Red.ToArgb (), bmp.GetPixel (0, 0).ToArgb (), "0,0");
105                                 Assert.AreEqual (Color.Red.ToArgb (), bmp.GetPixel (8, 8).ToArgb (), "8,8");
106                                 Assert.AreEqual (Color.Red.ToArgb (), bmp.GetPixel (9, 9).ToArgb (), "9,9"); // include end point
107                         }
108                         using (Bitmap bmp = new Bitmap (10, 10)) {
109                                 using (Graphics g = Graphics.FromImage (bmp)) {
110                                         SolidBrush sb = new SolidBrush (Color.Red);
111                                         Pen p = new Pen (sb);
112                                         g.DrawLine (p, float.NaN, float.NaN, 9, 9);
113                                 }
114                                 Assert.AreNotEqual (Color.Red.ToArgb (), bmp.GetPixel (0, 0).ToArgb (), "#DrawLine: Drew Line with NaN value when shouldn't have");
115                         }
116                 }
117
118                 [Test]
119                 public void Clone ()
120                 {
121                         using (SolidBrush sb = new SolidBrush (Color.Transparent)) {
122                                 // we still get a "named" color
123                                 Assert.AreEqual (Color.Transparent, sb.Color, "Color");
124                                 using (SolidBrush clone = (SolidBrush) sb.Clone ()) {
125                                         // but not after cloning the brush
126                                         Assert.IsFalse (Color.Transparent.Equals (clone.Color), "Color-Clone-Unnamed");
127                                         Assert.AreEqual (Color.Transparent.ToArgb (), clone.Color.ToArgb (), "Color-Clone-Argb");
128                                 }
129                         }
130                 }
131         }
132 }