ViewTests.cs 42 KB

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