2 // System.Drawing.TextureBrush unit tests
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.ComponentModel;
32 using System.Drawing.Drawing2D;
33 using System.Drawing.Imaging;
34 using System.Security.Permissions;
35 using NUnit.Framework;
37 namespace MonoTests.System.Drawing {
40 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
41 public class TextureBrushTest {
44 private Rectangle rect;
45 private RectangleF rectf;
46 private ImageAttributes attr;
50 public void FixtureSetUp ()
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);
59 private void Common (TextureBrush t, WrapMode wm)
61 using (Image img = t.Image) {
62 Assert.IsNotNull (img, "Image");
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");
70 [ExpectedException (typeof (ArgumentNullException))]
71 public void CtorImage_Null ()
73 new TextureBrush (null);
77 public void CtorImage ()
79 TextureBrush t = new TextureBrush (image);
80 Common (t, WrapMode.Tile);
84 [ExpectedException (typeof (ArgumentNullException))]
85 public void CtorImage_Null_WrapMode ()
87 new TextureBrush (null, WrapMode.Clamp);
91 public void CtorImageWrapMode ()
93 foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
94 TextureBrush t = new TextureBrush (image, wm);
100 [ExpectedException (typeof (InvalidEnumArgumentException))]
101 public void CtorImageWrapMode_Invalid ()
103 new TextureBrush (image, (WrapMode) Int32.MinValue);
107 [ExpectedException (typeof (ArgumentNullException))]
108 public void CtorImage_Null_Rectangle ()
110 new TextureBrush (null, rect);
114 [ExpectedException (typeof (OutOfMemoryException))]
115 public void CtorImageRectangle_Empty ()
117 new TextureBrush (image, new Rectangle ());
121 public void CtorImageRectangle ()
123 TextureBrush t = new TextureBrush (image, rect);
124 Common (t, WrapMode.Tile);
128 [ExpectedException (typeof (ArgumentNullException))]
129 public void CtorImage_Null_RectangleF ()
131 new TextureBrush (null, rectf);
135 [ExpectedException (typeof (OutOfMemoryException))]
136 public void CtorImageRectangleF_Empty ()
138 new TextureBrush (image, new RectangleF ());
142 public void CtorImageRectangleF ()
144 TextureBrush t = new TextureBrush (image, rectf);
145 Common (t, WrapMode.Tile);
149 [ExpectedException (typeof (ArgumentNullException))]
150 public void CtorImage_Null_RectangleAttributes ()
152 new TextureBrush (null, rect, attr);
156 [ExpectedException (typeof (OutOfMemoryException))]
157 public void CtorImageRectangle_Empty_Attributes ()
159 new TextureBrush (image, new Rectangle (), attr);
163 public void CtorImageRectangleAttributes_Null ()
165 TextureBrush t = new TextureBrush (image, rect, null);
166 Common (t, WrapMode.Tile);
170 public void CtorImageRectangleAttributes ()
172 TextureBrush t = new TextureBrush (image, rect, attr);
173 Common (t, WrapMode.Clamp);
177 [ExpectedException (typeof (ArgumentNullException))]
178 public void CtorImage_Null_RectangleFAttributes ()
180 new TextureBrush (null, rectf, attr);
184 [ExpectedException (typeof (OutOfMemoryException))]
185 public void CtorImageRectangleF_Empty_Attributes ()
187 new TextureBrush (image, new RectangleF ());
191 public void CtorImageRectangleFAttributes_Null ()
193 TextureBrush t = new TextureBrush (image, rectf, null);
194 Common (t, WrapMode.Tile);
198 public void CtorImageRectangleFAttributes ()
200 TextureBrush t = new TextureBrush (image, rectf, attr);
201 Common (t, WrapMode.Clamp);
205 public void CtorImageWrapModeRectangle ()
207 foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
208 TextureBrush t = new TextureBrush (image, wm, rect);
214 [ExpectedException (typeof (InvalidEnumArgumentException))]
215 public void CtorImageWrapMode_Invalid_Rectangle ()
217 new TextureBrush (image, (WrapMode) Int32.MinValue, rect);
221 public void CtorImageWrapModeRectangleF ()
223 foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
224 TextureBrush t = new TextureBrush (image, wm, rectf);
230 [ExpectedException (typeof (InvalidEnumArgumentException))]
231 public void CtorImageWrapMode_Invalid_RectangleF ()
233 new TextureBrush (image, (WrapMode) Int32.MinValue, rectf);
237 public void TextureBush_RectangleInsideBitmap ()
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");
247 Assert.IsTrue (b.Transform.IsIdentity, "Transform.IsIdentity");
248 Assert.AreEqual (WrapMode.Tile, b.WrapMode, "WrapMode");
252 [ExpectedException (typeof (OutOfMemoryException))]
253 public void TextureBush_RectangleOutsideBitmap ()
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);
262 [ExpectedException (typeof (ArgumentNullException))]
263 public void Transform_Null ()
265 new TextureBrush (image).Transform = null;
269 public void Transform ()
271 Matrix m = new Matrix ();
272 TextureBrush t = new TextureBrush (image);
274 Assert.IsFalse (Object.ReferenceEquals (m, t.Transform));
278 public void WrapMode_Valid ()
280 foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
281 TextureBrush t = new TextureBrush (image);
283 Assert.AreEqual (wm, t.WrapMode, wm.ToString ());
288 [ExpectedException (typeof (InvalidEnumArgumentException))]
289 public void WrapMode_Invalid ()
291 new TextureBrush (image).WrapMode = (WrapMode)Int32.MinValue;
297 TextureBrush t = new TextureBrush (image);
298 TextureBrush clone = (TextureBrush) t.Clone ();
299 Common (clone, t.WrapMode);
303 [ExpectedException (typeof (ArgumentException))]
304 public void Dispose_Clone ()
306 TextureBrush t = new TextureBrush (image);
312 public void Dispose_Dispose ()
314 TextureBrush t = new TextureBrush (image);
320 [NUnit.Framework.Category ("NotDotNet")] // AccessViolationException under 2.0
321 [ExpectedException (typeof (ArgumentException))]
322 public void Dispose_Image ()
324 TextureBrush t = new TextureBrush (image);
326 Assert.IsNotNull (t.Image, "Image");
330 [ExpectedException (typeof (ArgumentNullException))]
331 public void MultiplyTransform_Null ()
333 new TextureBrush (image).MultiplyTransform (null);
337 [ExpectedException (typeof (ArgumentNullException))]
338 public void MultiplyTransform_Null_Order ()
340 new TextureBrush (image).MultiplyTransform (null, MatrixOrder.Append);
344 public void MultiplyTransformOrder_Invalid ()
346 TextureBrush t = new TextureBrush (image);
347 t.MultiplyTransform (new Matrix (), (MatrixOrder) Int32.MinValue);
351 [ExpectedException (typeof (ArgumentException))]
352 public void MultiplyTransform_NonInvertible ()
354 TextureBrush t = new TextureBrush (image);
355 Matrix noninvertible = new Matrix (123, 24, 82, 16, 47, 30);
356 t.MultiplyTransform (noninvertible);
360 public void ResetTransform ()
362 TextureBrush t = new TextureBrush (image);
363 t.RotateTransform (90);
364 Assert.IsFalse (t.Transform.IsIdentity, "Transform.IsIdentity");
366 Assert.IsTrue (t.Transform.IsIdentity, "Reset.IsIdentity");
370 public void RotateTransform ()
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");
382 t.RotateTransform (270);
383 Assert.IsTrue (t.Transform.IsIdentity, "Transform.IsIdentity");
387 [ExpectedException (typeof (ArgumentException))]
388 public void RotateTransform_InvalidOrder ()
390 TextureBrush t = new TextureBrush (image);
391 t.RotateTransform (720, (MatrixOrder) Int32.MinValue);
395 public void ScaleTransform ()
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");
407 t.ScaleTransform (0.5f, 0.25f);
408 Assert.IsTrue (t.Transform.IsIdentity, "Transform.IsIdentity");
412 public void ScaleTransform_MaxMin ()
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");
426 [ExpectedException (typeof (ArgumentException))]
427 public void ScaleTransform_InvalidOrder ()
429 TextureBrush t = new TextureBrush (image);
430 t.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
434 public void TranslateTransform ()
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");
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");
457 [ExpectedException (typeof (ArgumentException))]
458 public void TranslateTransform_InvalidOrder ()
460 TextureBrush t = new TextureBrush (image);
461 t.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);