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