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