DrawTests.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 = Alignment.End,
  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 = Alignment.End,
  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. ViewportSettings = ViewportSettings.ClipContentOnly
  779. };
  780. view.SetContentSize (new Size (10, 10));
  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. };
  809. view.SetContentSize (new Size (10, 10));
  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. }