ViewTests.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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.Draw ();
  579. Assert.True (RunesCount () == 0);
  580. win.Visible = true;
  581. win.FocusFirst ();
  582. Assert.True (button.HasFocus);
  583. Assert.True (win.HasFocus);
  584. top.Draw ();
  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. e.Cancel = true;
  645. };
  646. Application.Top.Add (view);
  647. Application.Begin (Application.Top);
  648. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  649. var expected = @"
  650. ┌──────────────────┐
  651. │012345678910111213│
  652. │012345678910111213│
  653. │012345678910111213│
  654. │012345678910111213│
  655. │012345678910111213│
  656. │012345678910111213│
  657. │012345678910111213│
  658. │012345678910111213│
  659. └──────────────────┘
  660. ";
  661. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  662. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  663. view.Clear (view.Frame);
  664. expected = @"
  665. ";
  666. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  667. Assert.Equal (Rect.Empty, pos);
  668. }
  669. [Fact, AutoInitShutdown]
  670. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  671. {
  672. var view = new FrameView () {
  673. Width = Dim.Fill (),
  674. Height = Dim.Fill ()
  675. };
  676. view.DrawContent += (s, e) => {
  677. var savedClip = Application.Driver.Clip;
  678. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width, view.Bounds.Height);
  679. for (int row = 0; row < view.Bounds.Height; row++) {
  680. Application.Driver.Move (1, row + 1);
  681. for (int col = 0; col < view.Bounds.Width; col++) {
  682. Application.Driver.AddStr ($"{col}");
  683. }
  684. }
  685. Application.Driver.Clip = savedClip;
  686. e.Cancel = true;
  687. };
  688. Application.Top.Add (view);
  689. Application.Begin (Application.Top);
  690. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  691. var expected = @"
  692. ┌──────────────────┐
  693. │012345678910111213│
  694. │012345678910111213│
  695. │012345678910111213│
  696. │012345678910111213│
  697. │012345678910111213│
  698. │012345678910111213│
  699. │012345678910111213│
  700. │012345678910111213│
  701. └──────────────────┘
  702. ";
  703. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  704. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  705. view.Clear (view.Frame);
  706. expected = @"
  707. ";
  708. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  709. Assert.Equal (Rect.Empty, pos);
  710. }
  711. [Fact, AutoInitShutdown]
  712. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  713. {
  714. var text = "Say Hello 你";
  715. var horizontalView = new View () {
  716. Text = text,
  717. AutoSize = true,
  718. HotKeySpecifier = '_'
  719. };
  720. var verticalView = new View () {
  721. Text = text,
  722. AutoSize = true,
  723. HotKeySpecifier = '_',
  724. TextDirection = TextDirection.TopBottom_LeftRight
  725. };
  726. Application.Top.Add (horizontalView, verticalView);
  727. Application.Begin (Application.Top);
  728. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  729. Assert.True (horizontalView.AutoSize);
  730. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  731. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  732. //Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  733. //Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  734. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  735. Assert.True (verticalView.AutoSize);
  736. // BUGBUG: v2 - Autosize is broken; disabling this test
  737. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  738. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  739. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  740. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  741. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  742. text = "Say He_llo 你";
  743. horizontalView.Text = text;
  744. verticalView.Text = text;
  745. Assert.True (horizontalView.AutoSize);
  746. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  747. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  748. //Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  749. //Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  750. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  751. Assert.True (verticalView.AutoSize);
  752. // BUGBUG: v2 - Autosize is broken; disabling this test
  753. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  754. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  755. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  756. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  757. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  758. }
  759. [Fact]
  760. public void IsAdded_Added_Removed ()
  761. {
  762. var top = new Toplevel ();
  763. var view = new View ();
  764. Assert.False (view.IsAdded);
  765. top.Add (view);
  766. Assert.True (view.IsAdded);
  767. top.Remove (view);
  768. Assert.False (view.IsAdded);
  769. }
  770. [Fact, AutoInitShutdown]
  771. public void Visible_Clear_The_View_Output ()
  772. {
  773. var label = new Label ("Testing visibility.");
  774. var win = new Window ();
  775. win.Add (label);
  776. var top = Application.Top;
  777. top.Add (win);
  778. Application.Begin (top);
  779. Assert.True (label.Visible);
  780. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  781. TestHelpers.AssertDriverContentsWithFrameAre (@"
  782. ┌────────────────────────────┐
  783. │Testing visibility. │
  784. │ │
  785. │ │
  786. └────────────────────────────┘
  787. ", output);
  788. label.Visible = false;
  789. TestHelpers.AssertDriverContentsWithFrameAre (@"
  790. ┌────────────────────────────┐
  791. │ │
  792. │ │
  793. │ │
  794. └────────────────────────────┘
  795. ", output);
  796. }
  797. [Fact, AutoInitShutdown]
  798. public void DrawContentComplete_Event_Is_Always_Called ()
  799. {
  800. var viewCalled = false;
  801. var tvCalled = false;
  802. var view = new View ("View") { Width = 10, Height = 10 };
  803. view.DrawContentComplete += (s, e) => viewCalled = true;
  804. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  805. tv.DrawContentComplete += (s, e) => tvCalled = true;
  806. Application.Top.Add (view, tv);
  807. Application.Begin (Application.Top);
  808. Assert.True (viewCalled);
  809. Assert.True (tvCalled);
  810. }
  811. [Fact, AutoInitShutdown]
  812. public void GetNormalColor_ColorScheme ()
  813. {
  814. var view = new View { ColorScheme = Colors.Base };
  815. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  816. view.Enabled = false;
  817. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  818. }
  819. [Fact, AutoInitShutdown]
  820. public void GetHotNormalColor_ColorScheme ()
  821. {
  822. var view = new View { ColorScheme = Colors.Base };
  823. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  824. view.Enabled = false;
  825. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  826. }
  827. [Theory, AutoInitShutdown]
  828. [InlineData (true)]
  829. [InlineData (false)]
  830. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  831. {
  832. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  833. var v = label == true ?
  834. new Label (new string ('c', 100)) {
  835. Width = Dim.Fill (),
  836. } :
  837. (View)new TextView () {
  838. Height = 1,
  839. Text = new string ('c', 100),
  840. Width = Dim.Fill ()
  841. };
  842. root.Add (v);
  843. Application.Top.Add (root);
  844. Application.Begin (Application.Top);
  845. if (label) {
  846. Assert.True (v.AutoSize);
  847. Assert.False (v.CanFocus);
  848. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  849. } else {
  850. Assert.False (v.AutoSize);
  851. Assert.True (v.CanFocus);
  852. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  853. }
  854. TestHelpers.AssertDriverContentsWithFrameAre (@"
  855. cccccccccccccccccccc", output);
  856. var attributes = new Attribute [] {
  857. Colors.TopLevel.Normal,
  858. Colors.Base.Normal,
  859. Colors.Base.Focus
  860. };
  861. if (label) {
  862. TestHelpers.AssertDriverColorsAre (@"
  863. 111111111111111111110
  864. 111111111111111111110", attributes);
  865. } else {
  866. TestHelpers.AssertDriverColorsAre (@"
  867. 222222222222222222220
  868. 111111111111111111110", attributes);
  869. }
  870. if (label) {
  871. root.CanFocus = true;
  872. v.CanFocus = true;
  873. Assert.False (v.HasFocus);
  874. v.SetFocus ();
  875. Assert.True (v.HasFocus);
  876. Application.Refresh ();
  877. TestHelpers.AssertDriverColorsAre (@"
  878. 222222222222222222220
  879. 111111111111111111110", attributes);
  880. }
  881. }
  882. public class DerivedView : View {
  883. public DerivedView ()
  884. {
  885. CanFocus = true;
  886. }
  887. public bool IsKeyDown { get; set; }
  888. public bool IsKeyPress { get; set; }
  889. public bool IsKeyUp { get; set; }
  890. public override ustring Text { get; set; }
  891. public override bool OnKeyDown (KeyEvent keyEvent)
  892. {
  893. IsKeyDown = true;
  894. return true;
  895. }
  896. public override bool ProcessKey (KeyEvent keyEvent)
  897. {
  898. IsKeyPress = true;
  899. return true;
  900. }
  901. public override bool OnKeyUp (KeyEvent keyEvent)
  902. {
  903. IsKeyUp = true;
  904. return true;
  905. }
  906. public override void OnDrawContent (Rect contentArea)
  907. {
  908. var idx = 0;
  909. // BUGBUG: v2 - this should use Boudns, not Frame
  910. for (int r = 0; r < Frame.Height; r++) {
  911. for (int c = 0; c < Frame.Width; c++) {
  912. if (idx < Text.Length) {
  913. var rune = Text [idx];
  914. if (rune != '\n') {
  915. AddRune (c, r, Text [idx]);
  916. }
  917. idx++;
  918. if (rune == '\n') {
  919. break;
  920. }
  921. }
  922. }
  923. }
  924. ClearLayoutNeeded ();
  925. ClearNeedsDisplay ();
  926. }
  927. }
  928. [Fact, AutoInitShutdown]
  929. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  930. {
  931. var label = new Label ("At 0,0");
  932. var view = new DerivedView () {
  933. X = 2,
  934. Y = 2,
  935. Width = 30,
  936. Height = 2,
  937. Text = "A text with some long width\n and also with two lines."
  938. };
  939. var top = Application.Top;
  940. top.Add (label, view);
  941. Application.Begin (top);
  942. top.Draw ();
  943. TestHelpers.AssertDriverContentsWithFrameAre (@"
  944. At 0,0
  945. A text with some long width
  946. and also with two lines. ", output);
  947. view.Frame = new Rect (1, 1, 10, 1);
  948. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  949. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  950. view.LayoutStyle = LayoutStyle.Absolute;
  951. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  952. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  953. top.Draw ();
  954. TestHelpers.AssertDriverContentsWithFrameAre (@"
  955. At 0,0
  956. A text wit", output);
  957. }
  958. [Fact, AutoInitShutdown]
  959. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  960. {
  961. var label = new Label ("At 0,0");
  962. var view = new DerivedView () {
  963. X = 2,
  964. Y = 2,
  965. Width = 30,
  966. Height = 2,
  967. Text = "A text with some long width\n and also with two lines."
  968. };
  969. var top = Application.Top;
  970. top.Add (label, view);
  971. Application.Begin (top);
  972. top.Draw ();
  973. TestHelpers.AssertDriverContentsWithFrameAre (@"
  974. At 0,0
  975. A text with some long width
  976. and also with two lines. ", output);
  977. view.X = 1;
  978. view.Y = 1;
  979. view.Width = 10;
  980. view.Height = 1;
  981. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  982. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  983. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  984. top.Draw ();
  985. TestHelpers.AssertDriverContentsWithFrameAre (@"
  986. At 0,0
  987. A text wit", output);
  988. }
  989. [Fact, AutoInitShutdown]
  990. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  991. {
  992. var label = new Label ("At 0,0");
  993. var view = new DerivedView () {
  994. X = 2,
  995. Y = 2,
  996. Width = 30,
  997. Height = 2,
  998. Text = "A text with some long width\n and also with two lines."
  999. };
  1000. var top = Application.Top;
  1001. top.Add (label, view);
  1002. Application.Begin (top);
  1003. view.Draw ();
  1004. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1005. At 0,0
  1006. A text with some long width
  1007. and also with two lines. ", output);
  1008. view.Frame = new Rect (1, 1, 10, 1);
  1009. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1010. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1011. view.LayoutStyle = LayoutStyle.Absolute;
  1012. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1013. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1014. view.Draw ();
  1015. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1016. At 0,0
  1017. A text wit
  1018. A text with some long width
  1019. and also with two lines. ", output);
  1020. }
  1021. [Fact, AutoInitShutdown]
  1022. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  1023. {
  1024. var label = new Label ("At 0,0");
  1025. var view = new DerivedView () {
  1026. X = 2,
  1027. Y = 2,
  1028. Width = 30,
  1029. Height = 2,
  1030. Text = "A text with some long width\n and also with two lines."
  1031. };
  1032. var top = Application.Top;
  1033. top.Add (label, view);
  1034. Application.Begin (top);
  1035. view.Draw ();
  1036. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1037. At 0,0
  1038. A text with some long width
  1039. and also with two lines. ", output);
  1040. view.X = 1;
  1041. view.Y = 1;
  1042. view.Width = 10;
  1043. view.Height = 1;
  1044. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  1045. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1046. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1047. view.Draw ();
  1048. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1049. At 0,0
  1050. A text wit
  1051. A text with some long width
  1052. and also with two lines. ", output);
  1053. }
  1054. [Fact, AutoInitShutdown]
  1055. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1056. {
  1057. var label = new Label ("At 0,0");
  1058. var view = new DerivedView () {
  1059. X = 2,
  1060. Y = 2,
  1061. Width = 30,
  1062. Height = 2,
  1063. Text = "A text with some long width\n and also with two lines."
  1064. };
  1065. var top = Application.Top;
  1066. top.Add (label, view);
  1067. Application.Begin (top);
  1068. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1069. At 0,0
  1070. A text with some long width
  1071. and also with two lines. ", output);
  1072. view.Frame = new Rect (3, 3, 10, 1);
  1073. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1074. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  1075. view.LayoutStyle = LayoutStyle.Absolute;
  1076. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1077. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1078. top.Draw ();
  1079. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1080. At 0,0
  1081. A text wit", output);
  1082. }
  1083. [Fact, AutoInitShutdown]
  1084. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1085. {
  1086. var label = new Label ("At 0,0");
  1087. var view = new DerivedView () {
  1088. X = 2,
  1089. Y = 2,
  1090. Width = 30,
  1091. Height = 2,
  1092. Text = "A text with some long width\n and also with two lines."
  1093. };
  1094. var top = Application.Top;
  1095. top.Add (label, view);
  1096. Application.Begin (top);
  1097. top.Draw ();
  1098. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1099. At 0,0
  1100. A text with some long width
  1101. and also with two lines. ", output);
  1102. view.X = 3;
  1103. view.Y = 3;
  1104. view.Width = 10;
  1105. view.Height = 1;
  1106. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1107. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1108. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1109. top.Draw ();
  1110. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1111. At 0,0
  1112. A text wit", output);
  1113. }
  1114. [Fact, AutoInitShutdown]
  1115. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  1116. {
  1117. var label = new Label ("At 0,0");
  1118. var view = new DerivedView () {
  1119. X = 2,
  1120. Y = 2,
  1121. Width = 30,
  1122. Height = 2,
  1123. Text = "A text with some long width\n and also with two lines."
  1124. };
  1125. var top = Application.Top;
  1126. top.Add (label, view);
  1127. Application.Begin (top);
  1128. view.Draw ();
  1129. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1130. At 0,0
  1131. A text with some long width
  1132. and also with two lines. ", output);
  1133. view.Frame = new Rect (3, 3, 10, 1);
  1134. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1135. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  1136. view.Draw ();
  1137. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1138. At 0,0
  1139. A text with some long width
  1140. A text witith two lines. ", output);
  1141. }
  1142. [Fact, AutoInitShutdown]
  1143. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  1144. {
  1145. var label = new Label ("At 0,0");
  1146. var view = new DerivedView () {
  1147. X = 2,
  1148. Y = 2,
  1149. Width = 30,
  1150. Height = 2,
  1151. Text = "A text with some long width\n and also with two lines."
  1152. };
  1153. var top = Application.Top;
  1154. top.Add (label, view);
  1155. Application.Begin (top);
  1156. view.Draw ();
  1157. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1158. At 0,0
  1159. A text with some long width
  1160. and also with two lines. ", output);
  1161. view.X = 3;
  1162. view.Y = 3;
  1163. view.Width = 10;
  1164. view.Height = 1;
  1165. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  1166. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  1167. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  1168. view.Draw ();
  1169. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1170. At 0,0
  1171. A text with some long width
  1172. A text witith two lines. ", output);
  1173. }
  1174. [Fact, AutoInitShutdown]
  1175. public void Test_Nested_Views_With_Height_Equal_To_One ()
  1176. {
  1177. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  1178. var top = new View () { Width = Dim.Fill (), Height = 1 };
  1179. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  1180. top.Add (new Label ("111"));
  1181. v.Add (top);
  1182. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  1183. bottom.Add (new Label ("222"));
  1184. v.Add (bottom);
  1185. v.LayoutSubviews ();
  1186. v.Draw ();
  1187. string looksLike =
  1188. @"
  1189. 111
  1190. ───────────
  1191. 222";
  1192. TestHelpers.AssertDriverContentsAre (looksLike, output);
  1193. }
  1194. [Fact]
  1195. [AutoInitShutdown]
  1196. public void Frame_Set_After_Initialize_Update_NeededDisplay ()
  1197. {
  1198. var frame = new FrameView ();
  1199. var label = new Label ("This should be the first line.") {
  1200. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  1201. ColorScheme = Colors.Menu,
  1202. Width = Dim.Fill (),
  1203. X = Pos.Center (),
  1204. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  1205. };
  1206. var button = new Button ("Press me!") {
  1207. X = Pos.Center (),
  1208. Y = Pos.Center ()
  1209. };
  1210. frame.Add (label, button);
  1211. frame.X = Pos.Center ();
  1212. frame.Y = Pos.Center ();
  1213. frame.Width = 40;
  1214. frame.Height = 8;
  1215. var top = Application.Top;
  1216. top.Add (frame);
  1217. Application.Begin (top);
  1218. top.LayoutComplete += (s, e) => {
  1219. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  1220. };
  1221. frame.LayoutComplete += (s, e) => {
  1222. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  1223. };
  1224. label.LayoutComplete += (s, e) => {
  1225. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplay);
  1226. };
  1227. button.LayoutComplete += (s, e) => {
  1228. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  1229. };
  1230. Assert.True (label.AutoSize);
  1231. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  1232. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  1233. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  1234. frame.Frame.Left, frame.Frame.Top,
  1235. frame.Frame.Right, frame.Frame.Bottom));
  1236. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  1237. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  1238. var expected = @"
  1239. ┌──────────────────────────────────────┐
  1240. │ This should be the first line. │
  1241. │ │
  1242. │ [ Press me! ] │
  1243. │ │
  1244. │ │
  1245. │ │
  1246. └──────────────────────────────────────┘
  1247. ";
  1248. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1249. }
  1250. [Fact]
  1251. public void Dispose_View ()
  1252. {
  1253. var view = new View ();
  1254. Assert.NotNull (view.Margin);
  1255. Assert.NotNull (view.Border);
  1256. Assert.NotNull (view.Padding);
  1257. view.Dispose ();
  1258. Assert.Null (view.Margin);
  1259. Assert.Null (view.Border);
  1260. Assert.Null (view.Padding);
  1261. }
  1262. }
  1263. }