ViewTests.cs 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. using System.ComponentModel;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. public class ViewTests (ITestOutputHelper output)
  6. {
  7. [Fact]
  8. [AutoInitShutdown]
  9. public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  10. {
  11. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  12. view.DrawingContent += (s, e) =>
  13. {
  14. Rectangle savedClip = Application.Driver!.Clip;
  15. Application.Driver!.Clip = new (1, 1, view.Viewport.Width, view.Viewport.Height);
  16. for (var row = 0; row < view.Viewport.Height; row++)
  17. {
  18. Application.Driver?.Move (1, row + 1);
  19. for (var col = 0; col < view.Viewport.Width; col++)
  20. {
  21. Application.Driver?.AddStr ($"{col}");
  22. }
  23. }
  24. Application.Driver!.Clip = savedClip;
  25. e.Cancel = true;
  26. };
  27. var top = new Toplevel ();
  28. top.Add (view);
  29. Application.Begin (top);
  30. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  31. var expected = @"
  32. ┌──────────────────┐
  33. │012345678910111213│
  34. │012345678910111213│
  35. │012345678910111213│
  36. │012345678910111213│
  37. │012345678910111213│
  38. │012345678910111213│
  39. │012345678910111213│
  40. │012345678910111213│
  41. └──────────────────┘
  42. ";
  43. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  44. Assert.Equal (new (0, 0, 20, 10), pos);
  45. view.FillRect (view.Viewport);
  46. expected = @"
  47. ┌──────────────────┐
  48. │ │
  49. │ │
  50. │ │
  51. │ │
  52. │ │
  53. │ │
  54. │ │
  55. │ │
  56. └──────────────────┘
  57. ";
  58. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  59. top.Dispose ();
  60. }
  61. [Fact]
  62. [AutoInitShutdown]
  63. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  64. {
  65. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  66. view.DrawingContent += (s, e) =>
  67. {
  68. Rectangle savedClip = Application.Driver!.Clip;
  69. Application.Driver!.Clip = new (1, 1, view.Viewport.Width, view.Viewport.Height);
  70. for (var row = 0; row < view.Viewport.Height; row++)
  71. {
  72. Application.Driver?.Move (1, row + 1);
  73. for (var col = 0; col < view.Viewport.Width; col++)
  74. {
  75. Application.Driver?.AddStr ($"{col}");
  76. }
  77. }
  78. Application.Driver!.Clip = savedClip;
  79. e.Cancel = true;
  80. };
  81. var top = new Toplevel ();
  82. top.Add (view);
  83. Application.Begin (top);
  84. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  85. var expected = @"
  86. ┌──────────────────┐
  87. │012345678910111213│
  88. │012345678910111213│
  89. │012345678910111213│
  90. │012345678910111213│
  91. │012345678910111213│
  92. │012345678910111213│
  93. │012345678910111213│
  94. │012345678910111213│
  95. └──────────────────┘
  96. ";
  97. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  98. Assert.Equal (new (0, 0, 20, 10), pos);
  99. view.FillRect (view.Viewport);
  100. expected = @"
  101. ┌──────────────────┐
  102. │ │
  103. │ │
  104. │ │
  105. │ │
  106. │ │
  107. │ │
  108. │ │
  109. │ │
  110. └──────────────────┘
  111. ";
  112. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  113. top.Dispose ();
  114. }
  115. [Theory]
  116. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  117. [InlineData (true)]
  118. [InlineData (false)]
  119. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  120. {
  121. var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
  122. string text = new ('c', 100);
  123. View v = label
  124. // Label has Width/Height == AutoSize, so Frame.Size will be (100, 1)
  125. ? new Label { Text = text }
  126. // TextView has Width/Height == (Dim.Fill, 1), so Frame.Size will be 20 (width of root), 1
  127. : new TextView { Width = Dim.Fill (), Height = 1, Text = text };
  128. root.Add (v);
  129. var top = new Toplevel ();
  130. top.Add (root);
  131. RunState runState = Application.Begin (top);
  132. Application.RunIteration (ref runState);
  133. if (label)
  134. {
  135. Assert.False (v.CanFocus);
  136. Assert.Equal (new (0, 0, text.Length, 1), v.Frame);
  137. }
  138. else
  139. {
  140. Assert.True (v.CanFocus);
  141. Assert.Equal (new (0, 0, 20, 1), v.Frame);
  142. }
  143. TestHelpers.AssertDriverContentsWithFrameAre (
  144. @"
  145. cccccccccccccccccccc",
  146. output
  147. );
  148. Attribute [] attributes =
  149. {
  150. Colors.ColorSchemes ["TopLevel"].Normal,
  151. Colors.ColorSchemes ["Base"].Normal,
  152. Colors.ColorSchemes ["Base"].Focus
  153. };
  154. if (label)
  155. {
  156. TestHelpers.AssertDriverAttributesAre (
  157. @"
  158. 111111111111111111110
  159. 111111111111111111110",
  160. output,
  161. Application.Driver,
  162. attributes
  163. );
  164. }
  165. else
  166. {
  167. TestHelpers.AssertDriverAttributesAre (
  168. @"
  169. 222222222222222222220
  170. 111111111111111111110",
  171. output,
  172. Application.Driver,
  173. attributes
  174. );
  175. }
  176. if (label)
  177. {
  178. root.CanFocus = true;
  179. v.CanFocus = true;
  180. Assert.True (v.HasFocus);
  181. v.SetFocus ();
  182. Assert.True (v.HasFocus);
  183. Application.Refresh ();
  184. TestHelpers.AssertDriverAttributesAre (
  185. @"
  186. 222222222222222222220
  187. 111111111111111111110",
  188. output,
  189. Application.Driver,
  190. attributes
  191. );
  192. }
  193. Application.End (runState);
  194. top.Dispose ();
  195. }
  196. [Fact]
  197. [AutoInitShutdown]
  198. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  199. {
  200. var label = new Label { Text = "At 0,0" };
  201. var view = new DerivedView
  202. {
  203. X = 2,
  204. Y = 2,
  205. Width = 30,
  206. Height = 2,
  207. Text = "A text with some long width\n and also with two lines."
  208. };
  209. Toplevel top = new ();
  210. top.Add (label, view);
  211. RunState runState = Application.Begin (top);
  212. Application.RunIteration (ref runState);
  213. TestHelpers.AssertDriverContentsWithFrameAre (
  214. @"
  215. At 0,0
  216. A text with some long width
  217. and also with two lines. ",
  218. output
  219. );
  220. view.Frame = new (3, 3, 10, 1);
  221. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  222. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  223. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  224. //Application.Refresh();
  225. top.Draw ();
  226. TestHelpers.AssertDriverContentsWithFrameAre (
  227. @"
  228. At 0,0
  229. A text wit",
  230. output
  231. );
  232. Application.End (runState);
  233. top.Dispose ();
  234. }
  235. [Fact]
  236. [AutoInitShutdown]
  237. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  238. {
  239. var label = new Label { Text = "At 0,0" };
  240. var view = new DerivedView
  241. {
  242. X = 2,
  243. Y = 2,
  244. Width = 30,
  245. Height = 2,
  246. Text = "A text with some long width\n and also with two lines."
  247. };
  248. Toplevel top = new ();
  249. top.Add (label, view);
  250. RunState runState = Application.Begin (top);
  251. top.Draw ();
  252. TestHelpers.AssertDriverContentsWithFrameAre (
  253. @"
  254. At 0,0
  255. A text with some long width
  256. and also with two lines. ",
  257. output
  258. );
  259. view.X = 3;
  260. view.Y = 3;
  261. view.Width = 10;
  262. view.Height = 1;
  263. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  264. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  265. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  266. top.Draw ();
  267. TestHelpers.AssertDriverContentsWithFrameAre (
  268. @"
  269. At 0,0
  270. A text wit",
  271. output
  272. );
  273. Application.End (runState);
  274. top.Dispose ();
  275. }
  276. [Fact]
  277. [AutoInitShutdown]
  278. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  279. {
  280. var label = new Label { Text = "At 0,0" };
  281. var view = new DerivedView
  282. {
  283. X = 2,
  284. Y = 2,
  285. Width = 30,
  286. Height = 2,
  287. Text = "A text with some long width\n and also with two lines."
  288. };
  289. Toplevel top = new ();
  290. top.Add (label, view);
  291. RunState runState = Application.Begin (top);
  292. Application.RunIteration (ref runState);
  293. TestHelpers.AssertDriverContentsWithFrameAre (
  294. @"
  295. At 0,0
  296. A text with some long width
  297. and also with two lines. ",
  298. output
  299. );
  300. view.Frame = new (1, 1, 10, 1);
  301. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  302. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  303. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  304. top.Draw ();
  305. TestHelpers.AssertDriverContentsWithFrameAre (
  306. @"
  307. At 0,0
  308. A text wit",
  309. output
  310. );
  311. Application.End (runState);
  312. top.Dispose ();
  313. }
  314. [Fact]
  315. [AutoInitShutdown]
  316. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  317. {
  318. var label = new Label { Text = "At 0,0" };
  319. var view = new DerivedView
  320. {
  321. X = 2,
  322. Y = 2,
  323. Width = 30,
  324. Height = 2,
  325. Text = "A text with some long width\n and also with two lines."
  326. };
  327. Toplevel top = new ();
  328. top.Add (label, view);
  329. RunState runState = Application.Begin (top);
  330. top.Draw ();
  331. TestHelpers.AssertDriverContentsWithFrameAre (
  332. @"
  333. At 0,0
  334. A text with some long width
  335. and also with two lines. ",
  336. output
  337. );
  338. view.X = 1;
  339. view.Y = 1;
  340. view.Width = 10;
  341. view.Height = 1;
  342. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  343. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  344. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  345. top.Draw ();
  346. TestHelpers.AssertDriverContentsWithFrameAre (
  347. @"
  348. At 0,0
  349. A text wit",
  350. output
  351. );
  352. Application.End (runState);
  353. top.Dispose ();
  354. }
  355. [Fact]
  356. [TestRespondersDisposed]
  357. public void Dispose_View ()
  358. {
  359. var view = new View ();
  360. Assert.NotNull (view.Margin);
  361. Assert.NotNull (view.Border);
  362. Assert.NotNull (view.Padding);
  363. #if DEBUG_IDISPOSABLE
  364. Assert.Equal (4, Responder.Instances.Count);
  365. #endif
  366. view.Dispose ();
  367. Assert.Null (view.Margin);
  368. Assert.Null (view.Border);
  369. Assert.Null (view.Padding);
  370. }
  371. [Fact]
  372. [AutoInitShutdown]
  373. public void DrawContentComplete_Event_Is_Always_Called ()
  374. {
  375. var viewCalled = false;
  376. var tvCalled = false;
  377. var view = new View { Width = 10, Height = 10, Text = "View" };
  378. view.DrawComplete += (s, e) => viewCalled = true;
  379. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  380. tv.DrawComplete += (s, e) => tvCalled = true;
  381. var top = new Toplevel ();
  382. top.Add (view, tv);
  383. RunState runState = Application.Begin (top);
  384. Application.RunIteration (ref runState);
  385. Assert.True (viewCalled);
  386. Assert.True (tvCalled);
  387. top.Dispose ();
  388. }
  389. [Fact]
  390. [AutoInitShutdown]
  391. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  392. {
  393. var frame = new FrameView ();
  394. var label = new Label
  395. {
  396. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  397. };
  398. var view = new View
  399. {
  400. X = 0, // don't overcomplicate unit tests
  401. Y = 1,
  402. Height = Dim.Auto (DimAutoStyle.Text),
  403. Width = Dim.Auto (DimAutoStyle.Text),
  404. Text = "Press me!"
  405. };
  406. frame.Add (label, view);
  407. frame.X = Pos.Center ();
  408. frame.Y = Pos.Center ();
  409. frame.Width = 40;
  410. frame.Height = 8;
  411. Toplevel top = new ();
  412. top.Add (frame);
  413. RunState runState = Application.Begin (top);
  414. top.SubviewsLaidOut += (s, e) => { Assert.Equal (new (0, 0, 80, 25), top._needsDisplayRect); };
  415. frame.SubviewsLaidOut += (s, e) => { Assert.Equal (new (0, 0, 40, 8), frame._needsDisplayRect); };
  416. label.SubviewsLaidOut += (s, e) => { Assert.Equal (new (0, 0, 38, 1), label._needsDisplayRect); };
  417. view.SubviewsLaidOut += (s, e) => { Assert.Equal (new (0, 0, 13, 1), view._needsDisplayRect); };
  418. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  419. Assert.Equal (new (20, 8, 40, 8), frame.Frame);
  420. Assert.Equal (
  421. new (20, 8, 60, 16),
  422. new Rectangle (
  423. frame.Frame.Left,
  424. frame.Frame.Top,
  425. frame.Frame.Right,
  426. frame.Frame.Bottom
  427. )
  428. );
  429. Assert.Equal (new (0, 0, 30, 1), label.Frame);
  430. Assert.Equal (new (0, 1, 9, 1), view.Frame); // this proves frame was set
  431. Application.End (runState);
  432. top.Dispose ();
  433. }
  434. [Fact]
  435. public void GetHotNormalColor_ColorScheme ()
  436. {
  437. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  438. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  439. view.Enabled = false;
  440. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  441. view.Dispose ();
  442. }
  443. [Fact]
  444. public void GetNormalColor_ColorScheme ()
  445. {
  446. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  447. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  448. view.Enabled = false;
  449. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  450. view.Dispose ();
  451. }
  452. [Fact]
  453. [AutoInitShutdown]
  454. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  455. {
  456. var label = new Label { Text = "At 0,0" };
  457. var view = new DerivedView
  458. {
  459. X = 2,
  460. Y = 2,
  461. Width = 30,
  462. Height = 2,
  463. Text = "A text with some long width\n and also with two lines."
  464. };
  465. Toplevel top = new ();
  466. top.Add (label, view);
  467. RunState runState = Application.Begin (top);
  468. Application.RunIteration (ref runState);
  469. TestHelpers.AssertDriverContentsWithFrameAre (
  470. @"
  471. At 0,0
  472. A text with some long width
  473. and also with two lines. ",
  474. output
  475. );
  476. view.Frame = new (3, 3, 10, 1);
  477. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  478. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  479. view.Draw ();
  480. TestHelpers.AssertDriverContentsWithFrameAre (
  481. @"
  482. At 0,0
  483. A text with some long width
  484. A text witith two lines. ",
  485. output
  486. );
  487. Application.End (runState);
  488. top.Dispose ();
  489. }
  490. [Fact]
  491. [AutoInitShutdown]
  492. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  493. {
  494. var label = new Label { Text = "At 0,0" };
  495. var view = new DerivedView
  496. {
  497. X = 2,
  498. Y = 2,
  499. Width = 30,
  500. Height = 2,
  501. Text = "A text with some long width\n and also with two lines."
  502. };
  503. Toplevel top = new ();
  504. top.Add (label, view);
  505. RunState runState = Application.Begin (top);
  506. Application.RunIteration (ref runState);
  507. TestHelpers.AssertDriverContentsWithFrameAre (
  508. @"
  509. At 0,0
  510. A text with some long width
  511. and also with two lines. ",
  512. output
  513. );
  514. view.X = 3;
  515. view.Y = 3;
  516. view.Width = 10;
  517. view.Height = 1;
  518. Assert.Equal (new (3, 3, 10, 1), view.Frame);
  519. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  520. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  521. view.Draw ();
  522. TestHelpers.AssertDriverContentsWithFrameAre (
  523. @"
  524. At 0,0
  525. A text with some long width
  526. A text witith two lines. ",
  527. output
  528. );
  529. Application.End (runState);
  530. top.Dispose ();
  531. }
  532. [Fact]
  533. [AutoInitShutdown]
  534. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  535. {
  536. var label = new Label { Text = "At 0,0" };
  537. var view = new DerivedView
  538. {
  539. X = 2,
  540. Y = 2,
  541. Width = 30,
  542. Height = 2,
  543. Text = "A text with some long width\n and also with two lines."
  544. };
  545. Toplevel top = new ();
  546. top.Add (label, view);
  547. RunState runState = Application.Begin (top);
  548. Application.RunIteration (ref runState);
  549. TestHelpers.AssertDriverContentsWithFrameAre (
  550. @"
  551. At 0,0
  552. A text with some long width
  553. and also with two lines. ",
  554. output
  555. );
  556. view.Frame = new (1, 1, 10, 1);
  557. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  558. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  559. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  560. view.Draw ();
  561. TestHelpers.AssertDriverContentsWithFrameAre (
  562. @"
  563. At 0,0
  564. A text wit
  565. A text with some long width
  566. and also with two lines. ",
  567. output
  568. );
  569. Application.End (runState);
  570. top.Dispose ();
  571. }
  572. [Fact]
  573. [AutoInitShutdown]
  574. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  575. {
  576. var label = new Label { Text = "At 0,0" };
  577. var view = new DerivedView
  578. {
  579. X = 2,
  580. Y = 2,
  581. Width = 30,
  582. Height = 2,
  583. Text = "A text with some long width\n and also with two lines."
  584. };
  585. Toplevel top = new ();
  586. top.Add (label, view);
  587. RunState runState = Application.Begin (top);
  588. Application.RunIteration (ref runState);
  589. TestHelpers.AssertDriverContentsWithFrameAre (
  590. @"
  591. At 0,0
  592. A text with some long width
  593. and also with two lines. ",
  594. output
  595. );
  596. view.X = 1;
  597. view.Y = 1;
  598. view.Width = 10;
  599. view.Height = 1;
  600. Assert.Equal (new (1, 1, 10, 1), view.Frame);
  601. Assert.Equal (new (0, 0, 10, 1), view.Viewport);
  602. Assert.Equal (new (0, 0, 10, 1), view._needsDisplayRect);
  603. view.Draw ();
  604. TestHelpers.AssertDriverContentsWithFrameAre (
  605. @"
  606. At 0,0
  607. A text wit
  608. A text with some long width
  609. and also with two lines. ",
  610. output
  611. );
  612. Application.End (runState);
  613. top.Dispose ();
  614. }
  615. [Fact]
  616. public void Internal_Tests ()
  617. {
  618. var rect = new Rectangle (1, 1, 10, 1);
  619. var view = new View { Frame = rect };
  620. }
  621. [Fact]
  622. [SetupFakeDriver]
  623. public void SetText_RendersCorrectly ()
  624. {
  625. View view;
  626. var text = "test";
  627. view = new Label { Text = text };
  628. view.BeginInit ();
  629. view.EndInit ();
  630. view.Draw ();
  631. TestHelpers.AssertDriverContentsWithFrameAre (text, output);
  632. }
  633. [Fact]
  634. [TestRespondersDisposed]
  635. public void New_Initializes ()
  636. {
  637. // Parameterless
  638. var r = new View ();
  639. Assert.NotNull (r);
  640. Assert.True (r.Enabled);
  641. Assert.True (r.Visible);
  642. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  643. Assert.False (r.CanFocus);
  644. Assert.False (r.HasFocus);
  645. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  646. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  647. Assert.Null (r.Focused);
  648. Assert.Null (r.ColorScheme);
  649. Assert.Equal (0, r.Width);
  650. Assert.Equal (0, r.Height);
  651. Assert.Equal (0, r.X);
  652. Assert.Equal (0, r.Y);
  653. Assert.False (r.IsCurrentTop);
  654. Assert.Empty (r.Id);
  655. Assert.Empty (r.Subviews);
  656. Assert.False (r.WantContinuousButtonPressed);
  657. Assert.False (r.WantMousePositionReports);
  658. Assert.Null (r.SuperView);
  659. Assert.Null (r.MostFocused);
  660. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  661. r.Dispose ();
  662. // Empty Rect
  663. r = new () { Frame = Rectangle.Empty };
  664. Assert.NotNull (r);
  665. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  666. Assert.False (r.CanFocus);
  667. Assert.False (r.HasFocus);
  668. Assert.Equal (new (0, 0, 0, 0), r.Viewport);
  669. Assert.Equal (new (0, 0, 0, 0), r.Frame);
  670. Assert.Null (r.Focused);
  671. Assert.Null (r.ColorScheme);
  672. Assert.Equal (0, r.Width);
  673. Assert.Equal (0, r.Height);
  674. Assert.Equal (0, r.X);
  675. Assert.Equal (0, r.Y);
  676. Assert.False (r.IsCurrentTop);
  677. Assert.Empty (r.Id);
  678. Assert.Empty (r.Subviews);
  679. Assert.False (r.WantContinuousButtonPressed);
  680. Assert.False (r.WantMousePositionReports);
  681. Assert.Null (r.SuperView);
  682. Assert.Null (r.MostFocused);
  683. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  684. r.Dispose ();
  685. // Rect with values
  686. r = new () { Frame = new (1, 2, 3, 4) };
  687. Assert.NotNull (r);
  688. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  689. Assert.False (r.CanFocus);
  690. Assert.False (r.HasFocus);
  691. Assert.Equal (new (0, 0, 3, 4), r.Viewport);
  692. Assert.Equal (new (1, 2, 3, 4), r.Frame);
  693. Assert.Null (r.Focused);
  694. Assert.Null (r.ColorScheme);
  695. Assert.Equal (3, r.Width);
  696. Assert.Equal (4, r.Height);
  697. Assert.Equal (1, r.X);
  698. Assert.Equal (2, r.Y);
  699. Assert.False (r.IsCurrentTop);
  700. Assert.Empty (r.Id);
  701. Assert.Empty (r.Subviews);
  702. Assert.False (r.WantContinuousButtonPressed);
  703. Assert.False (r.WantMousePositionReports);
  704. Assert.Null (r.SuperView);
  705. Assert.Null (r.MostFocused);
  706. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  707. r.Dispose ();
  708. // Initializes a view with a vertical direction
  709. r = new ()
  710. {
  711. Text = "Vertical View",
  712. TextDirection = TextDirection.TopBottom_LeftRight,
  713. Width = Dim.Auto (),
  714. Height = Dim.Auto ()
  715. };
  716. r.TextFormatter.WordWrap = false;
  717. Assert.NotNull (r);
  718. r.BeginInit ();
  719. r.EndInit ();
  720. Assert.False (r.CanFocus);
  721. Assert.False (r.HasFocus);
  722. Assert.Equal (new (0, 0, 1, 13), r.Viewport);
  723. Assert.Equal (new (0, 0, 1, 13), r.Frame);
  724. Assert.Null (r.Focused);
  725. Assert.Null (r.ColorScheme);
  726. Assert.False (r.IsCurrentTop);
  727. #if DEBUG
  728. Assert.Equal ("Vertical View", r.Id);
  729. #else
  730. Assert.Equal (string.Empty, r.Id);
  731. #endif
  732. Assert.Empty (r.Subviews);
  733. Assert.False (r.WantContinuousButtonPressed);
  734. Assert.False (r.WantMousePositionReports);
  735. Assert.Null (r.SuperView);
  736. Assert.Null (r.MostFocused);
  737. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  738. r.Dispose ();
  739. }
  740. [Fact]
  741. [TestRespondersDisposed]
  742. public void New_Methods_Return_False ()
  743. {
  744. var r = new View ();
  745. Assert.False (r.NewKeyDownEvent (Key.Empty));
  746. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  747. Assert.False (r.NewKeyUpEvent (Key.Empty));
  748. Assert.False (r.NewMouseEvent (new () { Flags = MouseFlags.AllEvents }));
  749. r.Dispose ();
  750. // TODO: Add more
  751. }
  752. [Fact]
  753. [AutoInitShutdown]
  754. public void Test_Nested_Views_With_Height_Equal_To_One ()
  755. {
  756. var v = new View { Width = 11, Height = 3, ColorScheme = new () };
  757. var top = new View { Width = Dim.Fill (), Height = 1 };
  758. var bottom = new View { Width = Dim.Fill (), Height = 1, Y = 2 };
  759. top.Add (new Label { Text = "111" });
  760. v.Add (top);
  761. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  762. bottom.Add (new Label { Text = "222" });
  763. v.Add (bottom);
  764. v.BeginInit ();
  765. v.EndInit ();
  766. v.LayoutSubviews ();
  767. v.Draw ();
  768. var looksLike =
  769. @"
  770. 111
  771. ───────────
  772. 222";
  773. TestHelpers.AssertDriverContentsAre (looksLike, output);
  774. v.Dispose ();
  775. top.Dispose ();
  776. bottom.Dispose ();
  777. }
  778. [Fact]
  779. [TestRespondersDisposed]
  780. public void View_With_No_Difference_Between_An_Object_Initializer_Compute_And_A_Absolute ()
  781. {
  782. // Object Initializer Computed
  783. var view = new View { X = 1, Y = 2, Width = 3, Height = 4 };
  784. // Object Initializer Absolute
  785. var super = new View { Frame = new (0, 0, 10, 10) };
  786. super.Add (view);
  787. super.BeginInit ();
  788. super.EndInit ();
  789. super.LayoutSubviews ();
  790. Assert.Equal (1, view.X);
  791. Assert.Equal (2, view.Y);
  792. Assert.Equal (3, view.Width);
  793. Assert.Equal (4, view.Height);
  794. Assert.False (view.Frame.IsEmpty);
  795. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  796. Assert.False (view.Viewport.IsEmpty);
  797. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  798. view.LayoutSubviews ();
  799. Assert.Equal (1, view.X);
  800. Assert.Equal (2, view.Y);
  801. Assert.Equal (3, view.Width);
  802. Assert.Equal (4, view.Height);
  803. Assert.False (view.Frame.IsEmpty);
  804. Assert.False (view.Viewport.IsEmpty);
  805. super.Dispose ();
  806. #if DEBUG_IDISPOSABLE
  807. Assert.Empty (Responder.Instances);
  808. #endif
  809. // Default Constructor
  810. view = new ();
  811. Assert.Equal (0, view.X);
  812. Assert.Equal (0, view.Y);
  813. Assert.Equal (0, view.Width);
  814. Assert.Equal (0, view.Height);
  815. Assert.True (view.Frame.IsEmpty);
  816. Assert.True (view.Viewport.IsEmpty);
  817. view.Dispose ();
  818. // Object Initializer
  819. view = new () { X = 1, Y = 2, Text = "" };
  820. Assert.Equal (1, view.X);
  821. Assert.Equal (2, view.Y);
  822. Assert.Equal (0, view.Width);
  823. Assert.Equal (0, view.Height);
  824. Assert.False (view.Frame.IsEmpty);
  825. Assert.True (view.Viewport.IsEmpty);
  826. view.Dispose ();
  827. // Default Constructor and post assignment equivalent to Object Initializer
  828. view = new ();
  829. view.X = 1;
  830. view.Y = 2;
  831. view.Width = 3;
  832. view.Height = 4;
  833. super = new () { Frame = new (0, 0, 10, 10) };
  834. super.Add (view);
  835. super.BeginInit ();
  836. super.EndInit ();
  837. super.LayoutSubviews ();
  838. Assert.Equal (1, view.X);
  839. Assert.Equal (2, view.Y);
  840. Assert.Equal (3, view.Width);
  841. Assert.Equal (4, view.Height);
  842. Assert.False (view.Frame.IsEmpty);
  843. Assert.Equal (new (1, 2, 3, 4), view.Frame);
  844. Assert.False (view.Viewport.IsEmpty);
  845. Assert.Equal (new (0, 0, 3, 4), view.Viewport);
  846. super.Dispose ();
  847. }
  848. [Fact]
  849. [AutoInitShutdown]
  850. public void Visible_Clear_The_View_Output ()
  851. {
  852. var view = new View { Text = "Testing visibility." }; // use View, not Label to avoid AutoSize == true
  853. Assert.Equal (0, view.Frame.Width);
  854. Assert.Equal (0, view.Height);
  855. var win = new Window ();
  856. win.Add (view);
  857. Toplevel top = new ();
  858. top.Add (win);
  859. RunState rs = Application.Begin (top);
  860. view.Width = Dim.Auto ();
  861. view.Height = Dim.Auto ();
  862. Application.RunIteration (ref rs);
  863. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  864. Assert.True (view.Visible);
  865. ((FakeDriver)Application.Driver!).SetBufferSize (30, 5);
  866. TestHelpers.AssertDriverContentsWithFrameAre (
  867. @"
  868. ┌────────────────────────────┐
  869. │Testing visibility. │
  870. │ │
  871. │ │
  872. └────────────────────────────┘
  873. ",
  874. output
  875. );
  876. view.Visible = false;
  877. var firstIteration = false;
  878. Application.RunIteration (ref rs, firstIteration);
  879. TestHelpers.AssertDriverContentsWithFrameAre (
  880. @"
  881. ┌────────────────────────────┐
  882. │ │
  883. │ │
  884. │ │
  885. └────────────────────────────┘
  886. ",
  887. output
  888. );
  889. Application.End (rs);
  890. top.Dispose ();
  891. }
  892. [Fact]
  893. [AutoInitShutdown]
  894. public void Visible_Sets_Also_Sets_Subviews ()
  895. {
  896. var button = new Button { Text = "Click Me" };
  897. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  898. win.Add (button);
  899. Toplevel top = new ();
  900. top.Add (win);
  901. var iterations = 0;
  902. Application.Iteration += (s, a) =>
  903. {
  904. iterations++;
  905. Assert.True (button.Visible);
  906. Assert.True (button.CanFocus);
  907. Assert.True (button.HasFocus);
  908. Assert.True (win.Visible);
  909. Assert.True (win.CanFocus);
  910. Assert.True (win.HasFocus);
  911. win.Visible = false;
  912. Assert.True (button.Visible);
  913. Assert.True (button.CanFocus);
  914. Assert.False (button.HasFocus);
  915. Assert.False (win.Visible);
  916. Assert.True (win.CanFocus);
  917. Assert.False (win.HasFocus);
  918. button.SetFocus ();
  919. Assert.False (button.HasFocus);
  920. Assert.False (win.HasFocus);
  921. win.SetFocus ();
  922. Assert.False (button.HasFocus);
  923. Assert.False (win.HasFocus);
  924. win.Visible = true;
  925. Assert.True (button.HasFocus);
  926. Assert.True (win.HasFocus);
  927. Application.RequestStop ();
  928. };
  929. Application.Run (top);
  930. top.Dispose ();
  931. Assert.Equal (1, iterations);
  932. }
  933. public class DerivedView : View
  934. {
  935. public DerivedView () { CanFocus = true; }
  936. public bool IsKeyDown { get; set; }
  937. public bool IsKeyPress { get; set; }
  938. public bool IsKeyUp { get; set; }
  939. public override string Text { get; set; }
  940. protected override bool OnDrawingContent (Rectangle viewport)
  941. {
  942. var idx = 0;
  943. // BUGBUG: v2 - this should use Viewport, not Frame
  944. for (var r = 0; r < Frame.Height; r++)
  945. {
  946. for (var c = 0; c < Frame.Width; c++)
  947. {
  948. if (idx < Text.Length)
  949. {
  950. char rune = Text [idx];
  951. if (rune != '\n')
  952. {
  953. AddRune (c, r, (Rune)Text [idx]);
  954. }
  955. idx++;
  956. if (rune == '\n')
  957. {
  958. break;
  959. }
  960. }
  961. }
  962. }
  963. ClearNeedsDisplay ();
  964. return true;
  965. }
  966. protected override bool OnKeyDown (Key keyEvent)
  967. {
  968. IsKeyDown = true;
  969. return true;
  970. }
  971. public override bool OnKeyUp (Key keyEvent)
  972. {
  973. IsKeyUp = true;
  974. return true;
  975. }
  976. protected override bool OnKeyDownNotHandled (Key keyEvent)
  977. {
  978. IsKeyPress = true;
  979. return true;
  980. }
  981. }
  982. }