PathGradientBrushTest.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. //
  2. // System.Drawing.Drawing2D.PathGradientBrush 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 PathGradientBrushTest {
  38. private Point[] pts_2i;
  39. private PointF[] pts_2f;
  40. private Matrix empty_matrix;
  41. private void CheckDefaultRectangle (string message, RectangleF rect)
  42. {
  43. Assert.AreEqual (1f, rect.X, message + ".Rectangle.X");
  44. Assert.AreEqual (2f, rect.Y, message + ".Rectangle.Y");
  45. Assert.AreEqual (19f, rect.Width, message + ".Rectangle.Width");
  46. Assert.AreEqual (28f, rect.Height, message + ".Rectangle.Height");
  47. }
  48. private void CheckDefaults (PathGradientBrush pgb)
  49. {
  50. Assert.AreEqual (1, pgb.Blend.Factors.Length, "Blend.Factors.Length");
  51. Assert.AreEqual (1f, pgb.Blend.Factors[0], "Blend.Factors[0]");
  52. Assert.AreEqual (1, pgb.Blend.Positions.Length, "Blend.Positions.Length");
  53. Assert.AreEqual (0f, pgb.Blend.Positions[0], 1e-30, "Blend.Positions[0]");
  54. Assert.AreEqual (10.5f, pgb.CenterPoint.X, "CenterPoint.X");
  55. Assert.AreEqual (16f, pgb.CenterPoint.Y, "CenterPoint.Y");
  56. Assert.IsTrue (pgb.FocusScales.IsEmpty, "FocusScales");
  57. Assert.AreEqual (1, pgb.InterpolationColors.Colors.Length, "InterpolationColors.Colors.Length");
  58. Assert.AreEqual (0, pgb.InterpolationColors.Colors[0].ToArgb (), "InterpolationColors.Colors[0]");
  59. Assert.AreEqual (1, pgb.InterpolationColors.Positions.Length, "InterpolationColors.Positions.Length");
  60. Assert.AreEqual (0f, pgb.InterpolationColors.Positions[0], 1e-38, "InterpolationColors.Positions[0]");
  61. CheckDefaultRectangle (String.Empty, pgb.Rectangle);
  62. Assert.AreEqual (1, pgb.SurroundColors.Length, "SurroundColors.Length");
  63. Assert.AreEqual (-1, pgb.SurroundColors[0].ToArgb (), "SurroundColors[0]");
  64. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform");
  65. }
  66. private void CheckPointsDefaults (PathGradientBrush pgb)
  67. {
  68. CheckDefaults (pgb);
  69. Assert.AreEqual (-16777216, pgb.CenterColor.ToArgb (), "CenterColor");
  70. }
  71. private void CheckPathDefaults (PathGradientBrush pgb)
  72. {
  73. CheckDefaults (pgb);
  74. Assert.AreEqual (-1, pgb.CenterColor.ToArgb (), "CenterColor");
  75. }
  76. [TestFixtureSetUp]
  77. public void FixtureSetUp ()
  78. {
  79. pts_2i = new Point[2] { new Point (1, 2), new Point (20, 30) };
  80. pts_2f = new PointF[2] { new PointF (1, 2), new PointF (20, 30) };
  81. empty_matrix = new Matrix ();
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentNullException))]
  85. public void Constructor_GraphicsPath_Null ()
  86. {
  87. GraphicsPath gp = null;
  88. new PathGradientBrush (gp);
  89. }
  90. [Test]
  91. [ExpectedException (typeof (OutOfMemoryException))]
  92. public void Constructor_GraphicsPath_Empty ()
  93. {
  94. using (GraphicsPath gp = new GraphicsPath ()) {
  95. new PathGradientBrush (gp);
  96. }
  97. }
  98. [Test]
  99. [ExpectedException (typeof (OutOfMemoryException))]
  100. public void Constructor_GraphicsPath_SinglePoint ()
  101. {
  102. using (GraphicsPath gp = new GraphicsPath ()) {
  103. gp.AddLines (new Point[1] { new Point (1, 1) });
  104. // Special case - a line with a single point is valid
  105. new PathGradientBrush (gp);
  106. }
  107. }
  108. [Test]
  109. public void Constructor_GraphicsPath_Line ()
  110. {
  111. using (GraphicsPath gp = new GraphicsPath ()) {
  112. gp.AddLines (pts_2f);
  113. using (PathGradientBrush pgb = new PathGradientBrush (gp)) {
  114. CheckPathDefaults (pgb);
  115. Assert.AreEqual (WrapMode.Clamp, pgb.WrapMode, "WrapMode");
  116. }
  117. }
  118. }
  119. [Test]
  120. [ExpectedException (typeof (ArgumentNullException))]
  121. public void Constructor_Point_Null ()
  122. {
  123. Point[] pts = null;
  124. new PathGradientBrush (pts);
  125. }
  126. [Test]
  127. [ExpectedException (typeof (OutOfMemoryException))]
  128. public void Constructor_Point_Empty ()
  129. {
  130. Point[] pts = new Point [0];
  131. new PathGradientBrush (pts);
  132. }
  133. [Test]
  134. [ExpectedException (typeof (OutOfMemoryException))]
  135. public void Constructor_Point_One ()
  136. {
  137. Point[] pts = new Point[1] { new Point (1, 1) };
  138. new PathGradientBrush (pts);
  139. }
  140. [Test]
  141. public void Constructor_Point_Two ()
  142. {
  143. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i)) {
  144. CheckPointsDefaults (pgb);
  145. Assert.AreEqual (WrapMode.Clamp, pgb.WrapMode, "WrapMode");
  146. }
  147. }
  148. [Test]
  149. public void Constructor_Point_WrapMode_Clamp ()
  150. {
  151. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i, WrapMode.Clamp)) {
  152. CheckPointsDefaults (pgb);
  153. Assert.AreEqual (WrapMode.Clamp, pgb.WrapMode, "WrapMode");
  154. }
  155. }
  156. [Test]
  157. public void Constructor_Point_WrapMode_Tile ()
  158. {
  159. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i, WrapMode.Tile)) {
  160. CheckPointsDefaults (pgb);
  161. Assert.AreEqual (WrapMode.Tile, pgb.WrapMode, "WrapMode");
  162. }
  163. }
  164. [Test]
  165. public void Constructor_Point_WrapMode_TileFlipX ()
  166. {
  167. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i, WrapMode.TileFlipX)) {
  168. CheckPointsDefaults (pgb);
  169. Assert.AreEqual (WrapMode.TileFlipX, pgb.WrapMode, "WrapMode");
  170. }
  171. }
  172. [Test]
  173. public void Constructor_Point_WrapMode_TileFlipY ()
  174. {
  175. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i, WrapMode.TileFlipY)) {
  176. CheckPointsDefaults (pgb);
  177. Assert.AreEqual (WrapMode.TileFlipY, pgb.WrapMode, "WrapMode");
  178. }
  179. }
  180. [Test]
  181. public void Constructor_Point_WrapMode_TileFlipXY ()
  182. {
  183. using (PathGradientBrush pgb = new PathGradientBrush (pts_2i, WrapMode.TileFlipXY)) {
  184. CheckPointsDefaults (pgb);
  185. Assert.AreEqual (WrapMode.TileFlipXY, pgb.WrapMode, "WrapMode");
  186. }
  187. }
  188. [Test]
  189. [ExpectedException (typeof (ArgumentNullException))]
  190. public void Constructor_PointF_Null ()
  191. {
  192. PointF[] pts = null;
  193. new PathGradientBrush (pts);
  194. }
  195. [Test]
  196. [ExpectedException (typeof (OutOfMemoryException))]
  197. public void Constructor_PointF_Empty ()
  198. {
  199. PointF[] pts = new PointF[0];
  200. new PathGradientBrush (pts);
  201. }
  202. [Test]
  203. [ExpectedException (typeof (OutOfMemoryException))]
  204. public void Constructor_PointF_One ()
  205. {
  206. PointF[] pts = new PointF[1] { new PointF (1, 1) };
  207. new PathGradientBrush (pts);
  208. }
  209. [Test]
  210. public void Constructor_PointF_Two ()
  211. {
  212. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f)) {
  213. CheckPointsDefaults (pgb);
  214. Assert.AreEqual (WrapMode.Clamp, pgb.WrapMode, "WrapMode");
  215. }
  216. }
  217. [Test]
  218. [ExpectedException (typeof (InvalidEnumArgumentException))]
  219. public void Constructor_PointF_WrapMode_Invalid ()
  220. {
  221. PathGradientBrush pgb = new PathGradientBrush (pts_2f, (WrapMode)Int32.MinValue);
  222. }
  223. [Test]
  224. public void Constructor_PointF_WrapMode_Clamp ()
  225. {
  226. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  227. CheckPointsDefaults (pgb);
  228. Assert.AreEqual (WrapMode.Clamp, pgb.WrapMode, "WrapMode");
  229. }
  230. }
  231. [Test]
  232. public void Constructor_PointF_WrapMode_Tile ()
  233. {
  234. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Tile)) {
  235. CheckPointsDefaults (pgb);
  236. Assert.AreEqual (WrapMode.Tile, pgb.WrapMode, "WrapMode");
  237. }
  238. }
  239. [Test]
  240. public void Constructor_PointF_WrapMode_TileFlipX ()
  241. {
  242. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipX)) {
  243. CheckPointsDefaults (pgb);
  244. Assert.AreEqual (WrapMode.TileFlipX, pgb.WrapMode, "WrapMode");
  245. }
  246. }
  247. [Test]
  248. public void Constructor_PointF_WrapMode_TileFlipY ()
  249. {
  250. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipY)) {
  251. CheckPointsDefaults (pgb);
  252. Assert.AreEqual (WrapMode.TileFlipY, pgb.WrapMode, "WrapMode");
  253. }
  254. }
  255. [Test]
  256. public void Constructor_PointF_WrapMode_TileFlipXY ()
  257. {
  258. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  259. CheckPointsDefaults (pgb);
  260. Assert.AreEqual (WrapMode.TileFlipXY, pgb.WrapMode, "WrapMode");
  261. }
  262. }
  263. [Test]
  264. public void Blend ()
  265. {
  266. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  267. // change not accepted - but no exception is thrown
  268. pgb.Blend.Factors = new float[0];
  269. Assert.AreEqual (1, pgb.Blend.Factors.Length, "Factors-0");
  270. pgb.Blend.Factors = new float[2];
  271. Assert.AreEqual (1, pgb.Blend.Factors.Length, "Factors-1");
  272. // change not accepted - but no exception is thrown
  273. pgb.Blend.Positions = new float[0];
  274. Assert.AreEqual (1, pgb.Blend.Positions.Length, "Positions-0");
  275. pgb.Blend.Positions = new float[2];
  276. Assert.AreEqual (1, pgb.Blend.Positions.Length, "Positions-1");
  277. }
  278. }
  279. [Test]
  280. public void FocusScales ()
  281. {
  282. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  283. PointF fs = new PointF (Single.MaxValue, Single.MinValue);
  284. pgb.FocusScales = fs;
  285. Assert.AreEqual (Single.MaxValue, pgb.FocusScales.X, "MaxValue");
  286. Assert.AreEqual (Single.MinValue, pgb.FocusScales.Y, "MinValue");
  287. fs.X = Single.NaN;
  288. fs.Y = Single.NegativeInfinity;
  289. pgb.FocusScales = fs;
  290. Assert.AreEqual (Single.NaN, pgb.FocusScales.X, "NaN");
  291. Assert.AreEqual (Single.NegativeInfinity, pgb.FocusScales.Y, "NegativeInfinity");
  292. }
  293. }
  294. [Test]
  295. public void CenterColor ()
  296. {
  297. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  298. pgb.CenterColor = Color.Black;
  299. Assert.AreEqual (Color.Black.ToArgb (), pgb.CenterColor.ToArgb (), "Black");
  300. pgb.CenterColor = Color.Transparent;
  301. Assert.AreEqual (Color.Transparent.ToArgb (), pgb.CenterColor.ToArgb (), "Transparent");
  302. }
  303. }
  304. [Test]
  305. public void CenterPoint ()
  306. {
  307. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  308. PointF cp = new PointF (Single.MaxValue, Single.MinValue);
  309. pgb.CenterPoint = cp;
  310. Assert.AreEqual (Single.MaxValue, pgb.CenterPoint.X, "MaxValue");
  311. Assert.AreEqual (Single.MinValue, pgb.CenterPoint.Y, "MinValue");
  312. cp.X = Single.NaN;
  313. cp.Y = Single.NegativeInfinity;
  314. pgb.CenterPoint = cp;
  315. Assert.AreEqual (Single.NaN, pgb.CenterPoint.X, "NaN");
  316. Assert.AreEqual (Single.NegativeInfinity, pgb.CenterPoint.Y, "NegativeInfinity");
  317. }
  318. }
  319. [Test]
  320. public void InterpolationColors ()
  321. {
  322. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  323. // change not accepted - but no exception is thrown
  324. pgb.InterpolationColors.Colors = new Color[0];
  325. Assert.AreEqual (1, pgb.InterpolationColors.Colors.Length, "Colors-0");
  326. pgb.InterpolationColors.Colors = new Color[2];
  327. Assert.AreEqual (1, pgb.InterpolationColors.Colors.Length, "Colors-1");
  328. // change not accepted - but no exception is thrown
  329. pgb.InterpolationColors.Positions = new float[0];
  330. Assert.AreEqual (1, pgb.InterpolationColors.Positions.Length, "Positions-0");
  331. pgb.InterpolationColors.Positions = new float[2];
  332. Assert.AreEqual (1, pgb.InterpolationColors.Positions.Length, "Positions-1");
  333. }
  334. }
  335. [Test]
  336. public void Rectangle ()
  337. {
  338. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  339. CheckDefaultRectangle ("Original", pgb.Rectangle);
  340. pgb.MultiplyTransform (new Matrix (2, 0, 0, 2, 2, 2));
  341. CheckDefaultRectangle ("Multiply", pgb.Rectangle);
  342. pgb.ResetTransform ();
  343. CheckDefaultRectangle ("Reset", pgb.Rectangle);
  344. pgb.RotateTransform (90);
  345. CheckDefaultRectangle ("Rotate", pgb.Rectangle);
  346. pgb.ScaleTransform (4, 0.25f);
  347. CheckDefaultRectangle ("Scale", pgb.Rectangle);
  348. pgb.TranslateTransform (-10, -20);
  349. CheckDefaultRectangle ("Translate", pgb.Rectangle);
  350. pgb.SetBlendTriangularShape (0.5f);
  351. CheckDefaultRectangle ("SetBlendTriangularShape", pgb.Rectangle);
  352. pgb.SetSigmaBellShape (0.5f);
  353. CheckDefaultRectangle ("SetSigmaBellShape", pgb.Rectangle);
  354. }
  355. }
  356. [Test]
  357. [ExpectedException (typeof (ArgumentException))]
  358. public void SurroundColors_Empty ()
  359. {
  360. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  361. pgb.SurroundColors = new Color[0];
  362. }
  363. }
  364. [Test]
  365. public void SurroundColors_2PointF ()
  366. {
  367. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.TileFlipXY)) {
  368. // default values
  369. Assert.AreEqual (1, pgb.SurroundColors.Length, "Length-0");
  370. Assert.AreEqual (-1, pgb.SurroundColors[0].ToArgb (), "SurroundColors-0");
  371. // default can't be changed
  372. pgb.SurroundColors[0] = Color.Gold;
  373. Assert.AreEqual (-1, pgb.SurroundColors[0].ToArgb (), "SurroundColors-1");
  374. // 2 empty color isn't valid, change isn't accepted
  375. pgb.SurroundColors = new Color[2];
  376. Assert.AreEqual (1, pgb.SurroundColors.Length, "Length-1");
  377. pgb.SurroundColors = new Color[2] { Color.Black, Color.White };
  378. Assert.AreEqual (2, pgb.SurroundColors.Length, "Length-2");
  379. Assert.AreEqual (-16777216, pgb.SurroundColors[0].ToArgb (), "SurroundColors-2");
  380. Assert.AreEqual (-1, pgb.SurroundColors[1].ToArgb (), "SurroundColors-3");
  381. }
  382. }
  383. [Test]
  384. public void SurroundColors_3PointsF ()
  385. {
  386. PointF[] points = new PointF[3] { new PointF (5, 50), new PointF (10, 100), new PointF (20, 75) };
  387. using (PathGradientBrush pgb = new PathGradientBrush (points)) {
  388. // 3 empty color isn't valid, change isn't accepted
  389. pgb.SurroundColors = new Color[3] { Color.Empty, Color.Empty, Color.Empty };
  390. Assert.AreEqual (1, pgb.SurroundColors.Length, "Length-1");
  391. pgb.SurroundColors = new Color[3] { Color.Red, Color.Green, Color.Blue };
  392. // change not accepted - but no exception is thrown
  393. Assert.AreEqual (3, pgb.SurroundColors.Length, "Length-1");
  394. Assert.AreEqual (-65536, pgb.SurroundColors[0].ToArgb (), "SurroundColors-1");
  395. Assert.AreEqual (-16744448, pgb.SurroundColors[1].ToArgb (), "SurroundColors-2");
  396. Assert.AreEqual (-16776961, pgb.SurroundColors[2].ToArgb (), "SurroundColors-3");
  397. }
  398. }
  399. [Test]
  400. [ExpectedException (typeof (ArgumentNullException))]
  401. public void Transform_Null ()
  402. {
  403. new PathGradientBrush (pts_2f, WrapMode.Clamp).Transform = null;
  404. }
  405. [Test]
  406. public void Transform_Empty ()
  407. {
  408. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  409. pgb.Transform = new Matrix ();
  410. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity");
  411. }
  412. }
  413. [Test]
  414. [ExpectedException (typeof (ArgumentException))]
  415. public void Transform_NonInvertible ()
  416. {
  417. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  418. pgb.Transform = new Matrix (123, 24, 82, 16, 47, 30);
  419. }
  420. }
  421. [Test]
  422. public void WrapMode_All ()
  423. {
  424. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  425. foreach (WrapMode wm in Enum.GetValues (typeof (WrapMode))) {
  426. pgb.WrapMode = wm;
  427. Assert.AreEqual (wm, pgb.WrapMode, wm.ToString ());
  428. }
  429. }
  430. }
  431. [Test]
  432. [ExpectedException (typeof (InvalidEnumArgumentException))]
  433. public void WrapMode_Invalid ()
  434. {
  435. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  436. pgb.WrapMode = (WrapMode) Int32.MinValue;
  437. }
  438. }
  439. [Test]
  440. public void Clone ()
  441. {
  442. using (GraphicsPath gp = new GraphicsPath ()) {
  443. gp.AddLines (pts_2f);
  444. using (PathGradientBrush pgb = new PathGradientBrush (gp)) {
  445. using (PathGradientBrush clone = (PathGradientBrush) pgb.Clone ()) {
  446. CheckPathDefaults (clone);
  447. Assert.AreEqual (WrapMode.Clamp, clone.WrapMode, "WrapMode");
  448. }
  449. }
  450. }
  451. }
  452. [Test]
  453. [ExpectedException (typeof (ArgumentNullException))]
  454. public void MultiplyTransform1_Null ()
  455. {
  456. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  457. pgb.MultiplyTransform (null);
  458. }
  459. }
  460. [Test]
  461. [ExpectedException (typeof (ArgumentNullException))]
  462. public void MultiplyTransform2_Null ()
  463. {
  464. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  465. pgb.MultiplyTransform (null, MatrixOrder.Append);
  466. }
  467. }
  468. [Test]
  469. public void MultiplyTransform2_Invalid ()
  470. {
  471. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  472. pgb.MultiplyTransform (empty_matrix, (MatrixOrder) Int32.MinValue);
  473. }
  474. }
  475. [Test]
  476. [ExpectedException (typeof (ArgumentException))]
  477. public void MultiplyTransform_NonInvertible ()
  478. {
  479. using (Matrix noninvertible = new Matrix (123, 24, 82, 16, 47, 30)) {
  480. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  481. pgb.MultiplyTransform (noninvertible);
  482. }
  483. }
  484. }
  485. [Test]
  486. public void ResetTransform ()
  487. {
  488. using (Matrix m = new Matrix (2, 0, 0, 2, 10, -10)) {
  489. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  490. pgb.Transform = m;
  491. Assert.IsFalse (pgb.Transform.IsIdentity, "Transform.IsIdentity");
  492. pgb.ResetTransform ();
  493. Assert.IsTrue (pgb.Transform.IsIdentity, "Reset.IsIdentity");
  494. }
  495. }
  496. }
  497. [Test]
  498. public void RotateTransform ()
  499. {
  500. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  501. pgb.RotateTransform (90);
  502. float[] elements = pgb.Transform.Elements;
  503. Assert.AreEqual (0, elements[0], 0.1, "matrix.0");
  504. Assert.AreEqual (1, elements[1], 0.1, "matrix.1");
  505. Assert.AreEqual (-1, elements[2], 0.1, "matrix.2");
  506. Assert.AreEqual (0, elements[3], 0.1, "matrix.3");
  507. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  508. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  509. pgb.RotateTransform (270);
  510. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity");
  511. }
  512. }
  513. [Test]
  514. [NUnit.Framework.Category ("NotWorking")]
  515. public void RotateTransform_Max ()
  516. {
  517. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  518. pgb.RotateTransform (Single.MaxValue);
  519. float[] elements = pgb.Transform.Elements;
  520. Assert.AreEqual (5.93904E+36, elements[0], 1e32, "matrix.0");
  521. Assert.AreEqual (5.93904E+36, elements[1], 1e32, "matrix.1");
  522. Assert.AreEqual (-5.93904E+36, elements[2], 1e32, "matrix.2");
  523. Assert.AreEqual (5.93904E+36, elements[3], 1e32, "matrix.3");
  524. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  525. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  526. }
  527. }
  528. [Test]
  529. [NUnit.Framework.Category ("NotWorking")]
  530. public void RotateTransform_Min ()
  531. {
  532. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  533. pgb.RotateTransform (Single.MinValue);
  534. float[] elements = pgb.Transform.Elements;
  535. Assert.AreEqual (-5.93904E+36, elements[0], 1e32, "matrix.0");
  536. Assert.AreEqual (-5.93904E+36, elements[1], 1e32, "matrix.1");
  537. Assert.AreEqual (5.93904E+36, elements[2], 1e32, "matrix.2");
  538. Assert.AreEqual (-5.93904E+36, elements[3], 1e32, "matrix.3");
  539. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  540. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  541. }
  542. }
  543. [Test]
  544. [ExpectedException (typeof (ArgumentException))]
  545. public void RotateTransform_InvalidOrder ()
  546. {
  547. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  548. pgb.RotateTransform (720, (MatrixOrder) Int32.MinValue);
  549. }
  550. }
  551. [Test]
  552. public void ScaleTransform ()
  553. {
  554. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  555. pgb.ScaleTransform (2, 4);
  556. float[] elements = pgb.Transform.Elements;
  557. Assert.AreEqual (2, elements[0], 0.1, "matrix.0");
  558. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  559. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  560. Assert.AreEqual (4, elements[3], 0.1, "matrix.3");
  561. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  562. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  563. pgb.ScaleTransform (0.5f, 0.25f);
  564. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity");
  565. }
  566. }
  567. [Test]
  568. public void ScaleTransform_MaxMin ()
  569. {
  570. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  571. pgb.ScaleTransform (Single.MaxValue, Single.MinValue);
  572. float[] elements = pgb.Transform.Elements;
  573. Assert.AreEqual (Single.MaxValue, elements[0], 1e33, "matrix.0");
  574. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  575. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  576. Assert.AreEqual (Single.MinValue, elements[3], 1e33, "matrix.3");
  577. Assert.AreEqual (0, elements[4], 0.1, "matrix.4");
  578. Assert.AreEqual (0, elements[5], 0.1, "matrix.5");
  579. }
  580. }
  581. [Test]
  582. [ExpectedException (typeof (ArgumentException))]
  583. public void ScaleTransform_InvalidOrder ()
  584. {
  585. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  586. pgb.ScaleTransform (1, 1, (MatrixOrder) Int32.MinValue);
  587. }
  588. }
  589. [Test]
  590. public void SetBlendTriangularShape_Focus ()
  591. {
  592. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  593. // max valid
  594. pgb.SetBlendTriangularShape (1);
  595. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  596. // min valid
  597. pgb.SetBlendTriangularShape (0);
  598. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  599. // middle
  600. pgb.SetBlendTriangularShape (0.5f);
  601. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  602. // no impact on matrix
  603. }
  604. }
  605. [Test]
  606. public void SetBlendTriangularShape_Scale ()
  607. {
  608. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  609. // max valid
  610. pgb.SetBlendTriangularShape (0, 1);
  611. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  612. // min valid
  613. pgb.SetBlendTriangularShape (1, 0);
  614. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  615. // middle
  616. pgb.SetBlendTriangularShape (0.5f, 0.5f);
  617. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  618. // no impact on matrix
  619. }
  620. }
  621. [Test]
  622. [ExpectedException (typeof (ArgumentException))]
  623. public void SetBlendTriangularShape_FocusTooSmall ()
  624. {
  625. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  626. pgb.SetBlendTriangularShape (-1);
  627. }
  628. }
  629. [Test]
  630. [ExpectedException (typeof (ArgumentException))]
  631. public void SetBlendTriangularShape_FocusTooBig ()
  632. {
  633. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  634. pgb.SetBlendTriangularShape (1.01f);
  635. }
  636. }
  637. [Test]
  638. [ExpectedException (typeof (ArgumentException))]
  639. public void SetBlendTriangularShape_ScaleTooSmall ()
  640. {
  641. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  642. pgb.SetBlendTriangularShape (1, -1);
  643. }
  644. }
  645. [Test]
  646. [ExpectedException (typeof (ArgumentException))]
  647. public void SetBlendTriangularShape_ScaleTooBig ()
  648. {
  649. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  650. pgb.SetBlendTriangularShape (1, 1.01f);
  651. }
  652. }
  653. [Test]
  654. public void SetSigmaBellShape_Focus ()
  655. {
  656. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  657. // max valid
  658. pgb.SetSigmaBellShape (1);
  659. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  660. // min valid
  661. pgb.SetSigmaBellShape (0);
  662. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  663. // middle
  664. pgb.SetSigmaBellShape (0.5f);
  665. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  666. // no impact on matrix
  667. }
  668. }
  669. [Test]
  670. public void SetSigmaBellShape_Scale ()
  671. {
  672. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  673. // max valid
  674. pgb.SetSigmaBellShape (0, 1);
  675. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-1");
  676. // min valid
  677. pgb.SetSigmaBellShape (1, 0);
  678. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-2");
  679. // middle
  680. pgb.SetSigmaBellShape (0.5f, 0.5f);
  681. Assert.IsTrue (pgb.Transform.IsIdentity, "Transform.IsIdentity-3");
  682. // no impact on matrix
  683. }
  684. }
  685. [Test]
  686. [ExpectedException (typeof (ArgumentException))]
  687. public void SetSigmaBellShape_FocusTooSmall ()
  688. {
  689. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  690. pgb.SetSigmaBellShape (-1);
  691. }
  692. }
  693. [Test]
  694. [ExpectedException (typeof (ArgumentException))]
  695. public void SetSigmaBellShape_FocusTooBig ()
  696. {
  697. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  698. pgb.SetSigmaBellShape (1.01f);
  699. }
  700. }
  701. [Test]
  702. [ExpectedException (typeof (ArgumentException))]
  703. public void SetSigmaBellShape_ScaleTooSmall ()
  704. {
  705. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  706. pgb.SetSigmaBellShape (1, -1);
  707. }
  708. }
  709. [Test]
  710. [ExpectedException (typeof (ArgumentException))]
  711. public void SetSigmaBellShape_ScaleTooBig ()
  712. {
  713. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  714. pgb.SetSigmaBellShape (1, 1.01f);
  715. }
  716. }
  717. [Test]
  718. public void TranslateTransform ()
  719. {
  720. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  721. pgb.TranslateTransform (1, 1);
  722. float[] elements = pgb.Transform.Elements;
  723. Assert.AreEqual (1, elements[0], 0.1, "matrix.0");
  724. Assert.AreEqual (0, elements[1], 0.1, "matrix.1");
  725. Assert.AreEqual (0, elements[2], 0.1, "matrix.2");
  726. Assert.AreEqual (1, elements[3], 0.1, "matrix.3");
  727. Assert.AreEqual (1, elements[4], 0.1, "matrix.4");
  728. Assert.AreEqual (1, elements[5], 0.1, "matrix.5");
  729. pgb.TranslateTransform (-1, -1);
  730. // strangely lgb.Transform.IsIdentity is false
  731. elements = pgb.Transform.Elements;
  732. Assert.AreEqual (1, elements[0], 0.1, "revert.matrix.0");
  733. Assert.AreEqual (0, elements[1], 0.1, "revert.matrix.1");
  734. Assert.AreEqual (0, elements[2], 0.1, "revert.matrix.2");
  735. Assert.AreEqual (1, elements[3], 0.1, "revert.matrix.3");
  736. Assert.AreEqual (0, elements[4], 0.1, "revert.matrix.4");
  737. Assert.AreEqual (0, elements[5], 0.1, "revert.matrix.5");
  738. }
  739. }
  740. [Test]
  741. [ExpectedException (typeof (ArgumentException))]
  742. public void TranslateTransform_InvalidOrder ()
  743. {
  744. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  745. pgb.TranslateTransform (1, 1, (MatrixOrder) Int32.MinValue);
  746. }
  747. }
  748. [Test]
  749. public void Transform_Operations ()
  750. {
  751. using (PathGradientBrush pgb = new PathGradientBrush (pts_2f, WrapMode.Clamp)) {
  752. Matrix clone = pgb.Transform.Clone ();
  753. Matrix mul = clone.Clone ();
  754. clone.Multiply (mul, MatrixOrder.Append);
  755. pgb.MultiplyTransform (mul, MatrixOrder.Append);
  756. Assert.AreEqual (pgb.Transform, clone, "Multiply/Append");
  757. clone.Multiply (mul, MatrixOrder.Prepend);
  758. pgb.MultiplyTransform (mul, MatrixOrder.Prepend);
  759. Assert.AreEqual (pgb.Transform, clone, "Multiply/Prepend");
  760. clone.Rotate (45, MatrixOrder.Append);
  761. pgb.RotateTransform (45, MatrixOrder.Append);
  762. Assert.AreEqual (pgb.Transform, clone, "Rotate/Append");
  763. clone.Rotate (45, MatrixOrder.Prepend);
  764. pgb.RotateTransform (45, MatrixOrder.Prepend);
  765. Assert.AreEqual (pgb.Transform, clone, "Rotate/Prepend");
  766. clone.Scale (0.25f, 2, MatrixOrder.Append);
  767. pgb.ScaleTransform (0.25f, 2, MatrixOrder.Append);
  768. Assert.AreEqual (pgb.Transform, clone, "Scale/Append");
  769. clone.Scale (0.25f, 2, MatrixOrder.Prepend);
  770. pgb.ScaleTransform (0.25f, 2, MatrixOrder.Prepend);
  771. Assert.AreEqual (pgb.Transform, clone, "Scale/Prepend");
  772. clone.Translate (10, 20, MatrixOrder.Append);
  773. pgb.TranslateTransform (10, 20, MatrixOrder.Append);
  774. Assert.AreEqual (pgb.Transform, clone, "Translate/Append");
  775. clone.Translate (30, 40, MatrixOrder.Prepend);
  776. pgb.TranslateTransform (30, 40, MatrixOrder.Prepend);
  777. Assert.AreEqual (pgb.Transform, clone, "Translate/Prepend");
  778. clone.Reset ();
  779. pgb.ResetTransform ();
  780. Assert.AreEqual (pgb.Transform, clone, "Reset");
  781. }
  782. }
  783. }
  784. }