ViewTests.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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_Bounds_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.Bounds.Width, view.Bounds.Height);
  18. for (var row = 0; row < view.Bounds.Height; row++)
  19. {
  20. Application.Driver.Move (1, row + 1);
  21. for (var col = 0; col < view.Bounds.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.Bounds);
  48. expected = @"
  49. ┌──────────────────┐
  50. │ │
  51. │ │
  52. │ │
  53. │ │
  54. │ │
  55. │ │
  56. │ │
  57. │ │
  58. └──────────────────┘
  59. ";
  60. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  61. }
  62. [Fact]
  63. [AutoInitShutdown]
  64. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  65. {
  66. var view = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
  67. view.DrawContent += (s, e) =>
  68. {
  69. Rectangle savedClip = Application.Driver.Clip;
  70. Application.Driver.Clip = new Rectangle (1, 1, view.Bounds.Width, view.Bounds.Height);
  71. for (var row = 0; row < view.Bounds.Height; row++)
  72. {
  73. Application.Driver.Move (1, row + 1);
  74. for (var col = 0; col < view.Bounds.Width; col++)
  75. {
  76. Application.Driver.AddStr ($"{col}");
  77. }
  78. }
  79. Application.Driver.Clip = savedClip;
  80. e.Cancel = true;
  81. };
  82. var top = new Toplevel ();
  83. top.Add (view);
  84. Application.Begin (top);
  85. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  86. var expected = @"
  87. ┌──────────────────┐
  88. │012345678910111213│
  89. │012345678910111213│
  90. │012345678910111213│
  91. │012345678910111213│
  92. │012345678910111213│
  93. │012345678910111213│
  94. │012345678910111213│
  95. │012345678910111213│
  96. └──────────────────┘
  97. ";
  98. Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  99. Assert.Equal (new Rectangle (0, 0, 20, 10), pos);
  100. view.Clear (view.Bounds);
  101. expected = @"
  102. ┌──────────────────┐
  103. │ │
  104. │ │
  105. │ │
  106. │ │
  107. │ │
  108. │ │
  109. │ │
  110. │ │
  111. └──────────────────┘
  112. ";
  113. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
  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 string ('c', 100) }
  124. : new TextView { Height = 1, Text = new string ('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.True (v.AutoSize);
  132. Assert.False (v.CanFocus);
  133. Assert.Equal (new Rectangle (0, 0, 100, 1), v.Frame);
  134. }
  135. else
  136. {
  137. Assert.False (v.AutoSize);
  138. Assert.True (v.CanFocus);
  139. Assert.Equal (new Rectangle (0, 0, 20, 1), v.Frame);
  140. }
  141. TestHelpers.AssertDriverContentsWithFrameAre (
  142. @"
  143. cccccccccccccccccccc",
  144. _output
  145. );
  146. Attribute [] attributes =
  147. {
  148. Colors.ColorSchemes ["TopLevel"].Normal,
  149. Colors.ColorSchemes ["Base"].Normal,
  150. Colors.ColorSchemes ["Base"].Focus
  151. };
  152. if (label)
  153. {
  154. TestHelpers.AssertDriverAttributesAre (
  155. @"
  156. 111111111111111111110
  157. 111111111111111111110",
  158. Application.Driver,
  159. attributes
  160. );
  161. }
  162. else
  163. {
  164. TestHelpers.AssertDriverAttributesAre (
  165. @"
  166. 222222222222222222220
  167. 111111111111111111110",
  168. Application.Driver,
  169. attributes
  170. );
  171. }
  172. if (label)
  173. {
  174. root.CanFocus = true;
  175. v.CanFocus = true;
  176. Assert.False (v.HasFocus);
  177. v.SetFocus ();
  178. Assert.True (v.HasFocus);
  179. Application.Refresh ();
  180. TestHelpers.AssertDriverAttributesAre (
  181. @"
  182. 222222222222222222220
  183. 111111111111111111110",
  184. Application.Driver,
  185. attributes
  186. );
  187. }
  188. Application.End (runState);
  189. }
  190. [Fact]
  191. [AutoInitShutdown]
  192. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  193. {
  194. var label = new Label { Text = "At 0,0" };
  195. var view = new DerivedView
  196. {
  197. X = 2,
  198. Y = 2,
  199. Width = 30,
  200. Height = 2,
  201. Text = "A text with some long width\n and also with two lines."
  202. };
  203. Toplevel top = new ();
  204. top.Add (label, view);
  205. RunState runState = Application.Begin (top);
  206. TestHelpers.AssertDriverContentsWithFrameAre (
  207. @"
  208. At 0,0
  209. A text with some long width
  210. and also with two lines. ",
  211. _output
  212. );
  213. view.Frame = new Rectangle (3, 3, 10, 1);
  214. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  215. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  216. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  217. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  218. top.Draw ();
  219. TestHelpers.AssertDriverContentsWithFrameAre (
  220. @"
  221. At 0,0
  222. A text wit",
  223. _output
  224. );
  225. Application.End (runState);
  226. }
  227. [Fact]
  228. [AutoInitShutdown]
  229. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  230. {
  231. var label = new Label { Text = "At 0,0" };
  232. var view = new DerivedView
  233. {
  234. X = 2,
  235. Y = 2,
  236. Width = 30,
  237. Height = 2,
  238. Text = "A text with some long width\n and also with two lines."
  239. };
  240. Toplevel top = new ();
  241. top.Add (label, view);
  242. RunState runState = Application.Begin (top);
  243. top.Draw ();
  244. TestHelpers.AssertDriverContentsWithFrameAre (
  245. @"
  246. At 0,0
  247. A text with some long width
  248. and also with two lines. ",
  249. _output
  250. );
  251. view.X = 3;
  252. view.Y = 3;
  253. view.Width = 10;
  254. view.Height = 1;
  255. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  256. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  257. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  258. top.Draw ();
  259. TestHelpers.AssertDriverContentsWithFrameAre (
  260. @"
  261. At 0,0
  262. A text wit",
  263. _output
  264. );
  265. Application.End (runState);
  266. }
  267. [Fact]
  268. [AutoInitShutdown]
  269. public void Correct_Redraw_Bounds_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 Rectangle (1, 1, 10, 1);
  292. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  293. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  294. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  295. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  296. top.Draw ();
  297. TestHelpers.AssertDriverContentsWithFrameAre (
  298. @"
  299. At 0,0
  300. A text wit",
  301. _output
  302. );
  303. Application.End (runState);
  304. }
  305. [Fact]
  306. [AutoInitShutdown]
  307. public void Correct_Redraw_Bounds_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 Rectangle (1, 1, 10, 1), view.Frame);
  334. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  335. Assert.Equal (new Rectangle (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. }
  345. [Fact]
  346. [TestRespondersDisposed]
  347. public void Dispose_View ()
  348. {
  349. var view = new View ();
  350. Assert.NotNull (view.Margin);
  351. Assert.NotNull (view.Border);
  352. Assert.NotNull (view.Padding);
  353. #if DEBUG_IDISPOSABLE
  354. Assert.Equal (4, Responder.Instances.Count);
  355. #endif
  356. view.Dispose ();
  357. Assert.Null (view.Margin);
  358. Assert.Null (view.Border);
  359. Assert.Null (view.Padding);
  360. }
  361. [Fact]
  362. [AutoInitShutdown]
  363. public void DrawContentComplete_Event_Is_Always_Called ()
  364. {
  365. var viewCalled = false;
  366. var tvCalled = false;
  367. var view = new View { Width = 10, Height = 10, Text = "View" };
  368. view.DrawContentComplete += (s, e) => viewCalled = true;
  369. var tv = new TextView { Y = 11, Width = 10, Height = 10 };
  370. tv.DrawContentComplete += (s, e) => tvCalled = true;
  371. var top = new Toplevel ();
  372. top.Add (view, tv);
  373. Application.Begin (top);
  374. Assert.True (viewCalled);
  375. Assert.True (tvCalled);
  376. }
  377. [Fact]
  378. [AutoInitShutdown]
  379. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  380. {
  381. var frame = new FrameView ();
  382. var label = new Label
  383. {
  384. ColorScheme = Colors.ColorSchemes ["Menu"], X = 0, Y = 0, Text = "This should be the first line."
  385. };
  386. var button = new Button
  387. {
  388. X = 0, // don't overcomplicate unit tests
  389. Y = 1,
  390. Text = "Press me!"
  391. };
  392. frame.Add (label, button);
  393. frame.X = Pos.Center ();
  394. frame.Y = Pos.Center ();
  395. frame.Width = 40;
  396. frame.Height = 8;
  397. Toplevel top = new ();
  398. top.Add (frame);
  399. RunState runState = Application.Begin (top);
  400. top.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 80, 25), top._needsDisplayRect); };
  401. frame.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 40, 8), frame._needsDisplayRect); };
  402. label.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 38, 1), label._needsDisplayRect); };
  403. button.LayoutComplete += (s, e) => { Assert.Equal (new Rectangle (0, 0, 13, 1), button._needsDisplayRect); };
  404. Assert.True (label.AutoSize);
  405. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  406. Assert.Equal (new Rectangle (20, 8, 40, 8), frame.Frame);
  407. Assert.Equal (
  408. new Rectangle (20, 8, 60, 16),
  409. new Rectangle (
  410. frame.Frame.Left,
  411. frame.Frame.Top,
  412. frame.Frame.Right,
  413. frame.Frame.Bottom
  414. )
  415. );
  416. Assert.Equal (new Rectangle (0, 0, 30, 1), label.Frame);
  417. Assert.Equal (new Rectangle (0, 1, 13, 1), button.Frame); // this proves frame was set
  418. Application.End (runState);
  419. }
  420. [Fact]
  421. [AutoInitShutdown]
  422. public void GetHotNormalColor_ColorScheme ()
  423. {
  424. var view = new View { ColorScheme = Colors.ColorSchemes ["Base"] };
  425. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  426. view.Enabled = false;
  427. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  428. view.Dispose ();
  429. }
  430. [Fact]
  431. [AutoInitShutdown]
  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_Bounds_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 Rectangle (3, 3, 10, 1);
  465. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  466. Assert.Equal (new Rectangle (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. }
  477. [Fact]
  478. [AutoInitShutdown]
  479. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  480. {
  481. var label = new Label { Text = "At 0,0" };
  482. var view = new DerivedView
  483. {
  484. X = 2,
  485. Y = 2,
  486. Width = 30,
  487. Height = 2,
  488. Text = "A text with some long width\n and also with two lines."
  489. };
  490. Toplevel top = new ();
  491. top.Add (label, view);
  492. RunState runState = Application.Begin (top);
  493. view.Draw ();
  494. TestHelpers.AssertDriverContentsWithFrameAre (
  495. @"
  496. At 0,0
  497. A text with some long width
  498. and also with two lines. ",
  499. _output
  500. );
  501. view.X = 3;
  502. view.Y = 3;
  503. view.Width = 10;
  504. view.Height = 1;
  505. Assert.Equal (new Rectangle (3, 3, 10, 1), view.Frame);
  506. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  507. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  508. view.Draw ();
  509. TestHelpers.AssertDriverContentsWithFrameAre (
  510. @"
  511. At 0,0
  512. A text with some long width
  513. A text witith two lines. ",
  514. _output
  515. );
  516. Application.End (runState);
  517. }
  518. [Fact]
  519. [AutoInitShutdown]
  520. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  521. {
  522. var label = new Label { Text = "At 0,0" };
  523. var view = new DerivedView
  524. {
  525. X = 2,
  526. Y = 2,
  527. Width = 30,
  528. Height = 2,
  529. Text = "A text with some long width\n and also with two lines."
  530. };
  531. Toplevel top = new ();
  532. top.Add (label, view);
  533. RunState runState = Application.Begin (top);
  534. view.Draw ();
  535. TestHelpers.AssertDriverContentsWithFrameAre (
  536. @"
  537. At 0,0
  538. A text with some long width
  539. and also with two lines. ",
  540. _output
  541. );
  542. view.Frame = new Rectangle (1, 1, 10, 1);
  543. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  544. Assert.Equal (LayoutStyle.Absolute, view.LayoutStyle);
  545. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  546. Assert.Equal (new Rectangle (0, 0, 10, 1), view._needsDisplayRect);
  547. view.Draw ();
  548. TestHelpers.AssertDriverContentsWithFrameAre (
  549. @"
  550. At 0,0
  551. A text wit
  552. A text with some long width
  553. and also with two lines. ",
  554. _output
  555. );
  556. Application.End (runState);
  557. }
  558. [Fact]
  559. [AutoInitShutdown]
  560. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  561. {
  562. var label = new Label { Text = "At 0,0" };
  563. var view = new DerivedView
  564. {
  565. X = 2,
  566. Y = 2,
  567. Width = 30,
  568. Height = 2,
  569. Text = "A text with some long width\n and also with two lines."
  570. };
  571. Toplevel top = new ();
  572. top.Add (label, view);
  573. RunState runState = Application.Begin (top);
  574. view.Draw ();
  575. TestHelpers.AssertDriverContentsWithFrameAre (
  576. @"
  577. At 0,0
  578. A text with some long width
  579. and also with two lines. ",
  580. _output
  581. );
  582. view.X = 1;
  583. view.Y = 1;
  584. view.Width = 10;
  585. view.Height = 1;
  586. Assert.Equal (new Rectangle (1, 1, 10, 1), view.Frame);
  587. Assert.Equal (new Rectangle (0, 0, 10, 1), view.Bounds);
  588. Assert.Equal (new Rectangle (0, 0, 30, 2), view._needsDisplayRect);
  589. view.Draw ();
  590. TestHelpers.AssertDriverContentsWithFrameAre (
  591. @"
  592. At 0,0
  593. A text wit
  594. A text with some long width
  595. and also with two lines. ",
  596. _output
  597. );
  598. Application.End (runState);
  599. }
  600. [Fact]
  601. public void Internal_Tests ()
  602. {
  603. var rect = new Rectangle (1, 1, 10, 1);
  604. var view = new View { Frame = rect };
  605. }
  606. [Fact]
  607. [SetupFakeDriver]
  608. public void SetText_RendersCorrectly ()
  609. {
  610. View view;
  611. var text = "test";
  612. view = new Label { Text = text };
  613. view.BeginInit ();
  614. view.EndInit ();
  615. view.Draw ();
  616. TestHelpers.AssertDriverContentsWithFrameAre ( text, _output);
  617. }
  618. [Fact]
  619. [TestRespondersDisposed]
  620. public void New_Initializes ()
  621. {
  622. // Parameterless
  623. var r = new View ();
  624. Assert.NotNull (r);
  625. Assert.True (r.Enabled);
  626. Assert.True (r.Visible);
  627. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  628. Assert.Equal ($"View(){r.Bounds}", r.ToString ());
  629. Assert.False (r.CanFocus);
  630. Assert.False (r.HasFocus);
  631. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Bounds);
  632. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Frame);
  633. Assert.Null (r.Focused);
  634. Assert.Null (r.ColorScheme);
  635. Assert.Equal (0, r.Width);
  636. Assert.Equal (0, r.Height);
  637. Assert.Equal (0, r.X);
  638. Assert.Equal (0, r.Y);
  639. Assert.False (r.IsCurrentTop);
  640. Assert.Empty (r.Id);
  641. Assert.Empty (r.Subviews);
  642. Assert.False (r.WantContinuousButtonPressed);
  643. Assert.False (r.WantMousePositionReports);
  644. Assert.Null (r.SuperView);
  645. Assert.Null (r.MostFocused);
  646. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  647. r.Dispose ();
  648. // Empty Rect
  649. r = new View { Frame = Rectangle.Empty };
  650. Assert.NotNull (r);
  651. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  652. Assert.Equal ($"View(){r.Bounds}", r.ToString ());
  653. Assert.False (r.CanFocus);
  654. Assert.False (r.HasFocus);
  655. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Bounds);
  656. Assert.Equal (new Rectangle (0, 0, 0, 0), r.Frame);
  657. Assert.Null (r.Focused);
  658. Assert.Null (r.ColorScheme);
  659. Assert.Equal (0, r.Width);
  660. Assert.Equal (0, r.Height);
  661. Assert.Equal (0, r.X);
  662. Assert.Equal (0, r.Y);
  663. Assert.False (r.IsCurrentTop);
  664. Assert.Empty (r.Id);
  665. Assert.Empty (r.Subviews);
  666. Assert.False (r.WantContinuousButtonPressed);
  667. Assert.False (r.WantMousePositionReports);
  668. Assert.Null (r.SuperView);
  669. Assert.Null (r.MostFocused);
  670. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  671. r.Dispose ();
  672. // Rect with values
  673. r = new View { Frame = new Rectangle (1, 2, 3, 4) };
  674. Assert.NotNull (r);
  675. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  676. Assert.Equal ($"View(){r.Frame}", r.ToString ());
  677. Assert.False (r.CanFocus);
  678. Assert.False (r.HasFocus);
  679. Assert.Equal (new Rectangle (0, 0, 3, 4), r.Bounds);
  680. Assert.Equal (new Rectangle (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 View
  698. {
  699. Text = "Vertical View", TextDirection = TextDirection.TopBottom_LeftRight, AutoSize = true
  700. }; // BUGBUG: AutoSize or Height need be set
  701. Assert.NotNull (r);
  702. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  703. // BUGBUG: IsInitialized must be true to process calculation
  704. r.BeginInit ();
  705. r.EndInit ();
  706. Assert.False (r.CanFocus);
  707. Assert.False (r.HasFocus);
  708. Assert.Equal (new Rectangle (0, 0, 1, 13), r.Bounds);
  709. Assert.Equal (new Rectangle (0, 0, 1, 13), r.Frame);
  710. Assert.Null (r.Focused);
  711. Assert.Null (r.ColorScheme);
  712. Assert.False (r.IsCurrentTop);
  713. #if DEBUG
  714. Assert.Equal ("Vertical View", r.Id);
  715. #else
  716. Assert.Equal (string.Empty, r.Id);
  717. #endif
  718. Assert.Empty (r.Subviews);
  719. Assert.False (r.WantContinuousButtonPressed);
  720. Assert.False (r.WantMousePositionReports);
  721. Assert.Null (r.SuperView);
  722. Assert.Null (r.MostFocused);
  723. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  724. r.Dispose ();
  725. }
  726. [Fact]
  727. [TestRespondersDisposed]
  728. public void New_Methods_Return_False ()
  729. {
  730. var r = new View ();
  731. Assert.False (r.OnKeyDown (new Key { KeyCode = KeyCode.Null }));
  732. //Assert.False (r.OnKeyDown (new KeyEventArgs () { Key = Key.Unknown }));
  733. Assert.False (r.OnKeyUp (new Key { KeyCode = KeyCode.Null }));
  734. Assert.False (r.NewMouseEvent (new MouseEvent { Flags = MouseFlags.AllEvents }));
  735. Assert.False (r.OnMouseEnter (new MouseEvent { Flags = MouseFlags.AllEvents }));
  736. Assert.False (r.OnMouseLeave (new MouseEvent { Flags = MouseFlags.AllEvents }));
  737. var v1 = new View ();
  738. Assert.False (r.OnEnter (v1));
  739. v1.Dispose ();
  740. var v2 = new View ();
  741. Assert.False (r.OnLeave (v2));
  742. v2.Dispose ();
  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 ColorScheme () };
  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 Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  790. Assert.False (view.Bounds.IsEmpty);
  791. Assert.Equal (new Rectangle (0, 0, 3, 4), view.Bounds);
  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.Bounds.IsEmpty);
  799. super.Dispose ();
  800. #if DEBUG_IDISPOSABLE
  801. Assert.Empty (Responder.Instances);
  802. #endif
  803. // Default Constructor
  804. view = new View ();
  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.Bounds.IsEmpty);
  811. view.Dispose ();
  812. // Object Initializer
  813. view = new View { 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.Bounds.IsEmpty);
  820. view.Dispose ();
  821. // Default Constructor and post assignment equivalent to Object Initializer
  822. view = new View ();
  823. view.X = 1;
  824. view.Y = 2;
  825. view.Width = 3;
  826. view.Height = 4;
  827. super = new View { Frame = new Rectangle (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 Rectangle (1, 2, 3, 4), view.Frame);
  838. Assert.False (view.Bounds.IsEmpty);
  839. Assert.Equal (new Rectangle (0, 0, 3, 4), view.Bounds);
  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. // BUGBUG: AutoSize is false and size wasn't provided so it's 0,0
  848. Assert.Equal (0, view.Frame.Width);
  849. Assert.Equal (0, view.Height);
  850. var win = new Window ();
  851. win.Add (view);
  852. Toplevel top = new ();
  853. top.Add (win);
  854. RunState rs = Application.Begin (top);
  855. view.AutoSize = true;
  856. Assert.Equal ("Testing visibility.".Length, view.Frame.Width);
  857. Assert.True (view.Visible);
  858. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  859. TestHelpers.AssertDriverContentsWithFrameAre (
  860. @"
  861. ┌────────────────────────────┐
  862. │Testing visibility. │
  863. │ │
  864. │ │
  865. └────────────────────────────┘
  866. ",
  867. _output
  868. );
  869. view.Visible = false;
  870. var firstIteration = false;
  871. Application.RunIteration (ref rs, ref firstIteration);
  872. TestHelpers.AssertDriverContentsWithFrameAre (
  873. @"
  874. ┌────────────────────────────┐
  875. │ │
  876. │ │
  877. │ │
  878. └────────────────────────────┘
  879. ",
  880. _output
  881. );
  882. Application.End (rs);
  883. }
  884. [Fact]
  885. [AutoInitShutdown]
  886. public void Visible_Sets_Also_Sets_Subviews ()
  887. {
  888. var button = new Button { Text = "Click Me" };
  889. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  890. win.Add (button);
  891. Toplevel top = new ();
  892. top.Add (win);
  893. var iterations = 0;
  894. Application.Iteration += (s, a) =>
  895. {
  896. iterations++;
  897. Assert.True (button.Visible);
  898. Assert.True (button.CanFocus);
  899. Assert.True (button.HasFocus);
  900. Assert.True (win.Visible);
  901. Assert.True (win.CanFocus);
  902. Assert.True (win.HasFocus);
  903. Assert.True (RunesCount () > 0);
  904. win.Visible = false;
  905. Assert.True (button.Visible);
  906. Assert.True (button.CanFocus);
  907. Assert.False (button.HasFocus);
  908. Assert.False (win.Visible);
  909. Assert.True (win.CanFocus);
  910. Assert.False (win.HasFocus);
  911. button.SetFocus ();
  912. Assert.False (button.HasFocus);
  913. Assert.False (win.HasFocus);
  914. win.SetFocus ();
  915. Assert.False (button.HasFocus);
  916. Assert.False (win.HasFocus);
  917. top.Draw ();
  918. Assert.True (RunesCount () == 0);
  919. win.Visible = true;
  920. win.FocusFirst ();
  921. Assert.True (button.HasFocus);
  922. Assert.True (win.HasFocus);
  923. top.Draw ();
  924. Assert.True (RunesCount () > 0);
  925. Application.RequestStop ();
  926. };
  927. Application.Run (top);
  928. Assert.Equal (1, iterations);
  929. int RunesCount ()
  930. {
  931. Cell [,] contents = ((FakeDriver)Application.Driver).Contents;
  932. var runesCount = 0;
  933. for (var i = 0; i < Application.Driver.Rows; i++)
  934. {
  935. for (var j = 0; j < Application.Driver.Cols; j++)
  936. {
  937. if (contents [i, j].Rune != (Rune)' ')
  938. {
  939. runesCount++;
  940. }
  941. }
  942. }
  943. return runesCount;
  944. }
  945. }
  946. public class DerivedView : View
  947. {
  948. public DerivedView () { CanFocus = true; }
  949. public bool IsKeyDown { get; set; }
  950. public bool IsKeyPress { get; set; }
  951. public bool IsKeyUp { get; set; }
  952. public override string Text { get; set; }
  953. public override void OnDrawContent (Rectangle contentArea)
  954. {
  955. var idx = 0;
  956. // BUGBUG: v2 - this should use Bounds, not Frame
  957. for (var r = 0; r < Frame.Height; r++)
  958. {
  959. for (var c = 0; c < Frame.Width; c++)
  960. {
  961. if (idx < Text.Length)
  962. {
  963. char rune = Text [idx];
  964. if (rune != '\n')
  965. {
  966. AddRune (c, r, (Rune)Text [idx]);
  967. }
  968. idx++;
  969. if (rune == '\n')
  970. {
  971. break;
  972. }
  973. }
  974. }
  975. }
  976. ClearLayoutNeeded ();
  977. ClearNeedsDisplay ();
  978. }
  979. public override bool OnKeyDown (Key keyEvent)
  980. {
  981. IsKeyDown = true;
  982. return true;
  983. }
  984. public override bool OnKeyUp (Key keyEvent)
  985. {
  986. IsKeyUp = true;
  987. return true;
  988. }
  989. public override bool OnProcessKeyDown (Key keyEvent)
  990. {
  991. IsKeyPress = true;
  992. return true;
  993. }
  994. }
  995. // OnAccept/Accept tests
  996. [Fact]
  997. public void OnAccept_Fires_Accept ()
  998. {
  999. var view = new View ();
  1000. var accepted = false;
  1001. view.Accept += ViewOnAccept;
  1002. view.InvokeCommand (Command.Accept);
  1003. Assert.True (accepted);
  1004. return;
  1005. void ViewOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  1006. }
  1007. [Fact]
  1008. public void Accept_Cancel_Event_OnAccept_Returns_True ()
  1009. {
  1010. var view = new View ();
  1011. var acceptInvoked = false;
  1012. view.Accept += ViewOnAccept;
  1013. var ret = view.InvokeCommand (Command.Accept);
  1014. Assert.True (ret);
  1015. Assert.True (acceptInvoked);
  1016. return;
  1017. void ViewOnAccept (object sender, CancelEventArgs e) {
  1018. acceptInvoked = true;
  1019. e.Cancel = true;
  1020. }
  1021. }
  1022. [Fact]
  1023. public void Accept_Command_Invokes_Accept_Event ()
  1024. {
  1025. var view = new View ();
  1026. var accepted = false;
  1027. view.Accept += ViewOnAccept;
  1028. view.InvokeCommand (Command.Accept);
  1029. Assert.True (accepted);
  1030. return;
  1031. void ViewOnAccept (object sender, CancelEventArgs e) { accepted = true; }
  1032. }
  1033. [Fact]
  1034. public void HotKey_Command_SetsFocus ()
  1035. {
  1036. var view = new View ();
  1037. view.CanFocus = true;
  1038. Assert.False (view.HasFocus);
  1039. view.InvokeCommand (Command.HotKey);
  1040. Assert.True (view.HasFocus);
  1041. }
  1042. }