In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TextureBrushTest.cs
1 //
2 // System.Drawing.TextureBrush 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.ComponentModel;
31 using System.Drawing;
32 using System.Drawing.Drawing2D;
33 using System.Drawing.Imaging;
34 using System.Security.Permissions;
35 using NUnit.Framework;
36
37 namespace MonoTests.System.Drawing {
38
39         [TestFixture]
40         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
41         public class TextureBrushTest {
42
43                 private Image image;
44                 private Rectangle rect;
45                 private RectangleF rectf;
46                 private ImageAttributes attr;
47                 private Bitmap bmp;
48
49                 [TestFixtureSetUp]
50                 public void FixtureSetUp ()
51                 {
52                         image = new Bitmap (10, 10);
53                         rect = new Rectangle (0, 0, 10, 10);
54                         rectf = new RectangleF (0, 0, 10, 10);
55                         attr = new ImageAttributes ();
56                         bmp = new Bitmap (50, 50);
57                 }
58
59                 private void Common (TextureBrush t, WrapMode wm)
60                 {
61                         using (Image img = t.Image) {
62                                 Assert.IsNotNull (img, "Image");
63                         }
64                         Assert.IsFalse (Object.ReferenceEquals (image, t.Image), "Image-Equals");
65                         Assert.IsTrue (t.Transform.IsIdentity, "Transform.IsIdentity");
66                         Assert.AreEqual (wm, t.WrapMode, "WrapMode");
67                 }
68
69                 [Test]
70                 [ExpectedException (typeof (ArgumentNullException))]
71                 public void CtorImage_Null ()
72                 {
73                         new TextureBrush (null);
74                 }
75
76                 [Test]
77                 public void CtorImage ()
78                 {
79                         TextureBrush t = new TextureBrush (image);
80                         Common (t, WrapMode.Tile);
81                 }
82
83                 [Test]
84                 [ExpectedException (typeof (ArgumentNullException))]
85                 public void CtorImage_Null_WrapMode ()
86                 {
87                         new TextureBrush (null, WrapMode.Clamp);
88                 }
89
90                 [Test]
91                 public void CtorImageWrapMode ()
92                 {
93                         foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
94                                 TextureBrush t = new TextureBrush (image, wm);
95                                 Common (t, wm);
96                         }
97                 }
98
99                 [Test]
100                 [ExpectedException (typeof (InvalidEnumArgumentException))]
101                 public void CtorImageWrapMode_Invalid ()
102                 {
103                         new TextureBrush (image, (WrapMode) Int32.MinValue);
104                 }
105
106                 [Test]
107                 [ExpectedException (typeof (ArgumentNullException))]
108                 public void CtorImage_Null_Rectangle ()
109                 {
110                         new TextureBrush (null, rect);
111                 }
112
113                 [Test]
114                 [ExpectedException (typeof (OutOfMemoryException))]
115                 public void CtorImageRectangle_Empty ()
116                 {
117                         new TextureBrush (image, new Rectangle ());
118                 }
119
120                 [Test]
121                 public void CtorImageRectangle ()
122                 {
123                         TextureBrush t = new TextureBrush (image, rect);
124                         Common (t, WrapMode.Tile);
125                 }
126
127                 [Test]
128                 [ExpectedException (typeof (ArgumentNullException))]
129                 public void CtorImage_Null_RectangleF ()
130                 {
131                         new TextureBrush (null, rectf);
132                 }
133
134                 [Test]
135                 [ExpectedException (typeof (OutOfMemoryException))]
136                 public void CtorImageRectangleF_Empty ()
137                 {
138                         new TextureBrush (image, new RectangleF ());
139                 }
140
141                 [Test]
142                 public void CtorImageRectangleF ()
143                 {
144                         TextureBrush t = new TextureBrush (image, rectf);
145                         Common (t, WrapMode.Tile);
146                 }
147
148                 [Test]
149                 [ExpectedException (typeof (ArgumentNullException))]
150                 public void CtorImage_Null_RectangleAttributes ()
151                 {
152                         new TextureBrush (null, rect, attr);
153                 }
154
155                 [Test]
156                 [ExpectedException (typeof (OutOfMemoryException))]
157                 public void CtorImageRectangle_Empty_Attributes ()
158                 {
159                         new TextureBrush (image, new Rectangle (), attr);
160                 }
161
162                 [Test]
163                 public void CtorImageRectangleAttributes_Null ()
164                 {
165                         TextureBrush t = new TextureBrush (image, rect, null);
166                         Common (t, WrapMode.Tile);
167                 }
168
169                 [Test]
170                 public void CtorImageRectangleAttributes ()
171                 {
172                         TextureBrush t = new TextureBrush (image, rect, attr);
173                         Common (t, WrapMode.Clamp);
174                 }
175
176                 [Test]
177                 [ExpectedException (typeof (ArgumentNullException))]
178                 public void CtorImage_Null_RectangleFAttributes ()
179                 {
180                         new TextureBrush (null, rectf, attr);
181                 }
182
183                 [Test]
184                 [ExpectedException (typeof (OutOfMemoryException))]
185                 public void CtorImageRectangleF_Empty_Attributes ()
186                 {
187                         new TextureBrush (image, new RectangleF ());
188                 }
189
190                 [Test]
191                 public void CtorImageRectangleFAttributes_Null ()
192                 {
193                         TextureBrush t = new TextureBrush (image, rectf, null);
194                         Common (t, WrapMode.Tile);
195                 }
196
197                 [Test]
198                 public void CtorImageRectangleFAttributes ()
199                 {
200                         TextureBrush t = new TextureBrush (image, rectf, attr);
201                         Common (t, WrapMode.Clamp);
202                 }
203
204                 [Test]
205                 public void CtorImageWrapModeRectangle ()
206                 {
207                         foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
208                                 TextureBrush t = new TextureBrush (image, wm, rect);
209                                 Common (t, wm);
210                         }
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (InvalidEnumArgumentException))]
215                 public void CtorImageWrapMode_Invalid_Rectangle ()
216                 {
217                         new TextureBrush (image, (WrapMode) Int32.MinValue, rect);
218                 }
219
220                 [Test]
221                 public void CtorImageWrapModeRectangleF ()
222                 {
223                         foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
224                                 TextureBrush t = new TextureBrush (image, wm, rectf);
225                                 Common (t, wm);
226                         }
227                 }
228
229                 [Test]
230                 [ExpectedException (typeof (InvalidEnumArgumentException))]
231                 public void CtorImageWrapMode_Invalid_RectangleF ()
232                 {
233                         new TextureBrush (image, (WrapMode) Int32.MinValue, rectf);
234                 }
235
236                 [Test]
237                 public void TextureBush_RectangleInsideBitmap ()
238                 {
239                         Rectangle r = new Rectangle (10, 10, 40, 40);
240                         Assert.IsTrue (r.Y + r.Height <= bmp.Height, "Height");
241                         Assert.IsTrue (r.X + r.Width <= bmp.Width, "Width");
242                         TextureBrush b = new TextureBrush (bmp, r);
243                         using (Image img = b.Image) {
244                                 Assert.AreEqual (r.Height, img.Height, "Image.Height");
245                                 Assert.AreEqual (r.Width, img.Width, "Image.Width");
246                         }
247                         Assert.IsTrue (b.Transform.IsIdentity, "Transform.IsIdentity");
248                         Assert.AreEqual (WrapMode.Tile, b.WrapMode, "WrapMode");
249                 }
250
251                 [Test]
252                 [ExpectedException (typeof (OutOfMemoryException))]
253                 public void TextureBush_RectangleOutsideBitmap ()
254                 {
255                         Rectangle r = new Rectangle (50, 50, 50, 50);
256                         Assert.IsFalse (r.Y + r.Height <= bmp.Height, "Height");
257                         Assert.IsFalse (r.X + r.Width <= bmp.Width, "Width");
258                         new TextureBrush (bmp, r);
259                 }
260
261                 [Test]
262                 [ExpectedException (typeof (ArgumentNullException))]
263                 public void Transform_Null ()
264                 {
265                         new TextureBrush (image).Transform = null;
266                 }
267
268                 [Test]
269                 public void Transform ()
270                 {
271                         Matrix m = new Matrix ();
272                         TextureBrush t = new TextureBrush (image);
273                         t.Transform = m;
274                         Assert.IsFalse (Object.ReferenceEquals (m, t.Transform));
275                 }
276
277                 [Test]
278                 public void WrapMode_Valid ()
279                 {
280                         foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
281                                 TextureBrush t = new TextureBrush (image);
282                                 t.WrapMode = wm;
283                                 Assert.AreEqual (wm, t.WrapMode, wm.ToString ());
284                         }
285                 }
286
287                 [Test]
288                 [ExpectedException (typeof (InvalidEnumArgumentException))]
289                 public void WrapMode_Invalid ()
290                 {
291                         new TextureBrush (image).WrapMode = (WrapMode)Int32.MinValue;
292                 }
293
294                 [Test]
295                 public void Clone ()
296                 {
297                         TextureBrush t = new TextureBrush (image);
298                         TextureBrush clone = (TextureBrush) t.Clone ();
299                         Common (clone, t.WrapMode);
300                 }
301
302                 [Test]
303                 [ExpectedException (typeof (ArgumentException))]
304                 public void Dispose_Clone ()
305                 {
306                         TextureBrush t = new TextureBrush (image);
307                         t.Dispose ();
308                         t.Clone ();
309                 }
310
311                 [Test]
312                 public void Dispose_Dispose ()
313                 {
314                         TextureBrush t = new TextureBrush (image);
315                         t.Dispose ();
316                         t.Dispose ();
317                 }
318
319                 [Test]
320                 [NUnit.Framework.Category ("NotDotNet")] // AccessViolationException under 2.0
321                 [ExpectedException (typeof (ArgumentException))]
322                 public void Dispose_Image ()
323                 {
324                         TextureBrush t = new TextureBrush (image);
325                         t.Dispose ();
326                         Assert.IsNotNull (t.Image, "Image");
327                 }
328
329                 [Test]
330                 [ExpectedException (typeof (ArgumentNullException))]
331                 public void MultiplyTransform_Null ()
332                 {
333                         new TextureBrush (image).MultiplyTransform (null);
334                 }
335
336                 [Test]
337                 [ExpectedException (typeof (ArgumentNullException))]
338                 public void MultiplyTransform_Null_Order ()
339                 {
340                         new TextureBrush (image).MultiplyTransform (null, MatrixOrder.Append);
341                 }
342
343                 [Test]
344                 public void MultiplyTransformOrder_Invalid ()
345                 {
346                         TextureBrush t = new TextureBrush (image);
347                         t.MultiplyTransform (new Matrix (), (MatrixOrder) Int32.MinValue);
348                 }
349
350                 [Test]
351                 [ExpectedException (typeof (ArgumentException))]
352                 public void MultiplyTransform_NonInvertible ()
353                 {
354                         TextureBrush t = new TextureBrush (image);
355                         Matrix noninvertible = new Matrix (123, 24, 82, 16, 47, 30);
356                         t.MultiplyTransform (noninvertible);
357                 }
358
359                 [Test]
360                 public void ResetTransform ()
361                 {
362                         TextureBrush t = new TextureBrush (image);
363                         t.RotateTransform (90);
364                         Assert.IsFalse (t.Transform.IsIdentity, "Transform.IsIdentity");
365                         t.ResetTransform ();
366                         Assert.IsTrue (t.Transform.IsIdentity, "Reset.IsIdentity");
367                 }
368
369                 [Test]
370                 public void RotateTransform ()
371                 {
372                         TextureBrush t = new TextureBrush (image);
373                         t.RotateTransform (90);
374                         float[] elements = t.Transform.Elements;
375                         Assert.AreEqual (0, elements[0], 0.1, "matrix.0");
376                         Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
377                         Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
378                         Assert.AreEqual (0, elements[3], 0.1, "matrix.3");
379                         Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
380                         Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
381
382                         t.RotateTransform (270);
383                         Assert.IsTrue (t.Transform.IsIdentity, "Transform.IsIdentity");
384                 }
385
386                 [Test]
387                 [ExpectedException (typeof (ArgumentException))]
388                 public void RotateTransform_InvalidOrder ()
389                 {
390                         TextureBrush t = new TextureBrush (image);
391                         t.RotateTransform (720, (MatrixOrder) Int32.MinValue);
392                 }
393
394                 [Test]
395                 public void ScaleTransform ()
396                 {
397                         TextureBrush t = new TextureBrush (image);
398                         t.ScaleTransform (2, 4);
399                         float[] elements = t.Transform.Elements;
400                         Assert.AreEqual (2, elements[0], 0.1, "matrix.0");
401                         Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
402                         Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
403                         Assert.AreEqual (4, elements[3], 0.1, "matrix.3");
404                         Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
405                         Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
406
407                         t.ScaleTransform (0.5f, 0.25f);
408                         Assert.IsTrue (t.Transform.IsIdentity, "Transform.IsIdentity");
409                 }
410
411                 [Test]
412                 public void ScaleTransform_MaxMin ()
413                 {
414                         TextureBrush t = new TextureBrush (image);
415                         t.ScaleTransform (Single.MaxValue, Single.MinValue);
416                         float[] elements = t.Transform.Elements;
417                         Assert.AreEqual (Single.MaxValue, elements[0], 1e33, "matrix.0");
418                         Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
419                         Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
420                         Assert.AreEqual (Single.MinValue, elements[3], 1e33, "matrix.3");
421                         Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
422                         Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
423                 }
424
425                 [Test]
426                 [ExpectedException (typeof (ArgumentException))]
427                 public void ScaleTransform_InvalidOrder ()
428                 {
429                         TextureBrush t = new TextureBrush (image);
430                         t.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
431                 }
432
433                 [Test]
434                 public void TranslateTransform ()
435                 {
436                         TextureBrush t = new TextureBrush (image);
437                         t.TranslateTransform (1, 1);
438                         float[] elements = t.Transform.Elements;
439                         Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
440                         Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
441                         Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
442                         Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
443                         Assert.AreEqual (1, elements[4], 0.1, "matrix.4");
444                         Assert.AreEqual (1, elements[5], 0.1, "matrix.5");
445
446                         t.TranslateTransform (-1, -1);
447                         elements = t.Transform.Elements;
448                         Assert.AreEqual (1, elements[0], 0.1, "revert.matrix.0");
449                         Assert.AreEqual (0, elements[1], 0.1, "revert.matrix.1");
450                         Assert.AreEqual (0, elements[2], 0.1, "revert.matrix.2");
451                         Assert.AreEqual (1, elements[3], 0.1, "revert.matrix.3");
452                         Assert.AreEqual (0, elements[4], 0.1, "revert.matrix.4");
453                         Assert.AreEqual (0, elements[5], 0.1, "revert.matrix.5");
454                 }
455
456                 [Test]
457                 [ExpectedException (typeof (ArgumentException))]
458                 public void TranslateTransform_InvalidOrder ()
459                 {
460                         TextureBrush t = new TextureBrush (image);
461                         t.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);
462                 }
463         }
464 }