DrawTests.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System.Text;
  2. using System;
  3. using Xunit;
  4. using Xunit.Abstractions;
  5. namespace Terminal.Gui.ViewsTests {
  6. public class DrawTests {
  7. readonly ITestOutputHelper output;
  8. public DrawTests (ITestOutputHelper output)
  9. {
  10. this.output = output;
  11. }
  12. // TODO: The tests below that use Label should use View instead.
  13. [Fact, AutoInitShutdown]
  14. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  15. {
  16. string us = "\U0001d539";
  17. Rune r = (Rune)0x1d539;
  18. Assert.Equal ("𝔹", us);
  19. Assert.Equal ("𝔹", r.ToString ());
  20. Assert.Equal (us, r.ToString ());
  21. Assert.Equal (1, us.GetColumns ());
  22. Assert.Equal (1, r.GetColumns ());
  23. var win = new Window () { Title = us };
  24. var label = new Label (r.ToString ());
  25. var tf = new TextField (us) { Y = 1, Width = 3 };
  26. win.Add (label, tf);
  27. var top = Application.Top;
  28. top.Add (win);
  29. Application.Begin (top);
  30. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  31. var expected = @"
  32. ┌┤𝔹├─────┐
  33. │𝔹 │
  34. │𝔹 │
  35. └────────┘";
  36. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  37. TestHelpers.AssertDriverContentsAre (expected, output);
  38. var expectedColors = new Attribute [] {
  39. // 0
  40. Colors.Base.Normal,
  41. // 1
  42. Colors.Base.Focus,
  43. // 2
  44. Colors.Base.HotNormal
  45. };
  46. TestHelpers.AssertDriverColorsAre (@"
  47. 0020000000
  48. 0000000000
  49. 0111000000
  50. 0000000000", expectedColors);
  51. }
  52. [Fact, AutoInitShutdown]
  53. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  54. {
  55. string us = "\U0000f900";
  56. Rune r = (Rune)0xf900;
  57. Assert.Equal ("豈", us);
  58. Assert.Equal ("豈", r.ToString ());
  59. Assert.Equal (us, r.ToString ());
  60. Assert.Equal (2, us.GetColumns ());
  61. Assert.Equal (2, r.GetColumns ());
  62. var win = new Window () { Title = us };
  63. var label = new Label (r.ToString ());
  64. var tf = new TextField (us) { Y = 1, Width = 3 };
  65. win.Add (label, tf);
  66. var top = Application.Top;
  67. top.Add (win);
  68. Application.Begin (top);
  69. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  70. var expected = @"
  71. ┌┤豈├────┐
  72. │豈 │
  73. │豈 │
  74. └────────┘";
  75. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  76. TestHelpers.AssertDriverContentsAre (expected, output);
  77. var expectedColors = new Attribute [] {
  78. // 0
  79. Colors.Base.Normal,
  80. // 1
  81. Colors.Base.Focus,
  82. // 2
  83. Colors.Base.HotNormal
  84. };
  85. TestHelpers.AssertDriverColorsAre (@"
  86. 0022000000
  87. 0000000000
  88. 0111000000
  89. 0000000000", expectedColors);
  90. }
  91. [Fact, AutoInitShutdown]
  92. public void Colors_On_TextAlignment_Right_And_Bottom ()
  93. {
  94. var labelRight = new Label ("Test") {
  95. Width = 6,
  96. Height = 1,
  97. TextAlignment = TextAlignment.Right,
  98. ColorScheme = Colors.Base
  99. };
  100. var labelBottom = new Label ("Test", TextDirection.TopBottom_LeftRight) {
  101. Y = 1,
  102. Width = 1,
  103. Height = 6,
  104. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  105. ColorScheme = Colors.Base
  106. };
  107. var top = Application.Top;
  108. top.Add (labelRight, labelBottom);
  109. Application.Begin (top);
  110. ((FakeDriver)Application.Driver).SetBufferSize (7, 7);
  111. TestHelpers.AssertDriverContentsWithFrameAre (@"
  112. Test
  113. T
  114. e
  115. s
  116. t ", output);
  117. TestHelpers.AssertDriverColorsAre (@"
  118. 000000
  119. 0
  120. 0
  121. 0
  122. 0
  123. 0
  124. 0", new Attribute [] { Colors.Base.Normal });
  125. }
  126. [Fact, AutoInitShutdown]
  127. public void Draw_Negative_Bounds_Horizontal_Without_New_Lines ()
  128. {
  129. // BUGBUG: This previously assumed the default height of a View was 1.
  130. var subView = new View () { Id = "subView", Y = 1, Width = 7, Height = 1, Text = "subView" };
  131. var view = new View () { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  132. view.Add (subView);
  133. var content = new View () { Id = "content", Width = 20, Height = 20 };
  134. content.Add (view);
  135. var container = new View () { Id = "container", X = 1, Y = 1, Width = 5, Height = 5 };
  136. container.Add (content);
  137. var top = Application.Top;
  138. top.Add (container);
  139. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  140. void Top_LayoutComplete (object sender, LayoutEventArgs e)
  141. {
  142. Application.Driver.Clip = container.Frame;
  143. }
  144. top.LayoutComplete += Top_LayoutComplete;
  145. Application.Begin (top);
  146. TestHelpers.AssertDriverContentsWithFrameAre (@"
  147. 01234
  148. subVi", output);
  149. content.X = -1;
  150. Application.Refresh ();
  151. TestHelpers.AssertDriverContentsWithFrameAre (@"
  152. 12345
  153. ubVie", output);
  154. content.Y = -1;
  155. Application.Refresh ();
  156. TestHelpers.AssertDriverContentsWithFrameAre (@"
  157. ubVie", output);
  158. content.Y = -2;
  159. Application.Refresh ();
  160. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  161. content.X = -20;
  162. content.Y = 0;
  163. Application.Refresh ();
  164. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  165. }
  166. [Fact, AutoInitShutdown]
  167. public void Draw_Negative_Bounds_Horizontal_With_New_Lines ()
  168. {
  169. var subView = new View () { Id = "subView", X = 1, Width = 1, Height = 7, Text = "s\nu\nb\nV\ni\ne\nw" };
  170. var view = new View () { Id = "view", Width = 2, Height = 20, Text = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9" };
  171. view.Add (subView);
  172. var content = new View () { Id = "content", Width = 20, Height = 20 };
  173. content.Add (view);
  174. var container = new View () { Id = "container", X = 1, Y = 1, Width = 5, Height = 5 };
  175. container.Add (content);
  176. var top = Application.Top;
  177. top.Add (container);
  178. Application.Driver.Clip = container.Frame;
  179. Application.Begin (top);
  180. TestHelpers.AssertDriverContentsWithFrameAre (@"
  181. 0s
  182. 1u
  183. 2b
  184. 3V
  185. 4i", output);
  186. content.X = -1;
  187. Application.Refresh ();
  188. TestHelpers.AssertDriverContentsWithFrameAre (@"
  189. s
  190. u
  191. b
  192. V
  193. i", output);
  194. content.X = -2;
  195. Application.Refresh ();
  196. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  197. content.X = 0;
  198. content.Y = -1;
  199. Application.Refresh ();
  200. TestHelpers.AssertDriverContentsWithFrameAre (@"
  201. 1u
  202. 2b
  203. 3V
  204. 4i
  205. 5e", output);
  206. content.Y = -6;
  207. Application.Refresh ();
  208. TestHelpers.AssertDriverContentsWithFrameAre (@"
  209. 6w
  210. 7
  211. 8
  212. 9
  213. 0 ", output);
  214. content.Y = -19;
  215. Application.Refresh ();
  216. TestHelpers.AssertDriverContentsWithFrameAre (@"
  217. 9", output);
  218. content.Y = -20;
  219. Application.Refresh ();
  220. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  221. content.X = -2;
  222. content.Y = 0;
  223. Application.Refresh ();
  224. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  225. }
  226. [Fact, AutoInitShutdown]
  227. public void Draw_Negative_Bounds_Vertical ()
  228. {
  229. var subView = new View () { Id = "subView", X = 1, Width = 1, Height = 7, Text = "subView", TextDirection = TextDirection.TopBottom_LeftRight };
  230. var view = new View () { Id = "view", Width = 2, Height = 20, Text = "01234567890123456789", TextDirection = TextDirection.TopBottom_LeftRight };
  231. view.Add (subView);
  232. var content = new View () { Id = "content", Width = 20, Height = 20 };
  233. content.Add (view);
  234. var container = new View () { Id = "container", X = 1, Y = 1, Width = 5, Height = 5 };
  235. container.Add (content);
  236. var top = Application.Top;
  237. top.Add (container);
  238. Application.Driver.Clip = container.Frame;
  239. Application.Begin (top);
  240. TestHelpers.AssertDriverContentsWithFrameAre (@"
  241. 0s
  242. 1u
  243. 2b
  244. 3V
  245. 4i", output);
  246. content.X = -1;
  247. Application.Refresh ();
  248. TestHelpers.AssertDriverContentsWithFrameAre (@"
  249. s
  250. u
  251. b
  252. V
  253. i", output);
  254. content.X = -2;
  255. Application.Refresh ();
  256. TestHelpers.AssertDriverContentsWithFrameAre (@"", output);
  257. content.X = 0;
  258. content.Y = -1;
  259. Application.Refresh ();
  260. TestHelpers.AssertDriverContentsWithFrameAre (@"
  261. 1u
  262. 2b
  263. 3V
  264. 4i
  265. 5e", output);
  266. content.Y = -6;
  267. Application.Refresh ();
  268. TestHelpers.AssertDriverContentsWithFrameAre (@"
  269. 6w
  270. 7
  271. 8
  272. 9
  273. 0 ", output);
  274. content.Y = -19;
  275. Application.Refresh ();
  276. TestHelpers.AssertDriverContentsWithFrameAre (@"
  277. 9", output);
  278. content.Y = -20;
  279. Application.Refresh ();
  280. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  281. content.X = -2;
  282. content.Y = 0;
  283. Application.Refresh ();
  284. TestHelpers.AssertDriverContentsWithFrameAre ("", output);
  285. }
  286. }
  287. }