DrawTests.cs 9.8 KB

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