2
0

ViewTests.cs 37 KB

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