DrawTests.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. #nullable enable
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. [Trait ("Category", "Output")]
  6. public class DrawTests (ITestOutputHelper _output)
  7. {
  8. [Fact]
  9. [SetupFakeDriver]
  10. public void Move_Is_Constrained_To_Viewport ()
  11. {
  12. var view = new View
  13. {
  14. X = 1,
  15. Y = 1,
  16. Width = 3, Height = 3
  17. };
  18. view.Margin.Thickness = new Thickness (1);
  19. // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen)
  20. view.Move (0, 0);
  21. Assert.Equal (new Point (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
  22. view.Move (-1, -1);
  23. Assert.Equal (new Point (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
  24. view.Move (1, 1);
  25. Assert.Equal (new Point (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
  26. }
  27. [Fact]
  28. [SetupFakeDriver]
  29. public void AddRune_Is_Constrained_To_Viewport ()
  30. {
  31. var view = new View
  32. {
  33. X = 1,
  34. Y = 1,
  35. Width = 3, Height = 3
  36. };
  37. view.Margin.Thickness = new Thickness (1);
  38. View.Diagnostics = ViewDiagnosticFlags.Padding;
  39. view.BeginInit ();
  40. view.EndInit ();
  41. view.Draw ();
  42. // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen)
  43. Assert.Equal ((Rune)' ', Application.Driver.Contents [2, 2].Rune);
  44. view.AddRune (0, 0, Rune.ReplacementChar);
  45. Assert.Equal (Rune.ReplacementChar, Application.Driver.Contents [2, 2].Rune);
  46. view.AddRune (-1, -1, Rune.ReplacementChar);
  47. Assert.Equal ((Rune)'M', Application.Driver.Contents [1, 1].Rune);
  48. view.AddRune (1, 1, Rune.ReplacementChar);
  49. Assert.Equal ((Rune)'M', Application.Driver.Contents [3, 3].Rune);
  50. View.Diagnostics = ViewDiagnosticFlags.Off;
  51. }
  52. [Theory]
  53. [InlineData (0, 0, 1, 1)]
  54. [InlineData (0, 0, 2, 2)]
  55. [InlineData (-1, -1, 2, 2)]
  56. [SetupFakeDriver]
  57. public void FillRect_Fills_HonorsClip (int x, int y, int width, int height)
  58. {
  59. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  60. var view = new View
  61. {
  62. Text = "X",
  63. X = 1, Y = 1,
  64. Width = 3, Height = 3,
  65. BorderStyle = LineStyle.Single
  66. };
  67. superView.Add (view);
  68. superView.BeginInit ();
  69. superView.EndInit ();
  70. superView.LayoutSubviews ();
  71. superView.Draw ();
  72. TestHelpers.AssertDriverContentsWithFrameAre (
  73. @"
  74. ┌─┐
  75. │X│
  76. └─┘",
  77. _output);
  78. Rectangle toFill = new (x, y, width, height);
  79. view.FillRect (toFill);
  80. TestHelpers.AssertDriverContentsWithFrameAre (
  81. @"
  82. ┌─┐
  83. │ │
  84. └─┘",
  85. _output);
  86. // Now try to clear beyond Viewport (invalid; clipping should prevent)
  87. superView.SetNeedsDisplay ();
  88. superView.Draw ();
  89. TestHelpers.AssertDriverContentsWithFrameAre (
  90. @"
  91. ┌─┐
  92. │X│
  93. └─┘",
  94. _output);
  95. toFill = new (-width, -height, width, height);
  96. view.FillRect (toFill);
  97. TestHelpers.AssertDriverContentsWithFrameAre (
  98. @"
  99. ┌─┐
  100. │X│
  101. └─┘",
  102. _output);
  103. // Now try to clear beyond Viewport (valid)
  104. superView.SetNeedsDisplay ();
  105. superView.Draw ();
  106. TestHelpers.AssertDriverContentsWithFrameAre (
  107. @"
  108. ┌─┐
  109. │X│
  110. └─┘",
  111. _output);
  112. toFill = new (-1, -1, width + 1, height + 1);
  113. view.FillRect (toFill);
  114. TestHelpers.AssertDriverContentsWithFrameAre (
  115. @"
  116. ┌─┐
  117. │ │
  118. └─┘",
  119. _output);
  120. // Now clear too much size
  121. superView.SetNeedsDisplay ();
  122. superView.Draw ();
  123. TestHelpers.AssertDriverContentsWithFrameAre (
  124. @"
  125. ┌─┐
  126. │X│
  127. └─┘",
  128. _output);
  129. toFill = new (0, 0, width * 2, height * 2);
  130. view.FillRect (toFill);
  131. TestHelpers.AssertDriverContentsWithFrameAre (
  132. @"
  133. ┌─┐
  134. │ │
  135. └─┘",
  136. _output);
  137. }
  138. [Theory]
  139. [InlineData (0, 0, 1, 1)]
  140. [InlineData (0, 0, 2, 2)]
  141. [InlineData (-1, -1, 2, 2)]
  142. [SetupFakeDriver]
  143. public void Clear_ClearsEntireViewport (int x, int y, int width, int height)
  144. {
  145. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  146. var view = new View
  147. {
  148. Text = "X",
  149. X = 1, Y = 1,
  150. Width = 3, Height = 3,
  151. BorderStyle = LineStyle.Single
  152. };
  153. superView.Add (view);
  154. superView.BeginInit ();
  155. superView.EndInit ();
  156. superView.LayoutSubviews ();
  157. superView.Draw ();
  158. TestHelpers.AssertDriverContentsWithFrameAre (
  159. @"
  160. ┌─┐
  161. │X│
  162. └─┘",
  163. _output);
  164. view.Clear ();
  165. TestHelpers.AssertDriverContentsWithFrameAre (
  166. @"
  167. ┌─┐
  168. │ │
  169. └─┘",
  170. _output);
  171. }
  172. [Theory]
  173. [InlineData (0, 0, 1, 1)]
  174. [InlineData (0, 0, 2, 2)]
  175. [InlineData (-1, -1, 2, 2)]
  176. [SetupFakeDriver]
  177. public void Clear_WithClearVisibleContentOnly_ClearsVisibleContentOnly (int x, int y, int width, int height)
  178. {
  179. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  180. var view = new View
  181. {
  182. Text = "X",
  183. X = 1, Y = 1,
  184. Width = 3, Height = 3,
  185. BorderStyle = LineStyle.Single,
  186. ViewportSettings = ViewportSettings.ClearContentOnly
  187. };
  188. superView.Add (view);
  189. superView.BeginInit ();
  190. superView.EndInit ();
  191. superView.LayoutSubviews ();
  192. superView.Draw ();
  193. TestHelpers.AssertDriverContentsWithFrameAre (
  194. @"
  195. ┌─┐
  196. │X│
  197. └─┘",
  198. _output);
  199. view.Clear ();
  200. TestHelpers.AssertDriverContentsWithFrameAre (
  201. @"
  202. ┌─┐
  203. │ │
  204. └─┘",
  205. _output);
  206. }
  207. [Fact]
  208. [AutoInitShutdown]
  209. [Trait ("Category", "Unicode")]
  210. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  211. {
  212. const string us = "\U0000f900";
  213. var r = (Rune)0xf900;
  214. Assert.Equal ("豈", us);
  215. Assert.Equal ("豈", r.ToString ());
  216. Assert.Equal (us, r.ToString ());
  217. Assert.Equal (2, us.GetColumns ());
  218. Assert.Equal (2, r.GetColumns ());
  219. var win = new Window { Title = us };
  220. var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill ()};
  221. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  222. win.Add (view, tf);
  223. Toplevel top = new ();
  224. top.Add (win);
  225. Application.Begin (top);
  226. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  227. const string expectedOutput = """
  228. ┌┤豈├────┐
  229. │豈 │
  230. │豈 │
  231. └────────┘
  232. """;
  233. TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
  234. TestHelpers.AssertDriverContentsAre (expectedOutput, _output);
  235. // This test has nothing to do with color - removing as it is not relevant and fragile
  236. }
  237. // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
  238. [Fact]
  239. [AutoInitShutdown]
  240. [Trait ("Category", "Unicode")]
  241. public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  242. {
  243. var tv = new TextView
  244. {
  245. Width = Dim.Fill (),
  246. Height = Dim.Fill (),
  247. Text = """
  248. これは広いルーンラインです。
  249. これは広いルーンラインです。
  250. これは広いルーンラインです。
  251. これは広いルーンラインです。
  252. これは広いルーンラインです。
  253. これは広いルーンラインです。
  254. これは広いルーンラインです。
  255. これは広いルーンラインです。
  256. """
  257. };
  258. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  259. win.Add (tv);
  260. var top = new Toplevel ();
  261. top.Add (win);
  262. // Don't use Label. It sets AutoSize = true which is not what we're testing here.
  263. var view = new View { Text = "ワイドルーン。", Height = Dim.Fill (), Width = Dim.Fill () };
  264. // Don't have unit tests use things that aren't absolutely critical for the test, like Dialog
  265. var dg = new Window { X = 2, Y = 2, Width = 14, Height = 3 };
  266. dg.Add (view);
  267. RunState rsTop = Application.Begin (top);
  268. RunState rsDiag = Application.Begin (dg);
  269. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  270. const string expectedOutput = """
  271. ┌────────────────────────────┐
  272. │これは広いルーンラインです。│
  273. │�┌────────────┐�ラインです。│
  274. │�│ワイドルーン│�ラインです。│
  275. │�└────────────┘�ラインです。│
  276. │これは広いルーンラインです。│
  277. │これは広いルーンラインです。│
  278. │これは広いルーンラインです。│
  279. │これは広いルーンラインです。│
  280. └────────────────────────────┘
  281. """;
  282. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
  283. Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
  284. Application.End (rsDiag);
  285. Application.End (rsTop);
  286. }
  287. [Fact]
  288. [AutoInitShutdown]
  289. [Trait ("Category", "Output")]
  290. public void Colors_On_TextAlignment_Right_And_Bottom ()
  291. {
  292. var viewRight = new View
  293. {
  294. Text = "Test",
  295. Width = 6,
  296. Height = 1,
  297. TextAlignment = TextAlignment.Right,
  298. ColorScheme = Colors.ColorSchemes ["Base"]
  299. };
  300. var viewBottom = new View
  301. {
  302. Text = "Test",
  303. TextDirection = TextDirection.TopBottom_LeftRight,
  304. Y = 1,
  305. Width = 1,
  306. Height = 6,
  307. VerticalTextAlignment = VerticalTextAlignment.Bottom,
  308. ColorScheme = Colors.ColorSchemes ["Base"]
  309. };
  310. Toplevel top = new ();
  311. top.Add (viewRight, viewBottom);
  312. Application.Begin (top);
  313. ((FakeDriver)Application.Driver).SetBufferSize (7, 7);
  314. TestHelpers.AssertDriverContentsWithFrameAre (
  315. """
  316. Test
  317. T
  318. e
  319. s
  320. t
  321. """,
  322. _output
  323. );
  324. TestHelpers.AssertDriverAttributesAre (
  325. """
  326. 000000
  327. 0
  328. 0
  329. 0
  330. 0
  331. 0
  332. 0
  333. """,
  334. Application.Driver,
  335. Colors.ColorSchemes ["Base"].Normal
  336. );
  337. }
  338. [Fact]
  339. [SetupFakeDriver]
  340. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  341. {
  342. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  343. view.BeginInit ();
  344. view.EndInit ();
  345. view.SetRelativeLayout (Application.Driver.Screen.Size);
  346. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  347. Assert.Equal (Rectangle.Empty, view.Viewport);
  348. view.Draw ();
  349. TestHelpers.AssertDriverContentsWithFrameAre (
  350. """
  351. ┌┐
  352. └┘
  353. """,
  354. _output
  355. );
  356. }
  357. [Fact]
  358. [SetupFakeDriver]
  359. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  360. {
  361. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  362. view.Border.Thickness = new Thickness (1, 1, 1, 0);
  363. view.BeginInit ();
  364. view.EndInit ();
  365. view.SetRelativeLayout (Application.Driver.Screen.Size);
  366. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  367. Assert.Equal (Rectangle.Empty, view.Viewport);
  368. view.Draw ();
  369. TestHelpers.AssertDriverContentsWithFrameAre (string.Empty, _output);
  370. }
  371. [Fact]
  372. [SetupFakeDriver]
  373. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  374. {
  375. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  376. view.Border.Thickness = new Thickness (0, 1, 1, 1);
  377. view.BeginInit ();
  378. view.EndInit ();
  379. view.SetRelativeLayout (Application.Driver.Screen.Size);
  380. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  381. Assert.Equal (Rectangle.Empty, view.Viewport);
  382. view.Draw ();
  383. TestHelpers.AssertDriverContentsWithFrameAre (
  384. """
  385. """,
  386. _output
  387. );
  388. }
  389. [Fact]
  390. [SetupFakeDriver]
  391. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  392. {
  393. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  394. view.Border.Thickness = new Thickness (1, 1, 0, 1);
  395. view.BeginInit ();
  396. view.EndInit ();
  397. view.SetRelativeLayout (Application.Driver.Screen.Size);
  398. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  399. Assert.Equal (Rectangle.Empty, view.Viewport);
  400. view.Draw ();
  401. TestHelpers.AssertDriverContentsWithFrameAre (
  402. """
  403. """,
  404. _output
  405. );
  406. }
  407. [Fact]
  408. [SetupFakeDriver]
  409. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  410. {
  411. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  412. view.Border.Thickness = new Thickness (1, 0, 1, 1);
  413. view.BeginInit ();
  414. view.EndInit ();
  415. view.SetRelativeLayout (Application.Driver.Screen.Size);
  416. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  417. Assert.Equal (Rectangle.Empty, view.Viewport);
  418. view.Draw ();
  419. // BUGBUG: Wha? Is this right? Shouldn't it be "└┘"???
  420. TestHelpers.AssertDriverContentsWithFrameAre (
  421. """
  422. ┌┐
  423. """,
  424. _output
  425. );
  426. }
  427. [Fact]
  428. [AutoInitShutdown]
  429. public void Draw_Negative_Viewport_Horizontal_With_New_Lines ()
  430. {
  431. var subView = new View
  432. {
  433. Id = "subView",
  434. X = 1,
  435. Width = 1,
  436. Height = 7,
  437. Text = """
  438. s
  439. u
  440. b
  441. V
  442. i
  443. e
  444. w
  445. """
  446. };
  447. var view = new View
  448. {
  449. Id = "view", Width = 2, Height = 20, Text = """
  450. 0
  451. 1
  452. 2
  453. 3
  454. 4
  455. 5
  456. 6
  457. 7
  458. 8
  459. 9
  460. 0
  461. 1
  462. 2
  463. 3
  464. 4
  465. 5
  466. 6
  467. 7
  468. 8
  469. 9
  470. """
  471. };
  472. view.Add (subView);
  473. var content = new View { Id = "content", Width = 20, Height = 20 };
  474. content.Add (view);
  475. var container = new View
  476. {
  477. Id = "container",
  478. X = 1,
  479. Y = 1,
  480. Width = 5,
  481. Height = 5
  482. };
  483. container.Add (content);
  484. Toplevel top = new ();
  485. top.Add (container);
  486. Application.Driver.Clip = container.Frame;
  487. Application.Begin (top);
  488. TestHelpers.AssertDriverContentsWithFrameAre (
  489. """
  490. 0s
  491. 1u
  492. 2b
  493. 3V
  494. 4i
  495. """,
  496. _output
  497. );
  498. content.X = -1;
  499. Application.Refresh ();
  500. TestHelpers.AssertDriverContentsWithFrameAre (
  501. """
  502. s
  503. u
  504. b
  505. V
  506. i
  507. """,
  508. _output
  509. );
  510. content.X = -2;
  511. Application.Refresh ();
  512. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  513. content.X = 0;
  514. content.Y = -1;
  515. Application.Refresh ();
  516. TestHelpers.AssertDriverContentsWithFrameAre (
  517. """
  518. 1u
  519. 2b
  520. 3V
  521. 4i
  522. 5e
  523. """,
  524. _output
  525. );
  526. content.Y = -6;
  527. Application.Refresh ();
  528. TestHelpers.AssertDriverContentsWithFrameAre (
  529. """
  530. 6w
  531. 7
  532. 8
  533. 9
  534. 0
  535. """,
  536. _output
  537. );
  538. content.Y = -19;
  539. Application.Refresh ();
  540. TestHelpers.AssertDriverContentsWithFrameAre (
  541. """
  542. 9
  543. """,
  544. _output
  545. );
  546. content.Y = -20;
  547. Application.Refresh ();
  548. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  549. content.X = -2;
  550. content.Y = 0;
  551. Application.Refresh ();
  552. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  553. }
  554. [Fact]
  555. [AutoInitShutdown]
  556. public void Draw_Negative_Viewport_Horizontal_Without_New_Lines ()
  557. {
  558. // BUGBUG: This previously assumed the default height of a View was 1.
  559. var subView = new View
  560. {
  561. Id = "subView",
  562. Y = 1,
  563. Width = 7,
  564. Height = 1,
  565. Text = "subView"
  566. };
  567. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  568. view.Add (subView);
  569. var content = new View { Id = "content", Width = 20, Height = 20 };
  570. content.Add (view);
  571. var container = new View
  572. {
  573. Id = "container",
  574. X = 1,
  575. Y = 1,
  576. Width = 5,
  577. Height = 5
  578. };
  579. container.Add (content);
  580. Toplevel top = new ();
  581. top.Add (container);
  582. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  583. void Top_LayoutComplete (object sender, LayoutEventArgs e) { Application.Driver.Clip = container.Frame; }
  584. top.LayoutComplete += Top_LayoutComplete;
  585. Application.Begin (top);
  586. TestHelpers.AssertDriverContentsWithFrameAre (
  587. """
  588. 01234
  589. subVi
  590. """,
  591. _output
  592. );
  593. content.X = -1;
  594. Application.Refresh ();
  595. TestHelpers.AssertDriverContentsWithFrameAre (
  596. """
  597. 12345
  598. ubVie
  599. """,
  600. _output
  601. );
  602. content.Y = -1;
  603. Application.Refresh ();
  604. TestHelpers.AssertDriverContentsWithFrameAre (
  605. """
  606. ubVie
  607. """,
  608. _output
  609. );
  610. content.Y = -2;
  611. Application.Refresh ();
  612. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  613. content.X = -20;
  614. content.Y = 0;
  615. Application.Refresh ();
  616. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  617. }
  618. [Fact]
  619. [AutoInitShutdown]
  620. public void Draw_Negative_Viewport_Vertical ()
  621. {
  622. var subView = new View
  623. {
  624. Id = "subView",
  625. X = 1,
  626. Width = 1,
  627. Height = 7,
  628. Text = "subView",
  629. TextDirection = TextDirection.TopBottom_LeftRight
  630. };
  631. var view = new View
  632. {
  633. Id = "view",
  634. Width = 2,
  635. Height = 20,
  636. Text = "01234567890123456789",
  637. TextDirection = TextDirection.TopBottom_LeftRight
  638. };
  639. view.Add (subView);
  640. var content = new View { Id = "content", Width = 20, Height = 20 };
  641. content.Add (view);
  642. var container = new View
  643. {
  644. Id = "container",
  645. X = 1,
  646. Y = 1,
  647. Width = 5,
  648. Height = 5
  649. };
  650. container.Add (content);
  651. Toplevel top = new ();
  652. top.Add (container);
  653. Application.Driver.Clip = container.Frame;
  654. Application.Begin (top);
  655. TestHelpers.AssertDriverContentsWithFrameAre (
  656. """
  657. 0s
  658. 1u
  659. 2b
  660. 3V
  661. 4i
  662. """,
  663. _output
  664. );
  665. content.X = -1;
  666. Application.Refresh ();
  667. TestHelpers.AssertDriverContentsWithFrameAre (
  668. """
  669. s
  670. u
  671. b
  672. V
  673. i
  674. """,
  675. _output
  676. );
  677. content.X = -2;
  678. Application.Refresh ();
  679. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  680. content.X = 0;
  681. content.Y = -1;
  682. Application.Refresh ();
  683. TestHelpers.AssertDriverContentsWithFrameAre (
  684. """
  685. 1u
  686. 2b
  687. 3V
  688. 4i
  689. 5e
  690. """,
  691. _output
  692. );
  693. content.Y = -6;
  694. Application.Refresh ();
  695. TestHelpers.AssertDriverContentsWithFrameAre (
  696. """
  697. 6w
  698. 7
  699. 8
  700. 9
  701. 0
  702. """,
  703. _output
  704. );
  705. content.Y = -19;
  706. Application.Refresh ();
  707. TestHelpers.AssertDriverContentsWithFrameAre (
  708. """
  709. 9
  710. """,
  711. _output
  712. );
  713. content.Y = -20;
  714. Application.Refresh ();
  715. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  716. content.X = -2;
  717. content.Y = 0;
  718. Application.Refresh ();
  719. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  720. }
  721. [Theory]
  722. [SetupFakeDriver]
  723. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  724. [InlineData ("a𐐀b")]
  725. public void DrawHotString_NonBmp (string expected)
  726. {
  727. var view = new View { Width = 10, Height = 1 };
  728. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  729. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  730. }
  731. // TODO: The tests below that use Label should use View instead.
  732. [Fact]
  733. [AutoInitShutdown]
  734. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  735. {
  736. var us = "\U0001d539";
  737. var r = (Rune)0x1d539;
  738. Assert.Equal ("𝔹", us);
  739. Assert.Equal ("𝔹", r.ToString ());
  740. Assert.Equal (us, r.ToString ());
  741. Assert.Equal (1, us.GetColumns ());
  742. Assert.Equal (1, r.GetColumns ());
  743. var win = new Window { Title = us };
  744. var view = new Label { Text = r.ToString () };
  745. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  746. win.Add (view, tf);
  747. Toplevel top = new ();
  748. top.Add (win);
  749. Application.Begin (top);
  750. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  751. var expected = """
  752. ┌┤𝔹├─────┐
  753. │𝔹 │
  754. │𝔹 │
  755. └────────┘
  756. """;
  757. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  758. TestHelpers.AssertDriverContentsAre (expected, _output);
  759. // This test has nothing to do with color - removing as it is not relevant and fragile
  760. }
  761. [Fact]
  762. [SetupFakeDriver]
  763. public void SetClip_ClipVisibleContentOnly_VisibleContentIsClipped ()
  764. {
  765. // Screen is 25x25
  766. // View is 25x25
  767. // Viewport is (0, 0, 23, 23)
  768. // ContentSize is (10, 10)
  769. // ViewportToScreen is (1, 1, 23, 23)
  770. // Visible content is (1, 1, 10, 10)
  771. // Expected clip is (1, 1, 10, 10) - same as visible content
  772. Rectangle expectedClip = new (1, 1, 10, 10);
  773. // Arrange
  774. var view = new View ()
  775. {
  776. Width = Dim.Fill (),
  777. Height = Dim.Fill (),
  778. ContentSize = new Size (10, 10),
  779. ViewportSettings = ViewportSettings.ClipContentOnly
  780. };
  781. view.Border.Thickness = new Thickness (1);
  782. view.BeginInit ();
  783. view.EndInit ();
  784. Assert.Equal (view.Frame, Application.Driver.Clip);
  785. // Act
  786. view.SetClip ();
  787. // Assert
  788. Assert.Equal (expectedClip, Application.Driver.Clip);
  789. view.Dispose ();
  790. }
  791. [Fact]
  792. [SetupFakeDriver]
  793. public void SetClip_Default_ClipsToViewport ()
  794. {
  795. // Screen is 25x25
  796. // View is 25x25
  797. // Viewport is (0, 0, 23, 23)
  798. // ContentSize is (10, 10)
  799. // ViewportToScreen is (1, 1, 23, 23)
  800. // Visible content is (1, 1, 10, 10)
  801. // Expected clip is (1, 1, 23, 23) - same as Viewport
  802. Rectangle expectedClip = new (1, 1, 23, 23);
  803. // Arrange
  804. var view = new View ()
  805. {
  806. Width = Dim.Fill (),
  807. Height = Dim.Fill (),
  808. ContentSize = new Size (10, 10),
  809. };
  810. view.Border.Thickness = new Thickness (1);
  811. view.BeginInit ();
  812. view.EndInit ();
  813. Assert.Equal (view.Frame, Application.Driver.Clip);
  814. view.Viewport = view.Viewport with { X = 1, Y = 1 };
  815. // Act
  816. view.SetClip ();
  817. // Assert
  818. Assert.Equal (expectedClip, Application.Driver.Clip);
  819. view.Dispose ();
  820. }
  821. [Fact]
  822. [TestRespondersDisposed]
  823. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  824. {
  825. Application.Init (new FakeDriver ());
  826. Toplevel top = new ();
  827. var view = new View { X = -2, Text = "view" };
  828. top.Add (view);
  829. Application.Iteration += (s, a) =>
  830. {
  831. Assert.Equal (-2, view.X);
  832. Application.RequestStop ();
  833. };
  834. try
  835. {
  836. Application.Run (top);
  837. }
  838. catch (IndexOutOfRangeException ex)
  839. {
  840. // After the fix this exception will not be caught.
  841. Assert.IsType<IndexOutOfRangeException> (ex);
  842. }
  843. top.Dispose ();
  844. // Shutdown must be called to safely clean up Application if Init has been called
  845. Application.Shutdown ();
  846. }
  847. [Fact]
  848. [TestRespondersDisposed]
  849. public void Draw_Vertical_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  850. {
  851. Application.Init (new FakeDriver ());
  852. Toplevel top = new ();
  853. var view = new View { Y = -2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight, Text = "view" };
  854. top.Add (view);
  855. Application.Iteration += (s, a) =>
  856. {
  857. Assert.Equal (-2, view.Y);
  858. Application.RequestStop ();
  859. };
  860. try
  861. {
  862. Application.Run (top);
  863. }
  864. catch (IndexOutOfRangeException ex)
  865. {
  866. // After the fix this exception will not be caught.
  867. Assert.IsType<IndexOutOfRangeException> (ex);
  868. }
  869. top.Dispose ();
  870. // Shutdown must be called to safely clean up Application if Init has been called
  871. Application.Shutdown ();
  872. }
  873. //TODO: Expand this test to cover Vertical Alignment as well
  874. [SetupFakeDriver]
  875. [Theory]
  876. [InlineData ("0 2 4", TextAlignment.Left, @"
  877. 0 2 4**
  878. *******
  879. *******
  880. *******
  881. *******
  882. *******
  883. *******")]
  884. [InlineData ("0 2 4", TextAlignment.Right, @"
  885. **0 2 4
  886. *******
  887. *******
  888. *******
  889. *******
  890. *******
  891. *******")]
  892. [InlineData ("0 2 4", TextAlignment.Centered, @"
  893. *0 2 4*
  894. *******
  895. *******
  896. *******
  897. *******
  898. *******
  899. *******")]
  900. [InlineData ("0 2 4", TextAlignment.Justified, @"
  901. 0 2 4
  902. *******
  903. *******
  904. *******
  905. *******
  906. *******
  907. *******")]
  908. [InlineData ("0 2 4", TextAlignment.Left, @"
  909. 0 2 4**
  910. *******
  911. *******
  912. *******
  913. *******
  914. *******
  915. *******")]
  916. [InlineData ("0 你 4", TextAlignment.Right, @"
  917. *0 你 4
  918. *******
  919. *******
  920. *******
  921. *******
  922. *******
  923. *******")]
  924. [InlineData ("0 你 4", TextAlignment.Centered, @"
  925. 0 你 4*
  926. *******
  927. *******
  928. *******
  929. *******
  930. *******
  931. *******")]
  932. [InlineData ("0 你 4", TextAlignment.Justified, @"
  933. 0 你 4
  934. *******
  935. *******
  936. *******
  937. *******
  938. *******
  939. *******")]
  940. public void Draw_Text_Alignment (string text, TextAlignment textAlignment, string expectedText)
  941. {
  942. View view = new ()
  943. {
  944. TextAlignment = textAlignment,
  945. Text = text,
  946. Width = 7,
  947. Height = 7
  948. };
  949. Assert.Equal (new Size (7, 7), view.TextFormatter.Size);
  950. Assert.True (view.NeedsDisplay);
  951. Application.Driver.FillRect (view.Frame, (Rune)'*');
  952. view.Draw ();
  953. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
  954. }
  955. }