DrawTests.cs 39 KB

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