LinearGradientBrushTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. private RectangleF rect;
  45. [TestFixtureSetUp]
  46. public void FixtureSetUp ()
  47. {
  48. pt1 = new Point (0, 0);
  49. pt2 = new Point (32, 32);
  50. c1 = Color.Blue;
  51. c2 = Color.Red;
  52. default_brush = new LinearGradientBrush (pt1, pt2, c1, c2);
  53. empty_matrix = new Matrix ();
  54. rect = new RectangleF (0, 0, 32, 32);
  55. }
  56. private void CheckDefaultRectangle (string msg, RectangleF rect)
  57. {
  58. Assert.AreEqual (pt1.X, rect.X, msg + ".Rectangle.X");
  59. Assert.AreEqual (pt1.Y, rect.Y, msg + ".Rectangle.Y");
  60. Assert.AreEqual (pt2.X, rect.Width, msg + ".Rectangle.Width");
  61. Assert.AreEqual (pt2.Y, rect.Height, msg + ".Rectangle.Height");
  62. }
  63. private void CheckDefaultMatrix (Matrix matrix)
  64. {
  65. float[] elements = matrix.Elements;
  66. Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
  67. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  68. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  69. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  70. Assert.AreEqual (16, elements[4], "matrix.4");
  71. Assert.AreEqual (-16, elements[5], "matrix.5");
  72. }
  73. private void CheckBrushAt45 (LinearGradientBrush lgb)
  74. {
  75. CheckDefaultRectangle ("4", lgb.Rectangle);
  76. Assert.AreEqual (1, lgb.Blend.Factors.Length, "Blend.Factors");
  77. Assert.AreEqual (1, lgb.Blend.Factors[0], "Blend.Factors [0]");
  78. Assert.AreEqual (1, lgb.Blend.Positions.Length, "Blend.Positions");
  79. // lgb.Blend.Positions [0] is always small (e-39) but never quite the same
  80. Assert.IsFalse (lgb.GammaCorrection, "GammaCorrection");
  81. Assert.AreEqual (2, lgb.LinearColors.Length, "LinearColors");
  82. Assert.IsNotNull (lgb.Transform, "Transform");
  83. CheckDefaultMatrix (lgb.Transform);
  84. }
  85. [Test]
  86. public void Constructor_Point_Point_Color_Color ()
  87. {
  88. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  89. CheckBrushAt45 (lgb);
  90. Assert.AreEqual (WrapMode.Tile, lgb.WrapMode, "WrapMode.Tile");
  91. lgb.WrapMode = WrapMode.TileFlipX;
  92. Assert.AreEqual (WrapMode.TileFlipX, lgb.WrapMode, "WrapMode.TileFlipX");
  93. lgb.WrapMode = WrapMode.TileFlipY;
  94. Assert.AreEqual (WrapMode.TileFlipY, lgb.WrapMode, "WrapMode.TileFlipY");
  95. lgb.WrapMode = WrapMode.TileFlipXY;
  96. Assert.AreEqual (WrapMode.TileFlipXY, lgb.WrapMode, "WrapMode.TileFlipXY");
  97. // can't set WrapMode.Clamp
  98. }
  99. [Test]
  100. public void Constructor_RectangleF_Color_Color_Single_0 ()
  101. {
  102. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  103. CheckDefaultRectangle ("Original", lgb.Rectangle);
  104. Assert.AreEqual (1, lgb.Blend.Factors.Length, "Blend.Factors");
  105. Assert.AreEqual (1, lgb.Blend.Factors[0], "Blend.Factors[0]");
  106. Assert.AreEqual (1, lgb.Blend.Positions.Length, "Blend.Positions");
  107. // lgb.Blend.Positions [0] is always small (e-39) but never quite the same
  108. Assert.IsFalse (lgb.GammaCorrection, "GammaCorrection");
  109. Assert.AreEqual (c1.ToArgb (), lgb.LinearColors[0].ToArgb (), "LinearColors[0]");
  110. Assert.AreEqual (c2.ToArgb (), lgb.LinearColors[1].ToArgb (), "LinearColors[1]");
  111. Assert.AreEqual (rect, lgb.Rectangle, "Rectangle");
  112. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity");
  113. Assert.AreEqual (WrapMode.Tile, lgb.WrapMode, "WrapMode");
  114. Matrix matrix = new Matrix (2, -1, 1, 2, 10, 10);
  115. lgb.Transform = matrix;
  116. Assert.AreEqual (matrix, lgb.Transform, "Transform");
  117. }
  118. [Test]
  119. public void Constructor_RectangleF_Color_Color_Single_22_5 ()
  120. {
  121. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 22.5f);
  122. CheckDefaultRectangle ("Original", lgb.Rectangle);
  123. float[] elements = lgb.Transform.Elements;
  124. Assert.AreEqual (1.207107, elements[0], 0.0001, "matrix.0");
  125. Assert.AreEqual (0.5, elements[1], 0.0001, "matrix.1");
  126. Assert.AreEqual (-0.5, elements[2], 0.0001, "matrix.2");
  127. Assert.AreEqual (1.207107, elements[3], 0.0001, "matrix.3");
  128. Assert.AreEqual (4.686291, elements[4], 0.0001, "matrix.4");
  129. Assert.AreEqual (-11.313709, elements[5], 0.0001, "matrix.5");
  130. }
  131. [Test]
  132. public void Constructor_RectangleF_Color_Color_Single_45 ()
  133. {
  134. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 45f);
  135. CheckBrushAt45 (lgb);
  136. }
  137. [Test]
  138. public void Constructor_RectangleF_Color_Color_Single_90 ()
  139. {
  140. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 90f);
  141. CheckDefaultRectangle ("Original", lgb.Rectangle);
  142. float[] elements = lgb.Transform.Elements;
  143. Assert.AreEqual (0, elements[0], 0.0001, "matrix.0");
  144. Assert.AreEqual (1, elements[1], 0.0001, "matrix.1");
  145. Assert.AreEqual (-1, elements[2], 0.0001, "matrix.2");
  146. Assert.AreEqual (0, elements[3], 0.0001, "matrix.3");
  147. Assert.AreEqual (32, elements[4], 0.0001, "matrix.4");
  148. Assert.AreEqual (0, elements[5], 0.0001, "matrix.5");
  149. }
  150. [Test]
  151. public void Constructor_RectangleF_Color_Color_Single_135 ()
  152. {
  153. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 135f);
  154. CheckDefaultRectangle ("Original", lgb.Rectangle);
  155. float[] elements = lgb.Transform.Elements;
  156. Assert.AreEqual (-1, elements[0], 0.0001, "matrix.0");
  157. Assert.AreEqual (1, elements[1], 0.0001, "matrix.1");
  158. Assert.AreEqual (-1, elements[2], 0.0001, "matrix.2");
  159. Assert.AreEqual (-1, elements[3], 0.0001, "matrix.3");
  160. Assert.AreEqual (48, elements[4], 0.0001, "matrix.4");
  161. Assert.AreEqual (16, elements[5], 0.0001, "matrix.5");
  162. }
  163. [Test]
  164. public void Constructor_RectangleF_Color_Color_Single_180 ()
  165. {
  166. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 180f);
  167. CheckDefaultRectangle ("Original", lgb.Rectangle);
  168. float[] elements = lgb.Transform.Elements;
  169. Assert.AreEqual (-1, elements[0], 0.0001, "matrix.0");
  170. Assert.AreEqual (0, elements[1], 0.0001, "matrix.1");
  171. Assert.AreEqual (0, elements[2], 0.0001, "matrix.2");
  172. Assert.AreEqual (-1, elements[3], 0.0001, "matrix.3");
  173. Assert.AreEqual (32, elements[4], 0.0001, "matrix.4");
  174. Assert.AreEqual (32, elements[5], 0.0001, "matrix.5");
  175. }
  176. [Test]
  177. public void Constructor_RectangleF_Color_Color_Single_270 ()
  178. {
  179. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 270f);
  180. CheckDefaultRectangle ("Original", lgb.Rectangle);
  181. float[] elements = lgb.Transform.Elements;
  182. Assert.AreEqual (0, elements[0], 0.0001, "matrix.0");
  183. Assert.AreEqual (-1, elements[1], 0.0001, "matrix.1");
  184. Assert.AreEqual (1, elements[2], 0.0001, "matrix.2");
  185. Assert.AreEqual (0, elements[3], 0.0001, "matrix.3");
  186. Assert.AreEqual (0, elements[4], 0.0001, "matrix.4");
  187. Assert.AreEqual (32, elements[5], 0.0001, "matrix.5");
  188. }
  189. [Test]
  190. public void Constructor_RectangleF_Color_Color_Single_315 ()
  191. {
  192. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 315f);
  193. CheckDefaultRectangle ("Original", lgb.Rectangle);
  194. float[] elements = lgb.Transform.Elements;
  195. Assert.AreEqual (1, elements[0], 0.0001, "matrix.0");
  196. Assert.AreEqual (-1, elements[1], 0.0001, "matrix.1");
  197. Assert.AreEqual (1, elements[2], 0.0001, "matrix.2");
  198. Assert.AreEqual (1, elements[3], 0.0001, "matrix.3");
  199. Assert.AreEqual (-16, elements[4], 0.0001, "matrix.4");
  200. Assert.AreEqual (16, elements[5], 0.0001, "matrix.5");
  201. }
  202. [Test]
  203. public void Constructor_RectangleF_Color_Color_Single_360()
  204. {
  205. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 360f);
  206. CheckDefaultRectangle ("Original", lgb.Rectangle);
  207. float[] elements = lgb.Transform.Elements;
  208. // just like 0'
  209. Assert.AreEqual (1, elements[0], 0.0001, "matrix.0");
  210. Assert.AreEqual (0, elements[1], 0.0001, "matrix.1");
  211. Assert.AreEqual (0, elements[2], 0.0001, "matrix.2");
  212. Assert.AreEqual (1, elements[3], 0.0001, "matrix.3");
  213. Assert.AreEqual (0, elements[4], 0.0001, "matrix.4");
  214. Assert.AreEqual (0, elements[5], 0.0001, "matrix.5");
  215. }
  216. [Test]
  217. public void Constructor_RectangleF_Color_Color_Single_540 ()
  218. {
  219. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 540f);
  220. CheckDefaultRectangle ("Original", lgb.Rectangle);
  221. float[] elements = lgb.Transform.Elements;
  222. // just like 180'
  223. Assert.AreEqual (-1, elements[0], 0.0001, "matrix.0");
  224. Assert.AreEqual (0, elements[1], 0.0001, "matrix.1");
  225. Assert.AreEqual (0, elements[2], 0.0001, "matrix.2");
  226. Assert.AreEqual (-1, elements[3], 0.0001, "matrix.3");
  227. Assert.AreEqual (32, elements[4], 0.0001, "matrix.4");
  228. Assert.AreEqual (32, elements[5], 0.0001, "matrix.5");
  229. }
  230. [Test]
  231. [ExpectedException (typeof (ArgumentException))]
  232. public void InterpolationColors_Colors_InvalidBlend ()
  233. {
  234. // default Blend doesn't allow getting this property
  235. Assert.IsNotNull (default_brush.InterpolationColors.Colors);
  236. }
  237. [Test]
  238. [ExpectedException (typeof (ArgumentException))]
  239. public void InterpolationColors_Positions_InvalidBlend ()
  240. {
  241. // default Blend doesn't allow getting this property
  242. Assert.IsNotNull (default_brush.InterpolationColors.Positions);
  243. }
  244. [Test]
  245. [ExpectedException (typeof (IndexOutOfRangeException))]
  246. public void LinearColors_Empty ()
  247. {
  248. default_brush.LinearColors = new Color[0];
  249. }
  250. [Test]
  251. [ExpectedException (typeof (IndexOutOfRangeException))]
  252. public void LinearColors_One ()
  253. {
  254. default_brush.LinearColors = new Color[1];
  255. }
  256. [Test]
  257. public void LinearColors_Two ()
  258. {
  259. Assert.AreEqual (Color.FromArgb (255, 0, 0, 255), default_brush.LinearColors[0], "0");
  260. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), default_brush.LinearColors[1], "1");
  261. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  262. lgb.LinearColors = new Color[2] { Color.Black, Color.White };
  263. // not the same, the alpha is changed to 255 so they can't compare
  264. Assert.AreEqual (Color.FromArgb (255, 0, 0, 0), lgb.LinearColors[0], "0");
  265. Assert.AreEqual (Color.FromArgb (255, 255, 255, 255), lgb.LinearColors[1], "1");
  266. }
  267. [Test]
  268. public void LinearColors_Three ()
  269. {
  270. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  271. lgb.LinearColors = new Color[3] { Color.Red, Color.Green, Color.Blue };
  272. // not the same, the alpha is changed to 255 so they can't compare
  273. Assert.AreEqual (Color.FromArgb (255, 255, 0, 0), lgb.LinearColors[0], "0");
  274. Assert.AreEqual (Color.FromArgb (255, 0, 128, 0), lgb.LinearColors[1], "1");
  275. }
  276. [Test]
  277. public void Rectangle ()
  278. {
  279. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  280. CheckDefaultRectangle ("Original", lgb.Rectangle);
  281. lgb.MultiplyTransform (new Matrix (2, 0, 0, 2, 2, 2));
  282. CheckDefaultRectangle ("Multiply", lgb.Rectangle);
  283. lgb.ResetTransform ();
  284. CheckDefaultRectangle ("Reset", lgb.Rectangle);
  285. lgb.RotateTransform (90);
  286. CheckDefaultRectangle ("Rotate", lgb.Rectangle);
  287. lgb.ScaleTransform (4, 0.25f);
  288. CheckDefaultRectangle ("Scale", lgb.Rectangle);
  289. lgb.TranslateTransform (-10, -20);
  290. CheckDefaultRectangle ("Translate", lgb.Rectangle);
  291. lgb.SetBlendTriangularShape (0.5f);
  292. CheckDefaultRectangle ("SetBlendTriangularShape", lgb.Rectangle);
  293. lgb.SetSigmaBellShape (0.5f);
  294. CheckDefaultRectangle ("SetSigmaBellShape", lgb.Rectangle);
  295. }
  296. [Test]
  297. [ExpectedException (typeof (ArgumentNullException))]
  298. public void Transform_Null ()
  299. {
  300. default_brush.Transform = null;
  301. }
  302. [Test]
  303. public void Transform_Empty ()
  304. {
  305. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  306. lgb.Transform = new Matrix ();
  307. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity");
  308. }
  309. [Test]
  310. [ExpectedException (typeof (ArgumentException))]
  311. public void Transform_NonInvertible ()
  312. {
  313. default_brush.Transform = new Matrix (123, 24, 82, 16, 47, 30);
  314. }
  315. [Test]
  316. public void WrapMode_AllValid ()
  317. {
  318. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  319. lgb.WrapMode = WrapMode.Tile;
  320. Assert.AreEqual (WrapMode.Tile, lgb.WrapMode, "WrapMode.Tile");
  321. lgb.WrapMode = WrapMode.TileFlipX;
  322. Assert.AreEqual (WrapMode.TileFlipX, lgb.WrapMode, "WrapMode.TileFlipX");
  323. lgb.WrapMode = WrapMode.TileFlipY;
  324. Assert.AreEqual (WrapMode.TileFlipY, lgb.WrapMode, "WrapMode.TileFlipY");
  325. lgb.WrapMode = WrapMode.TileFlipXY;
  326. Assert.AreEqual (WrapMode.TileFlipXY, lgb.WrapMode, "WrapMode.TileFlipXY");
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentException))]
  330. public void WrapMode_Clamp ()
  331. {
  332. default_brush.WrapMode = WrapMode.Clamp;
  333. }
  334. [Test]
  335. [ExpectedException (typeof (InvalidEnumArgumentException))]
  336. public void WrapMode_Invalid ()
  337. {
  338. default_brush.WrapMode = (WrapMode) Int32.MinValue;
  339. }
  340. [Test]
  341. public void Clone ()
  342. {
  343. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  344. LinearGradientBrush clone = (LinearGradientBrush) lgb.Clone ();
  345. Assert.AreEqual (lgb.Blend.Factors.Length, clone.Blend.Factors.Length, "Blend.Factors.Length");
  346. Assert.AreEqual (lgb.Blend.Positions.Length, clone.Blend.Positions.Length, "Blend.Positions.Length");
  347. Assert.AreEqual (lgb.GammaCorrection, clone.GammaCorrection, "GammaCorrection");
  348. Assert.AreEqual (lgb.LinearColors.Length, clone.LinearColors.Length, "LinearColors.Length");
  349. Assert.AreEqual (lgb.LinearColors.Length, clone.LinearColors.Length, "LinearColors.Length");
  350. Assert.AreEqual (lgb.Rectangle, clone.Rectangle, "Rectangle");
  351. Assert.AreEqual (lgb.Transform, clone.Transform, "Transform");
  352. Assert.AreEqual (lgb.WrapMode, clone.WrapMode, "WrapMode");
  353. }
  354. [Test]
  355. [ExpectedException (typeof (ArgumentNullException))]
  356. public void MultiplyTransform1_Null ()
  357. {
  358. default_brush.MultiplyTransform (null);
  359. }
  360. [Test]
  361. [ExpectedException (typeof (ArgumentNullException))]
  362. public void MultiplyTransform2_Null ()
  363. {
  364. default_brush.MultiplyTransform (null, MatrixOrder.Append);
  365. }
  366. [Test]
  367. public void MultiplyTransform2_Invalid ()
  368. {
  369. default_brush.MultiplyTransform (empty_matrix, (MatrixOrder) Int32.MinValue);
  370. }
  371. [Test]
  372. [ExpectedException (typeof (ArgumentException))]
  373. public void MultiplyTransform_NonInvertible ()
  374. {
  375. Matrix noninvertible = new Matrix (123, 24, 82, 16, 47, 30);
  376. default_brush.MultiplyTransform (noninvertible);
  377. }
  378. [Test]
  379. public void ResetTransform ()
  380. {
  381. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  382. Assert.IsFalse (lgb.Transform.IsIdentity, "Transform.IsIdentity");
  383. lgb.ResetTransform ();
  384. Assert.IsTrue (lgb.Transform.IsIdentity, "Reset.IsIdentity");
  385. }
  386. [Test]
  387. public void RotateTransform ()
  388. {
  389. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  390. lgb.RotateTransform (90);
  391. float[] elements = lgb.Transform.Elements;
  392. Assert.AreEqual (0, elements[0], 0.1, "matrix.0");
  393. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  394. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  395. Assert.AreEqual (0, elements[3], 0.1, "matrix.3");
  396. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  397. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  398. lgb.RotateTransform (270);
  399. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity");
  400. }
  401. [Test]
  402. [NUnit.Framework.Category ("NotWorking")]
  403. public void RotateTransform_Max ()
  404. {
  405. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  406. lgb.RotateTransform (Single.MaxValue);
  407. float[] elements = lgb.Transform.Elements;
  408. Assert.AreEqual (5.93904E+36, elements[0], 1e32, "matrix.0");
  409. Assert.AreEqual (5.93904E+36, elements[1], 1e32, "matrix.1");
  410. Assert.AreEqual (-5.93904E+36, elements[2], 1e32, "matrix.2");
  411. Assert.AreEqual (5.93904E+36, elements[3], 1e32, "matrix.3");
  412. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  413. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  414. }
  415. [Test]
  416. [NUnit.Framework.Category ("NotWorking")]
  417. public void RotateTransform_Min ()
  418. {
  419. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  420. lgb.RotateTransform (Single.MinValue);
  421. float[] elements = lgb.Transform.Elements;
  422. Assert.AreEqual (-5.93904E+36, elements[0], 1e32, "matrix.0");
  423. Assert.AreEqual (-5.93904E+36, elements[1], 1e32, "matrix.1");
  424. Assert.AreEqual (5.93904E+36, elements[2], 1e32, "matrix.2");
  425. Assert.AreEqual (-5.93904E+36, elements[3], 1e32, "matrix.3");
  426. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  427. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  428. }
  429. [Test]
  430. [ExpectedException (typeof (ArgumentException))]
  431. public void RotateTransform_InvalidOrder ()
  432. {
  433. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  434. lgb.RotateTransform (720, (MatrixOrder) Int32.MinValue);
  435. }
  436. [Test]
  437. public void ScaleTransform ()
  438. {
  439. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  440. lgb.ScaleTransform (2, 4);
  441. float[] elements = lgb.Transform.Elements;
  442. Assert.AreEqual (2, elements[0], 0.1, "matrix.0");
  443. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  444. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  445. Assert.AreEqual (4, elements[3], 0.1, "matrix.3");
  446. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  447. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  448. lgb.ScaleTransform (0.5f, 0.25f);
  449. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity");
  450. }
  451. [Test]
  452. public void ScaleTransform_45 ()
  453. {
  454. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 45f);
  455. lgb.ScaleTransform (3, 3);
  456. float[] elements = lgb.Transform.Elements;
  457. Assert.AreEqual (3, elements[0], 0.1, "matrix.0");
  458. Assert.AreEqual (3, elements[1], 0.1, "matrix.1");
  459. Assert.AreEqual (-3, elements[2], 0.1, "matrix.2");
  460. Assert.AreEqual (3, elements[3], 0.1, "matrix.3");
  461. Assert.AreEqual (16, elements[4], 0.1, "matrix.4");
  462. Assert.AreEqual (-16, elements[5], 0.1, "matrix.5");
  463. }
  464. [Test]
  465. public void ScaleTransform_MaxMin ()
  466. {
  467. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  468. lgb.ScaleTransform (Single.MaxValue, Single.MinValue);
  469. float[] elements = lgb.Transform.Elements;
  470. Assert.AreEqual (Single.MaxValue, elements[0], 1e33, "matrix.0");
  471. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  472. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  473. Assert.AreEqual (Single.MinValue, elements[3], 1e33, "matrix.3");
  474. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  475. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  476. }
  477. [Test]
  478. [ExpectedException (typeof (ArgumentException))]
  479. public void ScaleTransform_InvalidOrder ()
  480. {
  481. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  482. lgb.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
  483. }
  484. [Test]
  485. public void SetBlendTriangularShape_Focus ()
  486. {
  487. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  488. // max valid
  489. lgb.SetBlendTriangularShape (1);
  490. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  491. // min valid
  492. lgb.SetBlendTriangularShape (0);
  493. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  494. // middle
  495. lgb.SetBlendTriangularShape (0.5f);
  496. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  497. // no impact on matrix
  498. }
  499. [Test]
  500. public void SetBlendTriangularShape_Scale ()
  501. {
  502. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  503. // max valid
  504. lgb.SetBlendTriangularShape (0, 1);
  505. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  506. // min valid
  507. lgb.SetBlendTriangularShape (1, 0);
  508. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  509. // middle
  510. lgb.SetBlendTriangularShape (0.5f, 0.5f);
  511. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  512. // no impact on matrix
  513. }
  514. [Test]
  515. [ExpectedException (typeof (ArgumentException))]
  516. public void SetBlendTriangularShape_FocusTooSmall ()
  517. {
  518. default_brush.SetBlendTriangularShape (-1);
  519. }
  520. [Test]
  521. [ExpectedException (typeof (ArgumentException))]
  522. public void SetBlendTriangularShape_FocusTooBig ()
  523. {
  524. default_brush.SetBlendTriangularShape (1.01f);
  525. }
  526. [Test]
  527. [ExpectedException (typeof (ArgumentException))]
  528. public void SetBlendTriangularShape_ScaleTooSmall ()
  529. {
  530. default_brush.SetBlendTriangularShape (1, -1);
  531. }
  532. [Test]
  533. [ExpectedException (typeof (ArgumentException))]
  534. public void SetBlendTriangularShape_ScaleTooBig ()
  535. {
  536. default_brush.SetBlendTriangularShape (1, 1.01f);
  537. }
  538. [Test]
  539. public void SetSigmaBellShape_Focus ()
  540. {
  541. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  542. // max valid
  543. lgb.SetSigmaBellShape (1);
  544. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  545. // min valid
  546. lgb.SetSigmaBellShape (0);
  547. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  548. // middle
  549. lgb.SetSigmaBellShape (0.5f);
  550. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  551. // no impact on matrix
  552. }
  553. [Test]
  554. public void SetSigmaBellShape_Scale ()
  555. {
  556. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  557. // max valid
  558. lgb.SetSigmaBellShape (0, 1);
  559. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  560. // min valid
  561. lgb.SetSigmaBellShape (1, 0);
  562. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-2");
  563. // middle
  564. lgb.SetSigmaBellShape (0.5f, 0.5f);
  565. Assert.IsTrue (lgb.Transform.IsIdentity, "Transform.IsIdentity-3");
  566. // no impact on matrix
  567. }
  568. [Test]
  569. [ExpectedException (typeof (ArgumentException))]
  570. public void SetSigmaBellShape_FocusTooSmall ()
  571. {
  572. default_brush.SetSigmaBellShape (-1);
  573. }
  574. [Test]
  575. [ExpectedException (typeof (ArgumentException))]
  576. public void SetSigmaBellShape_FocusTooBig ()
  577. {
  578. default_brush.SetSigmaBellShape (1.01f);
  579. }
  580. [Test]
  581. [ExpectedException (typeof (ArgumentException))]
  582. public void SetSigmaBellShape_ScaleTooSmall ()
  583. {
  584. default_brush.SetSigmaBellShape (1, -1);
  585. }
  586. [Test]
  587. [ExpectedException (typeof (ArgumentException))]
  588. public void SetSigmaBellShape_ScaleTooBig ()
  589. {
  590. default_brush.SetSigmaBellShape (1, 1.01f);
  591. }
  592. [Test]
  593. public void TranslateTransform ()
  594. {
  595. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 0f);
  596. lgb.TranslateTransform (1, 1);
  597. float[] elements = lgb.Transform.Elements;
  598. Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
  599. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  600. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  601. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  602. Assert.AreEqual (1, elements[4], 0.1, "matrix.4");
  603. Assert.AreEqual (1, elements[5], 0.1, "matrix.5");
  604. lgb.TranslateTransform (-1, -1);
  605. // strangely lgb.Transform.IsIdentity is false
  606. elements = lgb.Transform.Elements;
  607. Assert.AreEqual (1, elements[0], 0.1, "revert.matrix.0");
  608. Assert.AreEqual (0, elements[1], 0.1, "revert.matrix.1");
  609. Assert.AreEqual (0, elements[2], 0.1, "revert.matrix.2");
  610. Assert.AreEqual (1, elements[3], 0.1, "revert.matrix.3");
  611. Assert.AreEqual (0, elements[4], 0.1, "revert.matrix.4");
  612. Assert.AreEqual (0, elements[5], 0.1, "revert.matrix.5");
  613. }
  614. [Test]
  615. [ExpectedException (typeof (ArgumentException))]
  616. public void TranslateTransform_InvalidOrder ()
  617. {
  618. LinearGradientBrush lgb = new LinearGradientBrush (pt1, pt2, c1, c2);
  619. lgb.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);
  620. }
  621. [Test]
  622. public void Transform_Operations ()
  623. {
  624. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 45f);
  625. Matrix clone = lgb.Transform.Clone ();
  626. Matrix mul = clone.Clone ();
  627. clone.Multiply (mul, MatrixOrder.Append);
  628. lgb.MultiplyTransform (mul, MatrixOrder.Append);
  629. Assert.AreEqual (lgb.Transform, clone, "Multiply/Append");
  630. clone.Multiply (mul, MatrixOrder.Prepend);
  631. lgb.MultiplyTransform (mul, MatrixOrder.Prepend);
  632. Assert.AreEqual (lgb.Transform, clone, "Multiply/Prepend");
  633. clone.Rotate (45, MatrixOrder.Append);
  634. lgb.RotateTransform (45, MatrixOrder.Append);
  635. Assert.AreEqual (lgb.Transform, clone, "Rotate/Append");
  636. clone.Rotate (45, MatrixOrder.Prepend);
  637. lgb.RotateTransform (45, MatrixOrder.Prepend);
  638. Assert.AreEqual (lgb.Transform, clone, "Rotate/Prepend");
  639. clone.Scale (0.25f, 2, MatrixOrder.Append);
  640. lgb.ScaleTransform (0.25f, 2, MatrixOrder.Append);
  641. Assert.AreEqual (lgb.Transform, clone, "Scale/Append");
  642. clone.Scale (0.25f, 2, MatrixOrder.Prepend);
  643. lgb.ScaleTransform (0.25f, 2, MatrixOrder.Prepend);
  644. Assert.AreEqual (lgb.Transform, clone, "Scale/Prepend");
  645. clone.Translate (10, 20, MatrixOrder.Append);
  646. lgb.TranslateTransform (10, 20, MatrixOrder.Append);
  647. Assert.AreEqual (lgb.Transform, clone, "Translate/Append");
  648. clone.Translate (30, 40, MatrixOrder.Prepend);
  649. lgb.TranslateTransform (30, 40, MatrixOrder.Prepend);
  650. Assert.AreEqual (lgb.Transform, clone, "Translate/Prepend");
  651. clone.Reset ();
  652. lgb.ResetTransform ();
  653. Assert.AreEqual (lgb.Transform, clone, "Reset");
  654. }
  655. [Test]
  656. public void Transform_Operations_OnScalableAngle ()
  657. {
  658. LinearGradientBrush lgb = new LinearGradientBrush (rect, c1, c2, 360f, true);
  659. Matrix clone = lgb.Transform.Clone ();
  660. Matrix mul = clone.Clone ();
  661. Matrix m = new Matrix ();
  662. m.Scale (2, 1);
  663. m.Translate (rect.Width, rect.Height);
  664. m.Rotate (30f);
  665. clone.Multiply (mul, MatrixOrder.Append);
  666. lgb.MultiplyTransform (mul, MatrixOrder.Append);
  667. Assert.AreEqual (lgb.Transform, clone, "Multiply/Append");
  668. clone.Multiply (mul, MatrixOrder.Prepend);
  669. lgb.MultiplyTransform (mul, MatrixOrder.Prepend);
  670. Assert.AreEqual (lgb.Transform, clone, "Multiply/Prepend");
  671. clone.Rotate (45, MatrixOrder.Append);
  672. lgb.RotateTransform (45, MatrixOrder.Append);
  673. Assert.AreEqual (lgb.Transform, clone, "Rotate/Append");
  674. clone.Rotate (45, MatrixOrder.Prepend);
  675. lgb.RotateTransform (45, MatrixOrder.Prepend);
  676. Assert.AreEqual (lgb.Transform, clone, "Rotate/Prepend");
  677. clone.Scale (0.25f, 2, MatrixOrder.Append);
  678. lgb.ScaleTransform (0.25f, 2, MatrixOrder.Append);
  679. Assert.AreEqual (lgb.Transform, clone, "Scale/Append");
  680. clone.Scale (0.25f, 2, MatrixOrder.Prepend);
  681. lgb.ScaleTransform (0.25f, 2, MatrixOrder.Prepend);
  682. Assert.AreEqual (lgb.Transform, clone, "Scale/Prepend");
  683. clone.Translate (10, 20, MatrixOrder.Append);
  684. lgb.TranslateTransform (10, 20, MatrixOrder.Append);
  685. Assert.AreEqual (lgb.Transform, clone, "Translate/Append");
  686. clone.Translate (30, 40, MatrixOrder.Prepend);
  687. lgb.TranslateTransform (30, 40, MatrixOrder.Prepend);
  688. Assert.AreEqual (lgb.Transform, clone, "Translate/Prepend");
  689. clone.Reset ();
  690. lgb.ResetTransform ();
  691. Assert.AreEqual (lgb.Transform, clone, "Reset");
  692. }
  693. }
  694. }