2
0

LineCanvasTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Terminal.Gui.Graphs;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. namespace Terminal.Gui.CoreTests {
  8. public class LineCanvasTests {
  9. readonly ITestOutputHelper output;
  10. public LineCanvasTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact, AutoInitShutdown]
  15. public void TestLineCanvas_Dot ()
  16. {
  17. var v = GetCanvas (out var canvas);
  18. canvas.AddLine (new Point (0, 0), 0, Orientation.Horizontal, BorderStyle.Single);
  19. v.Redraw (v.Bounds);
  20. string looksLike =
  21. @"
  22. .";
  23. TestHelpers.AssertDriverContentsAre (looksLike, output);
  24. }
  25. [InlineData (BorderStyle.Single)]
  26. [InlineData (BorderStyle.Rounded)]
  27. [Theory, AutoInitShutdown]
  28. public void TestLineCanvas_Horizontal (BorderStyle style)
  29. {
  30. var v = GetCanvas (out var canvas);
  31. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, style);
  32. v.Redraw (v.Bounds);
  33. string looksLike =
  34. @"
  35. ──";
  36. TestHelpers.AssertDriverContentsAre (looksLike, output);
  37. }
  38. [Fact, AutoInitShutdown]
  39. public void TestLineCanvas_Horizontal_Double ()
  40. {
  41. var v = GetCanvas (out var canvas);
  42. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Double);
  43. v.Redraw (v.Bounds);
  44. string looksLike =
  45. @"
  46. ══";
  47. TestHelpers.AssertDriverContentsAre (looksLike, output);
  48. }
  49. [InlineData (BorderStyle.Single)]
  50. [InlineData(BorderStyle.Rounded)]
  51. [Theory, AutoInitShutdown]
  52. public void TestLineCanvas_Vertical (BorderStyle style)
  53. {
  54. var v = GetCanvas (out var canvas);
  55. canvas.AddLine (new Point (0, 0), 1, Orientation.Vertical, style);
  56. v.Redraw (v.Bounds);
  57. string looksLike =
  58. @"
  59. │";
  60. TestHelpers.AssertDriverContentsAre (looksLike, output);
  61. }
  62. [Fact, AutoInitShutdown]
  63. public void TestLineCanvas_Vertical_Double ()
  64. {
  65. var v = GetCanvas (out var canvas);
  66. canvas.AddLine (new Point (0, 0), 1, Orientation.Vertical, BorderStyle.Double);
  67. v.Redraw (v.Bounds);
  68. string looksLike =
  69. @"
  70. ║";
  71. TestHelpers.AssertDriverContentsAre (looksLike, output);
  72. }
  73. /// <summary>
  74. /// This test demonstrates that corners are only drawn when lines overlap.
  75. /// Not when they terminate adjacent to one another.
  76. /// </summary>
  77. [Fact, AutoInitShutdown]
  78. public void TestLineCanvas_Corner_NoOverlap()
  79. {
  80. var v = GetCanvas (out var canvas);
  81. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Single);
  82. canvas.AddLine (new Point (0, 1), 1, Orientation.Vertical, BorderStyle.Single);
  83. v.Redraw (v.Bounds);
  84. string looksLike =
  85. @"
  86. ──
  87. │";
  88. TestHelpers.AssertDriverContentsAre (looksLike, output);
  89. }
  90. /// <summary>
  91. /// This test demonstrates how to correctly trigger a corner. By
  92. /// overlapping the lines in the same cell
  93. /// </summary>
  94. [Fact, AutoInitShutdown]
  95. public void TestLineCanvas_Corner_Correct ()
  96. {
  97. var v = GetCanvas (out var canvas);
  98. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Single);
  99. canvas.AddLine (new Point (0, 0), 2, Orientation.Vertical, BorderStyle.Single);
  100. v.Redraw (v.Bounds);
  101. string looksLike =
  102. @"
  103. ┌─
  104. │";
  105. TestHelpers.AssertDriverContentsAre (looksLike, output);
  106. }
  107. [Fact,AutoInitShutdown]
  108. public void TestLineCanvas_Window ()
  109. {
  110. var v = GetCanvas (out var canvas);
  111. // outer box
  112. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Single);
  113. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Single);
  114. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Single);
  115. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Single);
  116. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Single);
  117. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Single);
  118. v.Redraw (v.Bounds);
  119. string looksLike =
  120. @"
  121. ┌────┬───┐
  122. │ │ │
  123. ├────┼───┤
  124. │ │ │
  125. └────┴───┘";
  126. TestHelpers.AssertDriverContentsAre (looksLike, output);
  127. }
  128. /// <summary>
  129. /// Demonstrates when <see cref="BorderStyle.Rounded"/> corners are used. Notice how
  130. /// not all lines declare rounded. If there are 1+ lines intersecting and a corner is
  131. /// to be used then if any of them are rounded a rounded corner is used.
  132. /// </summary>
  133. [Fact, AutoInitShutdown]
  134. public void TestLineCanvas_Window_Rounded ()
  135. {
  136. var v = GetCanvas (out var canvas);
  137. // outer box
  138. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Rounded);
  139. // BorderStyle.Single is ignored because corner overlaps with the above line which is Rounded
  140. // this results in a rounded corner being used.
  141. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Single);
  142. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Rounded);
  143. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Single);
  144. // These lines say rounded but they will result in the T sections which are never rounded.
  145. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Rounded);
  146. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Rounded);
  147. v.Redraw (v.Bounds);
  148. string looksLike =
  149. @"
  150. ╭────┬───╮
  151. │ │ │
  152. ├────┼───┤
  153. │ │ │
  154. ╰────┴───╯";
  155. TestHelpers.AssertDriverContentsAre (looksLike, output);
  156. }
  157. [Fact, AutoInitShutdown]
  158. public void TestLineCanvas_Window_Double ()
  159. {
  160. var v = GetCanvas (out var canvas);
  161. // outer box
  162. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Double);
  163. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  164. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  165. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  166. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  167. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
  168. v.Redraw (v.Bounds);
  169. string looksLike =
  170. @"
  171. ╔════╦═══╗
  172. ║ ║ ║
  173. ╠════╬═══╣
  174. ║ ║ ║
  175. ╚════╩═══╝";
  176. TestHelpers.AssertDriverContentsAre (looksLike, output);
  177. }
  178. [Theory, AutoInitShutdown]
  179. [InlineData(BorderStyle.Single)]
  180. [InlineData(BorderStyle.Rounded)]
  181. public void TestLineCanvas_Window_DoubleTop_SingleSides (BorderStyle thinStyle)
  182. {
  183. var v = GetCanvas (out var canvas);
  184. // outer box
  185. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Double);
  186. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, thinStyle);
  187. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  188. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, thinStyle);
  189. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical,thinStyle);
  190. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
  191. v.Redraw (v.Bounds);
  192. string looksLike =
  193. @"
  194. ╒════╤═══╕
  195. │ │ │
  196. ╞════╪═══╡
  197. │ │ │
  198. ╘════╧═══╛
  199. ";
  200. TestHelpers.AssertDriverContentsAre (looksLike, output);
  201. }
  202. [Theory, AutoInitShutdown]
  203. [InlineData(BorderStyle.Single)]
  204. [InlineData(BorderStyle.Rounded)]
  205. public void TestLineCanvas_Window_SingleTop_DoubleSides (BorderStyle thinStyle)
  206. {
  207. var v = GetCanvas (out var canvas);
  208. // outer box
  209. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, thinStyle);
  210. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  211. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal,thinStyle);
  212. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  213. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  214. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, thinStyle);
  215. v.Redraw (v.Bounds);
  216. string looksLike =
  217. @"
  218. ╓────╥───╖
  219. ║ ║ ║
  220. ╟────╫───╢
  221. ║ ║ ║
  222. ╙────╨───╜
  223. ";
  224. TestHelpers.AssertDriverContentsAre (looksLike, output);
  225. }
  226. [Theory, AutoInitShutdown]
  227. [InlineData(0,0,@"
  228. ═══
  229. ══
  230. ═══")]
  231. [InlineData (1, 0,@"
  232. ══
  233. ══")]
  234. [InlineData (2, 0,@"
  235. ═")]
  236. [InlineData (0, 1,@"
  237. ══
  238. ═══")]
  239. [InlineData (0, 2,@"
  240. ═══")]
  241. public void TestLineCanvasRenderOffset_NoOffset (int xOffset,int yOffset, string expect)
  242. {
  243. var canvas = new LineCanvas ();
  244. canvas.AddLine (new Point (0, 0), 2, Orientation.Horizontal, BorderStyle.Double);
  245. canvas.AddLine (new Point (0, 1), 1, Orientation.Horizontal, BorderStyle.Double);
  246. canvas.AddLine (new Point (0, 2), 2, Orientation.Horizontal, BorderStyle.Double);
  247. var bmp = canvas.GenerateImage (new Rect (xOffset, yOffset, 3, 3));
  248. var actual = BmpToString (bmp);
  249. Assert.Equal (expect.TrimStart (), actual);
  250. }
  251. private string BmpToString (System.Rune? [,] bmp)
  252. {
  253. var sb = new StringBuilder ();
  254. for (int y = 0; y < bmp.GetLength (1); y++) {
  255. for (int x = 0; x < bmp.GetLength (0); x++) {
  256. sb.Append (bmp [y, x]);
  257. }
  258. sb.AppendLine ();
  259. }
  260. return sb.ToString ().TrimEnd ();
  261. }
  262. private View GetCanvas (out LineCanvas canvas)
  263. {
  264. var v = new View {
  265. Width = 10,
  266. Height = 5,
  267. Bounds = new Rect (0, 0, 10, 5)
  268. };
  269. var canvasCopy = canvas = new LineCanvas ();
  270. v.DrawContentComplete += (r)=> canvasCopy.Draw (v, v.Bounds);
  271. return v;
  272. }
  273. }
  274. }