DrawTests.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  1. #nullable enable
  2. using System.Text;
  3. using UnitTests;
  4. using Xunit.Abstractions;
  5. namespace UnitTests.ViewTests;
  6. [Trait ("Category", "Output")]
  7. public class DrawTests (ITestOutputHelper output)
  8. {
  9. [Fact]
  10. [AutoInitShutdown]
  11. [Trait ("Category", "Unicode")]
  12. public void CJK_Compatibility_Ideographs_ConsoleWidth_ColumnWidth_Equal_Two ()
  13. {
  14. const string us = "\U0000f900";
  15. var r = (Rune)0xf900;
  16. Assert.Equal ("豈", us);
  17. Assert.Equal ("豈", r.ToString ());
  18. Assert.Equal (us, r.ToString ());
  19. Assert.Equal (2, us.GetColumns ());
  20. Assert.Equal (2, r.GetColumns ());
  21. var win = new Window { Title = us };
  22. var view = new View { Text = r.ToString (), Height = Dim.Fill (), Width = Dim.Fill () };
  23. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  24. win.Add (view, tf);
  25. Toplevel top = new ();
  26. top.Add (win);
  27. Application.Begin (top);
  28. Application.Driver!.SetScreenSize (10, 4);
  29. const string expectedOutput = """
  30. ┌┤豈├────┐
  31. │豈 │
  32. │豈 │
  33. └────────┘
  34. """;
  35. DriverAssert.AssertDriverContentsWithFrameAre (expectedOutput, output);
  36. DriverAssert.AssertDriverContentsAre (expectedOutput, output);
  37. // This test has nothing to do with color - removing as it is not relevant and fragile
  38. top.Dispose ();
  39. }
  40. [Fact]
  41. [AutoInitShutdown]
  42. [Trait ("Category", "Output")]
  43. public void Colors_On_TextAlignment_Right_And_Bottom ()
  44. {
  45. var viewRight = new View
  46. {
  47. Text = "Test",
  48. Width = 6,
  49. Height = 1,
  50. TextAlignment = Alignment.End,
  51. };
  52. var viewBottom = new View
  53. {
  54. Text = "Test",
  55. TextDirection = TextDirection.TopBottom_LeftRight,
  56. Y = 1,
  57. Width = 1,
  58. Height = 6,
  59. VerticalTextAlignment = Alignment.End,
  60. };
  61. Toplevel top = new ();
  62. top.Add (viewRight, viewBottom);
  63. var rs = Application.Begin (top);
  64. Application.Driver!.SetScreenSize (7, 7);
  65. AutoInitShutdownAttribute.RunIteration ();
  66. DriverAssert.AssertDriverContentsWithFrameAre (
  67. """
  68. Test
  69. T
  70. e
  71. s
  72. t
  73. """,
  74. output
  75. );
  76. DriverAssert.AssertDriverAttributesAre (
  77. """
  78. 000000
  79. 0
  80. 0
  81. 0
  82. 0
  83. 0
  84. 0
  85. """,
  86. output,
  87. Application.Driver,
  88. SchemeManager.GetSchemes () ["Base"]!.Normal
  89. );
  90. top.Dispose ();
  91. }
  92. [Fact]
  93. [SetupFakeApplication]
  94. public void Draw_Minimum_Full_Border_With_Empty_Viewport ()
  95. {
  96. var view = new View { Width = 2, Height = 2, BorderStyle = LineStyle.Single };
  97. Assert.True (view.NeedsLayout);
  98. Assert.True (view.NeedsDraw);
  99. view.Layout ();
  100. Assert.Equal (new (0, 0, 2, 2), view.Frame);
  101. Assert.Equal (Rectangle.Empty, view.Viewport);
  102. Assert.True (view.NeedsDraw);
  103. view.Draw ();
  104. DriverAssert.AssertDriverContentsWithFrameAre (
  105. """
  106. ┌┐
  107. └┘
  108. """,
  109. output
  110. );
  111. }
  112. [Fact]
  113. [SetupFakeApplication]
  114. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Bottom ()
  115. {
  116. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  117. view.Border!.Thickness = new (1, 1, 1, 0);
  118. view.BeginInit ();
  119. view.EndInit ();
  120. view.SetRelativeLayout (Application.Screen.Size);
  121. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  122. Assert.Equal (Rectangle.Empty, view.Viewport);
  123. view.Draw ();
  124. DriverAssert.AssertDriverContentsWithFrameAre ("──", output);
  125. }
  126. [Fact]
  127. [SetupFakeApplication]
  128. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Left ()
  129. {
  130. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  131. view.Border!.Thickness = new (0, 1, 1, 1);
  132. view.BeginInit ();
  133. view.EndInit ();
  134. view.SetRelativeLayout (Application.Screen.Size);
  135. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  136. Assert.Equal (Rectangle.Empty, view.Viewport);
  137. view.Draw ();
  138. DriverAssert.AssertDriverContentsWithFrameAre (
  139. """
  140. """,
  141. output
  142. );
  143. }
  144. [Fact]
  145. [SetupFakeApplication]
  146. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Right ()
  147. {
  148. var view = new View { Width = 1, Height = 2, BorderStyle = LineStyle.Single };
  149. view.Border!.Thickness = new (1, 1, 0, 1);
  150. view.BeginInit ();
  151. view.EndInit ();
  152. view.SetRelativeLayout (Application.Screen.Size);
  153. Assert.Equal (new (0, 0, 1, 2), view.Frame);
  154. Assert.Equal (Rectangle.Empty, view.Viewport);
  155. view.Draw ();
  156. DriverAssert.AssertDriverContentsWithFrameAre (
  157. """
  158. """,
  159. output
  160. );
  161. }
  162. [Fact]
  163. [SetupFakeApplication]
  164. public void Draw_Minimum_Full_Border_With_Empty_Viewport_Without_Top ()
  165. {
  166. var view = new View { Width = 2, Height = 1, BorderStyle = LineStyle.Single };
  167. view.Border!.Thickness = new (1, 0, 1, 1);
  168. view.BeginInit ();
  169. view.EndInit ();
  170. view.SetRelativeLayout (Application.Screen.Size);
  171. Assert.Equal (new (0, 0, 2, 1), view.Frame);
  172. Assert.Equal (Rectangle.Empty, view.Viewport);
  173. view.Draw ();
  174. DriverAssert.AssertDriverContentsWithFrameAre (
  175. "││",
  176. output
  177. );
  178. }
  179. [Fact]
  180. [AutoInitShutdown]
  181. public void Draw_Negative_Viewport_Horizontal_With_New_Lines ()
  182. {
  183. var subView = new View
  184. {
  185. Id = "subView",
  186. X = 1,
  187. Width = 1,
  188. Height = 7,
  189. Text = """
  190. s
  191. u
  192. b
  193. V
  194. i
  195. e
  196. w
  197. """
  198. };
  199. var view = new View
  200. {
  201. Id = "view", Width = 2, Height = 20, Text = """
  202. 0
  203. 1
  204. 2
  205. 3
  206. 4
  207. 5
  208. 6
  209. 7
  210. 8
  211. 9
  212. 0
  213. 1
  214. 2
  215. 3
  216. 4
  217. 5
  218. 6
  219. 7
  220. 8
  221. 9
  222. """
  223. };
  224. view.Add (subView);
  225. var content = new View { Id = "content", Width = 20, Height = 20 };
  226. content.Add (view);
  227. var container = new View
  228. {
  229. Id = "container",
  230. X = 1,
  231. Y = 1,
  232. Width = 5,
  233. Height = 5
  234. };
  235. container.Add (content);
  236. Toplevel top = new ();
  237. top.Add (container);
  238. var rs = Application.Begin (top);
  239. top.Draw ();
  240. DriverAssert.AssertDriverContentsWithFrameAre (
  241. """
  242. 0s
  243. 1u
  244. 2b
  245. 3V
  246. 4i
  247. """,
  248. output
  249. );
  250. content.X = -1;
  251. AutoInitShutdownAttribute.RunIteration ();
  252. DriverAssert.AssertDriverContentsWithFrameAre (
  253. """
  254. s
  255. u
  256. b
  257. V
  258. i
  259. """,
  260. output
  261. );
  262. content.X = -2;
  263. AutoInitShutdownAttribute.RunIteration ();
  264. DriverAssert.AssertDriverContentsWithFrameAre (@"", output);
  265. content.X = 0;
  266. content.Y = -1;
  267. AutoInitShutdownAttribute.RunIteration ();
  268. DriverAssert.AssertDriverContentsWithFrameAre (
  269. """
  270. 1u
  271. 2b
  272. 3V
  273. 4i
  274. 5e
  275. """,
  276. output
  277. );
  278. content.Y = -6;
  279. AutoInitShutdownAttribute.RunIteration ();
  280. DriverAssert.AssertDriverContentsWithFrameAre (
  281. """
  282. 6w
  283. 7
  284. 8
  285. 9
  286. 0
  287. """,
  288. output
  289. );
  290. content.Y = -19;
  291. AutoInitShutdownAttribute.RunIteration ();
  292. DriverAssert.AssertDriverContentsWithFrameAre (
  293. """
  294. 9
  295. """,
  296. output
  297. );
  298. content.Y = -20;
  299. AutoInitShutdownAttribute.RunIteration ();
  300. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  301. content.X = -2;
  302. content.Y = 0;
  303. AutoInitShutdownAttribute.RunIteration ();
  304. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  305. top.Dispose ();
  306. }
  307. [Fact]
  308. [AutoInitShutdown]
  309. public void Draw_Negative_Viewport_Horizontal_Without_New_Lines ()
  310. {
  311. // BUGBUG: This previously assumed the default height of a View was 1.
  312. var subView = new View
  313. {
  314. Id = "subView",
  315. Y = 1,
  316. Width = 7,
  317. Height = 1,
  318. Text = "subView"
  319. };
  320. var view = new View { Id = "view", Width = 20, Height = 2, Text = "01234567890123456789" };
  321. view.Add (subView);
  322. var content = new View { Id = "content", Width = 20, Height = 20 };
  323. content.Add (view);
  324. var container = new View
  325. {
  326. Id = "container",
  327. X = 1,
  328. Y = 1,
  329. Width = 5,
  330. Height = 5
  331. };
  332. container.Add (content);
  333. Toplevel top = new ();
  334. top.Add (container);
  335. // BUGBUG: v2 - it's bogus to reference .Frame before BeginInit. And why is the clip being set anyway???
  336. top.SubViewsLaidOut += Top_LayoutComplete;
  337. Application.Begin (top);
  338. AutoInitShutdownAttribute.RunIteration ();
  339. DriverAssert.AssertDriverContentsWithFrameAre (
  340. """
  341. 01234
  342. subVi
  343. """,
  344. output
  345. );
  346. content.X = -1;
  347. AutoInitShutdownAttribute.RunIteration ();
  348. DriverAssert.AssertDriverContentsWithFrameAre (
  349. """
  350. 12345
  351. ubVie
  352. """,
  353. output
  354. );
  355. content.Y = -1;
  356. AutoInitShutdownAttribute.RunIteration ();
  357. DriverAssert.AssertDriverContentsWithFrameAre (
  358. """
  359. ubVie
  360. """,
  361. output
  362. );
  363. content.Y = -2;
  364. AutoInitShutdownAttribute.RunIteration ();
  365. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  366. content.X = -20;
  367. content.Y = 0;
  368. AutoInitShutdownAttribute.RunIteration ();
  369. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  370. top.Dispose ();
  371. return;
  372. void Top_LayoutComplete (object? sender, LayoutEventArgs e) { Application.Driver!.Clip = new (container.Frame); }
  373. }
  374. [Fact]
  375. [AutoInitShutdown]
  376. public void Draw_Negative_Viewport_Vertical ()
  377. {
  378. var subView = new View
  379. {
  380. Id = "subView",
  381. X = 1,
  382. Width = 1,
  383. Height = 7,
  384. Text = "subView",
  385. TextDirection = TextDirection.TopBottom_LeftRight
  386. };
  387. var view = new View
  388. {
  389. Id = "view",
  390. Width = 2,
  391. Height = 20,
  392. Text = "01234567890123456789",
  393. TextDirection = TextDirection.TopBottom_LeftRight
  394. };
  395. view.Add (subView);
  396. var content = new View { Id = "content", Width = 20, Height = 20 };
  397. content.Add (view);
  398. var container = new View
  399. {
  400. Id = "container",
  401. X = 1,
  402. Y = 1,
  403. Width = 5,
  404. Height = 5
  405. };
  406. container.Add (content);
  407. Toplevel top = new ();
  408. top.Add (container);
  409. Application.Begin (top);
  410. AutoInitShutdownAttribute.RunIteration ();
  411. DriverAssert.AssertDriverContentsWithFrameAre (
  412. """
  413. 0s
  414. 1u
  415. 2b
  416. 3V
  417. 4i
  418. """,
  419. output
  420. );
  421. content.X = -1;
  422. AutoInitShutdownAttribute.RunIteration ();
  423. DriverAssert.AssertDriverContentsWithFrameAre (
  424. """
  425. s
  426. u
  427. b
  428. V
  429. i
  430. """,
  431. output
  432. );
  433. content.X = -2;
  434. AutoInitShutdownAttribute.RunIteration ();
  435. DriverAssert.AssertDriverContentsWithFrameAre (@"", output);
  436. content.X = 0;
  437. content.Y = -1;
  438. AutoInitShutdownAttribute.RunIteration ();
  439. DriverAssert.AssertDriverContentsWithFrameAre (
  440. """
  441. 1u
  442. 2b
  443. 3V
  444. 4i
  445. 5e
  446. """,
  447. output
  448. );
  449. content.Y = -6;
  450. AutoInitShutdownAttribute.RunIteration ();
  451. DriverAssert.AssertDriverContentsWithFrameAre (
  452. """
  453. 6w
  454. 7
  455. 8
  456. 9
  457. 0
  458. """,
  459. output
  460. );
  461. content.Y = -19;
  462. AutoInitShutdownAttribute.RunIteration ();
  463. DriverAssert.AssertDriverContentsWithFrameAre (
  464. """
  465. 9
  466. """,
  467. output
  468. );
  469. content.Y = -20;
  470. AutoInitShutdownAttribute.RunIteration ();
  471. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  472. content.X = -2;
  473. content.Y = 0;
  474. AutoInitShutdownAttribute.RunIteration ();
  475. DriverAssert.AssertDriverContentsWithFrameAre ("", output);
  476. top.Dispose ();
  477. }
  478. [Theory]
  479. [SetupFakeApplication]
  480. [InlineData ("𝔽𝕆𝕆𝔹𝔸R")]
  481. [InlineData ("a𐐀b")]
  482. public void DrawHotString_NonBmp (string expected)
  483. {
  484. var view = new View { Width = 10, Height = 1 };
  485. view.DrawHotString (expected, Attribute.Default, Attribute.Default);
  486. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  487. }
  488. // TODO: The tests below that use Label should use View instead.
  489. [Fact]
  490. [AutoInitShutdown]
  491. public void Non_Bmp_ConsoleWidth_ColumnWidth_Equal_Two ()
  492. {
  493. var us = "\U0001d539";
  494. var r = (Rune)0x1d539;
  495. Assert.Equal ("𝔹", us);
  496. Assert.Equal ("𝔹", r.ToString ());
  497. Assert.Equal (us, r.ToString ());
  498. Assert.Equal (1, us.GetColumns ());
  499. Assert.Equal (1, r.GetColumns ());
  500. var win = new Window { Title = us };
  501. var view = new Label { Text = r.ToString () };
  502. var tf = new TextField { Text = us, Y = 1, Width = 3 };
  503. win.Add (view, tf);
  504. Toplevel top = new ();
  505. top.Add (win);
  506. Application.Begin (top);
  507. Application.Driver!.SetScreenSize (10, 4);
  508. var expected = """
  509. ┌┤𝔹├─────┐
  510. │𝔹 │
  511. │𝔹 │
  512. └────────┘
  513. """;
  514. DriverAssert.AssertDriverContentsWithFrameAre (expected, output);
  515. DriverAssert.AssertDriverContentsAre (expected, output);
  516. top.Dispose ();
  517. // This test has nothing to do with color - removing as it is not relevant and fragile
  518. }
  519. [Fact]
  520. [AutoInitShutdown]
  521. public void Draw_Throws_IndexOutOfRangeException_With_Negative_Bounds ()
  522. {
  523. Toplevel top = new ();
  524. var view = new View { X = -2, Text = "view" };
  525. top.Add (view);
  526. Application.Iteration += OnApplicationOnIteration;
  527. try
  528. {
  529. Application.Run (top);
  530. }
  531. catch (IndexOutOfRangeException ex)
  532. {
  533. // After the fix this exception will not be caught.
  534. Assert.IsType<IndexOutOfRangeException> (ex);
  535. }
  536. finally
  537. {
  538. Application.Iteration -= OnApplicationOnIteration;
  539. }
  540. top.Dispose ();
  541. // Shutdown must be called to safely clean up Application if Init has been called
  542. Application.Shutdown ();
  543. return;
  544. void OnApplicationOnIteration (object? s, IterationEventArgs a)
  545. {
  546. Assert.Equal (-2, view.X);
  547. Application.RequestStop ();
  548. }
  549. }
  550. [Fact]
  551. [AutoInitShutdown]
  552. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  553. {
  554. var label = new Label { Text = "At 0,0" };
  555. var view = new DerivedView
  556. {
  557. X = 2,
  558. Y = 2,
  559. Width = 30,
  560. Height = 2,
  561. Text = "A text with some long width\n and also with two lines."
  562. };
  563. Toplevel top = new ();
  564. top.Add (label, view);
  565. SessionToken sessionToken = Application.Begin (top);
  566. AutoInitShutdownAttribute.RunIteration ();
  567. DriverAssert.AssertDriverContentsWithFrameAre (
  568. @"
  569. At 0,0
  570. A text with some long width
  571. and also with two lines. "
  572. ,
  573. output
  574. );
  575. view.Frame = new (3, 3, 10, 1);
  576. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  577. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  578. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  579. //Application.Refresh();
  580. top.Draw ();
  581. DriverAssert.AssertDriverContentsWithFrameAre (
  582. @"
  583. At 0,0
  584. A text wit",
  585. output
  586. );
  587. Application.End (sessionToken);
  588. top.Dispose ();
  589. }
  590. [Fact]
  591. [AutoInitShutdown]
  592. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  593. {
  594. var label = new Label { Text = "At 0,0" };
  595. var view = new DerivedView
  596. {
  597. X = 2,
  598. Y = 2,
  599. Width = 30,
  600. Height = 2,
  601. Text = "A text with some long width\n and also with two lines."
  602. };
  603. Toplevel top = new ();
  604. top.Add (label, view);
  605. SessionToken sessionToken = Application.Begin (top);
  606. top.Draw ();
  607. DriverAssert.AssertDriverContentsWithFrameAre (
  608. @"
  609. At 0,0
  610. A text with some long width
  611. and also with two lines. "
  612. ,
  613. output
  614. );
  615. view.X = 3;
  616. view.Y = 3;
  617. view.Width = 10;
  618. view.Height = 1;
  619. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  620. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  621. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  622. View.SetClipToScreen ();
  623. top.Draw ();
  624. DriverAssert.AssertDriverContentsWithFrameAre (
  625. @"
  626. At 0,0
  627. A text wit"
  628. ,
  629. output
  630. );
  631. Application.End (sessionToken);
  632. top.Dispose ();
  633. }
  634. [Fact]
  635. [AutoInitShutdown]
  636. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  637. {
  638. var label = new Label { Text = "At 0,0" };
  639. var view = new DerivedView
  640. {
  641. X = 2,
  642. Y = 2,
  643. Width = 30,
  644. Height = 2,
  645. Text = "A text with some long width\n and also with two lines."
  646. };
  647. Toplevel top = new ();
  648. top.Add (label, view);
  649. SessionToken sessionToken = Application.Begin (top);
  650. AutoInitShutdownAttribute.RunIteration ();
  651. DriverAssert.AssertDriverContentsWithFrameAre (
  652. @"
  653. At 0,0
  654. A text with some long width
  655. and also with two lines. "
  656. ,
  657. output
  658. );
  659. view.Frame = new (1, 1, 10, 1);
  660. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  661. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  662. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  663. top.Draw ();
  664. DriverAssert.AssertDriverContentsWithFrameAre (
  665. @"
  666. At 0,0
  667. A text wit"
  668. ,
  669. output
  670. );
  671. Application.End (sessionToken);
  672. top.Dispose ();
  673. }
  674. [Fact]
  675. [AutoInitShutdown]
  676. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  677. {
  678. var label = new Label { Text = "At 0,0" };
  679. var view = new DerivedView
  680. {
  681. X = 2,
  682. Y = 2,
  683. Width = 30,
  684. Height = 2,
  685. Text = "A text with some long width\n and also with two lines."
  686. };
  687. Toplevel top = new ();
  688. top.Add (label, view);
  689. SessionToken sessionToken = Application.Begin (top);
  690. top.Draw ();
  691. DriverAssert.AssertDriverContentsWithFrameAre (
  692. @"
  693. At 0,0
  694. A text with some long width
  695. and also with two lines. "
  696. ,
  697. output
  698. );
  699. view.X = 1;
  700. view.Y = 1;
  701. view.Width = 10;
  702. view.Height = 1;
  703. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  704. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  705. Assert.Equal (new (0, 0, 10, 1), view.NeedsDrawRect);
  706. View.SetClipToScreen ();
  707. top.Draw ();
  708. DriverAssert.AssertDriverContentsWithFrameAre (
  709. @"
  710. At 0,0
  711. A text wit"
  712. ,
  713. output
  714. );
  715. Application.End (sessionToken);
  716. top.Dispose ();
  717. }
  718. public class DerivedView : View
  719. {
  720. public DerivedView () { CanFocus = true; }
  721. public bool IsKeyDown { get; set; }
  722. public bool IsKeyPress { get; set; }
  723. public bool IsKeyUp { get; set; }
  724. public override string Text { get; set; } = null!;
  725. protected override bool OnDrawingContent ()
  726. {
  727. var idx = 0;
  728. // BUGBUG: v2 - this should use Viewport, not Frame
  729. for (var r = 0; r < Frame.Height; r++)
  730. {
  731. for (var c = 0; c < Frame.Width; c++)
  732. {
  733. if (idx < Text.Length)
  734. {
  735. char rune = Text [idx];
  736. if (rune != '\n')
  737. {
  738. AddRune (c, r, (Rune)Text [idx]);
  739. }
  740. idx++;
  741. if (rune == '\n')
  742. {
  743. break;
  744. }
  745. }
  746. }
  747. }
  748. ClearNeedsDraw ();
  749. return true;
  750. }
  751. protected override bool OnKeyDown (Key keyEvent)
  752. {
  753. IsKeyDown = true;
  754. return true;
  755. }
  756. public override bool OnKeyUp (Key keyEvent)
  757. {
  758. IsKeyUp = true;
  759. return true;
  760. }
  761. protected override bool OnKeyDownNotHandled (Key keyEvent)
  762. {
  763. IsKeyPress = true;
  764. return true;
  765. }
  766. }
  767. }