DrawTests.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 (1);
  19. // Only valid location w/in Viewport is 0, 0 (view) - 2, 2 (screen)
  20. view.Move (0, 0);
  21. Assert.Equal (new (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
  22. view.Move (-1, -1);
  23. Assert.Equal (new (2, 2), new Point (Application.Driver.Col, Application.Driver.Row));
  24. view.Move (1, 1);
  25. Assert.Equal (new (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 (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. top.Dispose ();
  236. // This test has nothing to do with color - removing as it is not relevant and fragile
  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 (0, 0, 30, 10), pos);
  285. Application.End (rsDiag);
  286. Application.End (rsTop);
  287. top.Dispose ();
  288. }
  289. [Fact]
  290. [AutoInitShutdown]
  291. [Trait ("Category", "Output")]
  292. public void Colors_On_TextAlignment_Right_And_Bottom ()
  293. {
  294. var viewRight = new View
  295. {
  296. Text = "Test",
  297. Width = 6,
  298. Height = 1,
  299. TextAlignment = Alignment.End,
  300. ColorScheme = Colors.ColorSchemes ["Base"]
  301. };
  302. var viewBottom = new View
  303. {
  304. Text = "Test",
  305. TextDirection = TextDirection.TopBottom_LeftRight,
  306. Y = 1,
  307. Width = 1,
  308. Height = 6,
  309. VerticalTextAlignment = Alignment.End,
  310. ColorScheme = Colors.ColorSchemes ["Base"]
  311. };
  312. Toplevel top = new ();
  313. top.Add (viewRight, viewBottom);
  314. Application.Begin (top);
  315. ((FakeDriver)Application.Driver).SetBufferSize (7, 7);
  316. TestHelpers.AssertDriverContentsWithFrameAre (
  317. """
  318. Test
  319. T
  320. e
  321. s
  322. t
  323. """,
  324. output
  325. );
  326. TestHelpers.AssertDriverAttributesAre (
  327. """
  328. 000000
  329. 0
  330. 0
  331. 0
  332. 0
  333. 0
  334. 0
  335. """,
  336. Application.Driver,
  337. Colors.ColorSchemes ["Base"].Normal
  338. );
  339. top.Dispose ();
  340. }
  341. [Fact]
  342. [SetupFakeDriver]
  343. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  344. {
  345. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  346. view.BeginInit ();
  347. view.EndInit ();
  348. view.SetRelativeLayout (Application.Driver.Screen.Size);
  349. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  350. Assert.Equal (Rectangle.Empty, view.Viewport);
  351. view.Draw ();
  352. TestHelpers.AssertDriverContentsWithFrameAre (
  353. """
  354. ┌┐
  355. └┘
  356. """,
  357. output
  358. );
  359. }
  360. [Fact]
  361. [SetupFakeDriver]
  362. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  363. {
  364. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  365. view.Border.Thickness = new (1, 1, 1, 0);
  366. view.BeginInit ();
  367. view.EndInit ();
  368. view.SetRelativeLayout (Application.Driver.Screen.Size);
  369. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  370. Assert.Equal (Rectangle.Empty, view.Viewport);
  371. view.Draw ();
  372. TestHelpers.AssertDriverContentsWithFrameAre ("──", output);
  373. }
  374. [Fact]
  375. [SetupFakeDriver]
  376. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  377. {
  378. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  379. view.Border.Thickness = new (0, 1, 1, 1);
  380. view.BeginInit ();
  381. view.EndInit ();
  382. view.SetRelativeLayout (Application.Driver.Screen.Size);
  383. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  384. Assert.Equal (Rectangle.Empty, view.Viewport);
  385. view.Draw ();
  386. TestHelpers.AssertDriverContentsWithFrameAre (
  387. """
  388. """,
  389. output
  390. );
  391. }
  392. [Fact]
  393. [SetupFakeDriver]
  394. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  395. {
  396. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  397. view.Border.Thickness = new (1, 1, 0, 1);
  398. view.BeginInit ();
  399. view.EndInit ();
  400. view.SetRelativeLayout (Application.Driver.Screen.Size);
  401. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  402. Assert.Equal (Rectangle.Empty, view.Viewport);
  403. view.Draw ();
  404. TestHelpers.AssertDriverContentsWithFrameAre (
  405. """
  406. """,
  407. output
  408. );
  409. }
  410. [Fact]
  411. [SetupFakeDriver]
  412. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  413. {
  414. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  415. view.Border.Thickness = new (1, 0, 1, 1);
  416. view.BeginInit ();
  417. view.EndInit ();
  418. view.SetRelativeLayout (Application.Driver.Screen.Size);
  419. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  420. Assert.Equal (Rectangle.Empty, view.Viewport);
  421. view.Draw ();
  422. TestHelpers.AssertDriverContentsWithFrameAre (
  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. 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 (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 (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. }