LineCanvasTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. [Fact, AutoInitShutdown]
  227. public void TestLineCanvas_LeaveMargin_Top1_Left1 ()
  228. {
  229. // Draw at 1,1 within client area of View (i.e. leave a top and left margin of 1)
  230. var v = GetCanvas (out var canvas, 1, 1);
  231. // outer box
  232. canvas.AddLine (new Point (0, 0), 8, Orientation.Horizontal, BorderStyle.Single);
  233. canvas.AddLine (new Point (8, 0), 3, Orientation.Vertical, BorderStyle.Single);
  234. canvas.AddLine (new Point (8, 3), -8, Orientation.Horizontal, BorderStyle.Single);
  235. canvas.AddLine (new Point (0, 3), -3, Orientation.Vertical, BorderStyle.Single);
  236. canvas.AddLine (new Point (5, 0), 3, Orientation.Vertical, BorderStyle.Single);
  237. canvas.AddLine (new Point (0, 2), 8, Orientation.Horizontal, BorderStyle.Single);
  238. v.Redraw (v.Bounds);
  239. string looksLike =
  240. @"
  241. ┌────┬──┐
  242. │ │ │
  243. ├────┼──┤
  244. └────┴──┘
  245. ";
  246. TestHelpers.AssertDriverContentsAre (looksLike, output);
  247. }
  248. [Theory, AutoInitShutdown]
  249. [InlineData (0, 0, @"
  250. ═══
  251. ══
  252. ═══")]
  253. [InlineData (1, 0, @"
  254. ══
  255. ══")]
  256. [InlineData (2, 0, @"
  257. ═")]
  258. [InlineData (0, 1, @"
  259. ══
  260. ═══")]
  261. [InlineData (0, 2, @"
  262. ═══")]
  263. public void TestLineCanvasRenderOffset_NoOffset (int xOffset, int yOffset, string expect)
  264. {
  265. var canvas = new LineCanvas ();
  266. canvas.AddLine (new Point (0, 0), 2, Orientation.Horizontal, BorderStyle.Double);
  267. canvas.AddLine (new Point (0, 1), 1, Orientation.Horizontal, BorderStyle.Double);
  268. canvas.AddLine (new Point (0, 2), 2, Orientation.Horizontal, BorderStyle.Double);
  269. var bmp = canvas.GenerateImage (new Rect (xOffset, yOffset, 3, 3));
  270. var actual = BmpToString (bmp);
  271. Assert.Equal (expect.TrimStart (), actual);
  272. }
  273. private string BmpToString (System.Rune? [,] bmp)
  274. {
  275. var sb = new StringBuilder ();
  276. for (int y = 0; y < bmp.GetLength (1); y++) {
  277. for (int x = 0; x < bmp.GetLength (0); x++) {
  278. sb.Append (bmp [y, x]);
  279. }
  280. sb.AppendLine ();
  281. }
  282. return sb.ToString ().TrimEnd ();
  283. }
  284. /// <summary>
  285. /// Creates a new <see cref="View"/> into which a <see cref="LineCanvas"/> is rendered
  286. /// at <see cref="View.DrawContentComplete"/> time.
  287. /// </summary>
  288. /// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
  289. /// <param name="offsetX">How far to offset drawing in X</param>
  290. /// <param name="offsetY">How far to offset drawing in Y</param>
  291. /// <returns></returns>
  292. private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
  293. {
  294. var v = new View {
  295. Width = 10,
  296. Height = 5,
  297. Bounds = new Rect (0, 0, 10, 5)
  298. };
  299. var canvasCopy = canvas = new LineCanvas ();
  300. v.DrawContentComplete += (r) => canvasCopy.Draw (v, v.Bounds, new Point(offsetX,offsetY));
  301. return v;
  302. }
  303. }
  304. }