Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / PaintEventArgsTest.cs
1 //
2 // System.Windows.Forms.PaintEventArgs unit tests
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2006 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.Drawing.Drawing2D;
32 using System.Windows.Forms;
33 using NUnit.Framework;
34
35 namespace MonoTests.System.Windows.Forms {
36
37         [TestFixture]
38         public class PaintEventArgsTest : TestHelper {
39
40                 private Graphics default_graphics;
41                 private Rectangle default_rect;
42
43                 [TestFixtureSetUp]
44                 public void FixtureSetUp ()
45                 {
46                         Bitmap bmp = new Bitmap (200, 200);
47                         default_graphics = Graphics.FromImage (bmp);
48                         default_rect = new Rectangle (Int32.MinValue, Int32.MinValue, Int32.MaxValue, Int32.MaxValue);
49                 }
50
51 #if NET_2_0
52                 [Test]
53                 [ExpectedException (typeof (ArgumentNullException))]
54                 public void Constructor_NullGraphics ()
55                 {
56                         new PaintEventArgs (null, default_rect);
57                 }
58 #else
59                 [Test]
60                 public void Constructor_NullGraphics ()
61                 {
62                         PaintEventArgs pea = new PaintEventArgs (null, default_rect);
63                         Assert.IsNull (pea.Graphics, "Graphics");
64                         Assert.AreEqual (default_rect, pea.ClipRectangle);
65                 }
66 #endif
67                 [Test]
68                 public void Constructor ()
69                 {
70                         PaintEventArgs pea = new PaintEventArgs (default_graphics, default_rect);
71                         Assert.AreSame (default_graphics, pea.Graphics, "Graphics");
72                         Assert.AreEqual (default_rect, pea.ClipRectangle);
73                 }
74
75                 [Test]
76                 public void Dispose ()
77                 {
78                         PaintEventArgs pea = new PaintEventArgs (default_graphics, default_rect);
79                         pea.Dispose ();
80                         // uho, under 2.0 we not really disposing the stuff - it means it's not ours to dispose!
81                         Assert.IsTrue (pea.Graphics.Transform.IsIdentity, "Graphics.Transform");
82                 }
83
84                 [Test]
85                 public void IDisposable_IDispose ()
86                 {
87                         Bitmap bmp = new Bitmap (1, 1);
88                         Graphics default_graphics = Graphics.FromImage (bmp);
89                         Rectangle default_rect = new Rectangle (Int32.MinValue, Int32.MinValue, Int32.MaxValue, Int32.MaxValue);
90
91                         PaintEventArgs pea = new PaintEventArgs (default_graphics, default_rect);
92                         (pea as IDisposable).Dispose ();
93                         // uho, under 2.0 we not really disposing the stuff - it means it's not ours to dispose!
94                         Assert.IsTrue (pea.Graphics.Transform.IsIdentity, "Graphics");
95                 }
96
97                 [Test]
98                 [ExpectedException (typeof (ArgumentException))]
99                 public void GraphicsDispose ()
100                 {
101                         PaintEventArgs pea = new PaintEventArgs (default_graphics, default_rect);
102                         pea.Graphics.Dispose ();
103                         // a disposed graphics won't accept to return it's transformation matrix
104                         Assert.IsTrue (pea.Graphics.Transform.IsIdentity, "Graphics");
105                 }
106
107                 public class PaintEventArgsTester: PaintEventArgs {
108
109                         public PaintEventArgsTester (Graphics graphics, Rectangle clipRect)
110                                 : base (graphics, clipRect)
111                         {
112                         }
113
114                         public void DisposeBool (bool disposing)
115                         {
116                                 base.Dispose (disposing);
117                         }
118                 }
119
120                 [Test]
121 #if NET_2_0
122                 // under MS runtime it throws an exception under nunit-console, but not when running under NUnit GUI
123                 [Category ("NotDotNet")]
124 #endif
125                 [ExpectedException (typeof (ArgumentException))]
126                 public void Dispose_True ()
127                 {
128                         Bitmap bmp = new Bitmap (1, 1);
129                         Graphics default_graphics = Graphics.FromImage (bmp);
130                         Rectangle default_rect = new Rectangle (Int32.MinValue, Int32.MinValue, Int32.MaxValue, Int32.MaxValue);
131
132                         PaintEventArgsTester pea = new PaintEventArgsTester (default_graphics, default_rect);
133                         pea.Graphics.Dispose ();
134                         pea.DisposeBool (true);
135                         Assert.IsTrue (pea.Graphics.Transform.IsIdentity, "Graphics.Transform");
136                 }
137
138                 [Test]
139 #if NET_2_0
140                 // under MS runtime it throws an exception under nunit-console, but not when running under NUnit GUI
141                 [Category ("NotDotNet")]
142 #endif
143                 [ExpectedException (typeof (ArgumentException))]
144                 public void Dispose_False ()
145                 {
146                         Bitmap bmp = new Bitmap (1, 1);
147                         Graphics default_graphics = Graphics.FromImage (bmp);
148                         Rectangle default_rect = new Rectangle (Int32.MinValue, Int32.MinValue, Int32.MaxValue, Int32.MaxValue);
149
150                         PaintEventArgsTester pea = new PaintEventArgsTester (default_graphics, default_rect);
151                         pea.Graphics.Dispose ();
152                         pea.DisposeBool (false);
153                         Assert.IsTrue (pea.Graphics.Transform.IsIdentity, "Graphics.Transform");
154                 }
155         }
156 }