ViewTests.cs 52 KB

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