ViewTests.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. using System.ComponentModel;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.ViewTests;
  5. public class ViewTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public ViewTests (ITestOutputHelper output) { _output = output; }
  9. [Fact]
  10. [AutoInitShutdown]
  11. public void Clear_Viewport_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  12. {
  13. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  14. view.DrawContent += (s, e) =>
  15. {
  16. Rectangle savedClip = Application.Driver.Clip;
  17. Application.Driver.Clip = new Rectangle (1, 1, view.Viewport.Width, view.Viewport.Height);
  18. for (var row = 0; row < view.Viewport.Height; row++)
  19. {
  20. Application.Driver.Move (1, row + 1);
  21. for (var col = 0; col < view.Viewport.Width; col++)
  22. {
  23. Application.Driver.AddStr ($"{col}");
  24. }
  25. }
  26. Application.Driver.Clip = savedClip;
  27. e.Cancel = true;
  28. };
  29. var top = new Toplevel ();
  30. top.Add (view);
  31. Application.Begin (top);
  32. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  33. var expected = @"
  34. ┌──────────────────┐
  35. │012345678910111213│
  36. │012345678910111213│
  37. │012345678910111213│
  38. │012345678910111213│
  39. │012345678910111213│
  40. │012345678910111213│
  41. │012345678910111213│
  42. │012345678910111213│
  43. └──────────────────┘
  44. ";
  45. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  46. Assert.Equal (new Rectangle (0, 0, 20, 10), pos);
  47. view.Clear (view.Viewport);
  48. expected = @"
  49. ┌──────────────────┐
  50. │ │
  51. │ │
  52. │ │
  53. │ │
  54. │ │
  55. │ │
  56. │ │
  57. │ │
  58. └──────────────────┘
  59. ";
  60. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  61. top.Dispose ();
  62. }
  63. [Fact]
  64. [AutoInitShutdown]
  65. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  66. {
  67. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  68. view.DrawContent += (s, e) =>
  69. {
  70. Rectangle savedClip = Application.Driver.Clip;
  71. Application.Driver.Clip = new Rectangle (1, 1, view.Viewport.Width, view.Viewport.Height);
  72. for (var row = 0; row < view.Viewport.Height; row++)
  73. {
  74. Application.Driver.Move (1, row + 1);
  75. for (var col = 0; col < view.Viewport.Width; col++)
  76. {
  77. Application.Driver.AddStr ($"{col}");
  78. }
  79. }
  80. Application.Driver.Clip = savedClip;
  81. e.Cancel = true;
  82. };
  83. var top = new Toplevel ();
  84. top.Add (view);
  85. Application.Begin (top);
  86. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  87. var expected = @"
  88. ┌──────────────────┐
  89. │012345678910111213│
  90. │012345678910111213│
  91. │012345678910111213│
  92. │012345678910111213│
  93. │012345678910111213│
  94. │012345678910111213│
  95. │012345678910111213│
  96. │012345678910111213│
  97. └──────────────────┘
  98. ";
  99. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  100. Assert.Equal (new Rectangle (0, 0, 20, 10), pos);
  101. view.Clear (view.Viewport);
  102. expected = @"
  103. ┌──────────────────┐
  104. │ │
  105. │ │
  106. │ │
  107. │ │
  108. │ │
  109. │ │
  110. │ │
  111. │ │
  112. └──────────────────┘
  113. ";
  114. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  115. top.Dispose ();
  116. }
  117. [Theory]
  118. [AutoInitShutdown]
  119. [InlineData (true)]
  120. [InlineData (false)]
  121. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  122. {
  123. var root = new View { Width = 20, Height = 10, ColorScheme = Colors.ColorSchemes ["Base"] };
  124. View v = label
  125. ? new Label { Text = new string ('c', 100) }
  126. : new TextView { Height = 1, Text = new string ('c', 100), Width = Dim.Fill () };
  127. root.Add (v);
  128. var top = new Toplevel ();
  129. top.Add (root);
  130. RunState runState = Application.Begin (top);
  131. if (label)
  132. {
  133. Assert.True (v.AutoSize);
  134. Assert.False (v.CanFocus);
  135. Assert.Equal (new Rectangle (0, 0, 100, 1), v.Frame);
  136. }
  137. else
  138. {
  139. Assert.False (v.AutoSize);
  140. Assert.True (v.CanFocus);
  141. Assert.Equal (new Rectangle (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. Application.Driver,
  161. attributes
  162. );
  163. }
  164. else
  165. {
  166. TestHelpers.AssertDriverAttributesAre (
  167. @"
  168. 222222222222222222220
  169. 111111111111111111110",
  170. Application.Driver,
  171. attributes
  172. );
  173. }
  174. if (label)
  175. {
  176. root.CanFocus = true;
  177. v.CanFocus = true;
  178. Assert.False (v.HasFocus);
  179. v.SetFocus ();
  180. Assert.True (v.HasFocus);
  181. Application.Refresh ();
  182. TestHelpers.AssertDriverAttributesAre (
  183. @"
  184. 222222222222222222220
  185. 111111111111111111110",
  186. Application.Driver,
  187. attributes
  188. );
  189. }
  190. Application.End (runState);
  191. }
  192. [Fact]
  193. [AutoInitShutdown]
  194. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  195. {
  196. var label = new Label { Text = "At 0,0" };
  197. var view = new DerivedView
  198. {
  199. X = 2,
  200. Y = 2,
  201. Width = 30,
  202. Height = 2,
  203. Text = "A text with some long width\n and also with two lines."
  204. };
  205. Toplevel top = new ();
  206. top.Add (label, view);
  207. RunState runState = Application.Begin (top);
  208. TestHelpers.AssertDriverContentsWithFrameAre (
  209. @"
  210. At 0,0
  211. A text with some long width
  212. and also with two lines. ",
  213. _output
  214. );
  215. view.Frame = new Rectangle (3, 3, 10, 1);
  216. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  217. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  218. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  219. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  220. top.Draw ();
  221. TestHelpers.AssertDriverContentsWithFrameAre (
  222. @"
  223. At 0,0
  224. A text wit",
  225. _output
  226. );
  227. Application.End (runState);
  228. }
  229. [Fact]
  230. [AutoInitShutdown]
  231. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  232. {
  233. var label = new Label { Text = "At 0,0" };
  234. var view = new DerivedView
  235. {
  236. X = 2,
  237. Y = 2,
  238. Width = 30,
  239. Height = 2,
  240. Text = "A text with some long width\n and also with two lines."
  241. };
  242. Toplevel top = new ();
  243. top.Add (label, view);
  244. RunState runState = Application.Begin (top);
  245. top.Draw ();
  246. TestHelpers.AssertDriverContentsWithFrameAre (
  247. @"
  248. At 0,0
  249. A text with some long width
  250. and also with two lines. ",
  251. _output
  252. );
  253. view.X = 3;
  254. view.Y = 3;
  255. view.Width = 10;
  256. view.Height = 1;
  257. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  258. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  259. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  260. top.Draw ();
  261. TestHelpers.AssertDriverContentsWithFrameAre (
  262. @"
  263. At 0,0
  264. A text wit",
  265. _output
  266. );
  267. Application.End (runState);
  268. }
  269. [Fact]
  270. [AutoInitShutdown]
  271. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  272. {
  273. var label = new Label { Text = "At 0,0" };
  274. var view = new DerivedView
  275. {
  276. X = 2,
  277. Y = 2,
  278. Width = 30,
  279. Height = 2,
  280. Text = "A text with some long width\n and also with two lines."
  281. };
  282. Toplevel top = new ();
  283. top.Add (label, view);
  284. RunState runState = Application.Begin (top);
  285. top.Draw ();
  286. TestHelpers.AssertDriverContentsWithFrameAre (
  287. @"
  288. At 0,0
  289. A text with some long width
  290. and also with two lines. ",
  291. _output
  292. );
  293. view.Frame = new Rectangle (1, 1, 10, 1);
  294. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  295. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  296. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  297. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  298. top.Draw ();
  299. TestHelpers.AssertDriverContentsWithFrameAre (
  300. @"
  301. At 0,0
  302. A text wit",
  303. _output
  304. );
  305. Application.End (runState);
  306. }
  307. [Fact]
  308. [AutoInitShutdown]
  309. public void Correct_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  310. {
  311. var label = new Label { Text = "At 0,0" };
  312. var view = new DerivedView
  313. {
  314. X = 2,
  315. Y = 2,
  316. Width = 30,
  317. Height = 2,
  318. Text = "A text with some long width\n and also with two lines."
  319. };
  320. Toplevel top = new ();
  321. top.Add (label, view);
  322. RunState runState = Application.Begin (top);
  323. top.Draw ();
  324. TestHelpers.AssertDriverContentsWithFrameAre (
  325. @"
  326. At 0,0
  327. A text with some long width
  328. and also with two lines. ",
  329. _output
  330. );
  331. view.X = 1;
  332. view.Y = 1;
  333. view.Width = 10;
  334. view.Height = 1;
  335. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  336. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  337. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  338. top.Draw ();
  339. TestHelpers.AssertDriverContentsWithFrameAre (
  340. @"
  341. At 0,0
  342. A text wit",
  343. _output
  344. );
  345. Application.End (runState);
  346. }
  347. [Fact]
  348. [TestRespondersDisposed]
  349. public void Dispose_View ()
  350. {
  351. var view = new View ();
  352. Assert.NotNull (view.Margin);
  353. Assert.NotNull (view.Border);
  354. Assert.NotNull (view.Padding);
  355. #if DEBUG_IDISPOSABLE
  356. Assert.Equal (4, Responder.Instances.Count);
  357. #endif
  358. view.Dispose ();
  359. Assert.Null (view.Margin);
  360. Assert.Null (view.Border);
  361. Assert.Null (view.Padding);
  362. }
  363. [Fact]
  364. [AutoInitShutdown]
  365. public void DrawContentComplete_Event_Is_Always_Called ()
  366. {
  367. var viewCalled = false;
  368. var tvCalled = false;
  369. var view = new View { Width = 10, Height = 10, Text = "View" };
  370. view.DrawContentComplete += (s, e) => viewCalled = true;
  371. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  372. tv.DrawContentComplete += (s, e) => tvCalled = true;
  373. var top = new Toplevel ();
  374. top.Add (view, tv);
  375. Application.Begin (top);
  376. Assert.True (viewCalled);
  377. Assert.True (tvCalled);
  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 Rectangle (0, 0, 80, 25), top._needsDisplayRect); };
  403. frame.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 40, 8), frame._needsDisplayRect); };
  404. label.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 38, 1), label._needsDisplayRect); };
  405. button.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 13, 1), button._needsDisplayRect); };
  406. Assert.True (label.AutoSize);
  407. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  408. Assert.Equal (new Rectangle (20, 8, 40, 8), frame.Frame);
  409. Assert.Equal (
  410. new Rectangle (20, 8, 60, 16),
  411. new Rectangle (
  412. frame.Frame.Left,
  413. frame.Frame.Top,
  414. frame.Frame.Right,
  415. frame.Frame.Bottom
  416. )
  417. );
  418. Assert.Equal (new Rectangle (0, 0, 30, 1), label.Frame);
  419. Assert.Equal (new Rectangle (0, 1, 13, 1), button.Frame); // this proves frame was set
  420. Application.End (runState);
  421. }
  422. [Fact]
  423. [AutoInitShutdown]
  424. public void GetHotNormalColor_ColorScheme ()
  425. {
  426. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  427. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  428. view.Enabled = false;
  429. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  430. view.Dispose ();
  431. }
  432. [Fact]
  433. [AutoInitShutdown]
  434. public void GetNormalColor_ColorScheme ()
  435. {
  436. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  437. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  438. view.Enabled = false;
  439. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  440. view.Dispose ();
  441. }
  442. [Fact]
  443. [AutoInitShutdown]
  444. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  445. {
  446. var label = new Label { Text = "At 0,0" };
  447. var view = new DerivedView
  448. {
  449. X = 2,
  450. Y = 2,
  451. Width = 30,
  452. Height = 2,
  453. Text = "A text with some long width\n and also with two lines."
  454. };
  455. Toplevel top = new ();
  456. top.Add (label, view);
  457. RunState runState = Application.Begin (top);
  458. view.Draw ();
  459. TestHelpers.AssertDriverContentsWithFrameAre (
  460. @"
  461. At 0,0
  462. A text with some long width
  463. and also with two lines. ",
  464. _output
  465. );
  466. view.Frame = new Rectangle (3, 3, 10, 1);
  467. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  468. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  469. view.Draw ();
  470. TestHelpers.AssertDriverContentsWithFrameAre (
  471. @"
  472. At 0,0
  473. A text with some long width
  474. A text witith two lines. ",
  475. _output
  476. );
  477. Application.End (runState);
  478. }
  479. [Fact]
  480. [AutoInitShutdown]
  481. public void Incorrect_Redraw_Viewport_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  482. {
  483. var label = new Label { Text = "At 0,0" };
  484. var view = new DerivedView
  485. {
  486. X = 2,
  487. Y = 2,
  488. Width = 30,
  489. Height = 2,
  490. Text = "A text with some long width\n and also with two lines."
  491. };
  492. Toplevel top = new ();
  493. top.Add (label, view);
  494. RunState runState = Application.Begin (top);
  495. view.Draw ();
  496. TestHelpers.AssertDriverContentsWithFrameAre (
  497. @"
  498. At 0,0
  499. A text with some long width
  500. and also with two lines. ",
  501. _output
  502. );
  503. view.X = 3;
  504. view.Y = 3;
  505. view.Width = 10;
  506. view.Height = 1;
  507. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  508. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  509. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  510. view.Draw ();
  511. TestHelpers.AssertDriverContentsWithFrameAre (
  512. @"
  513. At 0,0
  514. A text with some long width
  515. A text witith two lines. ",
  516. _output
  517. );
  518. Application.End (runState);
  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 Rectangle (1, 1, 10, 1);
  545. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  546. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  547. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  548. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  549. view.Draw ();
  550. TestHelpers.AssertDriverContentsWithFrameAre (
  551. @"
  552. At 0,0
  553. A text wit
  554. A text with some long width
  555. and also with two lines. ",
  556. _output
  557. );
  558. Application.End (runState);
  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 Rectangle (1, 1, 10, 1), view.Frame);
  589. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Viewport);
  590. Assert.Equal (new Rectangle (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. }
  602. [Fact]
  603. public void Internal_Tests ()
  604. {
  605. var rect = new Rectangle (1, 1, 10, 1);
  606. var view = new View { Frame = rect };
  607. }
  608. [Fact]
  609. [SetupFakeDriver]
  610. public void SetText_RendersCorrectly ()
  611. {
  612. View view;
  613. var text = "test";
  614. view = new Label { Text = text };
  615. view.BeginInit ();
  616. view.EndInit ();
  617. view.Draw ();
  618. TestHelpers.AssertDriverContentsWithFrameAre ( text, _output);
  619. }
  620. [Fact]
  621. [TestRespondersDisposed]
  622. public void New_Initializes ()
  623. {
  624. // Parameterless
  625. var r = new View ();
  626. Assert.NotNull (r);
  627. Assert.True (r.Enabled);
  628. Assert.True (r.Visible);
  629. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  630. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  631. Assert.False (r.CanFocus);
  632. Assert.False (r.HasFocus);
  633. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Viewport);
  634. Assert.Equal (new Rectangle (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 View { Frame = Rectangle.Empty };
  652. Assert.NotNull (r);
  653. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  654. Assert.Equal ($"View(){r.Viewport}", r.ToString ());
  655. Assert.False (r.CanFocus);
  656. Assert.False (r.HasFocus);
  657. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Viewport);
  658. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Frame);
  659. Assert.Null (r.Focused);
  660. Assert.Null (r.ColorScheme);
  661. Assert.Equal (0, r.Width);
  662. Assert.Equal (0, r.Height);
  663. Assert.Equal (0, r.X);
  664. Assert.Equal (0, r.Y);
  665. Assert.False (r.IsCurrentTop);
  666. Assert.Empty (r.Id);
  667. Assert.Empty (r.Subviews);
  668. Assert.False (r.WantContinuousButtonPressed);
  669. Assert.False (r.WantMousePositionReports);
  670. Assert.Null (r.SuperView);
  671. Assert.Null (r.MostFocused);
  672. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  673. r.Dispose ();
  674. // Rect with values
  675. r = new View { Frame = new Rectangle (1, 2, 3, 4) };
  676. Assert.NotNull (r);
  677. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  678. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  679. Assert.False (r.CanFocus);
  680. Assert.False (r.HasFocus);
  681. Assert.Equal (new Rectangle (0, 0, 3, 4), r.Viewport);
  682. Assert.Equal (new Rectangle (1, 2, 3, 4), r.Frame);
  683. Assert.Null (r.Focused);
  684. Assert.Null (r.ColorScheme);
  685. Assert.Equal (3, r.Width);
  686. Assert.Equal (4, r.Height);
  687. Assert.Equal (1, r.X);
  688. Assert.Equal (2, r.Y);
  689. Assert.False (r.IsCurrentTop);
  690. Assert.Empty (r.Id);
  691. Assert.Empty (r.Subviews);
  692. Assert.False (r.WantContinuousButtonPressed);
  693. Assert.False (r.WantMousePositionReports);
  694. Assert.Null (r.SuperView);
  695. Assert.Null (r.MostFocused);
  696. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  697. r.Dispose ();
  698. // Initializes a view with a vertical direction
  699. r = new View
  700. {
  701. Text = "Vertical View", TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true
  702. }; // BUGBUG: AutoSize or Height need be set
  703. Assert.NotNull (r);
  704. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  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 Rectangle (0, 0, 1, 13), r.Viewport);
  711. Assert.Equal (new Rectangle (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 Key { KeyCode = KeyCode.Null }));
  734. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  735. Assert.False (r.OnKeyUp (new Key { KeyCode = KeyCode.Null }));
  736. Assert.False (r.NewMouseEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  737. Assert.False (r.NewMouseEnterEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  738. Assert.False (r.NewMouseLeaveEvent (new MouseEvent { 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 ColorScheme () };
  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 Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  792. Assert.False (view.Viewport.IsEmpty);
  793. Assert.Equal (new Rectangle (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 View ();
  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 View { 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 View ();
  825. view.X = 1;
  826. view.Y = 2;
  827. view.Width = 3;
  828. view.Height = 4;
  829. super = new View { Frame = new Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  840. Assert.False (view.Viewport.IsEmpty);
  841. Assert.Equal (new Rectangle (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.AutoSize = true;
  858. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  859. Assert.True (view.Visible);
  860. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  861. TestHelpers.AssertDriverContentsWithFrameAre (
  862. @"
  863. ┌────────────────────────────┐
  864. │Testing visibility. │
  865. │ │
  866. │ │
  867. └────────────────────────────┘
  868. ",
  869. _output
  870. );
  871. view.Visible = false;
  872. var firstIteration = false;
  873. Application.RunIteration (ref rs, ref firstIteration);
  874. TestHelpers.AssertDriverContentsWithFrameAre (
  875. @"
  876. ┌────────────────────────────┐
  877. │ │
  878. │ │
  879. │ │
  880. └────────────────────────────┘
  881. ",
  882. _output
  883. );
  884. Application.End (rs);
  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. Assert.True (RunesCount () > 0);
  906. win.Visible = false;
  907. Assert.True (button.Visible);
  908. Assert.True (button.CanFocus);
  909. Assert.False (button.HasFocus);
  910. Assert.False (win.Visible);
  911. Assert.True (win.CanFocus);
  912. Assert.False (win.HasFocus);
  913. button.SetFocus ();
  914. Assert.False (button.HasFocus);
  915. Assert.False (win.HasFocus);
  916. win.SetFocus ();
  917. Assert.False (button.HasFocus);
  918. Assert.False (win.HasFocus);
  919. top.Draw ();
  920. Assert.True (RunesCount () == 0);
  921. win.Visible = true;
  922. win.FocusFirst ();
  923. Assert.True (button.HasFocus);
  924. Assert.True (win.HasFocus);
  925. top.Draw ();
  926. Assert.True (RunesCount () > 0);
  927. Application.RequestStop ();
  928. };
  929. Application.Run (top);
  930. Assert.Equal (1, iterations);
  931. int RunesCount ()
  932. {
  933. Cell [,] contents = ((FakeDriver)Application.Driver).Contents;
  934. var runesCount = 0;
  935. for (var i = 0; i < Application.Driver.Rows; i++)
  936. {
  937. for (var j = 0; j < Application.Driver.Cols; j++)
  938. {
  939. if (contents [i, j].Rune != (Rune)' ')
  940. {
  941. runesCount++;
  942. }
  943. }
  944. }
  945. return runesCount;
  946. }
  947. }
  948. public class DerivedView : View
  949. {
  950. public DerivedView () { CanFocus = true; }
  951. public bool IsKeyDown { get; set; }
  952. public bool IsKeyPress { get; set; }
  953. public bool IsKeyUp { get; set; }
  954. public override string Text { get; set; }
  955. public override void OnDrawContent (Rectangle viewport)
  956. {
  957. var idx = 0;
  958. // BUGBUG: v2 - this should use Viewport, not Frame
  959. for (var r = 0; r < Frame.Height; r++)
  960. {
  961. for (var c = 0; c < Frame.Width; c++)
  962. {
  963. if (idx < Text.Length)
  964. {
  965. char rune = Text [idx];
  966. if (rune != '\n')
  967. {
  968. AddRune (c, r, (Rune)Text [idx]);
  969. }
  970. idx++;
  971. if (rune == '\n')
  972. {
  973. break;
  974. }
  975. }
  976. }
  977. }
  978. ClearLayoutNeeded ();
  979. ClearNeedsDisplay ();
  980. }
  981. public override bool OnKeyDown (Key keyEvent)
  982. {
  983. IsKeyDown = true;
  984. return true;
  985. }
  986. public override bool OnKeyUp (Key keyEvent)
  987. {
  988. IsKeyUp = true;
  989. return true;
  990. }
  991. public override bool OnProcessKeyDown (Key keyEvent)
  992. {
  993. IsKeyPress = true;
  994. return true;
  995. }
  996. }
  997. // OnAccept/Accept tests
  998. [Fact]
  999. public void OnAccept_Fires_Accept ()
  1000. {
  1001. var view = new View ();
  1002. var accepted = false;
  1003. view.Accept += ViewOnAccept;
  1004. view.InvokeCommand (Command.Accept);
  1005. Assert.True (accepted);
  1006. return;
  1007. void ViewOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  1008. }
  1009. [Fact]
  1010. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  1011. {
  1012. var view = new View ();
  1013. var acceptInvoked = false;
  1014. view.Accept += ViewOnAccept;
  1015. var ret = view.InvokeCommand (Command.Accept);
  1016. Assert.True (ret);
  1017. Assert.True (acceptInvoked);
  1018. return;
  1019. void ViewOnAccept (object sender, CancelEventArgs e) {
  1020. acceptInvoked = true;
  1021. e.Cancel = true;
  1022. }
  1023. }
  1024. [Fact]
  1025. public void Accept_Command_Invokes_Accept_Event ()
  1026. {
  1027. var view = new View ();
  1028. var accepted = false;
  1029. view.Accept += ViewOnAccept;
  1030. view.InvokeCommand (Command.Accept);
  1031. Assert.True (accepted);
  1032. return;
  1033. void ViewOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  1034. }
  1035. [Fact]
  1036. public void HotKey_Command_SetsFocus ()
  1037. {
  1038. var view = new View ();
  1039. view.CanFocus = true;
  1040. Assert.False (view.HasFocus);
  1041. view.InvokeCommand (Command.HotKey);
  1042. Assert.True (view.HasFocus);
  1043. }
  1044. }