ViewTests.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using System.Text;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.ViewTests {
  8. public class ViewTests {
  9. readonly ITestOutputHelper output;
  10. public ViewTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact, TestRespondersDisposed]
  15. public void New_Initializes ()
  16. {
  17. // Parameterless
  18. var r = new View ();
  19. Assert.NotNull (r);
  20. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  21. Assert.Equal ("View()((0,0,0,0))", r.ToString ());
  22. Assert.False (r.CanFocus);
  23. Assert.False (r.HasFocus);
  24. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  25. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  26. Assert.Null (r.Focused);
  27. Assert.Null (r.ColorScheme);
  28. Assert.Null (r.Width);
  29. Assert.Null (r.Height);
  30. Assert.Null (r.X);
  31. Assert.Null (r.Y);
  32. Assert.False (r.IsCurrentTop);
  33. Assert.Empty (r.Id);
  34. Assert.Empty (r.Subviews);
  35. Assert.False (r.WantContinuousButtonPressed);
  36. Assert.False (r.WantMousePositionReports);
  37. Assert.Null (r.SuperView);
  38. Assert.Null (r.MostFocused);
  39. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  40. r.Dispose ();
  41. // Empty Rect
  42. r = new View (Rect.Empty);
  43. Assert.NotNull (r);
  44. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  45. Assert.Equal ("View()((0,0,0,0))", r.ToString ());
  46. Assert.False (r.CanFocus);
  47. Assert.False (r.HasFocus);
  48. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  50. Assert.Null (r.Focused);
  51. Assert.Null (r.ColorScheme);
  52. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  53. Assert.Null (r.Height); // avoiding Dim errors.
  54. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  55. Assert.Null (r.Y); // avoiding Pos errors.
  56. Assert.False (r.IsCurrentTop);
  57. Assert.Empty (r.Id);
  58. Assert.Empty (r.Subviews);
  59. Assert.False (r.WantContinuousButtonPressed);
  60. Assert.False (r.WantMousePositionReports);
  61. Assert.Null (r.SuperView);
  62. Assert.Null (r.MostFocused);
  63. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  64. r.Dispose ();
  65. // Rect with values
  66. r = new View (new Rect (1, 2, 3, 4));
  67. Assert.NotNull (r);
  68. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  69. Assert.Equal ("View()((1,2,3,4))", r.ToString ());
  70. Assert.False (r.CanFocus);
  71. Assert.False (r.HasFocus);
  72. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  73. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  74. Assert.Null (r.Focused);
  75. Assert.Null (r.ColorScheme);
  76. Assert.Null (r.Width);
  77. Assert.Null (r.Height);
  78. Assert.Null (r.X);
  79. Assert.Null (r.Y);
  80. Assert.False (r.IsCurrentTop);
  81. Assert.Empty (r.Id);
  82. Assert.Empty (r.Subviews);
  83. Assert.False (r.WantContinuousButtonPressed);
  84. Assert.False (r.WantMousePositionReports);
  85. Assert.Null (r.SuperView);
  86. Assert.Null (r.MostFocused);
  87. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  88. r.Dispose ();
  89. // Initializes a view with a vertical direction
  90. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  91. Assert.NotNull (r);
  92. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  93. Assert.Equal ("View(Vertical View)((0,0,1,13))", r.ToString ());
  94. Assert.False (r.CanFocus);
  95. Assert.False (r.HasFocus);
  96. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  97. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  98. Assert.Null (r.Focused);
  99. Assert.Null (r.ColorScheme);
  100. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  101. Assert.Null (r.Height); // avoiding Dim errors.
  102. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  103. Assert.Null (r.Y); // avoiding Pos errors.
  104. Assert.False (r.IsCurrentTop);
  105. Assert.Equal ("Vertical View", r.Id);
  106. Assert.Empty (r.Subviews);
  107. Assert.False (r.WantContinuousButtonPressed);
  108. Assert.False (r.WantMousePositionReports);
  109. Assert.Null (r.SuperView);
  110. Assert.Null (r.MostFocused);
  111. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  112. r.Dispose ();
  113. }
  114. [Fact, TestRespondersDisposed]
  115. public void New_Methods_Return_False ()
  116. {
  117. var r = new View ();
  118. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  120. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  121. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  122. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  123. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  124. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  125. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  126. var v1 = new View ();
  127. Assert.False (r.OnEnter (v1));
  128. v1.Dispose ();
  129. var v2 = new View ();
  130. Assert.False (r.OnLeave (v2));
  131. v2.Dispose ();
  132. r.Dispose ();
  133. // TODO: Add more
  134. }
  135. [Fact, TestRespondersDisposed]
  136. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  137. {
  138. // Object Initializer
  139. var view = new View () {
  140. X = 1,
  141. Y = 2,
  142. Width = 3,
  143. Height = 4
  144. };
  145. var super = new View (new Rect (0, 0, 10, 10));
  146. super.Add (view);
  147. super.BeginInit ();
  148. super.EndInit ();
  149. super.LayoutSubviews ();
  150. Assert.Equal (1, view.X);
  151. Assert.Equal (2, view.Y);
  152. Assert.Equal (3, view.Width);
  153. Assert.Equal (4, view.Height);
  154. Assert.False (view.Frame.IsEmpty);
  155. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  156. Assert.False (view.Bounds.IsEmpty);
  157. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  158. view.LayoutSubviews ();
  159. Assert.Equal (1, view.X);
  160. Assert.Equal (2, view.Y);
  161. Assert.Equal (3, view.Width);
  162. Assert.Equal (4, view.Height);
  163. Assert.False (view.Frame.IsEmpty);
  164. Assert.False (view.Bounds.IsEmpty);
  165. super.Dispose ();
  166. #if DEBUG_IDISPOSABLE
  167. Assert.Empty (Responder.Instances);
  168. #endif
  169. // Default Constructor
  170. view = new View ();
  171. Assert.Null (view.X);
  172. Assert.Null (view.Y);
  173. Assert.Null (view.Width);
  174. Assert.Null (view.Height);
  175. Assert.True (view.Frame.IsEmpty);
  176. Assert.True (view.Bounds.IsEmpty);
  177. view.Dispose ();
  178. // Constructor
  179. view = new View (1, 2, "");
  180. Assert.Null (view.X);
  181. Assert.Null (view.Y);
  182. Assert.Null (view.Width);
  183. Assert.Null (view.Height);
  184. Assert.False (view.Frame.IsEmpty);
  185. Assert.True (view.Bounds.IsEmpty);
  186. view.Dispose ();
  187. // Default Constructor and post assignment equivalent to Object Initializer
  188. view = new View ();
  189. view.X = 1;
  190. view.Y = 2;
  191. view.Width = 3;
  192. view.Height = 4;
  193. super = new View (new Rect (0, 0, 10, 10));
  194. super.Add (view);
  195. super.BeginInit ();
  196. super.EndInit ();
  197. super.LayoutSubviews ();
  198. Assert.Equal (1, view.X);
  199. Assert.Equal (2, view.Y);
  200. Assert.Equal (3, view.Width);
  201. Assert.Equal (4, view.Height);
  202. Assert.False (view.Frame.IsEmpty);
  203. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  204. Assert.False (view.Bounds.IsEmpty);
  205. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  206. super.Dispose ();
  207. }
  208. [Fact, TestRespondersDisposed]
  209. public void Added_Removed ()
  210. {
  211. var v = new View (new Rect (0, 0, 10, 24));
  212. var t = new View ();
  213. v.Added += (s, e) => {
  214. Assert.Same (v.SuperView, e.Parent);
  215. Assert.Same (t, e.Parent);
  216. Assert.Same (v, e.Child);
  217. };
  218. v.Removed += (s, e) => {
  219. Assert.Same (t, e.Parent);
  220. Assert.Same (v, e.Child);
  221. Assert.True (v.SuperView == null);
  222. };
  223. t.Add (v);
  224. Assert.True (t.Subviews.Count == 1);
  225. t.Remove (v);
  226. Assert.True (t.Subviews.Count == 0);
  227. t.Dispose ();
  228. v.Dispose ();
  229. }
  230. [Fact, TestRespondersDisposed]
  231. public void Initialized_Event_Comparing_With_Added_Event ()
  232. {
  233. Application.Init (new FakeDriver ());
  234. var t = new Toplevel () { Id = "0", };
  235. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  236. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  237. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  238. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  239. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  240. w.Added += (s, e) => {
  241. Assert.Equal (e.Parent.Frame.Width, w.Frame.Width);
  242. Assert.Equal (e.Parent.Frame.Height, w.Frame.Height);
  243. };
  244. v1.Added += (s, e) => {
  245. Assert.Equal (e.Parent.Frame.Width, v1.Frame.Width);
  246. Assert.Equal (e.Parent.Frame.Height, v1.Frame.Height);
  247. };
  248. v2.Added += (s, e) => {
  249. Assert.Equal (e.Parent.Frame.Width, v2.Frame.Width);
  250. Assert.Equal (e.Parent.Frame.Height, v2.Frame.Height);
  251. };
  252. sv1.Added += (s, e) => {
  253. Assert.Equal (e.Parent.Frame.Width, sv1.Frame.Width);
  254. Assert.Equal (e.Parent.Frame.Height, sv1.Frame.Height);
  255. };
  256. t.Initialized += (s, e) => {
  257. tc++;
  258. Assert.Equal (1, tc);
  259. Assert.Equal (1, wc);
  260. Assert.Equal (1, v1c);
  261. Assert.Equal (1, v2c);
  262. Assert.Equal (1, sv1c);
  263. Assert.True (t.CanFocus);
  264. Assert.True (w.CanFocus);
  265. Assert.False (v1.CanFocus);
  266. Assert.False (v2.CanFocus);
  267. Assert.False (sv1.CanFocus);
  268. Application.Refresh ();
  269. };
  270. w.Initialized += (s, e) => {
  271. wc++;
  272. Assert.Equal (t.Frame.Width, w.Frame.Width);
  273. Assert.Equal (t.Frame.Height, w.Frame.Height);
  274. };
  275. v1.Initialized += (s, e) => {
  276. v1c++;
  277. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  278. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  279. };
  280. v2.Initialized += (s, e) => {
  281. v2c++;
  282. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  283. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  284. };
  285. sv1.Initialized += (s, e) => {
  286. sv1c++;
  287. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  288. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  289. Assert.False (sv1.CanFocus);
  290. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  291. Assert.False (sv1.CanFocus);
  292. };
  293. v1.Add (sv1);
  294. w.Add (v1, v2);
  295. t.Add (w);
  296. Application.Iteration += (s, a) => {
  297. Application.Refresh ();
  298. t.Running = false;
  299. };
  300. Application.Run (t);
  301. Application.Shutdown ();
  302. Assert.Equal (1, tc);
  303. Assert.Equal (1, wc);
  304. Assert.Equal (1, v1c);
  305. Assert.Equal (1, v2c);
  306. Assert.Equal (1, sv1c);
  307. Assert.True (t.CanFocus);
  308. Assert.True (w.CanFocus);
  309. Assert.False (v1.CanFocus);
  310. Assert.False (v2.CanFocus);
  311. Assert.False (sv1.CanFocus);
  312. v1.CanFocus = true;
  313. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  314. }
  315. [Fact, TestRespondersDisposed]
  316. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  317. {
  318. Application.Init (new FakeDriver ());
  319. var t = new Toplevel () { Id = "0", };
  320. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  321. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  322. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  323. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  324. t.Initialized += (s, e) => {
  325. tc++;
  326. Assert.Equal (1, tc);
  327. Assert.Equal (1, wc);
  328. Assert.Equal (1, v1c);
  329. Assert.Equal (1, v2c);
  330. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  331. Assert.True (t.CanFocus);
  332. Assert.True (w.CanFocus);
  333. Assert.False (v1.CanFocus);
  334. Assert.False (v2.CanFocus);
  335. Application.Refresh ();
  336. };
  337. w.Initialized += (s, e) => {
  338. wc++;
  339. Assert.Equal (t.Frame.Width, w.Frame.Width);
  340. Assert.Equal (t.Frame.Height, w.Frame.Height);
  341. };
  342. v1.Initialized += (s, e) => {
  343. v1c++;
  344. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  345. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  346. };
  347. v2.Initialized += (s, e) => {
  348. v2c++;
  349. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  350. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  351. };
  352. w.Add (v1, v2);
  353. t.Add (w);
  354. Application.Iteration += (s, a) => {
  355. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  356. sv1.Initialized += (s, e) => {
  357. sv1c++;
  358. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  359. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  360. Assert.False (sv1.CanFocus);
  361. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  362. Assert.False (sv1.CanFocus);
  363. };
  364. v1.Add (sv1);
  365. Application.Refresh ();
  366. t.Running = false;
  367. };
  368. Application.Run (t);
  369. Application.Shutdown ();
  370. Assert.Equal (1, tc);
  371. Assert.Equal (1, wc);
  372. Assert.Equal (1, v1c);
  373. Assert.Equal (1, v2c);
  374. Assert.Equal (1, sv1c);
  375. Assert.True (t.CanFocus);
  376. Assert.True (w.CanFocus);
  377. Assert.False (v1.CanFocus);
  378. Assert.False (v2.CanFocus);
  379. }
  380. [Theory, TestRespondersDisposed]
  381. [InlineData (1)]
  382. [InlineData (2)]
  383. [InlineData (3)]
  384. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  385. {
  386. var driver = new FakeDriver ();
  387. Application.Init (driver);
  388. try {
  389. // Create a label with a short text
  390. Label lbl;
  391. var text = "test";
  392. if (choice == 1) {
  393. // An object initializer should call the default constructor.
  394. lbl = new Label { Text = text };
  395. } else if (choice == 2) {
  396. // Calling the default constructor followed by the object initializer.
  397. lbl = new Label () { Text = text };
  398. } else {
  399. // Calling the Text constructor.
  400. lbl = new Label (text);
  401. }
  402. Application.Top.Add (lbl);
  403. Application.Begin (Application.Top);
  404. // should have the initial text
  405. Assert.Equal ((Rune)'t', driver.Contents [0, 0].Runes [0]);
  406. Assert.Equal ((Rune)'e', driver.Contents [0, 1].Runes [0]);
  407. Assert.Equal ((Rune)'s', driver.Contents [0, 2].Runes [0]);
  408. Assert.Equal ((Rune)'t', driver.Contents [0, 3].Runes [0]);
  409. Assert.Equal ((Rune)' ', driver.Contents [0, 4].Runes [0]);
  410. } finally {
  411. Application.Shutdown ();
  412. }
  413. }
  414. [Fact, AutoInitShutdown]
  415. public void Internal_Tests ()
  416. {
  417. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  418. Enum.GetValues (typeof (View.Direction)));
  419. var rect = new Rect (1, 1, 10, 1);
  420. var view = new View (rect);
  421. var top = Application.Top;
  422. top.Add (view);
  423. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  424. view.FocusDirection = View.Direction.Backward;
  425. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  426. Assert.Empty (view.InternalSubviews);
  427. // BUGBUG: v2 - _needsDisplay needs debugging - test disabled for now.
  428. //Assert.Equal (new Rect (new Point (0, 0), rect.Size), view._needsDisplay);
  429. Assert.True (view.LayoutNeeded);
  430. Assert.False (view.SubViewNeedsDisplay);
  431. Assert.False (view._addingView);
  432. view._addingView = true;
  433. Assert.True (view._addingView);
  434. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  435. Assert.Equal (1, rcol);
  436. Assert.Equal (1, rrow);
  437. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  438. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  439. Assert.True (view.LayoutStyle == LayoutStyle.Absolute);
  440. var runState = Application.Begin (top);
  441. view.Width = Dim.Fill ();
  442. view.Height = Dim.Fill ();
  443. Assert.Equal (10, view.Bounds.Width);
  444. Assert.Equal (1, view.Bounds.Height);
  445. view.LayoutStyle = LayoutStyle.Computed;
  446. view.SetRelativeLayout (top.Bounds);
  447. Assert.Equal (1, view.Frame.X);
  448. Assert.Equal (1, view.Frame.Y);
  449. Assert.Equal (79, view.Frame.Width);
  450. Assert.Equal (24, view.Frame.Height);
  451. Assert.Equal (0, view.Bounds.X);
  452. Assert.Equal (0, view.Bounds.Y);
  453. Assert.Equal (79, view.Bounds.Width);
  454. Assert.Equal (24, view.Bounds.Height);
  455. view.X = 0;
  456. view.Y = 0;
  457. Assert.Equal ("Absolute(0)", view.X.ToString ());
  458. Assert.Equal ("Fill(0)", view.Width.ToString ());
  459. view.SetRelativeLayout (top.Bounds);
  460. Assert.Equal (0, view.Frame.X);
  461. Assert.Equal (0, view.Frame.Y);
  462. Assert.Equal (80, view.Frame.Width);
  463. Assert.Equal (25, view.Frame.Height);
  464. Assert.Equal (0, view.Bounds.X);
  465. Assert.Equal (0, view.Bounds.Y);
  466. Assert.Equal (80, view.Bounds.Width);
  467. Assert.Equal (25, view.Bounds.Height);
  468. bool layoutStarted = false;
  469. view.LayoutStarted += (s, e) => layoutStarted = true;
  470. view.OnLayoutStarted (null);
  471. Assert.True (layoutStarted);
  472. view.LayoutComplete += (s, e) => layoutStarted = false;
  473. view.OnLayoutComplete (null);
  474. Assert.False (layoutStarted);
  475. view.X = Pos.Center () - 41;
  476. view.Y = Pos.Center () - 13;
  477. view.SetRelativeLayout (top.Bounds);
  478. top.LayoutSubviews (); // BUGBUG: v2 - ??
  479. view.ViewToScreen (0, 0, out rcol, out rrow);
  480. Assert.Equal (-41, rcol);
  481. Assert.Equal (-13, rrow);
  482. Application.End (runState);
  483. }
  484. [Fact, AutoInitShutdown]
  485. public void Visible_Sets_Also_Sets_Subviews ()
  486. {
  487. var button = new Button ("Click Me");
  488. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  489. win.Add (button);
  490. var top = Application.Top;
  491. top.Add (win);
  492. var iterations = 0;
  493. Application.Iteration += (s, a) => {
  494. iterations++;
  495. Assert.True (button.Visible);
  496. Assert.True (button.CanFocus);
  497. Assert.True (button.HasFocus);
  498. Assert.True (win.Visible);
  499. Assert.True (win.CanFocus);
  500. Assert.True (win.HasFocus);
  501. Assert.True (RunesCount () > 0);
  502. win.Visible = false;
  503. Assert.True (button.Visible);
  504. Assert.True (button.CanFocus);
  505. Assert.False (button.HasFocus);
  506. Assert.False (win.Visible);
  507. Assert.True (win.CanFocus);
  508. Assert.False (win.HasFocus);
  509. button.SetFocus ();
  510. Assert.False (button.HasFocus);
  511. Assert.False (win.HasFocus);
  512. win.SetFocus ();
  513. Assert.False (button.HasFocus);
  514. Assert.False (win.HasFocus);
  515. top.Draw ();
  516. Assert.True (RunesCount () == 0);
  517. win.Visible = true;
  518. win.FocusFirst ();
  519. Assert.True (button.HasFocus);
  520. Assert.True (win.HasFocus);
  521. top.Draw ();
  522. Assert.True (RunesCount () > 0);
  523. Application.RequestStop ();
  524. };
  525. Application.Run ();
  526. Assert.Equal (1, iterations);
  527. int RunesCount ()
  528. {
  529. var contents = ((FakeDriver)Application.Driver).Contents;
  530. var runesCount = 0;
  531. for (int i = 0; i < Application.Driver.Rows; i++) {
  532. for (int j = 0; j < Application.Driver.Cols; j++) {
  533. if (contents [i, j].Runes[0] != (Rune)' ') {
  534. runesCount++;
  535. }
  536. }
  537. }
  538. return runesCount;
  539. }
  540. }
  541. [Fact, AutoInitShutdown]
  542. public void GetTopSuperView_Test ()
  543. {
  544. var v1 = new View ();
  545. var fv1 = new FrameView ();
  546. fv1.Add (v1);
  547. var tf1 = new TextField ();
  548. var w1 = new Window ();
  549. w1.Add (fv1, tf1);
  550. var top1 = new Toplevel ();
  551. top1.Add (w1);
  552. var v2 = new View ();
  553. var fv2 = new FrameView ();
  554. fv2.Add (v2);
  555. var tf2 = new TextField ();
  556. var w2 = new Window ();
  557. w2.Add (fv2, tf2);
  558. var top2 = new Toplevel ();
  559. top2.Add (w2);
  560. Assert.Equal (top1, v1.GetTopSuperView ());
  561. Assert.Equal (top2, v2.GetTopSuperView ());
  562. v1.Dispose ();
  563. fv1.Dispose ();
  564. tf1.Dispose ();
  565. w1.Dispose ();
  566. top1.Dispose ();
  567. v2.Dispose ();
  568. fv2.Dispose ();
  569. tf2.Dispose ();
  570. w2.Dispose ();
  571. top2.Dispose ();
  572. }
  573. [Fact, AutoInitShutdown]
  574. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  575. {
  576. var view = new FrameView () {
  577. Width = Dim.Fill (),
  578. Height = Dim.Fill ()
  579. };
  580. view.DrawContent += (s, e) => {
  581. var savedClip = Application.Driver.Clip;
  582. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  583. for (int row = 0; row < view.Bounds.Height; row++) {
  584. Application.Driver.Move (1, row + 1);
  585. for (int col = 0; col < view.Bounds.Width; col++) {
  586. Application.Driver.AddStr ($"{col}");
  587. }
  588. }
  589. Application.Driver.Clip = savedClip;
  590. e.Cancel = true;
  591. };
  592. Application.Top.Add (view);
  593. Application.Begin (Application.Top);
  594. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  595. var expected = @"
  596. ┌──────────────────┐
  597. │012345678910111213│
  598. │012345678910111213│
  599. │012345678910111213│
  600. │012345678910111213│
  601. │012345678910111213│
  602. │012345678910111213│
  603. │012345678910111213│
  604. │012345678910111213│
  605. └──────────────────┘
  606. ";
  607. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  608. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  609. view.Clear (view.Frame);
  610. expected = @"
  611. ";
  612. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  613. Assert.Equal (Rect.Empty, pos);
  614. }
  615. [Fact, AutoInitShutdown]
  616. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  617. {
  618. var view = new FrameView () {
  619. Width = Dim.Fill (),
  620. Height = Dim.Fill ()
  621. };
  622. view.DrawContent += (s, e) => {
  623. var savedClip = Application.Driver.Clip;
  624. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  625. for (int row = 0; row < view.Bounds.Height; row++) {
  626. Application.Driver.Move (1, row + 1);
  627. for (int col = 0; col < view.Bounds.Width; col++) {
  628. Application.Driver.AddStr ($"{col}");
  629. }
  630. }
  631. Application.Driver.Clip = savedClip;
  632. e.Cancel = true;
  633. };
  634. Application.Top.Add (view);
  635. Application.Begin (Application.Top);
  636. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  637. var expected = @"
  638. ┌──────────────────┐
  639. │012345678910111213│
  640. │012345678910111213│
  641. │012345678910111213│
  642. │012345678910111213│
  643. │012345678910111213│
  644. │012345678910111213│
  645. │012345678910111213│
  646. │012345678910111213│
  647. └──────────────────┘
  648. ";
  649. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  650. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  651. view.Clear (view.Frame);
  652. expected = @"
  653. ";
  654. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  655. Assert.Equal (Rect.Empty, pos);
  656. }
  657. [Fact, AutoInitShutdown]
  658. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  659. {
  660. var text = "Say Hello 你";
  661. var horizontalView = new View () {
  662. Text = text,
  663. AutoSize = true,
  664. HotKeySpecifier = (Rune)'_'
  665. };
  666. var verticalView = new View () {
  667. Text = text,
  668. AutoSize = true,
  669. HotKeySpecifier = (Rune)'_',
  670. TextDirection = TextDirection.TopBottom_LeftRight
  671. };
  672. Application.Top.Add (horizontalView, verticalView);
  673. Application.Begin (Application.Top);
  674. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  675. Assert.True (horizontalView.AutoSize);
  676. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  677. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  678. //Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  679. //Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  680. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  681. Assert.True (verticalView.AutoSize);
  682. // BUGBUG: v2 - Autosize is broken; disabling this test
  683. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  684. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  685. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  686. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  687. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  688. text = "Say He_llo 你";
  689. horizontalView.Text = text;
  690. verticalView.Text = text;
  691. Assert.True (horizontalView.AutoSize);
  692. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  693. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  694. //Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  695. //Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  696. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  697. Assert.True (verticalView.AutoSize);
  698. // BUGBUG: v2 - Autosize is broken; disabling this test
  699. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  700. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  701. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  702. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  703. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  704. }
  705. [Fact, TestRespondersDisposed]
  706. public void IsAdded_Added_Removed ()
  707. {
  708. var top = new Toplevel ();
  709. var view = new View ();
  710. Assert.False (view.IsAdded);
  711. top.Add (view);
  712. Assert.True (view.IsAdded);
  713. top.Remove (view);
  714. Assert.False (view.IsAdded);
  715. top.Dispose ();
  716. view.Dispose ();
  717. }
  718. [Fact, AutoInitShutdown]
  719. public void Visible_Clear_The_View_Output ()
  720. {
  721. var label = new Label ("Testing visibility.");
  722. var win = new Window ();
  723. win.Add (label);
  724. var top = Application.Top;
  725. top.Add (win);
  726. var rs = Application.Begin (top);
  727. Assert.True (label.Visible);
  728. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  729. TestHelpers.AssertDriverContentsWithFrameAre (@"
  730. ┌────────────────────────────┐
  731. │Testing visibility. │
  732. │ │
  733. │ │
  734. └────────────────────────────┘
  735. ", output);
  736. label.Visible = false;
  737. bool firstIteration = false;
  738. Application.RunIteration (ref rs, ref firstIteration);
  739. TestHelpers.AssertDriverContentsWithFrameAre (@"
  740. ┌────────────────────────────┐
  741. │ │
  742. │ │
  743. │ │
  744. └────────────────────────────┘
  745. ", output);
  746. Application.End (rs);
  747. }
  748. [Fact, AutoInitShutdown]
  749. public void DrawContentComplete_Event_Is_Always_Called ()
  750. {
  751. var viewCalled = false;
  752. var tvCalled = false;
  753. var view = new View ("View") { Width = 10, Height = 10 };
  754. view.DrawContentComplete += (s, e) => viewCalled = true;
  755. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  756. tv.DrawContentComplete += (s, e) => tvCalled = true;
  757. Application.Top.Add (view, tv);
  758. Application.Begin (Application.Top);
  759. Assert.True (viewCalled);
  760. Assert.True (tvCalled);
  761. }
  762. [Fact, AutoInitShutdown]
  763. public void GetNormalColor_ColorScheme ()
  764. {
  765. var view = new View { ColorScheme = Colors.Base };
  766. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  767. view.Enabled = false;
  768. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  769. view.Dispose ();
  770. }
  771. [Fact, AutoInitShutdown]
  772. public void GetHotNormalColor_ColorScheme ()
  773. {
  774. var view = new View { ColorScheme = Colors.Base };
  775. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  776. view.Enabled = false;
  777. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  778. view.Dispose ();
  779. }
  780. [Theory, AutoInitShutdown]
  781. [InlineData (true)]
  782. [InlineData (false)]
  783. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  784. {
  785. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  786. var v = label == true ?
  787. new Label (new string ('c', 100)) {
  788. Width = Dim.Fill (),
  789. } :
  790. (View)new TextView () {
  791. Height = 1,
  792. Text = new string ('c', 100),
  793. Width = Dim.Fill ()
  794. };
  795. root.Add (v);
  796. Application.Top.Add (root);
  797. var runState = Application.Begin (Application.Top);
  798. if (label) {
  799. Assert.True (v.AutoSize);
  800. Assert.False (v.CanFocus);
  801. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  802. } else {
  803. Assert.False (v.AutoSize);
  804. Assert.True (v.CanFocus);
  805. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  806. }
  807. TestHelpers.AssertDriverContentsWithFrameAre (@"
  808. cccccccccccccccccccc", output);
  809. var attributes = new Attribute [] {
  810. Colors.TopLevel.Normal,
  811. Colors.Base.Normal,
  812. Colors.Base.Focus
  813. };
  814. if (label) {
  815. TestHelpers.AssertDriverColorsAre (@"
  816. 111111111111111111110
  817. 111111111111111111110", attributes);
  818. } else {
  819. TestHelpers.AssertDriverColorsAre (@"
  820. 222222222222222222220
  821. 111111111111111111110", attributes);
  822. }
  823. if (label) {
  824. root.CanFocus = true;
  825. v.CanFocus = true;
  826. Assert.False (v.HasFocus);
  827. v.SetFocus ();
  828. Assert.True (v.HasFocus);
  829. Application.Refresh ();
  830. TestHelpers.AssertDriverColorsAre (@"
  831. 222222222222222222220
  832. 111111111111111111110", attributes);
  833. }
  834. Application.End (runState);
  835. }
  836. public class DerivedView : View {
  837. public DerivedView ()
  838. {
  839. CanFocus = true;
  840. }
  841. public bool IsKeyDown { get; set; }
  842. public bool IsKeyPress { get; set; }
  843. public bool IsKeyUp { get; set; }
  844. public override string Text { get; set; }
  845. public override bool OnKeyDown (KeyEvent keyEvent)
  846. {
  847. IsKeyDown = true;
  848. return true;
  849. }
  850. public override bool ProcessKey (KeyEvent keyEvent)
  851. {
  852. IsKeyPress = true;
  853. return true;
  854. }
  855. public override bool OnKeyUp (KeyEvent keyEvent)
  856. {
  857. IsKeyUp = true;
  858. return true;
  859. }
  860. public override void OnDrawContent (Rect contentArea)
  861. {
  862. var idx = 0;
  863. // BUGBUG: v2 - this should use Boudns, not Frame
  864. for (int r = 0; r < Frame.Height; r++) {
  865. for (int c = 0; c < Frame.Width; c++) {
  866. if (idx < Text.Length) {
  867. var rune = Text [idx];
  868. if (rune != '\n') {
  869. AddRune (c, r, (Rune)Text [idx]);
  870. }
  871. idx++;
  872. if (rune == '\n') {
  873. break;
  874. }
  875. }
  876. }
  877. }
  878. ClearLayoutNeeded ();
  879. ClearNeedsDisplay ();
  880. }
  881. }
  882. [Fact, AutoInitShutdown]
  883. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  884. {
  885. var label = new Label ("At 0,0");
  886. var view = new DerivedView () {
  887. X = 2,
  888. Y = 2,
  889. Width = 30,
  890. Height = 2,
  891. Text = "A text with some long width\n and also with two lines."
  892. };
  893. var top = Application.Top;
  894. top.Add (label, view);
  895. var runState = Application.Begin (top);
  896. top.Draw ();
  897. TestHelpers.AssertDriverContentsWithFrameAre (@"
  898. At 0,0
  899. A text with some long width
  900. and also with two lines. ", output);
  901. view.Frame = new Rect (1, 1, 10, 1);
  902. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  903. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  904. view.LayoutStyle = LayoutStyle.Absolute;
  905. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  906. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  907. top.Draw ();
  908. TestHelpers.AssertDriverContentsWithFrameAre (@"
  909. At 0,0
  910. A text wit", output);
  911. Application.End (runState);
  912. }
  913. [Fact, AutoInitShutdown]
  914. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  915. {
  916. var label = new Label ("At 0,0");
  917. var view = new DerivedView () {
  918. X = 2,
  919. Y = 2,
  920. Width = 30,
  921. Height = 2,
  922. Text = "A text with some long width\n and also with two lines."
  923. };
  924. var top = Application.Top;
  925. top.Add (label, view);
  926. var runState = Application.Begin (top);
  927. top.Draw ();
  928. TestHelpers.AssertDriverContentsWithFrameAre (@"
  929. At 0,0
  930. A text with some long width
  931. and also with two lines. ", output);
  932. view.X = 1;
  933. view.Y = 1;
  934. view.Width = 10;
  935. view.Height = 1;
  936. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  937. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  938. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  939. top.Draw ();
  940. TestHelpers.AssertDriverContentsWithFrameAre (@"
  941. At 0,0
  942. A text wit", output);
  943. Application.End (runState);
  944. }
  945. [Fact, AutoInitShutdown]
  946. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  947. {
  948. var label = new Label ("At 0,0");
  949. var view = new DerivedView () {
  950. X = 2,
  951. Y = 2,
  952. Width = 30,
  953. Height = 2,
  954. Text = "A text with some long width\n and also with two lines."
  955. };
  956. var top = Application.Top;
  957. top.Add (label, view);
  958. var runState = Application.Begin (top);
  959. view.Draw ();
  960. TestHelpers.AssertDriverContentsWithFrameAre (@"
  961. At 0,0
  962. A text with some long width
  963. and also with two lines. ", output);
  964. view.Frame = new Rect (1, 1, 10, 1);
  965. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  966. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  967. view.LayoutStyle = LayoutStyle.Absolute;
  968. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  969. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  970. view.Draw ();
  971. TestHelpers.AssertDriverContentsWithFrameAre (@"
  972. At 0,0
  973. A text wit
  974. A text with some long width
  975. and also with two lines. ", output);
  976. Application.End (runState);
  977. }
  978. [Fact, AutoInitShutdown]
  979. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  980. {
  981. var label = new Label ("At 0,0");
  982. var view = new DerivedView () {
  983. X = 2,
  984. Y = 2,
  985. Width = 30,
  986. Height = 2,
  987. Text = "A text with some long width\n and also with two lines."
  988. };
  989. var top = Application.Top;
  990. top.Add (label, view);
  991. var runState = Application.Begin (top);
  992. view.Draw ();
  993. TestHelpers.AssertDriverContentsWithFrameAre (@"
  994. At 0,0
  995. A text with some long width
  996. and also with two lines. ", output);
  997. view.X = 1;
  998. view.Y = 1;
  999. view.Width = 10;
  1000. view.Height = 1;
  1001. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1002. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1003. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  1004. view.Draw ();
  1005. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1006. At 0,0
  1007. A text wit
  1008. A text with some long width
  1009. and also with two lines. ", output);
  1010. Application.End (runState);
  1011. }
  1012. [Fact, AutoInitShutdown]
  1013. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1014. {
  1015. var label = new Label ("At 0,0");
  1016. var view = new DerivedView () {
  1017. X = 2,
  1018. Y = 2,
  1019. Width = 30,
  1020. Height = 2,
  1021. Text = "A text with some long width\n and also with two lines."
  1022. };
  1023. var top = Application.Top;
  1024. top.Add (label, view);
  1025. var runState = Application.Begin (top);
  1026. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1027. At 0,0
  1028. A text with some long width
  1029. and also with two lines. ", output);
  1030. view.Frame = new Rect (3, 3, 10, 1);
  1031. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1032. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1033. view.LayoutStyle = LayoutStyle.Absolute;
  1034. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1035. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  1036. top.Draw ();
  1037. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1038. At 0,0
  1039. A text wit", output);
  1040. Application.End (runState);
  1041. }
  1042. [Fact, AutoInitShutdown]
  1043. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1044. {
  1045. var label = new Label ("At 0,0");
  1046. var view = new DerivedView () {
  1047. X = 2,
  1048. Y = 2,
  1049. Width = 30,
  1050. Height = 2,
  1051. Text = "A text with some long width\n and also with two lines."
  1052. };
  1053. var top = Application.Top;
  1054. top.Add (label, view);
  1055. var runState = Application.Begin (top);
  1056. top.Draw ();
  1057. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1058. At 0,0
  1059. A text with some long width
  1060. and also with two lines. ", output);
  1061. view.X = 3;
  1062. view.Y = 3;
  1063. view.Width = 10;
  1064. view.Height = 1;
  1065. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1066. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1067. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  1068. top.Draw ();
  1069. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1070. At 0,0
  1071. A text wit", output);
  1072. Application.End (runState);
  1073. }
  1074. [Fact, AutoInitShutdown]
  1075. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1076. {
  1077. var label = new Label ("At 0,0");
  1078. var view = new DerivedView () {
  1079. X = 2,
  1080. Y = 2,
  1081. Width = 30,
  1082. Height = 2,
  1083. Text = "A text with some long width\n and also with two lines."
  1084. };
  1085. var top = Application.Top;
  1086. top.Add (label, view);
  1087. var runState = Application.Begin (top);
  1088. view.Draw ();
  1089. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1090. At 0,0
  1091. A text with some long width
  1092. and also with two lines. ", output);
  1093. view.Frame = new Rect (3, 3, 10, 1);
  1094. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1095. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplayRect);
  1096. view.Draw ();
  1097. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1098. At 0,0
  1099. A text with some long width
  1100. A text witith two lines. ", output);
  1101. Application.End (runState);
  1102. }
  1103. [Fact, AutoInitShutdown]
  1104. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1105. {
  1106. var label = new Label ("At 0,0");
  1107. var view = new DerivedView () {
  1108. X = 2,
  1109. Y = 2,
  1110. Width = 30,
  1111. Height = 2,
  1112. Text = "A text with some long width\n and also with two lines."
  1113. };
  1114. var top = Application.Top;
  1115. top.Add (label, view);
  1116. var runState = Application.Begin (top);
  1117. view.Draw ();
  1118. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1119. At 0,0
  1120. A text with some long width
  1121. and also with two lines. ", output);
  1122. view.X = 3;
  1123. view.Y = 3;
  1124. view.Width = 10;
  1125. view.Height = 1;
  1126. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1127. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1128. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplayRect);
  1129. view.Draw ();
  1130. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1131. At 0,0
  1132. A text with some long width
  1133. A text witith two lines. ", output);
  1134. Application.End (runState);
  1135. }
  1136. [Fact, AutoInitShutdown]
  1137. public void Test_Nested_Views_With_Height_Equal_To_One ()
  1138. {
  1139. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  1140. var top = new View () { Width = Dim.Fill (), Height = 1 };
  1141. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  1142. top.Add (new Label ("111"));
  1143. v.Add (top);
  1144. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  1145. bottom.Add (new Label ("222"));
  1146. v.Add (bottom);
  1147. v.BeginInit ();
  1148. v.EndInit ();
  1149. v.LayoutSubviews ();
  1150. v.Draw ();
  1151. string looksLike =
  1152. @"
  1153. 111
  1154. ───────────
  1155. 222";
  1156. TestHelpers.AssertDriverContentsAre (looksLike, output);
  1157. v.Dispose ();
  1158. top.Dispose ();
  1159. bottom.Dispose ();
  1160. }
  1161. [Fact, AutoInitShutdown]
  1162. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  1163. {
  1164. var frame = new FrameView ();
  1165. var label = new Label ("This should be the first line.") {
  1166. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1167. ColorScheme = Colors.Menu,
  1168. Width = Dim.Fill (),
  1169. X = Pos.Center (),
  1170. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  1171. };
  1172. var button = new Button ("Press me!") {
  1173. X = Pos.Center (),
  1174. Y = Pos.Center ()
  1175. };
  1176. frame.Add (label, button);
  1177. frame.X = Pos.Center ();
  1178. frame.Y = Pos.Center ();
  1179. frame.Width = 40;
  1180. frame.Height = 8;
  1181. var top = Application.Top;
  1182. top.Add (frame);
  1183. var runState = Application.Begin (top);
  1184. top.LayoutComplete += (s, e) => {
  1185. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplayRect);
  1186. };
  1187. frame.LayoutComplete += (s, e) => {
  1188. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplayRect);
  1189. };
  1190. label.LayoutComplete += (s, e) => {
  1191. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplayRect);
  1192. };
  1193. button.LayoutComplete += (s, e) => {
  1194. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplayRect);
  1195. };
  1196. Assert.True (label.AutoSize);
  1197. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1198. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  1199. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  1200. frame.Frame.Left, frame.Frame.Top,
  1201. frame.Frame.Right, frame.Frame.Bottom));
  1202. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1203. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  1204. var expected = @$"
  1205. ┌──────────────────────────────────────┐
  1206. │ This should be the first line. │
  1207. │ │
  1208. │ {CM.Glyphs.LeftBracket} Press me! {CM.Glyphs.RightBracket} │
  1209. │ │
  1210. │ │
  1211. │ │
  1212. └──────────────────────────────────────┘
  1213. ";
  1214. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1215. Application.End (runState);
  1216. }
  1217. [Fact, TestRespondersDisposed]
  1218. public void Dispose_View ()
  1219. {
  1220. var view = new View ();
  1221. Assert.NotNull (view.Margin);
  1222. Assert.NotNull (view.Border);
  1223. Assert.NotNull (view.Padding);
  1224. #if DEBUG_IDISPOSABLE
  1225. Assert.Equal (4, Responder.Instances.Count);
  1226. #endif
  1227. view.Dispose ();
  1228. Assert.Null (view.Margin);
  1229. Assert.Null (view.Border);
  1230. Assert.Null (view.Padding);
  1231. }
  1232. }
  1233. }