DrawTests.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. top.Dispose ();
  237. }
  238. // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
  239. [Fact]
  240. [AutoInitShutdown]
  241. [Trait ("Category", "Unicode")]
  242. public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  243. {
  244. var tv = new TextView
  245. {
  246. Width = Dim.Fill (),
  247. Height = Dim.Fill (),
  248. Text = """
  249. これは広いルーンラインです。
  250. これは広いルーンラインです。
  251. これは広いルーンラインです。
  252. これは広いルーンラインです。
  253. これは広いルーンラインです。
  254. これは広いルーンラインです。
  255. これは広いルーンラインです。
  256. これは広いルーンラインです。
  257. """
  258. };
  259. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  260. win.Add (tv);
  261. var top = new Toplevel ();
  262. top.Add (win);
  263. // Don't use Label. It sets AutoSize = true which is not what we're testing here.
  264. var view = new View { Text = "ワイドルーン。", Height = Dim.Fill (), Width = Dim.Fill () };
  265. // Don't have unit tests use things that aren't absolutely critical for the test, like Dialog
  266. var dg = new Window { X = 2, Y = 2, Width = 14, Height = 3 };
  267. dg.Add (view);
  268. RunState rsTop = Application.Begin (top);
  269. RunState rsDiag = Application.Begin (dg);
  270. ((FakeDriver)Application.Driver).SetBufferSize (30, 10);
  271. const string expectedOutput = """
  272. ┌────────────────────────────┐
  273. │これは広いルーンラインです。│
  274. │�┌────────────┐�ラインです。│
  275. │�│ワイドルーン│�ラインです。│
  276. │�└────────────┘�ラインです。│
  277. │これは広いルーンラインです。│
  278. │これは広いルーンラインです。│
  279. │これは広いルーンラインです。│
  280. │これは広いルーンラインです。│
  281. └────────────────────────────┘
  282. """;
  283. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
  284. Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
  285. Application.End (rsDiag);
  286. dg.Dispose ();
  287. Application.End (rsTop);
  288. top.Dispose ();
  289. }
  290. [Fact]
  291. [AutoInitShutdown]
  292. [Trait ("Category", "Output")]
  293. public void Colors_On_TextAlignment_Right_And_Bottom ()
  294. {
  295. var viewRight = new View
  296. {
  297. Text = "Test",
  298. Width = 6,
  299. Height = 1,
  300. TextAlignment = Alignment.End,
  301. ColorScheme = Colors.ColorSchemes ["Base"]
  302. };
  303. var viewBottom = new View
  304. {
  305. Text = "Test",
  306. TextDirection = TextDirection.TopBottom_LeftRight,
  307. Y = 1,
  308. Width = 1,
  309. Height = 6,
  310. VerticalTextAlignment = Alignment.End,
  311. ColorScheme = Colors.ColorSchemes ["Base"]
  312. };
  313. Toplevel top = new ();
  314. top.Add (viewRight, viewBottom);
  315. Application.Begin (top);
  316. ((FakeDriver)Application.Driver).SetBufferSize (7, 7);
  317. TestHelpers.AssertDriverContentsWithFrameAre (
  318. """
  319. Test
  320. T
  321. e
  322. s
  323. t
  324. """,
  325. _output
  326. );
  327. TestHelpers.AssertDriverAttributesAre (
  328. """
  329. 000000
  330. 0
  331. 0
  332. 0
  333. 0
  334. 0
  335. 0
  336. """,
  337. Application.Driver,
  338. Colors.ColorSchemes ["Base"].Normal
  339. );
  340. top.Dispose ();
  341. }
  342. [Fact]
  343. [SetupFakeDriver]
  344. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  345. {
  346. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  347. view.BeginInit ();
  348. view.EndInit ();
  349. view.SetRelativeLayout (Application.Driver.Screen.Size);
  350. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  351. Assert.Equal (Rectangle.Empty, view.Viewport);
  352. view.Draw ();
  353. TestHelpers.AssertDriverContentsWithFrameAre (
  354. """
  355. ┌┐
  356. └┘
  357. """,
  358. _output
  359. );
  360. }
  361. [Fact]
  362. [SetupFakeDriver]
  363. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  364. {
  365. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  366. view.Border.Thickness = new Thickness (1, 1, 1, 0);
  367. view.BeginInit ();
  368. view.EndInit ();
  369. view.SetRelativeLayout (Application.Driver.Screen.Size);
  370. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  371. Assert.Equal (Rectangle.Empty, view.Viewport);
  372. view.Draw ();
  373. TestHelpers.AssertDriverContentsWithFrameAre ("──", _output);
  374. }
  375. [Fact]
  376. [SetupFakeDriver]
  377. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  378. {
  379. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  380. view.Border.Thickness = new Thickness (0, 1, 1, 1);
  381. view.BeginInit ();
  382. view.EndInit ();
  383. view.SetRelativeLayout (Application.Driver.Screen.Size);
  384. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  385. Assert.Equal (Rectangle.Empty, view.Viewport);
  386. view.Draw ();
  387. TestHelpers.AssertDriverContentsWithFrameAre (
  388. """
  389. """,
  390. _output
  391. );
  392. }
  393. [Fact]
  394. [SetupFakeDriver]
  395. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  396. {
  397. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  398. view.Border.Thickness = new Thickness (1, 1, 0, 1);
  399. view.BeginInit ();
  400. view.EndInit ();
  401. view.SetRelativeLayout (Application.Driver.Screen.Size);
  402. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  403. Assert.Equal (Rectangle.Empty, view.Viewport);
  404. view.Draw ();
  405. TestHelpers.AssertDriverContentsWithFrameAre (
  406. """
  407. """,
  408. _output
  409. );
  410. }
  411. [Fact]
  412. [SetupFakeDriver]
  413. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  414. {
  415. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  416. view.Border.Thickness = new Thickness (1, 0, 1, 1);
  417. view.BeginInit ();
  418. view.EndInit ();
  419. view.SetRelativeLayout (Application.Driver.Screen.Size);
  420. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  421. Assert.Equal (Rectangle.Empty, view.Viewport);
  422. view.Draw ();
  423. TestHelpers.AssertDriverContentsWithFrameAre ("││",
  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. top.Dispose ();
  554. }
  555. [Fact]
  556. [AutoInitShutdown]
  557. public void Draw_Negative_Viewport_Horizontal_Without_New_Lines ()
  558. {
  559. // BUGBUG: This previously assumed the default height of a View was 1.
  560. var subView = new View
  561. {
  562. Id = "subView",
  563. Y = 1,
  564. Width = 7,
  565. Height = 1,
  566. Text = "subView"
  567. };
  568. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  569. view.Add (subView);
  570. var content = new View { Id = "content", Width = 20, Height = 20 };
  571. content.Add (view);
  572. var container = new View
  573. {
  574. Id = "container",
  575. X = 1,
  576. Y = 1,
  577. Width = 5,
  578. Height = 5
  579. };
  580. container.Add (content);
  581. Toplevel top = new ();
  582. top.Add (container);
  583. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  584. void Top_LayoutComplete (object sender, LayoutEventArgs e) { Application.Driver.Clip = container.Frame; }
  585. top.LayoutComplete += Top_LayoutComplete;
  586. Application.Begin (top);
  587. TestHelpers.AssertDriverContentsWithFrameAre (
  588. """
  589. 01234
  590. subVi
  591. """,
  592. _output
  593. );
  594. content.X = -1;
  595. Application.Refresh ();
  596. TestHelpers.AssertDriverContentsWithFrameAre (
  597. """
  598. 12345
  599. ubVie
  600. """,
  601. _output
  602. );
  603. content.Y = -1;
  604. Application.Refresh ();
  605. TestHelpers.AssertDriverContentsWithFrameAre (
  606. """
  607. ubVie
  608. """,
  609. _output
  610. );
  611. content.Y = -2;
  612. Application.Refresh ();
  613. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  614. content.X = -20;
  615. content.Y = 0;
  616. Application.Refresh ();
  617. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  618. top.Dispose ();
  619. }
  620. [Fact]
  621. [AutoInitShutdown]
  622. public void Draw_Negative_Viewport_Vertical ()
  623. {
  624. var subView = new View
  625. {
  626. Id = "subView",
  627. X = 1,
  628. Width = 1,
  629. Height = 7,
  630. Text = "subView",
  631. TextDirection = TextDirection.TopBottom_LeftRight
  632. };
  633. var view = new View
  634. {
  635. Id = "view",
  636. Width = 2,
  637. Height = 20,
  638. Text = "01234567890123456789",
  639. TextDirection = TextDirection.TopBottom_LeftRight
  640. };
  641. view.Add (subView);
  642. var content = new View { Id = "content", Width = 20, Height = 20 };
  643. content.Add (view);
  644. var container = new View
  645. {
  646. Id = "container",
  647. X = 1,
  648. Y = 1,
  649. Width = 5,
  650. Height = 5
  651. };
  652. container.Add (content);
  653. Toplevel top = new ();
  654. top.Add (container);
  655. Application.Driver.Clip = container.Frame;
  656. Application.Begin (top);
  657. TestHelpers.AssertDriverContentsWithFrameAre (
  658. """
  659. 0s
  660. 1u
  661. 2b
  662. 3V
  663. 4i
  664. """,
  665. _output
  666. );
  667. content.X = -1;
  668. Application.Refresh ();
  669. TestHelpers.AssertDriverContentsWithFrameAre (
  670. """
  671. s
  672. u
  673. b
  674. V
  675. i
  676. """,
  677. _output
  678. );
  679. content.X = -2;
  680. Application.Refresh ();
  681. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  682. content.X = 0;
  683. content.Y = -1;
  684. Application.Refresh ();
  685. TestHelpers.AssertDriverContentsWithFrameAre (
  686. """
  687. 1u
  688. 2b
  689. 3V
  690. 4i
  691. 5e
  692. """,
  693. _output
  694. );
  695. content.Y = -6;
  696. Application.Refresh ();
  697. TestHelpers.AssertDriverContentsWithFrameAre (
  698. """
  699. 6w
  700. 7
  701. 8
  702. 9
  703. 0
  704. """,
  705. _output
  706. );
  707. content.Y = -19;
  708. Application.Refresh ();
  709. TestHelpers.AssertDriverContentsWithFrameAre (
  710. """
  711. 9
  712. """,
  713. _output
  714. );
  715. content.Y = -20;
  716. Application.Refresh ();
  717. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  718. content.X = -2;
  719. content.Y = 0;
  720. Application.Refresh ();
  721. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  722. top.Dispose ();
  723. }
  724. [Theory]
  725. [SetupFakeDriver]
  726. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  727. [InlineData ("a𐐀b")]
  728. public void DrawHotString_NonBmp (string expected)
  729. {
  730. var view = new View { Width = 10, Height = 1 };
  731. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  732. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  733. }
  734. // TODO: The tests below that use Label should use View instead.
  735. [Fact]
  736. [AutoInitShutdown]
  737. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  738. {
  739. var us = "\U0001d539";
  740. var r = (Rune)0x1d539;
  741. Assert.Equal ("𝔹", us);
  742. Assert.Equal ("𝔹", r.ToString ());
  743. Assert.Equal (us, r.ToString ());
  744. Assert.Equal (1, us.GetColumns ());
  745. Assert.Equal (1, r.GetColumns ());
  746. var win = new Window { Title = us };
  747. var view = new Label { Text = r.ToString () };
  748. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  749. win.Add (view, tf);
  750. Toplevel top = new ();
  751. top.Add (win);
  752. Application.Begin (top);
  753. ((FakeDriver)Application.Driver).SetBufferSize (10, 4);
  754. var expected = """
  755. ┌┤𝔹├─────┐
  756. │𝔹 │
  757. │𝔹 │
  758. └────────┘
  759. """;
  760. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  761. TestHelpers.AssertDriverContentsAre (expected, _output);
  762. top.Dispose ();
  763. // This test has nothing to do with color - removing as it is not relevant and fragile
  764. }
  765. [Fact]
  766. [SetupFakeDriver]
  767. public void SetClip_ClipVisibleContentOnly_VisibleContentIsClipped ()
  768. {
  769. // Screen is 25x25
  770. // View is 25x25
  771. // Viewport is (0, 0, 23, 23)
  772. // ContentSize is (10, 10)
  773. // ViewportToScreen is (1, 1, 23, 23)
  774. // Visible content is (1, 1, 10, 10)
  775. // Expected clip is (1, 1, 10, 10) - same as visible content
  776. Rectangle expectedClip = new (1, 1, 10, 10);
  777. // Arrange
  778. var view = new View ()
  779. {
  780. Width = Dim.Fill (),
  781. Height = Dim.Fill (),
  782. ViewportSettings = ViewportSettings.ClipContentOnly
  783. };
  784. view.SetContentSize (new Size (10, 10));
  785. view.Border.Thickness = new Thickness (1);
  786. view.BeginInit ();
  787. view.EndInit ();
  788. Assert.Equal (view.Frame, Application.Driver.Clip);
  789. // Act
  790. view.SetClip ();
  791. // Assert
  792. Assert.Equal (expectedClip, Application.Driver.Clip);
  793. view.Dispose ();
  794. }
  795. [Fact]
  796. [SetupFakeDriver]
  797. public void SetClip_Default_ClipsToViewport ()
  798. {
  799. // Screen is 25x25
  800. // View is 25x25
  801. // Viewport is (0, 0, 23, 23)
  802. // ContentSize is (10, 10)
  803. // ViewportToScreen is (1, 1, 23, 23)
  804. // Visible content is (1, 1, 10, 10)
  805. // Expected clip is (1, 1, 23, 23) - same as Viewport
  806. Rectangle expectedClip = new (1, 1, 23, 23);
  807. // Arrange
  808. var view = new View ()
  809. {
  810. Width = Dim.Fill (),
  811. Height = Dim.Fill (),
  812. };
  813. view.SetContentSize (new Size (10, 10));
  814. view.Border.Thickness = new Thickness (1);
  815. view.BeginInit ();
  816. view.EndInit ();
  817. Assert.Equal (view.Frame, Application.Driver.Clip);
  818. view.Viewport = view.Viewport with { X = 1, Y = 1 };
  819. // Act
  820. view.SetClip ();
  821. // Assert
  822. Assert.Equal (expectedClip, Application.Driver.Clip);
  823. view.Dispose ();
  824. }
  825. [Fact]
  826. [TestRespondersDisposed]
  827. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  828. {
  829. Application.Init (new FakeDriver ());
  830. Toplevel top = new ();
  831. var view = new View { X = -2, Text = "view" };
  832. top.Add (view);
  833. Application.Iteration += (s, a) =>
  834. {
  835. Assert.Equal (-2, view.X);
  836. Application.RequestStop ();
  837. };
  838. try
  839. {
  840. Application.Run (top);
  841. }
  842. catch (IndexOutOfRangeException ex)
  843. {
  844. // After the fix this exception will not be caught.
  845. Assert.IsType<IndexOutOfRangeException> (ex);
  846. }
  847. top.Dispose ();
  848. // Shutdown must be called to safely clean up Application if Init has been called
  849. Application.Shutdown ();
  850. }
  851. }