DrawTests.cs 10 KB

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