ViewTests.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Views {
  11. public class ViewTests {
  12. [Fact]
  13. public void New_Initializes ()
  14. {
  15. // Parameterless
  16. var r = new View ();
  17. Assert.NotNull (r);
  18. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  19. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  20. Assert.False (r.CanFocus);
  21. Assert.False (r.HasFocus);
  22. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  23. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  24. Assert.Null (r.Focused);
  25. Assert.Null (r.ColorScheme);
  26. Assert.Equal (Dim.Sized (0), r.Width);
  27. Assert.Equal (Dim.Sized (0), r.Height);
  28. // FIXED: Pos needs equality implemented
  29. Assert.Equal (Pos.At (0), r.X);
  30. Assert.Equal (Pos.At (0), r.Y);
  31. Assert.False (r.IsCurrentTop);
  32. Assert.Empty (r.Id);
  33. Assert.Empty (r.Subviews);
  34. Assert.False (r.WantContinuousButtonPressed);
  35. Assert.False (r.WantMousePositionReports);
  36. Assert.Null (r.SuperView);
  37. Assert.Null (r.MostFocused);
  38. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  39. // Empty Rect
  40. r = new View (Rect.Empty);
  41. Assert.NotNull (r);
  42. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  43. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  44. Assert.False (r.CanFocus);
  45. Assert.False (r.HasFocus);
  46. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  47. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  48. Assert.Null (r.Focused);
  49. Assert.Null (r.ColorScheme);
  50. Assert.NotNull (r.Width); // All view Dim are initialized now,
  51. Assert.NotNull (r.Height); // avoiding Dim errors.
  52. Assert.NotNull (r.X); // All view Pos are initialized now,
  53. Assert.NotNull (r.Y); // avoiding Pos errors.
  54. Assert.False (r.IsCurrentTop);
  55. Assert.Empty (r.Id);
  56. Assert.Empty (r.Subviews);
  57. Assert.False (r.WantContinuousButtonPressed);
  58. Assert.False (r.WantMousePositionReports);
  59. Assert.Null (r.SuperView);
  60. Assert.Null (r.MostFocused);
  61. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  62. // Rect with values
  63. r = new View (new Rect (1, 2, 3, 4));
  64. Assert.NotNull (r);
  65. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  66. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  67. Assert.False (r.CanFocus);
  68. Assert.False (r.HasFocus);
  69. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  70. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  71. Assert.Null (r.Focused);
  72. Assert.Null (r.ColorScheme);
  73. Assert.NotNull (r.Width);
  74. Assert.NotNull (r.Height);
  75. Assert.NotNull (r.X);
  76. Assert.NotNull (r.Y);
  77. Assert.False (r.IsCurrentTop);
  78. Assert.Empty (r.Id);
  79. Assert.Empty (r.Subviews);
  80. Assert.False (r.WantContinuousButtonPressed);
  81. Assert.False (r.WantMousePositionReports);
  82. Assert.Null (r.SuperView);
  83. Assert.Null (r.MostFocused);
  84. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  85. // Initializes a view with a vertical direction
  86. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  87. Assert.NotNull (r);
  88. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  89. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  90. Assert.False (r.CanFocus);
  91. Assert.False (r.HasFocus);
  92. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  93. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  94. Assert.Null (r.Focused);
  95. Assert.Null (r.ColorScheme);
  96. Assert.NotNull (r.Width); // All view Dim are initialized now,
  97. Assert.NotNull (r.Height); // avoiding Dim errors.
  98. Assert.NotNull (r.X); // All view Pos are initialized now,
  99. Assert.NotNull (r.Y); // avoiding Pos errors.
  100. Assert.False (r.IsCurrentTop);
  101. Assert.Empty (r.Id);
  102. Assert.Empty (r.Subviews);
  103. Assert.False (r.WantContinuousButtonPressed);
  104. Assert.False (r.WantMousePositionReports);
  105. Assert.Null (r.SuperView);
  106. Assert.Null (r.MostFocused);
  107. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  108. }
  109. [Fact]
  110. public void New_Methods_Return_False ()
  111. {
  112. var r = new View ();
  113. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  114. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  115. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  116. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  119. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  120. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  121. Assert.False (r.OnEnter (new View ()));
  122. Assert.False (r.OnLeave (new View ()));
  123. // TODO: Add more
  124. }
  125. [Fact]
  126. public void TopologicalSort_Missing_Add ()
  127. {
  128. var root = new View ();
  129. var sub1 = new View ();
  130. root.Add (sub1);
  131. var sub2 = new View ();
  132. sub1.Width = Dim.Width (sub2);
  133. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  134. sub2.Width = Dim.Width (sub1);
  135. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  136. }
  137. [Fact]
  138. public void TopologicalSort_Recursive_Ref ()
  139. {
  140. var root = new View ();
  141. var sub1 = new View ();
  142. root.Add (sub1);
  143. var sub2 = new View ();
  144. root.Add (sub2);
  145. sub2.Width = Dim.Width (sub2);
  146. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  147. }
  148. [Fact]
  149. public void Added_Removed ()
  150. {
  151. var v = new View (new Rect (0, 0, 10, 24));
  152. var t = new View ();
  153. v.Added += (View e) => {
  154. Assert.True (v.SuperView == e);
  155. };
  156. v.Removed += (View e) => {
  157. Assert.True (v.SuperView == null);
  158. };
  159. t.Add (v);
  160. Assert.True (t.Subviews.Count == 1);
  161. t.Remove (v);
  162. Assert.True (t.Subviews.Count == 0);
  163. }
  164. [Fact]
  165. public void Subviews_TabIndexes_AreEqual ()
  166. {
  167. var r = new View ();
  168. var v1 = new View () { CanFocus = true };
  169. var v2 = new View () { CanFocus = true };
  170. var v3 = new View () { CanFocus = true };
  171. r.Add (v1, v2, v3);
  172. Assert.True (r.Subviews.IndexOf (v1) == 0);
  173. Assert.True (r.Subviews.IndexOf (v2) == 1);
  174. Assert.True (r.Subviews.IndexOf (v3) == 2);
  175. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  176. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  177. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  178. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  179. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  180. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  181. }
  182. [Fact]
  183. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  184. {
  185. var r = new View ();
  186. var v1 = new View () { CanFocus = true };
  187. var v2 = new View () { CanFocus = true };
  188. var v3 = new View () { CanFocus = true };
  189. r.Add (v1, v2, v3);
  190. r.BringSubviewToFront (v1);
  191. Assert.True (r.Subviews.IndexOf (v1) == 2);
  192. Assert.True (r.Subviews.IndexOf (v2) == 0);
  193. Assert.True (r.Subviews.IndexOf (v3) == 1);
  194. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  195. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  196. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  197. }
  198. [Fact]
  199. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  200. {
  201. var r = new View ();
  202. var v1 = new View () { CanFocus = true };
  203. var v2 = new View () { CanFocus = true };
  204. var v3 = new View () { CanFocus = true };
  205. r.Add (v1, v2, v3);
  206. r.BringSubviewForward (v1);
  207. Assert.True (r.Subviews.IndexOf (v1) == 1);
  208. Assert.True (r.Subviews.IndexOf (v2) == 0);
  209. Assert.True (r.Subviews.IndexOf (v3) == 2);
  210. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  211. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  212. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  213. }
  214. [Fact]
  215. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  216. {
  217. var r = new View ();
  218. var v1 = new View () { CanFocus = true };
  219. var v2 = new View () { CanFocus = true };
  220. var v3 = new View () { CanFocus = true };
  221. r.Add (v1, v2, v3);
  222. r.SendSubviewToBack (v3);
  223. Assert.True (r.Subviews.IndexOf (v1) == 1);
  224. Assert.True (r.Subviews.IndexOf (v2) == 2);
  225. Assert.True (r.Subviews.IndexOf (v3) == 0);
  226. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  227. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  228. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  229. }
  230. [Fact]
  231. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  232. {
  233. var r = new View ();
  234. var v1 = new View () { CanFocus = true };
  235. var v2 = new View () { CanFocus = true };
  236. var v3 = new View () { CanFocus = true };
  237. r.Add (v1, v2, v3);
  238. r.SendSubviewBackwards (v3);
  239. Assert.True (r.Subviews.IndexOf (v1) == 0);
  240. Assert.True (r.Subviews.IndexOf (v2) == 2);
  241. Assert.True (r.Subviews.IndexOf (v3) == 1);
  242. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  243. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  244. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  245. }
  246. [Fact]
  247. public void TabIndex_Set_CanFocus_ValidValues ()
  248. {
  249. var r = new View ();
  250. var v1 = new View () { CanFocus = true };
  251. var v2 = new View () { CanFocus = true };
  252. var v3 = new View () { CanFocus = true };
  253. r.Add (v1, v2, v3);
  254. v1.TabIndex = 1;
  255. Assert.True (r.Subviews.IndexOf (v1) == 0);
  256. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  257. v1.TabIndex = 2;
  258. Assert.True (r.Subviews.IndexOf (v1) == 0);
  259. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  260. }
  261. [Fact]
  262. public void TabIndex_Set_CanFocus_HigherValues ()
  263. {
  264. var r = new View ();
  265. var v1 = new View () { CanFocus = true };
  266. var v2 = new View () { CanFocus = true };
  267. var v3 = new View () { CanFocus = true };
  268. r.Add (v1, v2, v3);
  269. v1.TabIndex = 3;
  270. Assert.True (r.Subviews.IndexOf (v1) == 0);
  271. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  272. }
  273. [Fact]
  274. public void TabIndex_Set_CanFocus_LowerValues ()
  275. {
  276. var r = new View ();
  277. var v1 = new View () { CanFocus = true };
  278. var v2 = new View () { CanFocus = true };
  279. var v3 = new View () { CanFocus = true };
  280. r.Add (v1, v2, v3);
  281. v1.TabIndex = -1;
  282. Assert.True (r.Subviews.IndexOf (v1) == 0);
  283. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  284. }
  285. [Fact]
  286. public void TabIndex_Set_CanFocus_False ()
  287. {
  288. var r = new View ();
  289. var v1 = new View () { CanFocus = true };
  290. var v2 = new View () { CanFocus = true };
  291. var v3 = new View () { CanFocus = true };
  292. r.Add (v1, v2, v3);
  293. v1.CanFocus = false;
  294. v1.TabIndex = 0;
  295. Assert.True (r.Subviews.IndexOf (v1) == 0);
  296. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  297. Assert.Equal (-1, v1.TabIndex);
  298. }
  299. [Fact]
  300. public void TabIndex_Set_CanFocus_False_To_True ()
  301. {
  302. var r = new View ();
  303. var v1 = new View ();
  304. var v2 = new View () { CanFocus = true };
  305. var v3 = new View () { CanFocus = true };
  306. r.Add (v1, v2, v3);
  307. v1.CanFocus = true;
  308. v1.TabIndex = 1;
  309. Assert.True (r.Subviews.IndexOf (v1) == 0);
  310. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  311. }
  312. [Fact]
  313. public void TabStop_And_CanFocus_Are_All_True ()
  314. {
  315. var r = new View ();
  316. var v1 = new View () { CanFocus = true };
  317. var v2 = new View () { CanFocus = true };
  318. var v3 = new View () { CanFocus = true };
  319. r.Add (v1, v2, v3);
  320. r.FocusNext ();
  321. Assert.True (v1.HasFocus);
  322. Assert.False (v2.HasFocus);
  323. Assert.False (v3.HasFocus);
  324. r.FocusNext ();
  325. Assert.False (v1.HasFocus);
  326. Assert.True (v2.HasFocus);
  327. Assert.False (v3.HasFocus);
  328. r.FocusNext ();
  329. Assert.False (v1.HasFocus);
  330. Assert.False (v2.HasFocus);
  331. Assert.True (v3.HasFocus);
  332. }
  333. [Fact]
  334. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  335. {
  336. var r = new View ();
  337. var v1 = new View ();
  338. var v2 = new View ();
  339. var v3 = new View ();
  340. r.Add (v1, v2, v3);
  341. r.FocusNext ();
  342. Assert.False (v1.HasFocus);
  343. Assert.False (v2.HasFocus);
  344. Assert.False (v3.HasFocus);
  345. r.FocusNext ();
  346. Assert.False (v1.HasFocus);
  347. Assert.False (v2.HasFocus);
  348. Assert.False (v3.HasFocus);
  349. r.FocusNext ();
  350. Assert.False (v1.HasFocus);
  351. Assert.False (v2.HasFocus);
  352. Assert.False (v3.HasFocus);
  353. }
  354. [Fact]
  355. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  356. {
  357. var r = new View ();
  358. var v1 = new View () { CanFocus = true, TabStop = false };
  359. var v2 = new View () { CanFocus = true, TabStop = false };
  360. var v3 = new View () { CanFocus = true, TabStop = false };
  361. r.Add (v1, v2, v3);
  362. r.FocusNext ();
  363. Assert.False (v1.HasFocus);
  364. Assert.False (v2.HasFocus);
  365. Assert.False (v3.HasFocus);
  366. r.FocusNext ();
  367. Assert.False (v1.HasFocus);
  368. Assert.False (v2.HasFocus);
  369. Assert.False (v3.HasFocus);
  370. r.FocusNext ();
  371. Assert.False (v1.HasFocus);
  372. Assert.False (v2.HasFocus);
  373. Assert.False (v3.HasFocus);
  374. }
  375. [Fact]
  376. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  377. {
  378. var r = new View ();
  379. var v1 = new View () { CanFocus = true, TabStop = false };
  380. var v2 = new View () { CanFocus = false, TabStop = true };
  381. var v3 = new View () { CanFocus = false, TabStop = false };
  382. r.Add (v1, v2, v3);
  383. r.FocusNext ();
  384. Assert.False (v1.HasFocus);
  385. Assert.False (v2.HasFocus);
  386. Assert.False (v3.HasFocus);
  387. r.FocusNext ();
  388. Assert.False (v1.HasFocus);
  389. Assert.False (v2.HasFocus);
  390. Assert.False (v3.HasFocus);
  391. r.FocusNext ();
  392. Assert.False (v1.HasFocus);
  393. Assert.False (v2.HasFocus);
  394. Assert.False (v3.HasFocus);
  395. }
  396. [Fact]
  397. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  398. {
  399. var r = new View ();
  400. var v1 = new View ();
  401. var v2 = new View ();
  402. var v3 = new View ();
  403. r.Add (v1, v2, v3);
  404. r.FocusNext ();
  405. Assert.False (v1.HasFocus);
  406. Assert.False (v2.HasFocus);
  407. Assert.False (v3.HasFocus);
  408. v1.CanFocus = true;
  409. r.FocusNext ();
  410. Assert.True (v1.HasFocus);
  411. Assert.False (v2.HasFocus);
  412. Assert.False (v3.HasFocus);
  413. v2.CanFocus = true;
  414. r.FocusNext ();
  415. Assert.False (v1.HasFocus);
  416. Assert.True (v2.HasFocus);
  417. Assert.False (v3.HasFocus);
  418. v3.CanFocus = true;
  419. r.FocusNext ();
  420. Assert.False (v1.HasFocus);
  421. Assert.False (v2.HasFocus);
  422. Assert.True (v3.HasFocus);
  423. }
  424. [Fact]
  425. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  426. {
  427. var r = new View ();
  428. var v1 = new View () { CanFocus = true, TabStop = false };
  429. var v2 = new View () { CanFocus = true, TabStop = false };
  430. var v3 = new View () { CanFocus = true, TabStop = false };
  431. r.Add (v1, v2, v3);
  432. r.FocusNext ();
  433. Assert.False (v1.HasFocus);
  434. Assert.False (v2.HasFocus);
  435. Assert.False (v3.HasFocus);
  436. v1.TabStop = true;
  437. r.FocusNext ();
  438. Assert.True (v1.HasFocus);
  439. Assert.False (v2.HasFocus);
  440. Assert.False (v3.HasFocus);
  441. v2.TabStop = true;
  442. r.FocusNext ();
  443. Assert.False (v1.HasFocus);
  444. Assert.True (v2.HasFocus);
  445. Assert.False (v3.HasFocus);
  446. v3.TabStop = true;
  447. r.FocusNext ();
  448. Assert.False (v1.HasFocus);
  449. Assert.False (v2.HasFocus);
  450. Assert.True (v3.HasFocus);
  451. }
  452. [Fact]
  453. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  454. {
  455. var r = new View ();
  456. var v1 = new View ("1");
  457. var v2 = new View ("2");
  458. var v3 = new View ("3");
  459. r.Add (v1, v2, v3);
  460. v2.CanFocus = true;
  461. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  462. Assert.Equal (0, v2.TabIndex);
  463. Assert.True (v2.TabStop);
  464. v1.CanFocus = true;
  465. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  466. Assert.Equal (1, v1.TabIndex);
  467. Assert.True (v1.TabStop);
  468. v1.TabIndex = 2;
  469. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  470. Assert.Equal (1, v1.TabIndex);
  471. v3.CanFocus = true;
  472. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  473. Assert.Equal (1, v1.TabIndex);
  474. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  475. Assert.Equal (2, v3.TabIndex);
  476. Assert.True (v3.TabStop);
  477. v2.CanFocus = false;
  478. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  479. Assert.Equal (1, v1.TabIndex);
  480. Assert.True (v1.TabStop);
  481. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  482. Assert.Equal (-1, v2.TabIndex);
  483. Assert.False (v2.TabStop);
  484. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  485. Assert.Equal (2, v3.TabIndex);
  486. Assert.True (v3.TabStop);
  487. }
  488. [Fact]
  489. public void Initialized_Event_Comparing_With_Added_Event ()
  490. {
  491. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  492. var t = new Toplevel () { Id = "0", };
  493. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  494. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  495. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  496. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  497. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  498. w.Added += (e) => {
  499. Assert.Equal (e.Frame.Width, w.Frame.Width);
  500. Assert.Equal (e.Frame.Height, w.Frame.Height);
  501. };
  502. v1.Added += (e) => {
  503. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  504. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  505. };
  506. v2.Added += (e) => {
  507. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  508. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  509. };
  510. sv1.Added += (e) => {
  511. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  512. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  513. };
  514. t.Initialized += (s, e) => {
  515. tc++;
  516. Assert.Equal (1, tc);
  517. Assert.Equal (1, wc);
  518. Assert.Equal (1, v1c);
  519. Assert.Equal (1, v2c);
  520. Assert.Equal (1, sv1c);
  521. Assert.True (t.CanFocus);
  522. Assert.True (w.CanFocus);
  523. Assert.False (v1.CanFocus);
  524. Assert.False (v2.CanFocus);
  525. Assert.False (sv1.CanFocus);
  526. Application.Refresh ();
  527. };
  528. w.Initialized += (s, e) => {
  529. wc++;
  530. Assert.Equal (t.Frame.Width, w.Frame.Width);
  531. Assert.Equal (t.Frame.Height, w.Frame.Height);
  532. };
  533. v1.Initialized += (s, e) => {
  534. v1c++;
  535. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  536. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  537. };
  538. v2.Initialized += (s, e) => {
  539. v2c++;
  540. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  541. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  542. };
  543. sv1.Initialized += (s, e) => {
  544. sv1c++;
  545. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  546. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  547. Assert.False (sv1.CanFocus);
  548. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  549. Assert.False (sv1.CanFocus);
  550. };
  551. v1.Add (sv1);
  552. w.Add (v1, v2);
  553. t.Add (w);
  554. Application.Iteration = () => {
  555. Application.Refresh ();
  556. t.Running = false;
  557. };
  558. Application.Run (t);
  559. Application.Shutdown ();
  560. Assert.Equal (1, tc);
  561. Assert.Equal (1, wc);
  562. Assert.Equal (1, v1c);
  563. Assert.Equal (1, v2c);
  564. Assert.Equal (1, sv1c);
  565. Assert.True (t.CanFocus);
  566. Assert.True (w.CanFocus);
  567. Assert.False (v1.CanFocus);
  568. Assert.False (v2.CanFocus);
  569. Assert.False (sv1.CanFocus);
  570. v1.CanFocus = true;
  571. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  572. }
  573. [Fact]
  574. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  575. {
  576. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  577. var t = new Toplevel () { Id = "0", };
  578. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  579. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  580. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  581. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  582. t.Initialized += (s, e) => {
  583. tc++;
  584. Assert.Equal (1, tc);
  585. Assert.Equal (1, wc);
  586. Assert.Equal (1, v1c);
  587. Assert.Equal (1, v2c);
  588. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  589. Assert.True (t.CanFocus);
  590. Assert.True (w.CanFocus);
  591. Assert.False (v1.CanFocus);
  592. Assert.False (v2.CanFocus);
  593. Application.Refresh ();
  594. };
  595. w.Initialized += (s, e) => {
  596. wc++;
  597. Assert.Equal (t.Frame.Width, w.Frame.Width);
  598. Assert.Equal (t.Frame.Height, w.Frame.Height);
  599. };
  600. v1.Initialized += (s, e) => {
  601. v1c++;
  602. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  603. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  604. };
  605. v2.Initialized += (s, e) => {
  606. v2c++;
  607. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  608. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  609. };
  610. w.Add (v1, v2);
  611. t.Add (w);
  612. Application.Iteration = () => {
  613. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  614. sv1.Initialized += (s, e) => {
  615. sv1c++;
  616. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  617. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  618. Assert.False (sv1.CanFocus);
  619. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  620. Assert.False (sv1.CanFocus);
  621. };
  622. v1.Add (sv1);
  623. Application.Refresh ();
  624. t.Running = false;
  625. };
  626. Application.Run (t);
  627. Application.Shutdown ();
  628. Assert.Equal (1, tc);
  629. Assert.Equal (1, wc);
  630. Assert.Equal (1, v1c);
  631. Assert.Equal (1, v2c);
  632. Assert.Equal (1, sv1c);
  633. Assert.True (t.CanFocus);
  634. Assert.True (w.CanFocus);
  635. Assert.False (v1.CanFocus);
  636. Assert.False (v2.CanFocus);
  637. }
  638. [Fact]
  639. [AutoInitShutdown]
  640. public void CanFocus_Faced_With_Container ()
  641. {
  642. var t = new Toplevel ();
  643. var w = new Window ();
  644. var f = new FrameView ();
  645. var v = new View () { CanFocus = true };
  646. f.Add (v);
  647. w.Add (f);
  648. t.Add (w);
  649. Assert.True (t.CanFocus);
  650. Assert.True (w.CanFocus);
  651. Assert.True (f.CanFocus);
  652. Assert.True (v.CanFocus);
  653. f.CanFocus = false;
  654. Assert.False (f.CanFocus);
  655. Assert.True (v.CanFocus);
  656. v.CanFocus = false;
  657. Assert.False (f.CanFocus);
  658. Assert.False (v.CanFocus);
  659. v.CanFocus = true;
  660. Assert.False (f.CanFocus);
  661. Assert.True (v.CanFocus);
  662. }
  663. [Fact]
  664. public void CanFocus_Faced_With_Container_Before_Run ()
  665. {
  666. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  667. var t = Application.Top;
  668. var w = new Window ("w");
  669. var f = new FrameView ("f");
  670. var v = new View ("v") { CanFocus = true };
  671. f.Add (v);
  672. w.Add (f);
  673. t.Add (w);
  674. Assert.True (t.CanFocus);
  675. Assert.True (w.CanFocus);
  676. Assert.True (f.CanFocus);
  677. Assert.True (v.CanFocus);
  678. f.CanFocus = false;
  679. Assert.False (f.CanFocus);
  680. Assert.True (v.CanFocus);
  681. v.CanFocus = false;
  682. Assert.False (f.CanFocus);
  683. Assert.False (v.CanFocus);
  684. v.CanFocus = true;
  685. Assert.False (f.CanFocus);
  686. Assert.True (v.CanFocus);
  687. Application.Iteration += () => Application.RequestStop ();
  688. Application.Run ();
  689. Application.Shutdown ();
  690. }
  691. [Fact]
  692. public void CanFocus_Faced_With_Container_After_Run ()
  693. {
  694. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  695. var t = Application.Top;
  696. var w = new Window ("w");
  697. var f = new FrameView ("f");
  698. var v = new View ("v") { CanFocus = true };
  699. f.Add (v);
  700. w.Add (f);
  701. t.Add (w);
  702. t.Ready += () => {
  703. Assert.True (t.CanFocus);
  704. Assert.True (w.CanFocus);
  705. Assert.True (f.CanFocus);
  706. Assert.True (v.CanFocus);
  707. f.CanFocus = false;
  708. Assert.False (f.CanFocus);
  709. Assert.False (v.CanFocus);
  710. v.CanFocus = false;
  711. Assert.False (f.CanFocus);
  712. Assert.False (v.CanFocus);
  713. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  714. Assert.False (f.CanFocus);
  715. Assert.False (v.CanFocus);
  716. f.CanFocus = true;
  717. Assert.True (f.CanFocus);
  718. Assert.True (v.CanFocus);
  719. };
  720. Application.Iteration += () => Application.RequestStop ();
  721. Application.Run ();
  722. Application.Shutdown ();
  723. }
  724. [Fact]
  725. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  726. {
  727. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  728. var t = Application.Top;
  729. var w = new Window ("w");
  730. var f = new FrameView ("f");
  731. var v1 = new View ("v1") { CanFocus = true };
  732. var v2 = new View ("v2") { CanFocus = true };
  733. f.Add (v1, v2);
  734. w.Add (f);
  735. t.Add (w);
  736. t.Ready += () => {
  737. Assert.True (t.CanFocus);
  738. Assert.True (w.CanFocus);
  739. Assert.True (f.CanFocus);
  740. Assert.True (v1.CanFocus);
  741. Assert.True (v2.CanFocus);
  742. w.CanFocus = false;
  743. Assert.False (w.CanFocus);
  744. Assert.False (f.CanFocus);
  745. Assert.False (v1.CanFocus);
  746. Assert.False (v2.CanFocus);
  747. };
  748. Application.Iteration += () => Application.RequestStop ();
  749. Application.Run ();
  750. Application.Shutdown ();
  751. }
  752. [Fact]
  753. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  754. {
  755. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  756. var t = Application.Top;
  757. var w = new Window ("w");
  758. var f = new FrameView ("f");
  759. var v1 = new View ("v1");
  760. var v2 = new View ("v2") { CanFocus = true };
  761. f.Add (v1, v2);
  762. w.Add (f);
  763. t.Add (w);
  764. t.Ready += () => {
  765. Assert.True (t.CanFocus);
  766. Assert.True (w.CanFocus);
  767. Assert.True (f.CanFocus);
  768. Assert.False (v1.CanFocus);
  769. Assert.True (v2.CanFocus);
  770. w.CanFocus = false;
  771. Assert.False (w.CanFocus);
  772. Assert.False (f.CanFocus);
  773. Assert.False (v1.CanFocus);
  774. Assert.False (v2.CanFocus);
  775. w.CanFocus = true;
  776. Assert.True (w.CanFocus);
  777. Assert.True (f.CanFocus);
  778. Assert.False (v1.CanFocus);
  779. Assert.True (v2.CanFocus);
  780. };
  781. Application.Iteration += () => Application.RequestStop ();
  782. Application.Run ();
  783. Application.Shutdown ();
  784. }
  785. [Fact]
  786. public void Navigation_With_Null_Focused_View ()
  787. {
  788. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  789. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  790. Application.Top.Ready += () => {
  791. Assert.Null (Application.Top.Focused);
  792. };
  793. // Keyboard navigation with tab
  794. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  795. Application.Iteration += () => Application.RequestStop ();
  796. Application.Run ();
  797. Application.Shutdown ();
  798. }
  799. [Fact]
  800. public void Multi_Thread_Toplevels ()
  801. {
  802. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  803. var t = Application.Top;
  804. var w = new Window ();
  805. t.Add (w);
  806. int count = 0, count1 = 0, count2 = 0;
  807. bool log = false, log1 = false, log2 = false;
  808. bool fromTopStillKnowFirstIsRunning = false;
  809. bool fromTopStillKnowSecondIsRunning = false;
  810. bool fromFirstStillKnowSecondIsRunning = false;
  811. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  812. count++;
  813. if (count1 == 5) {
  814. log1 = true;
  815. }
  816. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  817. fromTopStillKnowFirstIsRunning = true;
  818. }
  819. if (count1 == 7 && count2 == 7 && count == 8) {
  820. fromTopStillKnowSecondIsRunning = true;
  821. }
  822. if (count == 30) {
  823. Assert.Equal (30, count);
  824. Assert.Equal (20, count1);
  825. Assert.Equal (10, count2);
  826. Assert.True (log);
  827. Assert.True (log1);
  828. Assert.True (log2);
  829. Assert.True (fromTopStillKnowFirstIsRunning);
  830. Assert.True (fromTopStillKnowSecondIsRunning);
  831. Assert.True (fromFirstStillKnowSecondIsRunning);
  832. Application.RequestStop ();
  833. return false;
  834. }
  835. return true;
  836. });
  837. t.Ready += FirstDialogToplevel;
  838. void FirstDialogToplevel ()
  839. {
  840. var od = new OpenDialog ();
  841. od.Ready += SecoundDialogToplevel;
  842. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  843. count1++;
  844. if (count2 == 5) {
  845. log2 = true;
  846. }
  847. if (count2 == 4 && count1 == 5 && count == 5) {
  848. fromFirstStillKnowSecondIsRunning = true;
  849. }
  850. if (count1 == 20) {
  851. Assert.Equal (20, count1);
  852. Application.RequestStop ();
  853. return false;
  854. }
  855. return true;
  856. });
  857. Application.Run (od);
  858. }
  859. void SecoundDialogToplevel ()
  860. {
  861. var d = new Dialog ();
  862. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  863. count2++;
  864. if (count < 30) {
  865. log = true;
  866. }
  867. if (count2 == 10) {
  868. Assert.Equal (10, count2);
  869. Application.RequestStop ();
  870. return false;
  871. }
  872. return true;
  873. });
  874. Application.Run (d);
  875. }
  876. Application.Run ();
  877. Application.Shutdown ();
  878. }
  879. [Fact]
  880. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  881. {
  882. // Object Initializer
  883. var view = new View () {
  884. X = 1,
  885. Y = 2,
  886. Width = 3,
  887. Height = 4
  888. };
  889. Assert.Equal (1, view.X);
  890. Assert.Equal (2, view.Y);
  891. Assert.Equal (3, view.Width);
  892. Assert.Equal (4, view.Height);
  893. Assert.False (view.Frame.IsEmpty);
  894. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  895. Assert.False (view.Bounds.IsEmpty);
  896. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  897. view.LayoutSubviews ();
  898. Assert.Equal (1, view.X);
  899. Assert.Equal (2, view.Y);
  900. Assert.Equal (3, view.Width);
  901. Assert.Equal (4, view.Height);
  902. Assert.False (view.Frame.IsEmpty);
  903. Assert.False (view.Bounds.IsEmpty);
  904. // Default Constructor
  905. view = new View ();
  906. Assert.Equal (0, view.X);
  907. Assert.Equal (0, view.Y);
  908. Assert.Equal (0, view.Width);
  909. Assert.Equal (0, view.Height);
  910. Assert.True (view.Frame.IsEmpty);
  911. Assert.True (view.Bounds.IsEmpty);
  912. // Constructor
  913. view = new View (1, 2, "");
  914. Assert.NotNull (view.X);
  915. Assert.NotNull (view.Y);
  916. Assert.NotNull (view.Width);
  917. Assert.NotNull (view.Height);
  918. Assert.False (view.Frame.IsEmpty);
  919. Assert.True (view.Bounds.IsEmpty);
  920. // Default Constructor and post assignment equivalent to Object Initializer
  921. view = new View ();
  922. view.X = 1;
  923. view.Y = 2;
  924. view.Width = 3;
  925. view.Height = 4;
  926. Assert.Equal (1, view.X);
  927. Assert.Equal (2, view.Y);
  928. Assert.Equal (3, view.Width);
  929. Assert.Equal (4, view.Height);
  930. Assert.False (view.Frame.IsEmpty);
  931. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  932. Assert.False (view.Bounds.IsEmpty);
  933. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  934. }
  935. [Fact]
  936. public void FocusNearestView_Ensure_Focus_Ordered ()
  937. {
  938. var top = new Toplevel ();
  939. var win = new Window ();
  940. var winSubview = new View ("WindowSubview") {
  941. CanFocus = true
  942. };
  943. win.Add (winSubview);
  944. top.Add (win);
  945. var frm = new FrameView ();
  946. var frmSubview = new View ("FrameSubview") {
  947. CanFocus = true
  948. };
  949. frm.Add (frmSubview);
  950. top.Add (frm);
  951. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  952. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  953. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  954. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  955. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  956. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  957. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  958. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  959. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  960. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  961. }
  962. [Fact]
  963. public void KeyPress_Handled_To_True_Prevents_Changes ()
  964. {
  965. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  966. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  967. var top = Application.Top;
  968. var text = new TextField ("");
  969. text.KeyPress += (e) => {
  970. e.Handled = true;
  971. Assert.True (e.Handled);
  972. Assert.Equal (Key.N, e.KeyEvent.Key);
  973. };
  974. top.Add (text);
  975. Application.Iteration += () => {
  976. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  977. Assert.Equal ("", text.Text);
  978. Application.RequestStop ();
  979. };
  980. Application.Run ();
  981. // Shutdown must be called to safely clean up Application if Init has been called
  982. Application.Shutdown ();
  983. }
  984. [Fact]
  985. public void SetWidth_CanSetWidth ()
  986. {
  987. var top = new View () {
  988. X = 0,
  989. Y = 0,
  990. Width = 80,
  991. };
  992. var v = new View () {
  993. Width = Dim.Fill ()
  994. };
  995. top.Add (v);
  996. Assert.False (v.SetWidth (70, out int rWidth));
  997. Assert.Equal (70, rWidth);
  998. v.Width = Dim.Fill (1);
  999. Assert.False (v.SetWidth (70, out rWidth));
  1000. Assert.Equal (69, rWidth);
  1001. v.Width = null;
  1002. Assert.True (v.SetWidth (70, out rWidth));
  1003. Assert.Equal (70, rWidth);
  1004. v.IsInitialized = true;
  1005. v.Width = Dim.Fill (1);
  1006. Assert.Throws<ArgumentException> (() => v.Width = 75);
  1007. v.LayoutStyle = LayoutStyle.Absolute;
  1008. v.Width = 75;
  1009. Assert.True (v.SetWidth (60, out rWidth));
  1010. Assert.Equal (60, rWidth);
  1011. }
  1012. [Fact]
  1013. public void SetHeight_CanSetHeight ()
  1014. {
  1015. var top = new View () {
  1016. X = 0,
  1017. Y = 0,
  1018. Height = 20
  1019. };
  1020. var v = new View () {
  1021. Height = Dim.Fill ()
  1022. };
  1023. top.Add (v);
  1024. Assert.False (v.SetHeight (10, out int rHeight));
  1025. Assert.Equal (10, rHeight);
  1026. v.Height = Dim.Fill (1);
  1027. Assert.False (v.SetHeight (10, out rHeight));
  1028. Assert.Equal (9, rHeight);
  1029. v.Height = null;
  1030. Assert.True (v.SetHeight (10, out rHeight));
  1031. Assert.Equal (10, rHeight);
  1032. v.IsInitialized = true;
  1033. v.Height = Dim.Fill (1);
  1034. Assert.Throws<ArgumentException> (() => v.Height = 15);
  1035. v.LayoutStyle = LayoutStyle.Absolute;
  1036. v.Height = 15;
  1037. Assert.True (v.SetHeight (5, out rHeight));
  1038. Assert.Equal (5, rHeight);
  1039. }
  1040. [Fact]
  1041. public void GetCurrentWidth_CanSetWidth ()
  1042. {
  1043. var top = new View () {
  1044. X = 0,
  1045. Y = 0,
  1046. Width = 80,
  1047. };
  1048. var v = new View () {
  1049. Width = Dim.Fill ()
  1050. };
  1051. top.Add (v);
  1052. Assert.False (v.GetCurrentWidth (out int cWidth));
  1053. Assert.Equal (80, cWidth);
  1054. v.Width = Dim.Fill (1);
  1055. Assert.False (v.GetCurrentWidth (out cWidth));
  1056. Assert.Equal (79, cWidth);
  1057. }
  1058. [Fact]
  1059. public void GetCurrentHeight_CanSetHeight ()
  1060. {
  1061. var top = new View () {
  1062. X = 0,
  1063. Y = 0,
  1064. Height = 20
  1065. };
  1066. var v = new View () {
  1067. Height = Dim.Fill ()
  1068. };
  1069. top.Add (v);
  1070. Assert.False (v.GetCurrentHeight (out int cHeight));
  1071. Assert.Equal (20, cHeight);
  1072. v.Height = Dim.Fill (1);
  1073. Assert.False (v.GetCurrentHeight (out cHeight));
  1074. Assert.Equal (19, cHeight);
  1075. }
  1076. [Fact]
  1077. public void AutoSize_False_ResizeView_Is_Always_False ()
  1078. {
  1079. var label = new Label () { AutoSize = false };
  1080. label.Text = "New text";
  1081. Assert.False (label.AutoSize);
  1082. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1083. }
  1084. [Fact]
  1085. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1086. {
  1087. var label = new Label ();
  1088. label.Text = "New text";
  1089. Assert.True (label.AutoSize);
  1090. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  1091. }
  1092. [Fact]
  1093. public void AutoSize_True_ResizeView_With_Dim_Fill ()
  1094. {
  1095. var win = new Window (new Rect (0, 0, 30, 80), "");
  1096. var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
  1097. win.Add (label);
  1098. label.Text = "New text\nNew line";
  1099. win.LayoutSubviews ();
  1100. Assert.True (label.AutoSize);
  1101. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  1102. }
  1103. [Fact]
  1104. public void AutoSize_True_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute ()
  1105. {
  1106. var win = new Window (new Rect (0, 0, 30, 80), "");
  1107. var label = new Label () { Width = Dim.Fill () };
  1108. win.Add (label);
  1109. label.Text = "New text\nNew line";
  1110. win.LayoutSubviews ();
  1111. Assert.True (label.AutoSize);
  1112. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1113. }
  1114. [Theory]
  1115. [InlineData (1)]
  1116. [InlineData (2)]
  1117. [InlineData (3)]
  1118. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  1119. {
  1120. var driver = new FakeDriver ();
  1121. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  1122. try {
  1123. // Create a label with a short text
  1124. Label lbl;
  1125. var text = "test";
  1126. if (choice == 1) {
  1127. // An object initializer should call the default constructor.
  1128. lbl = new Label { Text = text };
  1129. } else if (choice == 2) {
  1130. // Calling the default constructor followed by the object initializer.
  1131. lbl = new Label () { Text = text };
  1132. } else {
  1133. // Calling the Text constructor.
  1134. lbl = new Label (text);
  1135. }
  1136. lbl.ColorScheme = new ColorScheme ();
  1137. lbl.Redraw (lbl.Bounds);
  1138. // should have the initial text
  1139. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  1140. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  1141. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  1142. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  1143. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  1144. } finally {
  1145. Application.Shutdown ();
  1146. }
  1147. }
  1148. [Fact]
  1149. [AutoInitShutdown]
  1150. public void Internal_Tests ()
  1151. {
  1152. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1153. Enum.GetValues (typeof (View.Direction)));
  1154. var rect = new Rect (1, 1, 10, 1);
  1155. var view = new View (rect);
  1156. var top = Application.Top;
  1157. top.Add (view);
  1158. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1159. view.FocusDirection = View.Direction.Backward;
  1160. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1161. Assert.Empty (view.InternalSubviews);
  1162. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1163. Assert.True (view.LayoutNeeded);
  1164. Assert.False (view.ChildNeedsDisplay);
  1165. Assert.False (view.addingView);
  1166. view.addingView = true;
  1167. Assert.True (view.addingView);
  1168. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1169. Assert.Equal (1, rcol);
  1170. Assert.Equal (1, rrow);
  1171. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1172. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1173. view.Width = Dim.Fill ();
  1174. view.Height = Dim.Fill ();
  1175. Assert.Equal (10, view.Bounds.Width);
  1176. Assert.Equal (1, view.Bounds.Height);
  1177. view.SetRelativeLayout (top.Bounds);
  1178. Assert.Equal (79, view.Bounds.Width);
  1179. Assert.Equal (24, view.Bounds.Height);
  1180. bool layoutStarted = false;
  1181. view.LayoutStarted += (_) => { layoutStarted = true; };
  1182. view.OnLayoutStarted (null);
  1183. Assert.True (layoutStarted);
  1184. view.LayoutComplete += (_) => { layoutStarted = false; };
  1185. view.OnLayoutComplete (null);
  1186. Assert.False (layoutStarted);
  1187. view.X = Pos.Center () - 41;
  1188. view.Y = Pos.Center () - 13;
  1189. view.SetRelativeLayout (top.Bounds);
  1190. view.ViewToScreen (0, 0, out rcol, out rrow);
  1191. Assert.Equal (-1, rcol);
  1192. Assert.Equal (-1, rrow);
  1193. }
  1194. [Fact]
  1195. [AutoInitShutdown]
  1196. public void Enabled_False_Sets_HasFocus_To_False ()
  1197. {
  1198. var wasClicked = false;
  1199. var view = new Button ("Click Me");
  1200. view.Clicked += () => wasClicked = !wasClicked;
  1201. Application.Top.Add (view);
  1202. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1203. Assert.True (wasClicked);
  1204. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1205. Assert.False (wasClicked);
  1206. Assert.True (view.Enabled);
  1207. Assert.True (view.CanFocus);
  1208. Assert.True (view.HasFocus);
  1209. view.Enabled = false;
  1210. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1211. Assert.False (wasClicked);
  1212. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1213. Assert.False (wasClicked);
  1214. Assert.False (view.Enabled);
  1215. Assert.True (view.CanFocus);
  1216. Assert.False (view.HasFocus);
  1217. view.SetFocus ();
  1218. Assert.False (view.HasFocus);
  1219. }
  1220. [Fact]
  1221. [AutoInitShutdown]
  1222. public void Enabled_Sets_Also_Sets_Subviews ()
  1223. {
  1224. var wasClicked = false;
  1225. var button = new Button ("Click Me");
  1226. button.Clicked += () => wasClicked = !wasClicked;
  1227. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1228. win.Add (button);
  1229. Application.Top.Add (win);
  1230. var iterations = 0;
  1231. Application.Iteration += () => {
  1232. iterations++;
  1233. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1234. Assert.True (wasClicked);
  1235. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1236. Assert.False (wasClicked);
  1237. Assert.True (button.Enabled);
  1238. Assert.True (button.CanFocus);
  1239. Assert.True (button.HasFocus);
  1240. Assert.True (win.Enabled);
  1241. Assert.True (win.CanFocus);
  1242. Assert.True (win.HasFocus);
  1243. win.Enabled = false;
  1244. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1245. Assert.False (wasClicked);
  1246. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1247. Assert.False (wasClicked);
  1248. Assert.False (button.Enabled);
  1249. Assert.True (button.CanFocus);
  1250. Assert.False (button.HasFocus);
  1251. Assert.False (win.Enabled);
  1252. Assert.True (win.CanFocus);
  1253. Assert.False (win.HasFocus);
  1254. button.SetFocus ();
  1255. Assert.False (button.HasFocus);
  1256. Assert.False (win.HasFocus);
  1257. win.SetFocus ();
  1258. Assert.False (button.HasFocus);
  1259. Assert.False (win.HasFocus);
  1260. win.Enabled = true;
  1261. win.FocusFirst ();
  1262. Assert.True (button.HasFocus);
  1263. Assert.True (win.HasFocus);
  1264. Application.RequestStop ();
  1265. };
  1266. Application.Run ();
  1267. Assert.Equal (1, iterations);
  1268. }
  1269. [Fact]
  1270. [AutoInitShutdown]
  1271. public void Visible_Sets_Also_Sets_Subviews ()
  1272. {
  1273. var button = new Button ("Click Me");
  1274. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1275. win.Add (button);
  1276. var top = Application.Top;
  1277. top.Add (win);
  1278. var iterations = 0;
  1279. Application.Iteration += () => {
  1280. iterations++;
  1281. Assert.True (button.Visible);
  1282. Assert.True (button.CanFocus);
  1283. Assert.True (button.HasFocus);
  1284. Assert.True (win.Visible);
  1285. Assert.True (win.CanFocus);
  1286. Assert.True (win.HasFocus);
  1287. Assert.True (RunesCount () > 0);
  1288. win.Visible = false;
  1289. Assert.True (button.Visible);
  1290. Assert.True (button.CanFocus);
  1291. Assert.False (button.HasFocus);
  1292. Assert.False (win.Visible);
  1293. Assert.True (win.CanFocus);
  1294. Assert.False (win.HasFocus);
  1295. button.SetFocus ();
  1296. Assert.False (button.HasFocus);
  1297. Assert.False (win.HasFocus);
  1298. win.SetFocus ();
  1299. Assert.False (button.HasFocus);
  1300. Assert.False (win.HasFocus);
  1301. top.Redraw (top.Bounds);
  1302. Assert.True (RunesCount () == 0);
  1303. win.Visible = true;
  1304. win.FocusFirst ();
  1305. Assert.True (button.HasFocus);
  1306. Assert.True (win.HasFocus);
  1307. top.Redraw (top.Bounds);
  1308. Assert.True (RunesCount () > 0);
  1309. Application.RequestStop ();
  1310. };
  1311. Application.Run ();
  1312. Assert.Equal (1, iterations);
  1313. int RunesCount ()
  1314. {
  1315. var contents = ((FakeDriver)Application.Driver).Contents;
  1316. var runesCount = 0;
  1317. for (int i = 0; i < Application.Driver.Rows; i++) {
  1318. for (int j = 0; j < Application.Driver.Cols; j++) {
  1319. if (contents [i, j, 0] != ' ') {
  1320. runesCount++;
  1321. }
  1322. }
  1323. }
  1324. return runesCount;
  1325. }
  1326. }
  1327. [Fact]
  1328. [AutoInitShutdown]
  1329. public void GetTopSuperView_Test ()
  1330. {
  1331. var v1 = new View ();
  1332. var fv1 = new FrameView ();
  1333. fv1.Add (v1);
  1334. var tf1 = new TextField ();
  1335. var w1 = new Window ();
  1336. w1.Add (fv1, tf1);
  1337. var top1 = new Toplevel ();
  1338. top1.Add (w1);
  1339. var v2 = new View ();
  1340. var fv2 = new FrameView ();
  1341. fv2.Add (v2);
  1342. var tf2 = new TextField ();
  1343. var w2 = new Window ();
  1344. w2.Add (fv2, tf2);
  1345. var top2 = new Toplevel ();
  1346. top2.Add (w2);
  1347. Assert.Equal (top1, v1.GetTopSuperView ());
  1348. Assert.Equal (top2, v2.GetTopSuperView ());
  1349. }
  1350. [Fact]
  1351. [AutoInitShutdown]
  1352. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1353. {
  1354. var lbl = new Label ("123");
  1355. Application.Top.Add (lbl);
  1356. Application.Begin (Application.Top);
  1357. Assert.Equal ("123 ", GetContents ());
  1358. lbl.Text = "12";
  1359. if (!lbl.SuperView.NeedDisplay.IsEmpty) {
  1360. lbl.SuperView.Redraw (lbl.SuperView.NeedDisplay);
  1361. }
  1362. Assert.Equal ("12 ", GetContents ());
  1363. string GetContents ()
  1364. {
  1365. var text = "";
  1366. for (int i = 0; i < 4; i++) {
  1367. text += (char)Application.Driver.Contents [0, i, 0];
  1368. }
  1369. return text;
  1370. }
  1371. }
  1372. [Fact]
  1373. [AutoInitShutdown]
  1374. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1375. {
  1376. var view = new View () { CanFocus = true };
  1377. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1378. win.Add (view);
  1379. Application.Top.Add (win);
  1380. Application.Begin (Application.Top);
  1381. Assert.True (view.CanFocus);
  1382. Assert.True (view.HasFocus);
  1383. view.CanFocus = false;
  1384. Assert.False (view.CanFocus);
  1385. Assert.False (view.HasFocus);
  1386. Assert.Null (Application.Current.Focused);
  1387. Assert.Null (Application.Current.MostFocused);
  1388. }
  1389. [Fact]
  1390. [AutoInitShutdown]
  1391. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1392. {
  1393. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1394. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1395. win1.Add (view1);
  1396. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1397. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1398. win2.Add (view2);
  1399. Application.Top.Add (win1, win2);
  1400. Application.Begin (Application.Top);
  1401. Assert.True (view1.CanFocus);
  1402. Assert.True (view1.HasFocus);
  1403. Assert.True (view2.CanFocus);
  1404. Assert.True (view2.HasFocus);
  1405. view1.CanFocus = false;
  1406. Assert.False (view1.CanFocus);
  1407. Assert.False (view1.HasFocus);
  1408. Assert.Equal (win2, Application.Current.Focused);
  1409. Assert.Equal (view2, Application.Current.MostFocused);
  1410. }
  1411. [Fact]
  1412. [AutoInitShutdown]
  1413. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1414. {
  1415. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1416. var view12 = new View () { Y = 5, Width = 10, Height = 1, CanFocus = true };
  1417. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1418. win1.Add (view1, view12);
  1419. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1420. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1421. win2.Add (view2);
  1422. Application.Top.Add (win1, win2);
  1423. Application.Begin (Application.Top);
  1424. Assert.True (view1.CanFocus);
  1425. Assert.True (view1.HasFocus);
  1426. Assert.True (view2.CanFocus);
  1427. Assert.True (view2.HasFocus);
  1428. view1.CanFocus = false;
  1429. Assert.False (view1.CanFocus);
  1430. Assert.False (view1.HasFocus);
  1431. Assert.Equal (win1, Application.Current.Focused);
  1432. Assert.Equal (view12, Application.Current.MostFocused);
  1433. }
  1434. [Fact]
  1435. [AutoInitShutdown]
  1436. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1437. {
  1438. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1439. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1440. win1.Add (view1);
  1441. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1442. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1443. win2.Add (view2);
  1444. Application.Top.Add (win1, win2);
  1445. Application.Begin (Application.Top);
  1446. Assert.True (view1.CanFocus);
  1447. Assert.True (view1.HasFocus);
  1448. Assert.True (view2.CanFocus);
  1449. Assert.True (view2.HasFocus);
  1450. win1.CanFocus = false;
  1451. Assert.False (view1.CanFocus);
  1452. Assert.False (view1.HasFocus);
  1453. Assert.False (win1.CanFocus);
  1454. Assert.False (win1.HasFocus);
  1455. Assert.Equal (win2, Application.Current.Focused);
  1456. Assert.Equal (view2, Application.Current.MostFocused);
  1457. }
  1458. }
  1459. }