DrawTests.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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. [Fact]
  139. [SetupFakeDriver]
  140. public void Clear_ClearsEntireViewport ()
  141. {
  142. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  143. var view = new View
  144. {
  145. Text = "X",
  146. X = 1, Y = 1,
  147. Width = 3, Height = 3,
  148. BorderStyle = LineStyle.Single
  149. };
  150. superView.Add (view);
  151. superView.BeginInit ();
  152. superView.EndInit ();
  153. superView.LayoutSubviews ();
  154. superView.Draw ();
  155. TestHelpers.AssertDriverContentsWithFrameAre (
  156. @"
  157. ┌─┐
  158. │X│
  159. └─┘",
  160. _output);
  161. view.Clear ();
  162. TestHelpers.AssertDriverContentsWithFrameAre (
  163. @"
  164. ┌─┐
  165. │ │
  166. └─┘",
  167. _output);
  168. }
  169. [Fact]
  170. [SetupFakeDriver]
  171. public void Clear_WithClearVisibleContentOnly_ClearsVisibleContentOnly ()
  172. {
  173. var superView = new View { Width = Dim.Fill (), Height = Dim.Fill () };
  174. var view = new View
  175. {
  176. Text = "X",
  177. X = 1, Y = 1,
  178. Width = 3, Height = 3,
  179. BorderStyle = LineStyle.Single,
  180. ViewportSettings = ViewportSettings.ClearContentOnly
  181. };
  182. superView.Add (view);
  183. superView.BeginInit ();
  184. superView.EndInit ();
  185. superView.LayoutSubviews ();
  186. superView.Draw ();
  187. TestHelpers.AssertDriverContentsWithFrameAre (
  188. @"
  189. ┌─┐
  190. │X│
  191. └─┘",
  192. _output);
  193. view.Clear ();
  194. TestHelpers.AssertDriverContentsWithFrameAre (
  195. @"
  196. ┌─┐
  197. │ │
  198. └─┘",
  199. _output);
  200. }
  201. [Fact]
  202. [AutoInitShutdown]
  203. [Trait ("Category", "Unicode")]
  204. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  205. {
  206. const string us = "\U0000f900";
  207. var r = (Rune)0xf900;
  208. Assert.Equal ("豈", us);
  209. Assert.Equal ("豈", r.ToString ());
  210. Assert.Equal (us, r.ToString ());
  211. Assert.Equal (2, us.GetColumns ());
  212. Assert.Equal (2, r.GetColumns ());
  213. var win = new Window { Title = us };
  214. var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill ()};
  215. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  216. win.Add (view, tf);
  217. Toplevel top = new ();
  218. top.Add (win);
  219. Application.Begin (top);
  220. ((FakeDriver)Application.Driver!).SetBufferSize (10, 4);
  221. const string expectedOutput = """
  222. ┌┤豈├────┐
  223. │豈 │
  224. │豈 │
  225. └────────┘
  226. """;
  227. TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
  228. TestHelpers.AssertDriverContentsAre (expectedOutput, _output);
  229. // This test has nothing to do with color - removing as it is not relevant and fragile
  230. top.Dispose ();
  231. }
  232. // TODO: Refactor this test to not depend on TextView etc... Make it as primitive as possible
  233. [Fact]
  234. [AutoInitShutdown]
  235. [Trait ("Category", "Unicode")]
  236. public void Clipping_AddRune_Left_Or_Right_Replace_Previous_Or_Next_Wide_Rune_With_Space ()
  237. {
  238. var tv = new TextView
  239. {
  240. Width = Dim.Fill (),
  241. Height = Dim.Fill (),
  242. Text = """
  243. これは広いルーンラインです。
  244. これは広いルーンラインです。
  245. これは広いルーンラインです。
  246. これは広いルーンラインです。
  247. これは広いルーンラインです。
  248. これは広いルーンラインです。
  249. これは広いルーンラインです。
  250. これは広いルーンラインです。
  251. """
  252. };
  253. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  254. win.Add (tv);
  255. var top = new Toplevel ();
  256. top.Add (win);
  257. var view = new View { Text = "ワイドルーン。", Height = Dim.Fill (), Width = Dim.Fill () };
  258. // Don't have unit tests use things that aren't absolutely critical for the test, like Dialog
  259. var dg = new Window { X = 2, Y = 2, Width = 14, Height = 3 };
  260. dg.Add (view);
  261. RunState rsTop = Application.Begin (top);
  262. RunState rsDiag = Application.Begin (dg);
  263. ((FakeDriver)Application.Driver!).SetBufferSize (30, 10);
  264. const string expectedOutput = """
  265. ┌────────────────────────────┐
  266. │これは広いルーンラインです。│
  267. │�┌────────────┐�ラインです。│
  268. │�│ワイドルーン│�ラインです。│
  269. │�└────────────┘�ラインです。│
  270. │これは広いルーンラインです。│
  271. │これは広いルーンラインです。│
  272. │これは広いルーンラインです。│
  273. │これは広いルーンラインです。│
  274. └────────────────────────────┘
  275. """;
  276. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expectedOutput, _output);
  277. Assert.Equal (new Rectangle (0, 0, 30, 10), pos);
  278. Application.End (rsDiag);
  279. dg.Dispose ();
  280. Application.End (rsTop);
  281. top.Dispose ();
  282. }
  283. [Fact]
  284. [AutoInitShutdown]
  285. [Trait ("Category", "Output")]
  286. public void Colors_On_TextAlignment_Right_And_Bottom ()
  287. {
  288. var viewRight = new View
  289. {
  290. Text = "Test",
  291. Width = 6,
  292. Height = 1,
  293. TextAlignment = Alignment.End,
  294. ColorScheme = Colors.ColorSchemes ["Base"]
  295. };
  296. var viewBottom = new View
  297. {
  298. Text = "Test",
  299. TextDirection = TextDirection.TopBottom_LeftRight,
  300. Y = 1,
  301. Width = 1,
  302. Height = 6,
  303. VerticalTextAlignment = Alignment.End,
  304. ColorScheme = Colors.ColorSchemes ["Base"]
  305. };
  306. Toplevel top = new ();
  307. top.Add (viewRight, viewBottom);
  308. Application.Begin (top);
  309. ((FakeDriver)Application.Driver!).SetBufferSize (7, 7);
  310. TestHelpers.AssertDriverContentsWithFrameAre (
  311. """
  312. Test
  313. T
  314. e
  315. s
  316. t
  317. """,
  318. _output
  319. );
  320. TestHelpers.AssertDriverAttributesAre (
  321. """
  322. 000000
  323. 0
  324. 0
  325. 0
  326. 0
  327. 0
  328. 0
  329. """,
  330. Application.Driver,
  331. Colors.ColorSchemes ["Base"]!.Normal
  332. );
  333. top.Dispose ();
  334. }
  335. [Fact]
  336. [SetupFakeDriver]
  337. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  338. {
  339. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  340. view.BeginInit ();
  341. view.EndInit ();
  342. view.SetRelativeLayout (Application.Screen.Size);
  343. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  344. Assert.Equal (Rectangle.Empty, view.Viewport);
  345. view.Draw ();
  346. TestHelpers.AssertDriverContentsWithFrameAre (
  347. """
  348. ┌┐
  349. └┘
  350. """,
  351. _output
  352. );
  353. }
  354. [Fact]
  355. [SetupFakeDriver]
  356. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  357. {
  358. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  359. view.Border.Thickness = new Thickness (1, 1, 1, 0);
  360. view.BeginInit ();
  361. view.EndInit ();
  362. view.SetRelativeLayout (Application.Screen.Size);
  363. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  364. Assert.Equal (Rectangle.Empty, view.Viewport);
  365. view.Draw ();
  366. TestHelpers.AssertDriverContentsWithFrameAre ("──", _output);
  367. }
  368. [Fact]
  369. [SetupFakeDriver]
  370. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  371. {
  372. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  373. view.Border.Thickness = new Thickness (0, 1, 1, 1);
  374. view.BeginInit ();
  375. view.EndInit ();
  376. view.SetRelativeLayout (Application.Screen.Size);
  377. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  378. Assert.Equal (Rectangle.Empty, view.Viewport);
  379. view.Draw ();
  380. TestHelpers.AssertDriverContentsWithFrameAre (
  381. """
  382. """,
  383. _output
  384. );
  385. }
  386. [Fact]
  387. [SetupFakeDriver]
  388. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  389. {
  390. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  391. view.Border.Thickness = new Thickness (1, 1, 0, 1);
  392. view.BeginInit ();
  393. view.EndInit ();
  394. view.SetRelativeLayout (Application.Screen.Size);
  395. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  396. Assert.Equal (Rectangle.Empty, view.Viewport);
  397. view.Draw ();
  398. TestHelpers.AssertDriverContentsWithFrameAre (
  399. """
  400. """,
  401. _output
  402. );
  403. }
  404. [Fact]
  405. [SetupFakeDriver]
  406. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  407. {
  408. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  409. view.Border.Thickness = new Thickness (1, 0, 1, 1);
  410. view.BeginInit ();
  411. view.EndInit ();
  412. view.SetRelativeLayout (Application.Screen.Size);
  413. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  414. Assert.Equal (Rectangle.Empty, view.Viewport);
  415. view.Draw ();
  416. TestHelpers.AssertDriverContentsWithFrameAre ("││",
  417. _output
  418. );
  419. }
  420. [Fact]
  421. [AutoInitShutdown]
  422. public void Draw_Negative_Viewport_Horizontal_With_New_Lines ()
  423. {
  424. var subView = new View
  425. {
  426. Id = "subView",
  427. X = 1,
  428. Width = 1,
  429. Height = 7,
  430. Text = """
  431. s
  432. u
  433. b
  434. V
  435. i
  436. e
  437. w
  438. """
  439. };
  440. var view = new View
  441. {
  442. Id = "view", Width = 2, Height = 20, Text = """
  443. 0
  444. 1
  445. 2
  446. 3
  447. 4
  448. 5
  449. 6
  450. 7
  451. 8
  452. 9
  453. 0
  454. 1
  455. 2
  456. 3
  457. 4
  458. 5
  459. 6
  460. 7
  461. 8
  462. 9
  463. """
  464. };
  465. view.Add (subView);
  466. var content = new View { Id = "content", Width = 20, Height = 20 };
  467. content.Add (view);
  468. var container = new View
  469. {
  470. Id = "container",
  471. X = 1,
  472. Y = 1,
  473. Width = 5,
  474. Height = 5
  475. };
  476. container.Add (content);
  477. Toplevel top = new ();
  478. top.Add (container);
  479. Application.Driver!.Clip = container.Frame;
  480. Application.Begin (top);
  481. TestHelpers.AssertDriverContentsWithFrameAre (
  482. """
  483. 0s
  484. 1u
  485. 2b
  486. 3V
  487. 4i
  488. """,
  489. _output
  490. );
  491. content.X = -1;
  492. Application.Refresh ();
  493. TestHelpers.AssertDriverContentsWithFrameAre (
  494. """
  495. s
  496. u
  497. b
  498. V
  499. i
  500. """,
  501. _output
  502. );
  503. content.X = -2;
  504. Application.Refresh ();
  505. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  506. content.X = 0;
  507. content.Y = -1;
  508. Application.Refresh ();
  509. TestHelpers.AssertDriverContentsWithFrameAre (
  510. """
  511. 1u
  512. 2b
  513. 3V
  514. 4i
  515. 5e
  516. """,
  517. _output
  518. );
  519. content.Y = -6;
  520. Application.Refresh ();
  521. TestHelpers.AssertDriverContentsWithFrameAre (
  522. """
  523. 6w
  524. 7
  525. 8
  526. 9
  527. 0
  528. """,
  529. _output
  530. );
  531. content.Y = -19;
  532. Application.Refresh ();
  533. TestHelpers.AssertDriverContentsWithFrameAre (
  534. """
  535. 9
  536. """,
  537. _output
  538. );
  539. content.Y = -20;
  540. Application.Refresh ();
  541. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  542. content.X = -2;
  543. content.Y = 0;
  544. Application.Refresh ();
  545. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  546. top.Dispose ();
  547. }
  548. [Fact]
  549. [AutoInitShutdown]
  550. public void Draw_Negative_Viewport_Horizontal_Without_New_Lines ()
  551. {
  552. // BUGBUG: This previously assumed the default height of a View was 1.
  553. var subView = new View
  554. {
  555. Id = "subView",
  556. Y = 1,
  557. Width = 7,
  558. Height = 1,
  559. Text = "subView"
  560. };
  561. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  562. view.Add (subView);
  563. var content = new View { Id = "content", Width = 20, Height = 20 };
  564. content.Add (view);
  565. var container = new View
  566. {
  567. Id = "container",
  568. X = 1,
  569. Y = 1,
  570. Width = 5,
  571. Height = 5
  572. };
  573. container.Add (content);
  574. Toplevel top = new ();
  575. top.Add (container);
  576. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  577. top.LayoutComplete += Top_LayoutComplete;
  578. Application.Begin (top);
  579. TestHelpers.AssertDriverContentsWithFrameAre (
  580. """
  581. 01234
  582. subVi
  583. """,
  584. _output
  585. );
  586. content.X = -1;
  587. Application.Refresh ();
  588. TestHelpers.AssertDriverContentsWithFrameAre (
  589. """
  590. 12345
  591. ubVie
  592. """,
  593. _output
  594. );
  595. content.Y = -1;
  596. Application.Refresh ();
  597. TestHelpers.AssertDriverContentsWithFrameAre (
  598. """
  599. ubVie
  600. """,
  601. _output
  602. );
  603. content.Y = -2;
  604. Application.Refresh ();
  605. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  606. content.X = -20;
  607. content.Y = 0;
  608. Application.Refresh ();
  609. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  610. top.Dispose ();
  611. return;
  612. void Top_LayoutComplete (object? sender, LayoutEventArgs e) { Application.Driver!.Clip = container.Frame; }
  613. }
  614. [Fact]
  615. [AutoInitShutdown]
  616. public void Draw_Negative_Viewport_Vertical ()
  617. {
  618. var subView = new View
  619. {
  620. Id = "subView",
  621. X = 1,
  622. Width = 1,
  623. Height = 7,
  624. Text = "subView",
  625. TextDirection = TextDirection.TopBottom_LeftRight
  626. };
  627. var view = new View
  628. {
  629. Id = "view",
  630. Width = 2,
  631. Height = 20,
  632. Text = "01234567890123456789",
  633. TextDirection = TextDirection.TopBottom_LeftRight
  634. };
  635. view.Add (subView);
  636. var content = new View { Id = "content", Width = 20, Height = 20 };
  637. content.Add (view);
  638. var container = new View
  639. {
  640. Id = "container",
  641. X = 1,
  642. Y = 1,
  643. Width = 5,
  644. Height = 5
  645. };
  646. container.Add (content);
  647. Toplevel top = new ();
  648. top.Add (container);
  649. Application.Driver!.Clip = container.Frame;
  650. Application.Begin (top);
  651. TestHelpers.AssertDriverContentsWithFrameAre (
  652. """
  653. 0s
  654. 1u
  655. 2b
  656. 3V
  657. 4i
  658. """,
  659. _output
  660. );
  661. content.X = -1;
  662. Application.Refresh ();
  663. TestHelpers.AssertDriverContentsWithFrameAre (
  664. """
  665. s
  666. u
  667. b
  668. V
  669. i
  670. """,
  671. _output
  672. );
  673. content.X = -2;
  674. Application.Refresh ();
  675. TestHelpers.AssertDriverContentsWithFrameAre (@"", _output);
  676. content.X = 0;
  677. content.Y = -1;
  678. Application.Refresh ();
  679. TestHelpers.AssertDriverContentsWithFrameAre (
  680. """
  681. 1u
  682. 2b
  683. 3V
  684. 4i
  685. 5e
  686. """,
  687. _output
  688. );
  689. content.Y = -6;
  690. Application.Refresh ();
  691. TestHelpers.AssertDriverContentsWithFrameAre (
  692. """
  693. 6w
  694. 7
  695. 8
  696. 9
  697. 0
  698. """,
  699. _output
  700. );
  701. content.Y = -19;
  702. Application.Refresh ();
  703. TestHelpers.AssertDriverContentsWithFrameAre (
  704. """
  705. 9
  706. """,
  707. _output
  708. );
  709. content.Y = -20;
  710. Application.Refresh ();
  711. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  712. content.X = -2;
  713. content.Y = 0;
  714. Application.Refresh ();
  715. TestHelpers.AssertDriverContentsWithFrameAre ("", _output);
  716. top.Dispose ();
  717. }
  718. [Theory]
  719. [SetupFakeDriver]
  720. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  721. [InlineData ("a𐐀b")]
  722. public void DrawHotString_NonBmp (string expected)
  723. {
  724. var view = new View { Width = 10, Height = 1 };
  725. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  726. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  727. }
  728. // TODO: The tests below that use Label should use View instead.
  729. [Fact]
  730. [AutoInitShutdown]
  731. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  732. {
  733. var us = "\U0001d539";
  734. var r = (Rune)0x1d539;
  735. Assert.Equal ("𝔹", us);
  736. Assert.Equal ("𝔹", r.ToString ());
  737. Assert.Equal (us, r.ToString ());
  738. Assert.Equal (1, us.GetColumns ());
  739. Assert.Equal (1, r.GetColumns ());
  740. var win = new Window { Title = us };
  741. var view = new Label { Text = r.ToString () };
  742. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  743. win.Add (view, tf);
  744. Toplevel top = new ();
  745. top.Add (win);
  746. Application.Begin (top);
  747. ((FakeDriver)Application.Driver!).SetBufferSize (10, 4);
  748. var expected = """
  749. ┌┤𝔹├─────┐
  750. │𝔹 │
  751. │𝔹 │
  752. └────────┘
  753. """;
  754. TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  755. TestHelpers.AssertDriverContentsAre (expected, _output);
  756. top.Dispose ();
  757. // This test has nothing to do with color - removing as it is not relevant and fragile
  758. }
  759. [Fact]
  760. [SetupFakeDriver]
  761. public void SetClip_ClipVisibleContentOnly_VisibleContentIsClipped ()
  762. {
  763. // Screen is 25x25
  764. // View is 25x25
  765. // Viewport is (0, 0, 23, 23)
  766. // ContentSize is (10, 10)
  767. // ViewportToScreen is (1, 1, 23, 23)
  768. // Visible content is (1, 1, 10, 10)
  769. // Expected clip is (1, 1, 10, 10) - same as visible content
  770. Rectangle expectedClip = new (1, 1, 10, 10);
  771. // Arrange
  772. var view = new View ()
  773. {
  774. Width = Dim.Fill (),
  775. Height = Dim.Fill (),
  776. ViewportSettings = ViewportSettings.ClipContentOnly
  777. };
  778. view.SetContentSize (new Size (10, 10));
  779. view.Border.Thickness = new Thickness (1);
  780. view.BeginInit ();
  781. view.EndInit ();
  782. Assert.Equal (view.Frame, Application.Driver?.Clip);
  783. // Act
  784. view.SetClip ();
  785. // Assert
  786. Assert.Equal (expectedClip, Application.Driver?.Clip);
  787. view.Dispose ();
  788. }
  789. [Fact]
  790. [SetupFakeDriver]
  791. public void SetClip_Default_ClipsToViewport ()
  792. {
  793. // Screen is 25x25
  794. // View is 25x25
  795. // Viewport is (0, 0, 23, 23)
  796. // ContentSize is (10, 10)
  797. // ViewportToScreen is (1, 1, 23, 23)
  798. // Visible content is (1, 1, 10, 10)
  799. // Expected clip is (1, 1, 23, 23) - same as Viewport
  800. Rectangle expectedClip = new (1, 1, 23, 23);
  801. // Arrange
  802. var view = new View ()
  803. {
  804. Width = Dim.Fill (),
  805. Height = Dim.Fill (),
  806. };
  807. view.SetContentSize (new Size (10, 10));
  808. view.Border.Thickness = new Thickness (1);
  809. view.BeginInit ();
  810. view.EndInit ();
  811. Assert.Equal (view.Frame, Application.Driver?.Clip);
  812. view.Viewport = view.Viewport with { X = 1, Y = 1 };
  813. // Act
  814. view.SetClip ();
  815. // Assert
  816. Assert.Equal (expectedClip, Application.Driver?.Clip);
  817. view.Dispose ();
  818. }
  819. [Fact]
  820. [TestRespondersDisposed]
  821. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  822. {
  823. Application.Init (new FakeDriver ());
  824. Toplevel top = new ();
  825. var view = new View { X = -2, Text = "view" };
  826. top.Add (view);
  827. Application.Iteration += (s, a) =>
  828. {
  829. Assert.Equal (-2, view.X);
  830. Application.RequestStop ();
  831. };
  832. try
  833. {
  834. Application.Run (top);
  835. }
  836. catch (IndexOutOfRangeException ex)
  837. {
  838. // After the fix this exception will not be caught.
  839. Assert.IsType<IndexOutOfRangeException> (ex);
  840. }
  841. top.Dispose ();
  842. // Shutdown must be called to safely clean up Application if Init has been called
  843. Application.Shutdown ();
  844. }
  845. }