Pen.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using NUnit.Framework;
  5. using DrawingTestHelper;
  6. namespace Test.Sys.Drawing
  7. {
  8. /// <summary>
  9. /// Summary description for Pen.
  10. /// </summary>
  11. [TestFixture]
  12. public class PenFixture {
  13. //TODO: Brush, CompoundArray, CustomEndCap, CustomStartCap,
  14. //StartCap, EndCap, PenType, Transform
  15. DrawingTest t;
  16. Pen p;
  17. [SetUp]
  18. public void SetUp () {
  19. t = DrawingTest.Create (256, 256);
  20. p = new Pen (Color.Blue);
  21. p.Width = 10;
  22. }
  23. [Test]
  24. public void InitAlignment () {
  25. Pen p = new Pen (Color.Blue);
  26. Assert.AreEqual (PenAlignment.Center, p.Alignment);
  27. }
  28. [Test]
  29. [Category ("test1")]
  30. public void Width () {
  31. Assert.AreEqual (10, p.Width);
  32. t.Graphics.DrawLine (p, 0, 0, 64, 64);
  33. t.Show ();
  34. Assert.IsTrue (t.Compare (10));
  35. p.Width = 0;
  36. t.Graphics.DrawLine (p, 32, 0, 35, 64);
  37. t.Show ();
  38. Assert.IsTrue (t.Compare (10));
  39. p.Width = 10;
  40. t.Graphics.DrawLine (p, 0, 64, 64, 70);
  41. t.Show ();
  42. Assert.IsTrue (t.Compare (10));
  43. }
  44. [Test]
  45. public void DashStyleTest () {
  46. Assert.AreEqual (DashStyle.Solid, p.DashStyle);
  47. t.Graphics.DrawLine (p, 0, 30, 256, 30);
  48. t.Show ();
  49. Assert.IsTrue (t.Compare (10));
  50. p.DashStyle = DashStyle.Dash;
  51. p.Width = 10;
  52. t.Graphics.DrawLine (p, 0, 600, 200, 60);
  53. t.Show ();
  54. Assert.IsTrue (t.Compare (10));
  55. p.DashStyle = DashStyle.DashDot;
  56. t.Graphics.FillRectangle (Brushes.Black, 0, 0, 256, 256);
  57. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  58. t.Show ();
  59. Assert.IsTrue (t.Compare (10));
  60. p.DashStyle = DashStyle.DashDotDot;
  61. t.Graphics.FillRectangle (Brushes.Black, 0, 0, 256, 256);
  62. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  63. t.Show ();
  64. Assert.IsTrue (t.Compare (10));
  65. p.DashStyle = DashStyle.Dot;
  66. t.Graphics.FillRectangle (Brushes.Black, 0, 0, 256, 256);
  67. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  68. t.Show ();
  69. Assert.IsTrue (t.Compare (10));
  70. p.DashStyle = DashStyle.Custom;
  71. t.Graphics.FillRectangle (Brushes.Black, 0, 0, 256, 256);
  72. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  73. t.Show ();
  74. Assert.IsTrue (t.Compare (10));
  75. }
  76. //The following tests DashOffset and DashPattern
  77. [Test]
  78. public void DashCustomStyle () {
  79. p.DashStyle = DashStyle.Custom;
  80. p.Width = 10;
  81. Assert.AreEqual (new float [] {1F}, p.DashPattern);
  82. Assert.AreEqual (0F, p.DashOffset);
  83. p.DashPattern = new float [] {2, 3, 1.15F, 0.05F};
  84. t.Graphics.FillRectangle (Brushes.Black, 0, 0, 256, 256);
  85. t.Graphics.DrawLine (p, 0, 0, 200, 10);
  86. t.Show ();
  87. Assert.IsTrue (t.Compare (10));
  88. p.DashOffset = 10F;
  89. t.Graphics.DrawLine (p, 0, 50, 200, 50);
  90. t.Show ();
  91. Assert.IsTrue (t.Compare (10));
  92. p.DashPattern = new float [] {2, 3, 1.15F, 0.05F, 1.74321F};
  93. p.DashOffset = 10.2F;
  94. t.Graphics.DrawLine (p, 0, 100, 200, 90);
  95. t.Show ();
  96. Assert.IsTrue (t.Compare (10));
  97. p.DashPattern = new float [] {2, 3, 1.15F, 0.05F, 1.74321F};
  98. p.DashOffset = 10.2F;
  99. t.Graphics.DrawLine (p, 0, 100, 200, 90);
  100. t.Show ();
  101. Assert.IsTrue (t.Compare (10));
  102. }
  103. [Test]
  104. public void DashCapTest () {
  105. Assert.AreEqual (DashCap.Flat, p.DashCap);
  106. p.DashStyle = DashStyle.DashDot;
  107. t.Graphics.DrawLine (p, 0, 0, 200, 10);
  108. t.Show ();
  109. Assert.IsTrue (t.Compare (10));
  110. p.DashCap = DashCap.Round;
  111. t.Graphics.DrawLine (p, 0, 50, 200, 61.7F);
  112. t.Show ();
  113. Assert.IsTrue (t.Compare (10));
  114. p.DashCap = DashCap.Triangle;
  115. t.Graphics.DrawLine (p, 0F, 92.3F, 200F, 117.9F);
  116. t.Show ();
  117. Assert.IsTrue (t.Compare (10));
  118. }
  119. [Test]
  120. public void LineJoinTest () {
  121. Assert.AreEqual (LineJoin.Miter, p.LineJoin);
  122. Point [] points = new Point [] {
  123. new Point(40, 10), new Point (200, 11),
  124. new Point (100, 40)};
  125. t.Graphics.DrawPolygon (p, points);
  126. t.Graphics.DrawPolygon (Pens.White, points);
  127. t.Show ();
  128. Assert.IsTrue (t.Compare (10));
  129. p.LineJoin = LineJoin.Bevel;
  130. points =new Point [] {
  131. new Point(40, 70), new Point (200, 79),
  132. new Point (100, 100)};
  133. t.Graphics.DrawPolygon (p, points);
  134. t.Graphics.DrawPolygon (Pens.White, points);
  135. t.Show ();
  136. Assert.IsTrue (t.Compare (10));
  137. p.LineJoin = LineJoin.MiterClipped;
  138. points = new Point [] {
  139. new Point(40, 120), new Point (200, 117),
  140. new Point (80, 135)};
  141. t.Graphics.DrawPolygon (p, points);
  142. t.Graphics.DrawPolygon (Pens.White, points);
  143. t.Show ();
  144. Assert.IsTrue (t.Compare (10));
  145. p.LineJoin = LineJoin.Round;
  146. points = new Point [] {
  147. new Point(40, 170), new Point (200, 175),
  148. new Point (100, 210)};
  149. t.Graphics.DrawPolygon (p, points);
  150. t.Graphics.DrawPolygon (Pens.White, points);
  151. t.Show ();
  152. Assert.IsTrue (t.Compare (10));
  153. }
  154. [Test]
  155. public void PenAlignmentTest () {
  156. Point [] points = new Point [] {
  157. new Point (20, 20), new Point (40, 40), new Point (60, 20),
  158. new Point (60, 60), new Point (20, 60)};
  159. GraphicsPath path = new GraphicsPath ();
  160. path.AddPolygon (points);
  161. Matrix mx = new Matrix (1, 0, 0, 1, 90, 0);
  162. Matrix mrx = new Matrix (1, 0, 0, 1, -180, 0);
  163. Matrix my = new Matrix (1, 0, 0, 1, 0, 100);
  164. Assert.AreEqual (PenAlignment.Center, p.Alignment);
  165. t.Graphics.DrawPath (p, path);
  166. t.Graphics.DrawPath (Pens.White, path);
  167. t.Show ();
  168. Assert.IsTrue (t.Compare (10));
  169. p.Alignment = PenAlignment.Left;
  170. path.Transform (mx);
  171. t.Graphics.DrawPath (p, path);
  172. t.Graphics.DrawPath (Pens.White, path);
  173. t.Show ();
  174. Assert.IsTrue (t.Compare (10));
  175. p.Alignment = PenAlignment.Inset;
  176. path.Transform (mx);
  177. t.Graphics.DrawPath (p, path);
  178. t.Graphics.DrawPath (Pens.White, path);
  179. t.Show ();
  180. Assert.IsTrue (t.Compare (10));
  181. p.Alignment = PenAlignment.Outset;
  182. path.Transform (mrx);
  183. path.Transform (my);
  184. t.Graphics.DrawPath (p, path);
  185. t.Graphics.DrawPath (Pens.White, path);
  186. t.Show ();
  187. Assert.IsTrue (t.Compare (10));
  188. p.Alignment = PenAlignment.Right;
  189. path.Transform (mx);
  190. t.Graphics.DrawPath (p, path);
  191. t.Graphics.DrawPath (Pens.White, path);
  192. t.Show ();
  193. Assert.IsTrue (t.Compare (10));
  194. }
  195. [Test]
  196. public void ColorTest () {
  197. Assert.AreEqual (Color.Blue, p.Color);
  198. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  199. t.Show ();
  200. Assert.IsTrue (t.Compare (10));
  201. p.Color = Color.Red;
  202. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  203. t.Show ();
  204. Assert.IsTrue (t.Compare (10));
  205. p.Color = Color.BurlyWood;
  206. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  207. t.Show ();
  208. Assert.IsTrue (t.Compare (10));
  209. p.Color = Color.FromArgb (100, 120, 255);
  210. t.Graphics.DrawLine (p, 0, 0, 200, 200);
  211. t.Show ();
  212. Assert.IsTrue (t.Compare (10));
  213. p.Color = Color.FromArgb (128, Color.White);
  214. t.Graphics.DrawLine (p, 0, 200, 200, 0);
  215. t.Show ();
  216. Assert.IsTrue (t.Compare (10));
  217. }
  218. [Test]
  219. public void MitterLimit () {
  220. p.LineJoin = LineJoin.MiterClipped;
  221. Point [] points = new Point [] {new Point (0,30), new Point (180, 31),
  222. new Point (0, 90)};
  223. Assert.AreEqual (10F, p.MiterLimit);
  224. t.Graphics.DrawLines (p, points);
  225. t.Graphics.DrawLines (Pens.White, points);
  226. t.Show ();
  227. Assert.IsTrue (t.Compare (10));
  228. Matrix dy = new Matrix (1, 0, 0, 1, 0, 110);
  229. dy.TransformPoints (points);
  230. p.MiterLimit=1F;
  231. t.Graphics.DrawLines (p, points);
  232. t.Graphics.DrawLines (Pens.White, points);
  233. t.Show ();
  234. Assert.IsTrue (t.Compare (10));
  235. }
  236. [Test]
  237. public void Transform () {
  238. p.ScaleTransform (0.5F, 2);
  239. t.Graphics.DrawArc (p, 0, 0, 100, 100, 0, 360);
  240. t.Graphics.DrawArc (Pens.White, 0, 0, 100, 100, 0, 360);
  241. t.Show ();
  242. }
  243. }
  244. }