ViewTests.cs 41 KB

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