DrawTests.cs 19 KB

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