ViewTests.cs 39 KB

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