LinearGradientBrushTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. //
  2. // System.Drawing.Drawing2D.LinearGradientBrush unit tests
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  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. using System;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. using System.Security.Permissions;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Drawing.Drawing2D {
  35. [TestFixture]
  36. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  37. public class LinearGradientBrushTest {
  38. private Point pt1;
  39. private Point pt2;
  40. private Color c1;
  41. private Color c2;
  42. private LinearGradientBrush default_brush;
  43. private Matrix empty_matrix;
  44. [TestFixtureSetUp]
  45. public void FixtureSetUp ()
  46. {
  47. pt1 = new Point (0, 0);
  48. pt2 = new Point (32, 32);
  49. c1 = Color.Blue;
  50. c2 = Color.Red;
  51. default_brush = new LinearGradientBrush (pt1, pt2, c1, c2);
  52. empty_matrix = new Matrix ();
  53. }
  54. private void CheckDefaultRectangle (string msg, RectangleF rect)
  55. {
  56. Assert.AreEqual (pt1.X, rect.X, msg + ".Rectangle.X");
  57. Assert.AreEqual (pt1.Y, rect.Y, msg + ".Rectangle.Y");
  58. Assert.AreEqual (pt2.X, rect.Width, msg + ".Rectangle.Width");
  59. Assert.AreEqual (pt2.Y, rect.Height, msg + ".Rectangle.Height");
  60. }
  61. private void CheckEmptyMatrix (Matrix matrix)
  62. {
  63. float[] elements = matrix.Elements;
  64. Assert.AreEqual (1, elements[0], "matrix.0");
  65. Assert.AreEqual (0, elements[1], "matrix.1");
  66. Assert.AreEqual (0, elements[2], "matrix.2");
  67. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  68. Assert.AreEqual (0, elements[4], "matrix.4");
  69. Assert.AreEqual (0, elements[5], "matrix.5");
  70. }
  71. private void CheckDefaultMatrix (Matrix matrix)
  72. {
  73. float[] elements = matrix.Elements;
  74. Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
  75. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  76. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  77. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  78. Assert.AreEqual (16, elements[4], "matrix.4");
  79. Assert.AreEqual (-16, elements[5], "matrix.5");
  80. }
  81. [Test]
  82. [NUnit.Framework.Category ("NotWorking")]
  83. public void Constructor4 ()
  84. {
  85. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  86. CheckDefaultRectangle ("4", lgb.Rectangle);
  87. Assert.AreEqual (1, lgb.Blend.Factors.Length, "Blend.Factors");
  88. Assert.AreEqual (1, lgb.Blend.Factors [0], "Blend.Factors [0]");
  89. Assert.AreEqual (1, lgb.Blend.Positions.Length, "Blend.Positions");
  90. Assert.IsFalse (lgb.GammaCorrection, "GammaCorrection");
  91. Assert.AreEqual (2, lgb.LinearColors.Length, "LinearColors");
  92. Assert.IsNotNull (lgb.Transform, "Transform");
  93. CheckDefaultMatrix (lgb.Transform);
  94. Assert.AreEqual (WrapMode.Tile, lgb.WrapMode, "WrapMode.Tile");
  95. lgb.WrapMode = WrapMode.TileFlipX;
  96. Assert.AreEqual (WrapMode.TileFlipX, lgb.WrapMode, "WrapMode.TileFlipX");
  97. lgb.WrapMode = WrapMode.TileFlipY;
  98. Assert.AreEqual (WrapMode.TileFlipY, lgb.WrapMode, "WrapMode.TileFlipY");
  99. lgb.WrapMode = WrapMode.TileFlipXY;
  100. Assert.AreEqual (WrapMode.TileFlipXY, lgb.WrapMode, "WrapMode.TileFlipXY");
  101. // can't set WrapMode.Clamp
  102. }
  103. [Test]
  104. [ExpectedException (typeof (ArgumentException))]
  105. public void InterpolationColors_Colors_InvalidBlend ()
  106. {
  107. // default Blend doesn't allow getting this property
  108. Assert.IsNotNull (default_brush.InterpolationColors.Colors);
  109. }
  110. [Test]
  111. [ExpectedException (typeof (ArgumentException))]
  112. public void InterpolationColors_Positions_InvalidBlend ()
  113. {
  114. // default Blend doesn't allow getting this property
  115. Assert.IsNotNull (default_brush.InterpolationColors.Positions);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (IndexOutOfRangeException))]
  119. public void LinearColors_Empty ()
  120. {
  121. default_brush.LinearColors = new Color[0];
  122. }
  123. [Test]
  124. [ExpectedException (typeof (IndexOutOfRangeException))]
  125. public void LinearColors_One ()
  126. {
  127. default_brush.LinearColors = new Color[1];
  128. }
  129. [Test]
  130. public void LinearColors_Two ()
  131. {
  132. Assert.AreEqual (Color.FromArgb (255, 0, 0, 255), default_brush.LinearColors[0], "0");
  133. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), default_brush.LinearColors[1], "1");
  134. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  135. lgb.LinearColors = new Color[2] { Color.Black, Color.White };
  136. // not the same, the alpha is changed to 255 so they can't compare
  137. Assert.AreEqual (Color.FromArgb (255, 0, 0, 0), lgb.LinearColors[0], "0");
  138. Assert.AreEqual (Color.FromArgb (255, 255, 255, 255), lgb.LinearColors[1], "1");
  139. }
  140. [Test]
  141. public void LinearColors_Three ()
  142. {
  143. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  144. lgb.LinearColors = new Color[3] { Color.Red, Color.Green, Color.Blue };
  145. // not the same, the alpha is changed to 255 so they can't compare
  146. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), lgb.LinearColors[0], "0");
  147. Assert.AreEqual (Color.FromArgb (255, 0, 128, 0), lgb.LinearColors[1], "1");
  148. }
  149. [Test]
  150. [ExpectedException (typeof (ArgumentNullException))]
  151. public void Transform_Null ()
  152. {
  153. default_brush.Transform = null;
  154. }
  155. [Test]
  156. public void Transform_Empty ()
  157. {
  158. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  159. lgb.Transform = new Matrix ();
  160. CheckEmptyMatrix (lgb.Transform);
  161. }
  162. [Test]
  163. public void WrapMode_AllValid ()
  164. {
  165. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  166. lgb.WrapMode = WrapMode.Tile;
  167. Assert.AreEqual (WrapMode.Tile, lgb.WrapMode, "WrapMode.Tile");
  168. lgb.WrapMode = WrapMode.TileFlipX;
  169. Assert.AreEqual (WrapMode.TileFlipX, lgb.WrapMode, "WrapMode.TileFlipX");
  170. lgb.WrapMode = WrapMode.TileFlipY;
  171. Assert.AreEqual (WrapMode.TileFlipY, lgb.WrapMode, "WrapMode.TileFlipY");
  172. lgb.WrapMode = WrapMode.TileFlipXY;
  173. Assert.AreEqual (WrapMode.TileFlipXY, lgb.WrapMode, "WrapMode.TileFlipXY");
  174. }
  175. [Test]
  176. [ExpectedException (typeof (ArgumentException))]
  177. public void WrapMode_Clamp ()
  178. {
  179. default_brush.WrapMode = WrapMode.Clamp;
  180. }
  181. [Test]
  182. [ExpectedException (typeof (InvalidEnumArgumentException))]
  183. public void WrapMode_Invalid ()
  184. {
  185. default_brush.WrapMode = (WrapMode) Int32.MinValue;
  186. }
  187. [Test]
  188. public void Clone ()
  189. {
  190. CheckDefaultRectangle ("default", default_brush.Rectangle);
  191. LinearGradientBrush clone = (LinearGradientBrush) default_brush.Clone ();
  192. CheckDefaultRectangle ("clone", clone.Rectangle);
  193. }
  194. [Test]
  195. [ExpectedException (typeof (ArgumentNullException))]
  196. public void MultiplyTransform1_Null ()
  197. {
  198. default_brush.MultiplyTransform (null);
  199. }
  200. [Test]
  201. [ExpectedException (typeof (ArgumentNullException))]
  202. public void MultiplyTransform2_Null ()
  203. {
  204. default_brush.MultiplyTransform (null, MatrixOrder.Append);
  205. }
  206. [Test]
  207. public void MultiplyTransform2_Invalid ()
  208. {
  209. default_brush.MultiplyTransform (empty_matrix, (MatrixOrder) Int32.MinValue);
  210. }
  211. [Test]
  212. public void ResetTransform ()
  213. {
  214. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  215. lgb.ResetTransform ();
  216. CheckEmptyMatrix (lgb.Transform);
  217. }
  218. [Test]
  219. [NUnit.Framework.Category ("NotWorking")]
  220. public void RotateTransform ()
  221. {
  222. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  223. lgb.RotateTransform (90);
  224. float[] elements = lgb.Transform.Elements;
  225. Assert.AreEqual (-1, elements[0], 0.1, "matrix.0");
  226. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  227. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  228. Assert.AreEqual (-1, elements[3], 0.1, "matrix.3");
  229. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  230. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  231. lgb.RotateTransform (270);
  232. CheckDefaultMatrix (lgb.Transform);
  233. }
  234. [Test]
  235. [NUnit.Framework.Category ("NotWorking")]
  236. public void RotateTransform_Max ()
  237. {
  238. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  239. lgb.RotateTransform (Single.MaxValue);
  240. float[] elements = lgb.Transform.Elements;
  241. Assert.AreEqual (-1.69580966E+30, elements[0], 1e25, "matrix.0");
  242. Assert.AreEqual (1.18780928E+37, elements[1], 1e32, "matrix.1");
  243. Assert.AreEqual (-1.18780953E+37, elements[2], 1e32, "matrix.2");
  244. Assert.AreEqual (-2.79830475E+29, elements[3], 1e24, "matrix.3");
  245. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  246. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  247. }
  248. [Test]
  249. [NUnit.Framework.Category ("NotWorking")]
  250. public void RotateTransform_Min ()
  251. {
  252. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  253. lgb.RotateTransform (Single.MinValue);
  254. float[] elements = lgb.Transform.Elements;
  255. Assert.AreEqual (1.06198436E+30, elements[0], 1e25, "matrix.0");
  256. Assert.AreEqual (-1.18780953E+37, elements[1], 1e32, "matrix.1");
  257. Assert.AreEqual (1.1878094E+37, elements[2], 1e32, "matrix.2");
  258. Assert.AreEqual (-3.53994825E+29, elements[3], 1e24, "matrix.3");
  259. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  260. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  261. }
  262. [Test]
  263. [ExpectedException (typeof (ArgumentException))]
  264. public void RotateTransform_InvalidOrder ()
  265. {
  266. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  267. lgb.RotateTransform (720, (MatrixOrder) Int32.MinValue);
  268. }
  269. [Test]
  270. [NUnit.Framework.Category ("NotWorking")]
  271. public void ScaleTransform ()
  272. {
  273. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  274. lgb.ScaleTransform (2, 4);
  275. float[] elements = lgb.Transform.Elements;
  276. Assert.AreEqual (2, elements[0], 0.1, "matrix.0");
  277. Assert.AreEqual (2, elements[1], 0.1, "matrix.1");
  278. Assert.AreEqual (-4, elements[2], 0.1, "matrix.2");
  279. Assert.AreEqual (4, elements[3], 0.1, "matrix.3");
  280. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  281. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  282. lgb.ScaleTransform (0.5f, 0.25f);
  283. CheckDefaultMatrix (lgb.Transform);
  284. }
  285. [Test]
  286. [NUnit.Framework.Category ("NotWorking")]
  287. public void ScaleTransform_MaxMin ()
  288. {
  289. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  290. lgb.ScaleTransform (Single.MaxValue, Single.MinValue);
  291. float[] elements = lgb.Transform.Elements;
  292. Assert.AreEqual (Single.MaxValue, elements[0], 1e33, "matrix.0");
  293. Assert.AreEqual (Single.MaxValue, elements[1], 1e33, "matrix.1");
  294. Assert.AreEqual (Single.MaxValue, elements[2], 1e33, "matrix.2");
  295. Assert.AreEqual (Single.MinValue, elements[3], 1e33, "matrix.3");
  296. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  297. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  298. }
  299. [Test]
  300. [ExpectedException (typeof (ArgumentException))]
  301. public void ScaleTransform_InvalidOrder ()
  302. {
  303. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  304. lgb.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
  305. }
  306. [Test]
  307. [NUnit.Framework.Category ("NotWorking")]
  308. public void SetBlendTriangularShape_Focus ()
  309. {
  310. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  311. // max valid
  312. lgb.SetBlendTriangularShape (1);
  313. CheckDefaultMatrix (lgb.Transform);
  314. // min valid
  315. lgb.SetBlendTriangularShape (0);
  316. CheckDefaultMatrix (lgb.Transform);
  317. // middle
  318. lgb.SetBlendTriangularShape (0.5f);
  319. CheckDefaultMatrix (lgb.Transform);
  320. // no impact on matrix
  321. }
  322. [Test]
  323. [NUnit.Framework.Category ("NotWorking")]
  324. public void SetBlendTriangularShape_Scale ()
  325. {
  326. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  327. // max valid
  328. lgb.SetBlendTriangularShape (0, 1);
  329. CheckDefaultMatrix (lgb.Transform);
  330. // min valid
  331. lgb.SetBlendTriangularShape (1, 0);
  332. CheckDefaultMatrix (lgb.Transform);
  333. // middle
  334. lgb.SetBlendTriangularShape (0.5f, 0.5f);
  335. CheckDefaultMatrix (lgb.Transform);
  336. // no impact on matrix
  337. }
  338. [Test]
  339. [ExpectedException (typeof (ArgumentException))]
  340. public void SetBlendTriangularShape_FocusTooSmall ()
  341. {
  342. default_brush.SetBlendTriangularShape (-1);
  343. }
  344. [Test]
  345. [ExpectedException (typeof (ArgumentException))]
  346. public void SetBlendTriangularShape_FocusTooBig ()
  347. {
  348. default_brush.SetBlendTriangularShape (1.01f);
  349. }
  350. [Test]
  351. [ExpectedException (typeof (ArgumentException))]
  352. public void SetBlendTriangularShape_ScaleTooSmall ()
  353. {
  354. default_brush.SetBlendTriangularShape (1, -1);
  355. }
  356. [Test]
  357. [ExpectedException (typeof (ArgumentException))]
  358. public void SetBlendTriangularShape_ScaleTooBig ()
  359. {
  360. default_brush.SetBlendTriangularShape (1, 1.01f);
  361. }
  362. [Test]
  363. [NUnit.Framework.Category ("NotWorking")]
  364. public void SetSigmaBellShape_Focus ()
  365. {
  366. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  367. // max valid
  368. lgb.SetSigmaBellShape (1);
  369. CheckDefaultMatrix (lgb.Transform);
  370. // min valid
  371. lgb.SetSigmaBellShape (0);
  372. CheckDefaultMatrix (lgb.Transform);
  373. // middle
  374. lgb.SetSigmaBellShape (0.5f);
  375. CheckDefaultMatrix (lgb.Transform);
  376. // no impact on matrix
  377. }
  378. [Test]
  379. [NUnit.Framework.Category ("NotWorking")]
  380. public void SetSigmaBellShape_Scale ()
  381. {
  382. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  383. // max valid
  384. lgb.SetSigmaBellShape (0, 1);
  385. CheckDefaultMatrix (lgb.Transform);
  386. // min valid
  387. lgb.SetSigmaBellShape (1, 0);
  388. CheckDefaultMatrix (lgb.Transform);
  389. // middle
  390. lgb.SetSigmaBellShape (0.5f, 0.5f);
  391. CheckDefaultMatrix (lgb.Transform);
  392. // no impact on matrix
  393. }
  394. [Test]
  395. [ExpectedException (typeof (ArgumentException))]
  396. public void SetSigmaBellShape_FocusTooSmall ()
  397. {
  398. default_brush.SetSigmaBellShape (-1);
  399. }
  400. [Test]
  401. [ExpectedException (typeof (ArgumentException))]
  402. public void SetSigmaBellShape_FocusTooBig ()
  403. {
  404. default_brush.SetSigmaBellShape (1.01f);
  405. }
  406. [Test]
  407. [ExpectedException (typeof (ArgumentException))]
  408. public void SetSigmaBellShape_ScaleTooSmall ()
  409. {
  410. default_brush.SetSigmaBellShape (1, -1);
  411. }
  412. [Test]
  413. [ExpectedException (typeof (ArgumentException))]
  414. public void SetSigmaBellShape_ScaleTooBig ()
  415. {
  416. default_brush.SetSigmaBellShape (1, 1.01f);
  417. }
  418. [Test]
  419. [NUnit.Framework.Category ("NotWorking")]
  420. public void TranslateTransform ()
  421. {
  422. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  423. lgb.TranslateTransform (1, 1);
  424. float[] elements = lgb.Transform.Elements;
  425. Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
  426. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  427. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  428. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  429. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  430. Assert.AreEqual (-14, elements[5], 0.1, "matrix.5");
  431. lgb.TranslateTransform (-1, -1);
  432. CheckDefaultMatrix (lgb.Transform);
  433. }
  434. [Test]
  435. [ExpectedException (typeof (ArgumentException))]
  436. public void TranslateTransform_InvalidOrder ()
  437. {
  438. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  439. lgb.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);
  440. }
  441. }
  442. }