DrawTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class DrawTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public DrawTests (ITestOutputHelper output) { _output = output; }
  8. [Fact]
  9. [AutoInitShutdown]
  10. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  11. {
  12. var us = "\U0000f900";
  13. var r = (Rune)0xf900;
  14. Assert.Equal ("豈", us);
  15. Assert.Equal ("豈", r.ToString ());
  16. Assert.Equal (us, r.ToString ());
  17. Assert.Equal (2, us.GetColumns ());
  18. Assert.Equal (2, r.GetColumns ());
  19. var win = new Window { Title = us };
  20. var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill () };
  21. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  22. win.Add (view, tf);
  23. Toplevel top = Application.Top;
  24. top.Add (win);
  25. Application.Begin (top);
  26. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  27. var expected = @"
  28. ┌┤豈├────┐
  29. │豈 │
  30. │豈 │
  31. └────────┘";
  32. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  33. TestHelpers.AssertDriverContentsAre (expected, _output);
  34. Attribute [] expectedColors =
  35. {
  36. // 0
  37. Colors.ColorSchemes ["Base"].Normal,
  38. // 1
  39. Colors.ColorSchemes ["Base"].Focus,
  40. // 2
  41. Colors.ColorSchemes ["Base"].HotNormal
  42. };
  43. TestHelpers.AssertDriverAttributesAre (
  44. @"
  45. 0011000000
  46. 0000000000
  47. 0111000000
  48. 0000000000",
  49. Application.Driver,
  50. expectedColors
  51. );
  52. }
  53. // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
  54. [Fact]
  55. [AutoInitShutdown]
  56. public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  57. {
  58. var tv = new TextView
  59. {
  60. Width = Dim.Fill (),
  61. Height = Dim.Fill (),
  62. Text = @"これは広いルーンラインです。
  63. これは広いルーンラインです。
  64. これは広いルーンラインです。
  65. これは広いルーンラインです。
  66. これは広いルーンラインです。
  67. これは広いルーンラインです。
  68. これは広いルーンラインです。
  69. これは広いルーンラインです。"
  70. };
  71. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  72. win.Add (tv);
  73. Application.Top.Add (win);
  74. // Don't use Label. It sets AutoSize = true which is not what we're testing here.
  75. var view = new View { Text = "ワイドルーン。", Height = Dim.Fill (), Width = Dim.Fill () };
  76. // Don't have unit tests use things that aren't absolutely critical for the test, like Dialog
  77. var dg = new Window { X = 2, Y = 2, Width = 14, Height = 3 };
  78. dg.Add (view);
  79. Application.Begin (Application.Top);
  80. Application.Begin (dg);
  81. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  82. var expected = @"
  83. ┌────────────────────────────┐
  84. │これは広いルーンラインです。│
  85. │�┌────────────┐�ラインです。│
  86. │�│ワイドルーン│�ラインです。│
  87. │�└────────────┘�ラインです。│
  88. │これは広いルーンラインです。│
  89. │これは広いルーンラインです。│
  90. │これは広いルーンラインです。│
  91. │これは広いルーンラインです。│
  92. └────────────────────────────┘";
  93. Rect pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  94. Assert.Equal (new Rect (0, 0, 30, 10), pos);
  95. }
  96. [Fact]
  97. [AutoInitShutdown]
  98. public void Colors_On_TextAlignment_Right_And_Bottom ()
  99. {
  100. var viewRight = new View
  101. {
  102. Text = "Test",
  103. Width = 6,
  104. Height = 1,
  105. TextAlignment = TextAlignment.Right,
  106. ColorScheme = Colors.ColorSchemes ["Base"]
  107. };
  108. var viewBottom = new View
  109. {
  110. Text = "Test",
  111. TextDirection = TextDirection.TopBottom_LeftRight,
  112. Y = 1,
  113. Width = 1,
  114. Height = 6,
  115. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  116. ColorScheme = Colors.ColorSchemes ["Base"]
  117. };
  118. Toplevel top = Application.Top;
  119. top.Add (viewRight, viewBottom);
  120. Application.Begin (top);
  121. ((FakeDriver)Application.Driver).SetBufferSize (7, 7);
  122. TestHelpers.AssertDriverContentsWithFrameAre (
  123. @"
  124. Test
  125. T
  126. e
  127. s
  128. t ",
  129. _output
  130. );
  131. TestHelpers.AssertDriverAttributesAre (
  132. @"
  133. 000000
  134. 0
  135. 0
  136. 0
  137. 0
  138. 0
  139. 0",
  140. Application.Driver,
  141. Colors.ColorSchemes ["Base"].Normal
  142. );
  143. }
  144. [Fact]
  145. [SetupFakeDriver]
  146. public void Draw_Minimum_Full_Border_With_Empty_Bounds ()
  147. {
  148. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  149. view.BeginInit ();
  150. view.EndInit ();
  151. view.SetRelativeLayout (Application.Driver.Bounds);
  152. Assert.Equal ("(0,0,2,2)", view.Frame.ToString ());
  153. Assert.Equal ("(0,0,0,0)", view.Bounds.ToString ());
  154. view.Draw ();
  155. TestHelpers.AssertDriverContentsWithFrameAre (
  156. @"
  157. ┌┐
  158. └┘",
  159. _output
  160. );
  161. }
  162. [Fact]
  163. [SetupFakeDriver]
  164. public void Draw_Minimum_Full_Border_With_Empty_Bounds_Without_Bottom ()
  165. {
  166. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  167. view.Border.Thickness = new Thickness (1, 1, 1, 0);
  168. view.BeginInit ();
  169. view.EndInit ();
  170. view.SetRelativeLayout (Application.Driver.Bounds);
  171. Assert.Equal ("(0,0,2,1)", view.Frame.ToString ());
  172. Assert.Equal ("(0,0,0,0)", view.Bounds.ToString ());
  173. view.Draw ();
  174. TestHelpers.AssertDriverContentsWithFrameAre (
  175. @"
  176. ",
  177. _output
  178. );
  179. }
  180. [Fact]
  181. [SetupFakeDriver]
  182. public void Draw_Minimum_Full_Border_With_Empty_Bounds_Without_Left ()
  183. {
  184. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  185. view.Border.Thickness = new Thickness (0, 1, 1, 1);
  186. view.BeginInit ();
  187. view.EndInit ();
  188. view.SetRelativeLayout (Application.Driver.Bounds);
  189. Assert.Equal ("(0,0,1,2)", view.Frame.ToString ());
  190. Assert.Equal ("(0,0,0,0)", view.Bounds.ToString ());
  191. view.Draw ();
  192. TestHelpers.AssertDriverContentsWithFrameAre (
  193. @"
  194. │",
  195. _output
  196. );
  197. }
  198. [Fact]
  199. [SetupFakeDriver]
  200. public void Draw_Minimum_Full_Border_With_Empty_Bounds_Without_Right ()
  201. {
  202. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  203. view.Border.Thickness = new Thickness (1, 1, 0, 1);
  204. view.BeginInit ();
  205. view.EndInit ();
  206. view.SetRelativeLayout (Application.Driver.Bounds);
  207. Assert.Equal ("(0,0,1,2)", view.Frame.ToString ());
  208. Assert.Equal ("(0,0,0,0)", view.Bounds.ToString ());
  209. view.Draw ();
  210. TestHelpers.AssertDriverContentsWithFrameAre (
  211. @"
  212. │",
  213. _output
  214. );
  215. }
  216. [Fact]
  217. [SetupFakeDriver]
  218. public void Draw_Minimum_Full_Border_With_Empty_Bounds_Without_Top ()
  219. {
  220. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  221. view.Border.Thickness = new Thickness (1, 0, 1, 1);
  222. view.BeginInit ();
  223. view.EndInit ();
  224. view.SetRelativeLayout (Application.Driver.Bounds);
  225. Assert.Equal ("(0,0,2,1)", view.Frame.ToString ());
  226. Assert.Equal ("(0,0,0,0)", view.Bounds.ToString ());
  227. view.Draw ();
  228. // BUGBUG: Wha? Is this right? Shouldn't it be "└┘"???
  229. TestHelpers.AssertDriverContentsWithFrameAre (
  230. @"
  231. ┌┐",
  232. _output
  233. );
  234. }
  235. [Fact]
  236. [AutoInitShutdown]
  237. public void Draw_Negative_Bounds_Horizontal_With_New_Lines ()
  238. {
  239. var subView = new View
  240. {
  241. Id = "subView",
  242. X = 1,
  243. Width = 1,
  244. Height = 7,
  245. Text = "s\nu\nb\nV\ni\ne\nw"
  246. };
  247. var view = new View
  248. {
  249. 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"
  250. };
  251. view.Add (subView);
  252. var content = new View { Id = "content", Width = 20, Height = 20 };
  253. content.Add (view);
  254. var container = new View
  255. {
  256. Id = "container",
  257. X = 1,
  258. Y = 1,
  259. Width = 5,
  260. Height = 5
  261. };
  262. container.Add (content);
  263. Toplevel top = Application.Top;
  264. top.Add (container);
  265. Application.Driver.Clip = container.Frame;
  266. Application.Begin (top);
  267. TestHelpers.AssertDriverContentsWithFrameAre (
  268. @"
  269. 0s
  270. 1u
  271. 2b
  272. 3V
  273. 4i",
  274. _output
  275. );
  276. content.X = -1;
  277. Application.Refresh ();
  278. TestHelpers.AssertDriverContentsWithFrameAre (
  279. @"
  280. s
  281. u
  282. b
  283. V
  284. i",
  285. _output
  286. );
  287. content.X = -2;
  288. Application.Refresh ();
  289. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  290. content.X = 0;
  291. content.Y = -1;
  292. Application.Refresh ();
  293. TestHelpers.AssertDriverContentsWithFrameAre (
  294. @"
  295. 1u
  296. 2b
  297. 3V
  298. 4i
  299. 5e",
  300. _output
  301. );
  302. content.Y = -6;
  303. Application.Refresh ();
  304. TestHelpers.AssertDriverContentsWithFrameAre (
  305. @"
  306. 6w
  307. 7
  308. 8
  309. 9
  310. 0 ",
  311. _output
  312. );
  313. content.Y = -19;
  314. Application.Refresh ();
  315. TestHelpers.AssertDriverContentsWithFrameAre (
  316. @"
  317. 9",
  318. _output
  319. );
  320. content.Y = -20;
  321. Application.Refresh ();
  322. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  323. content.X = -2;
  324. content.Y = 0;
  325. Application.Refresh ();
  326. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  327. }
  328. [Fact]
  329. [AutoInitShutdown]
  330. public void Draw_Negative_Bounds_Horizontal_Without_New_Lines ()
  331. {
  332. // BUGBUG: This previously assumed the default height of a View was 1.
  333. var subView = new View
  334. {
  335. Id = "subView",
  336. Y = 1,
  337. Width = 7,
  338. Height = 1,
  339. Text = "subView"
  340. };
  341. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  342. view.Add (subView);
  343. var content = new View { Id = "content", Width = 20, Height = 20 };
  344. content.Add (view);
  345. var container = new View
  346. {
  347. Id = "container",
  348. X = 1,
  349. Y = 1,
  350. Width = 5,
  351. Height = 5
  352. };
  353. container.Add (content);
  354. Toplevel top = Application.Top;
  355. top.Add (container);
  356. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  357. void Top_LayoutComplete (object sender, LayoutEventArgs e) { Application.Driver.Clip = container.Frame; }
  358. top.LayoutComplete += Top_LayoutComplete;
  359. Application.Begin (top);
  360. TestHelpers.AssertDriverContentsWithFrameAre (
  361. @"
  362. 01234
  363. subVi",
  364. _output
  365. );
  366. content.X = -1;
  367. Application.Refresh ();
  368. TestHelpers.AssertDriverContentsWithFrameAre (
  369. @"
  370. 12345
  371. ubVie",
  372. _output
  373. );
  374. content.Y = -1;
  375. Application.Refresh ();
  376. TestHelpers.AssertDriverContentsWithFrameAre (
  377. @"
  378. ubVie",
  379. _output
  380. );
  381. content.Y = -2;
  382. Application.Refresh ();
  383. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  384. content.X = -20;
  385. content.Y = 0;
  386. Application.Refresh ();
  387. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  388. }
  389. [Fact]
  390. [AutoInitShutdown]
  391. public void Draw_Negative_Bounds_Vertical ()
  392. {
  393. var subView = new View
  394. {
  395. Id = "subView",
  396. X = 1,
  397. Width = 1,
  398. Height = 7,
  399. Text = "subView",
  400. TextDirection = TextDirection.TopBottom_LeftRight
  401. };
  402. var view = new View
  403. {
  404. Id = "view",
  405. Width = 2,
  406. Height = 20,
  407. Text = "01234567890123456789",
  408. TextDirection = TextDirection.TopBottom_LeftRight
  409. };
  410. view.Add (subView);
  411. var content = new View { Id = "content", Width = 20, Height = 20 };
  412. content.Add (view);
  413. var container = new View
  414. {
  415. Id = "container",
  416. X = 1,
  417. Y = 1,
  418. Width = 5,
  419. Height = 5
  420. };
  421. container.Add (content);
  422. Toplevel top = Application.Top;
  423. top.Add (container);
  424. Application.Driver.Clip = container.Frame;
  425. Application.Begin (top);
  426. TestHelpers.AssertDriverContentsWithFrameAre (
  427. @"
  428. 0s
  429. 1u
  430. 2b
  431. 3V
  432. 4i",
  433. _output
  434. );
  435. content.X = -1;
  436. Application.Refresh ();
  437. TestHelpers.AssertDriverContentsWithFrameAre (
  438. @"
  439. s
  440. u
  441. b
  442. V
  443. i",
  444. _output
  445. );
  446. content.X = -2;
  447. Application.Refresh ();
  448. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  449. content.X = 0;
  450. content.Y = -1;
  451. Application.Refresh ();
  452. TestHelpers.AssertDriverContentsWithFrameAre (
  453. @"
  454. 1u
  455. 2b
  456. 3V
  457. 4i
  458. 5e",
  459. _output
  460. );
  461. content.Y = -6;
  462. Application.Refresh ();
  463. TestHelpers.AssertDriverContentsWithFrameAre (
  464. @"
  465. 6w
  466. 7
  467. 8
  468. 9
  469. 0 ",
  470. _output
  471. );
  472. content.Y = -19;
  473. Application.Refresh ();
  474. TestHelpers.AssertDriverContentsWithFrameAre (
  475. @"
  476. 9",
  477. _output
  478. );
  479. content.Y = -20;
  480. Application.Refresh ();
  481. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  482. content.X = -2;
  483. content.Y = 0;
  484. Application.Refresh ();
  485. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  486. }
  487. [Theory]
  488. [SetupFakeDriver]
  489. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  490. [InlineData ("a𐐀b")]
  491. public void DrawHotString_NonBmp (string expected)
  492. {
  493. var view = new View { Width = 10, Height = 1 };
  494. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  495. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  496. }
  497. // TODO: The tests below that use Label should use View instead.
  498. [Fact]
  499. [AutoInitShutdown]
  500. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  501. {
  502. var us = "\U0001d539";
  503. var r = (Rune)0x1d539;
  504. Assert.Equal ("𝔹", us);
  505. Assert.Equal ("𝔹", r.ToString ());
  506. Assert.Equal (us, r.ToString ());
  507. Assert.Equal (1, us.GetColumns ());
  508. Assert.Equal (1, r.GetColumns ());
  509. var win = new Window { Title = us };
  510. var view = new Label { Text = r.ToString () };
  511. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  512. win.Add (view, tf);
  513. Toplevel top = Application.Top;
  514. top.Add (win);
  515. Application.Begin (top);
  516. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  517. var expected = @"
  518. ┌┤𝔹├─────┐
  519. │𝔹 │
  520. │𝔹 │
  521. └────────┘";
  522. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  523. TestHelpers.AssertDriverContentsAre (expected, _output);
  524. Attribute [] expectedColors =
  525. {
  526. // 0
  527. Colors.ColorSchemes ["Base"].Normal,
  528. // 1
  529. Colors.ColorSchemes ["Base"].Focus,
  530. // 2
  531. Colors.ColorSchemes ["Base"].HotNormal
  532. };
  533. TestHelpers.AssertDriverAttributesAre (
  534. @"
  535. 0010000000
  536. 0000000000
  537. 0111000000
  538. 0000000000",
  539. Application.Driver,
  540. expectedColors
  541. );
  542. }
  543. }