BorderTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. using Rune = System.Rune;
  8. namespace Terminal.Gui.CoreTests {
  9. public class BorderTests {
  10. [Fact]
  11. [AutoInitShutdown]
  12. public void Constructor_Defaults ()
  13. {
  14. var b = new Border ();
  15. Assert.Equal (BorderStyle.None, b.BorderStyle);
  16. Assert.False (b.DrawMarginFrame);
  17. Assert.Equal (default, b.BorderThickness);
  18. Assert.Equal (default, b.BorderBrush);
  19. Assert.Equal (default, b.Background);
  20. Assert.Equal (default, b.Padding);
  21. Assert.Equal (0, b.ActualWidth);
  22. Assert.Equal (0, b.ActualHeight);
  23. Assert.Null (b.Child);
  24. Assert.Null (b.ChildContainer);
  25. Assert.False (b.Effect3D);
  26. Assert.Equal (new Point (1, 1), b.Effect3DOffset);
  27. Assert.Null (b.Effect3DBrush);
  28. Assert.Equal (NStack.ustring.Empty, b.Title);
  29. }
  30. [Fact]
  31. public void BorderStyle_Different_None_Ensures_DrawMarginFrame_To_True ()
  32. {
  33. var b = new Border () {
  34. BorderStyle = BorderStyle.Single,
  35. DrawMarginFrame = false
  36. };
  37. Assert.True (b.DrawMarginFrame);
  38. b.BorderStyle = BorderStyle.None;
  39. Assert.True (b.DrawMarginFrame);
  40. b.DrawMarginFrame = false;
  41. Assert.False (b.DrawMarginFrame);
  42. }
  43. [Fact]
  44. [AutoInitShutdown]
  45. public void ActualWidth_ActualHeight ()
  46. {
  47. var v = new View (new Rect (5, 10, 60, 20), "", new Border ());
  48. Assert.Equal (60, v.Border.ActualWidth);
  49. Assert.Equal (20, v.Border.ActualHeight);
  50. }
  51. [Fact]
  52. public void ToplevelContainer_LayoutStyle_Computed_Constuctor_ ()
  53. {
  54. var tc = new Border.ToplevelContainer (new Border ());
  55. Assert.Equal (LayoutStyle.Computed, tc.LayoutStyle);
  56. }
  57. [Fact]
  58. public void ToplevelContainer_LayoutStyle_Absolute_Constuctor_ ()
  59. {
  60. var tc = new Border.ToplevelContainer (new Rect (1, 2, 3, 4), new Border ());
  61. Assert.Equal (LayoutStyle.Absolute, tc.LayoutStyle);
  62. }
  63. [Fact]
  64. public void GetSumThickness_Test ()
  65. {
  66. var b = new Border () {
  67. BorderThickness = new Thickness (1, 2, 3, 4),
  68. Padding = new Thickness (4, 3, 2, 1)
  69. };
  70. Assert.Equal (new Thickness (5, 5, 5, 5), b.GetSumThickness ());
  71. }
  72. }
  73. }
  74. }
  75. Assert.Equal ("This is a test", text.Trim ());
  76. // Check the upper Effect3D
  77. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  78. r < frame.Y - drawMarginFrame - sumThickness.Top; r++) {
  79. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  80. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  81. var color = (Attribute)driver.Contents [r, c, 1];
  82. Assert.Equal (Color.DarkGray, color.Background);
  83. }
  84. }
  85. // Check the left Effect3D
  86. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  87. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  88. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  89. c < frame.X - drawMarginFrame - sumThickness.Left; c++) {
  90. var color = (Attribute)driver.Contents [r, c, 1];
  91. Assert.Equal (Color.DarkGray, color.Background);
  92. }
  93. }
  94. // Check the right Effect3D
  95. for (int r = frame.Y - drawMarginFrame - sumThickness.Top + effect3DOffset.Y;
  96. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  97. for (int c = frame.Right + drawMarginFrame + sumThickness.Right;
  98. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  99. var color = (Attribute)driver.Contents [r, c, 1];
  100. Assert.Equal (Color.DarkGray, color.Background);
  101. }
  102. }
  103. // Check the lower Effect3D
  104. for (int r = frame.Bottom + drawMarginFrame + sumThickness.Bottom;
  105. r < frame.Bottom + drawMarginFrame + sumThickness.Bottom + effect3DOffset.Y; r++) {
  106. for (int c = frame.X - drawMarginFrame - sumThickness.Left + effect3DOffset.X;
  107. c < frame.Right + drawMarginFrame + sumThickness.Right + effect3DOffset.X; c++) {
  108. var color = (Attribute)driver.Contents [r, c, 1];
  109. Assert.Equal (Color.DarkGray, color.Background);
  110. }
  111. }
  112. // Check the Child frame
  113. for (int r = frame.Y; r < frame.Y + frame.Height; r++) {
  114. for (int c = frame.X; c < frame.X + frame.Width; c++) {
  115. var color = (Attribute)driver.Contents [r, c, 1];
  116. Assert.Equal (Color.BrightGreen, color.Foreground);
  117. Assert.Equal (Color.Black, color.Background);
  118. }
  119. }
  120. }
  121. [Fact]
  122. [AutoInitShutdown]
  123. public void DrawContent_With_Parent_Border ()
  124. {
  125. var top = Application.Top;
  126. var driver = (FakeDriver)Application.Driver;
  127. var frameView = new FrameView () {
  128. X = Pos.Center (),
  129. Y = Pos.Center (),
  130. Width = 24,
  131. Height = 13,
  132. Border = new Border () {
  133. BorderStyle = BorderStyle.Single,
  134. Padding = new Thickness (2),
  135. BorderThickness = new Thickness (2),
  136. BorderBrush = Color.Red,
  137. Background = Color.BrightGreen,
  138. Effect3D = true,
  139. Effect3DOffset = new Point (2, -3)
  140. }
  141. };
  142. frameView.Add (new Label () {
  143. ColorScheme = Colors.TopLevel,
  144. Text = "This is a test"
  145. });
  146. //frameView.Border.Child = frameView;
  147. top.Add (frameView);
  148. top.LayoutSubviews ();
  149. frameView.Redraw (frameView.Bounds);
  150. var frame = frameView.Frame;
  151. var drawMarginFrame = frameView.Border.DrawMarginFrame ? 1 : 0;
  152. var sumThickness = frameView.Border.GetSumThickness ();
  153. var borderThickness = frameView.Border.BorderThickness;
  154. var padding = frameView.Border.Padding;
  155. var effect3DOffset = frameView.Border.Effect3DOffset;
  156. var borderStyle = frameView.Border.BorderStyle;
  157. // Check the upper BorderThickness
  158. for (int r = frame.Y;
  159. r < Math.Min (frame.Y + borderThickness.Top, frame.Bottom); r++) {
  160. for (int c = frame.X;
  161. c < frame.Right; c++) {
  162. var color = (Attribute)driver.Contents [r, c, 1];
  163. Assert.Equal (Color.Red, color.Background);
  164. }
  165. }
  166. // Check the left BorderThickness
  167. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  168. r < frame.Bottom - borderThickness.Bottom; r++) {
  169. for (int c = frame.X;
  170. c < Math.Min (frame.X + borderThickness.Left, frame.Right); c++) {
  171. var color = (Attribute)driver.Contents [r, c, 1];
  172. Assert.Equal (Color.Red, color.Background);
  173. }
  174. }
  175. // Check the right BorderThickness
  176. for (int r = Math.Min (frame.Y + borderThickness.Top, frame.Bottom);
  177. r < frame.Bottom - borderThickness.Bottom; r++) {
  178. for (int c = Math.Max (frame.Right - borderThickness.Right, frame.X);
  179. c < frame.Right; c++) {
  180. var color = (Attribute)driver.Contents [r, c, 1];
  181. Assert.Equal (Color.Red, color.Background);
  182. }
  183. }
  184. // Check the lower BorderThickness
  185. for (int r = Math.Max (frame.Bottom - borderThickness.Bottom, frame.Y);
  186. r < frame.Bottom; r++) {
  187. for (int c = frame.X;
  188. c < frame.Right; c++) {
  189. var color = (Attribute)driver.Contents [r, c, 1];
  190. Assert.Equal (Color.Red, color.Background);
  191. }
  192. }
  193. // Check the upper Padding
  194. for (int r = frame.Y + borderThickness.Top;
  195. r < Math.Min (frame.Y + sumThickness.Top, frame.Bottom - borderThickness.Bottom); r++) {
  196. for (int c = frame.X + borderThickness.Left;
  197. c < frame.Right - borderThickness.Right; c++) {
  198. var color = (Attribute)driver.Contents [r, c, 1];
  199. Assert.Equal (Color.BrightGreen, color.Background);
  200. }
  201. }
  202. // Check the left Padding
  203. for (int r = frame.Y + sumThickness.Top;
  204. r < frame.Bottom - sumThickness.Bottom; r++) {
  205. for (int c = frame.X + borderThickness.Left;
  206. c < Math.Min (frame.X + sumThickness.Left, frame.Right - borderThickness.Right); c++) {
  207. var color = (Attribute)driver.Contents [r, c, 1];
  208. Assert.Equal (Color.BrightGreen, color.Background);
  209. }
  210. }
  211. // Check the right Padding
  212. // Draw the right Padding
  213. for (int r = frame.Y + sumThickness.Top;
  214. r < frame.Bottom - sumThickness.Bottom; r++) {
  215. for (int c = Math.Max (frame.Right - sumThickness.Right, frame.X + sumThickness.Left);
  216. c < Math.Max (frame.Right - borderThickness.Right, frame.X + sumThickness.Left); c++) {
  217. var color = (Attribute)driver.Contents [r, c, 1];
  218. Assert.Equal (Color.BrightGreen, color.Background);
  219. }
  220. }
  221. // Check the lower Padding
  222. for (int r = Math.Max (frame.Bottom - sumThickness.Bottom, frame.Y + borderThickness.Top);
  223. r < frame.Bottom - borderThickness.Bottom; r++) {
  224. for (int c = frame.X + borderThickness.Left;
  225. c < frame.Right - borderThickness.Right; c++) {
  226. var color = (Attribute)driver.Contents [r, c, 1];
  227. Assert.Equal (Color.BrightGreen, color.Background);
  228. }
  229. }
  230. Rune hLine = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  231. ? driver.HLine : (borderStyle == BorderStyle.Double ? driver.HDLine : ' ')) : ' ';
  232. Rune vLine = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  233. ? driver.VLine : (borderStyle == BorderStyle.Double ? driver.VDLine : ' ')) : ' ';
  234. Rune uRCorner = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  235. ? driver.URCorner : (borderStyle == BorderStyle.Double ? driver.URDCorner : ' ')) : ' ';
  236. Rune uLCorner = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  237. ? driver.ULCorner : (borderStyle == BorderStyle.Double ? driver.ULDCorner : ' ')) : ' ';
  238. Rune lLCorner = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  239. ? driver.LLCorner : (borderStyle == BorderStyle.Double ? driver.LLDCorner : ' ')) : ' ';
  240. Rune lRCorner = drawMarginFrame > 0 ? (borderStyle == BorderStyle.Single
  241. ? driver.LRCorner : (borderStyle == BorderStyle.Double ? driver.LRDCorner : ' ')) : ' ';
  242. var text = "";
  243. // Check the MarginFrame
  244. for (int r = frame.Y + sumThickness.Top;
  245. r < frame.Bottom - sumThickness.Bottom; r++) {
  246. for (int c = frame.X + sumThickness.Left;
  247. c <= frame.Right - sumThickness.Right - 1; c++) {
  248. var color = (Attribute)driver.Contents [r, c, 1];
  249. var rune = (Rune)driver.Contents [r, c, 0];
  250. Assert.Equal (Color.Black, color.Background);
  251. if (c == frame.X + sumThickness.Left && r == frame.Y + sumThickness.Top) {
  252. Assert.Equal (uLCorner, rune);
  253. } else if (c == frame.Right - drawMarginFrame - sumThickness.Right
  254. && r == frame.Y + sumThickness.Top) {
  255. Assert.Equal (uRCorner, rune);
  256. } else if (c == frame.X + sumThickness.Left
  257. && r == frame.Bottom - drawMarginFrame - sumThickness.Bottom) {
  258. Assert.Equal (lLCorner, rune);
  259. } else if (c == frame.Right - drawMarginFrame - sumThickness.Right
  260. && r == frame.Bottom - drawMarginFrame - sumThickness.Bottom) {
  261. Assert.Equal (lRCorner, rune);
  262. } else if (c > frame.X + sumThickness.Left
  263. && (r == frame.Y + sumThickness.Top
  264. || r == frame.Bottom - drawMarginFrame - sumThickness.Bottom)) {
  265. Assert.Equal (hLine, rune);
  266. } else if ((c == frame.X + sumThickness.Left
  267. || c == frame.Right - drawMarginFrame - sumThickness.Right)
  268. && r >= frame.Y + drawMarginFrame + sumThickness.Top) {
  269. Assert.Equal (vLine, rune);
  270. } else {
  271. text += rune.ToString ();
  272. }
  273. }
  274. }
  275. Assert.Equal ("This is a test", text.Trim ());
  276. // Check the upper Effect3D
  277. for (int r = frame.Y + effect3DOffset.Y;
  278. r < frame.Y; r++) {
  279. for (int c = frame.X + effect3DOffset.X;
  280. c < frame.Right + effect3DOffset.X; c++) {
  281. var color = (Attribute)driver.Contents [r, c, 1];
  282. Assert.Equal (Color.DarkGray, color.Background);
  283. }
  284. }
  285. // Check the left Effect3D
  286. for (int r = frame.Y + effect3DOffset.Y;
  287. r < frame.Bottom + effect3DOffset.Y; r++) {
  288. for (int c = frame.X + effect3DOffset.X;
  289. c < frame.X; c++) {
  290. var color = (Attribute)driver.Contents [r, c, 1];
  291. Assert.Equal (Color.DarkGray, color.Background);
  292. }
  293. }
  294. // Check the right Effect3D
  295. for (int r = frame.Y + effect3DOffset.Y;
  296. r < frame.Bottom + effect3DOffset.Y; r++) {
  297. for (int c = frame.Right;
  298. c < frame.Right + effect3DOffset.X; c++) {
  299. var color = (Attribute)driver.Contents [r, c, 1];
  300. Assert.Equal (Color.DarkGray, color.Background);
  301. }
  302. }
  303. // Check the lower Effect3D
  304. for (int r = frame.Bottom;
  305. r < frame.Bottom + effect3DOffset.Y; r++) {
  306. for (int c = frame.X + effect3DOffset.X;
  307. c < frame.Right + effect3DOffset.X; c++) {
  308. var color = (Attribute)driver.Contents [r, c, 1];
  309. Assert.Equal (Color.DarkGray, color.Background);
  310. }
  311. }
  312. // Check the Child frame
  313. for (int r = frame.Y + drawMarginFrame + sumThickness.Top;
  314. r < frame.Bottom - drawMarginFrame - sumThickness.Bottom; r++) {
  315. for (int c = frame.X + drawMarginFrame + sumThickness.Left;
  316. c < frame.Right - drawMarginFrame - sumThickness.Right; c++) {
  317. var color = (Attribute)driver.Contents [r, c, 1];
  318. Assert.Equal (Color.BrightGreen, color.Foreground);
  319. Assert.Equal (Color.Black, color.Background);
  320. }
  321. }
  322. }
  323. [Fact]
  324. [AutoInitShutdown]
  325. public void BorderOnControlWithNoChildren ()
  326. {
  327. var label = new TextField ("Loading...") {
  328. Border = new Border () {
  329. BorderStyle = BorderStyle.Single,
  330. DrawMarginFrame = true,
  331. Padding = new Thickness (1),
  332. BorderBrush = Color.White
  333. }
  334. };
  335. Application.Top.Add (label);
  336. Assert.Null (Record.Exception (() => label.Redraw (label.Bounds)));
  337. }
  338. }
  339. }