ViewTests.cs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui.Graphs;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. using Console = Terminal.Gui.FakeConsole;
  9. namespace Terminal.Gui.CoreTests {
  10. public class ViewTests {
  11. readonly ITestOutputHelper output;
  12. public ViewTests (ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. [Fact]
  17. public void New_Initializes ()
  18. {
  19. // Parameterless
  20. var r = new View ();
  21. Assert.NotNull (r);
  22. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  23. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  24. Assert.False (r.CanFocus);
  25. Assert.False (r.HasFocus);
  26. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  27. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  28. Assert.Null (r.Focused);
  29. Assert.Null (r.ColorScheme);
  30. Assert.Null (r.Width);
  31. Assert.Null (r.Height);
  32. Assert.Null (r.X);
  33. Assert.Null (r.Y);
  34. Assert.False (r.IsCurrentTop);
  35. Assert.Empty (r.Id);
  36. Assert.Empty (r.Subviews);
  37. Assert.False (r.WantContinuousButtonPressed);
  38. Assert.False (r.WantMousePositionReports);
  39. Assert.Null (r.SuperView);
  40. Assert.Null (r.MostFocused);
  41. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  42. // Empty Rect
  43. r = new View (Rect.Empty);
  44. Assert.NotNull (r);
  45. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  46. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  47. Assert.False (r.CanFocus);
  48. Assert.False (r.HasFocus);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  50. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  51. Assert.Null (r.Focused);
  52. Assert.Null (r.ColorScheme);
  53. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  54. Assert.Null (r.Height); // avoiding Dim errors.
  55. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  56. Assert.Null (r.Y); // avoiding Pos errors.
  57. Assert.False (r.IsCurrentTop);
  58. Assert.Empty (r.Id);
  59. Assert.Empty (r.Subviews);
  60. Assert.False (r.WantContinuousButtonPressed);
  61. Assert.False (r.WantMousePositionReports);
  62. Assert.Null (r.SuperView);
  63. Assert.Null (r.MostFocused);
  64. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  65. // Rect with values
  66. r = new View (new Rect (1, 2, 3, 4));
  67. Assert.NotNull (r);
  68. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  69. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  70. Assert.False (r.CanFocus);
  71. Assert.False (r.HasFocus);
  72. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  73. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  74. Assert.Null (r.Focused);
  75. Assert.Null (r.ColorScheme);
  76. Assert.Null (r.Width);
  77. Assert.Null (r.Height);
  78. Assert.Null (r.X);
  79. Assert.Null (r.Y);
  80. Assert.False (r.IsCurrentTop);
  81. Assert.Empty (r.Id);
  82. Assert.Empty (r.Subviews);
  83. Assert.False (r.WantContinuousButtonPressed);
  84. Assert.False (r.WantMousePositionReports);
  85. Assert.Null (r.SuperView);
  86. Assert.Null (r.MostFocused);
  87. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  88. // Initializes a view with a vertical direction
  89. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  90. Assert.NotNull (r);
  91. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  92. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  93. Assert.False (r.CanFocus);
  94. Assert.False (r.HasFocus);
  95. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  96. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  97. Assert.Null (r.Focused);
  98. Assert.Null (r.ColorScheme);
  99. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  100. Assert.Null (r.Height); // avoiding Dim errors.
  101. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  102. Assert.Null (r.Y); // avoiding Pos errors.
  103. Assert.False (r.IsCurrentTop);
  104. Assert.Empty (r.Id);
  105. Assert.Empty (r.Subviews);
  106. Assert.False (r.WantContinuousButtonPressed);
  107. Assert.False (r.WantMousePositionReports);
  108. Assert.Null (r.SuperView);
  109. Assert.Null (r.MostFocused);
  110. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  111. }
  112. [Fact]
  113. public void New_Methods_Return_False ()
  114. {
  115. var r = new View ();
  116. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  120. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  121. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  122. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  123. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  124. Assert.False (r.OnEnter (new View ()));
  125. Assert.False (r.OnLeave (new View ()));
  126. // TODO: Add more
  127. }
  128. [Fact]
  129. public void TopologicalSort_Missing_Add ()
  130. {
  131. var root = new View ();
  132. var sub1 = new View ();
  133. root.Add (sub1);
  134. var sub2 = new View ();
  135. sub1.Width = Dim.Width (sub2);
  136. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  137. sub2.Width = Dim.Width (sub1);
  138. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  139. }
  140. [Fact]
  141. public void TopologicalSort_Recursive_Ref ()
  142. {
  143. var root = new View ();
  144. var sub1 = new View ();
  145. root.Add (sub1);
  146. var sub2 = new View ();
  147. root.Add (sub2);
  148. sub2.Width = Dim.Width (sub2);
  149. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  150. }
  151. [Fact]
  152. public void Added_Removed ()
  153. {
  154. var v = new View (new Rect (0, 0, 10, 24));
  155. var t = new View ();
  156. v.Added += (View e) => {
  157. Assert.True (v.SuperView == e);
  158. };
  159. v.Removed += (View e) => {
  160. Assert.True (v.SuperView == null);
  161. };
  162. t.Add (v);
  163. Assert.True (t.Subviews.Count == 1);
  164. t.Remove (v);
  165. Assert.True (t.Subviews.Count == 0);
  166. }
  167. [Fact]
  168. public void Subviews_TabIndexes_AreEqual ()
  169. {
  170. var r = new View ();
  171. var v1 = new View () { CanFocus = true };
  172. var v2 = new View () { CanFocus = true };
  173. var v3 = new View () { CanFocus = true };
  174. r.Add (v1, v2, v3);
  175. Assert.True (r.Subviews.IndexOf (v1) == 0);
  176. Assert.True (r.Subviews.IndexOf (v2) == 1);
  177. Assert.True (r.Subviews.IndexOf (v3) == 2);
  178. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  179. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  180. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  181. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  182. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  183. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  184. }
  185. [Fact]
  186. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  187. {
  188. var r = new View ();
  189. var v1 = new View () { CanFocus = true };
  190. var v2 = new View () { CanFocus = true };
  191. var v3 = new View () { CanFocus = true };
  192. r.Add (v1, v2, v3);
  193. r.BringSubviewToFront (v1);
  194. Assert.True (r.Subviews.IndexOf (v1) == 2);
  195. Assert.True (r.Subviews.IndexOf (v2) == 0);
  196. Assert.True (r.Subviews.IndexOf (v3) == 1);
  197. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  198. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  199. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  200. }
  201. [Fact]
  202. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  203. {
  204. var r = new View ();
  205. var v1 = new View () { CanFocus = true };
  206. var v2 = new View () { CanFocus = true };
  207. var v3 = new View () { CanFocus = true };
  208. r.Add (v1, v2, v3);
  209. r.BringSubviewForward (v1);
  210. Assert.True (r.Subviews.IndexOf (v1) == 1);
  211. Assert.True (r.Subviews.IndexOf (v2) == 0);
  212. Assert.True (r.Subviews.IndexOf (v3) == 2);
  213. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  214. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  215. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  216. }
  217. [Fact]
  218. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  219. {
  220. var r = new View ();
  221. var v1 = new View () { CanFocus = true };
  222. var v2 = new View () { CanFocus = true };
  223. var v3 = new View () { CanFocus = true };
  224. r.Add (v1, v2, v3);
  225. r.SendSubviewToBack (v3);
  226. Assert.True (r.Subviews.IndexOf (v1) == 1);
  227. Assert.True (r.Subviews.IndexOf (v2) == 2);
  228. Assert.True (r.Subviews.IndexOf (v3) == 0);
  229. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  230. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  231. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  232. }
  233. [Fact]
  234. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  235. {
  236. var r = new View ();
  237. var v1 = new View () { CanFocus = true };
  238. var v2 = new View () { CanFocus = true };
  239. var v3 = new View () { CanFocus = true };
  240. r.Add (v1, v2, v3);
  241. r.SendSubviewBackwards (v3);
  242. Assert.True (r.Subviews.IndexOf (v1) == 0);
  243. Assert.True (r.Subviews.IndexOf (v2) == 2);
  244. Assert.True (r.Subviews.IndexOf (v3) == 1);
  245. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  246. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  247. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  248. }
  249. [Fact]
  250. public void TabIndex_Set_CanFocus_ValidValues ()
  251. {
  252. var r = new View ();
  253. var v1 = new View () { CanFocus = true };
  254. var v2 = new View () { CanFocus = true };
  255. var v3 = new View () { CanFocus = true };
  256. r.Add (v1, v2, v3);
  257. v1.TabIndex = 1;
  258. Assert.True (r.Subviews.IndexOf (v1) == 0);
  259. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  260. v1.TabIndex = 2;
  261. Assert.True (r.Subviews.IndexOf (v1) == 0);
  262. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  263. }
  264. [Fact]
  265. public void TabIndex_Set_CanFocus_HigherValues ()
  266. {
  267. var r = new View ();
  268. var v1 = new View () { CanFocus = true };
  269. var v2 = new View () { CanFocus = true };
  270. var v3 = new View () { CanFocus = true };
  271. r.Add (v1, v2, v3);
  272. v1.TabIndex = 3;
  273. Assert.True (r.Subviews.IndexOf (v1) == 0);
  274. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  275. }
  276. [Fact]
  277. public void TabIndex_Set_CanFocus_LowerValues ()
  278. {
  279. var r = new View ();
  280. var v1 = new View () { CanFocus = true };
  281. var v2 = new View () { CanFocus = true };
  282. var v3 = new View () { CanFocus = true };
  283. r.Add (v1, v2, v3);
  284. v1.TabIndex = -1;
  285. Assert.True (r.Subviews.IndexOf (v1) == 0);
  286. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  287. }
  288. [Fact]
  289. public void TabIndex_Set_CanFocus_False ()
  290. {
  291. var r = new View ();
  292. var v1 = new View () { CanFocus = true };
  293. var v2 = new View () { CanFocus = true };
  294. var v3 = new View () { CanFocus = true };
  295. r.Add (v1, v2, v3);
  296. v1.CanFocus = false;
  297. v1.TabIndex = 0;
  298. Assert.True (r.Subviews.IndexOf (v1) == 0);
  299. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  300. Assert.Equal (-1, v1.TabIndex);
  301. }
  302. [Fact]
  303. public void TabIndex_Set_CanFocus_False_To_True ()
  304. {
  305. var r = new View ();
  306. var v1 = new View ();
  307. var v2 = new View () { CanFocus = true };
  308. var v3 = new View () { CanFocus = true };
  309. r.Add (v1, v2, v3);
  310. v1.CanFocus = true;
  311. v1.TabIndex = 1;
  312. Assert.True (r.Subviews.IndexOf (v1) == 0);
  313. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  314. }
  315. [Fact]
  316. public void TabStop_And_CanFocus_Are_All_True ()
  317. {
  318. var r = new View ();
  319. var v1 = new View () { CanFocus = true };
  320. var v2 = new View () { CanFocus = true };
  321. var v3 = new View () { CanFocus = true };
  322. r.Add (v1, v2, v3);
  323. r.FocusNext ();
  324. Assert.True (v1.HasFocus);
  325. Assert.False (v2.HasFocus);
  326. Assert.False (v3.HasFocus);
  327. r.FocusNext ();
  328. Assert.False (v1.HasFocus);
  329. Assert.True (v2.HasFocus);
  330. Assert.False (v3.HasFocus);
  331. r.FocusNext ();
  332. Assert.False (v1.HasFocus);
  333. Assert.False (v2.HasFocus);
  334. Assert.True (v3.HasFocus);
  335. }
  336. [Fact]
  337. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  338. {
  339. var r = new View ();
  340. var v1 = new View ();
  341. var v2 = new View ();
  342. var v3 = new View ();
  343. r.Add (v1, v2, v3);
  344. r.FocusNext ();
  345. Assert.False (v1.HasFocus);
  346. Assert.False (v2.HasFocus);
  347. Assert.False (v3.HasFocus);
  348. r.FocusNext ();
  349. Assert.False (v1.HasFocus);
  350. Assert.False (v2.HasFocus);
  351. Assert.False (v3.HasFocus);
  352. r.FocusNext ();
  353. Assert.False (v1.HasFocus);
  354. Assert.False (v2.HasFocus);
  355. Assert.False (v3.HasFocus);
  356. }
  357. [Fact]
  358. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  359. {
  360. var r = new View ();
  361. var v1 = new View () { CanFocus = true, TabStop = false };
  362. var v2 = new View () { CanFocus = true, TabStop = false };
  363. var v3 = new View () { CanFocus = true, TabStop = false };
  364. r.Add (v1, v2, v3);
  365. r.FocusNext ();
  366. Assert.False (v1.HasFocus);
  367. Assert.False (v2.HasFocus);
  368. Assert.False (v3.HasFocus);
  369. r.FocusNext ();
  370. Assert.False (v1.HasFocus);
  371. Assert.False (v2.HasFocus);
  372. Assert.False (v3.HasFocus);
  373. r.FocusNext ();
  374. Assert.False (v1.HasFocus);
  375. Assert.False (v2.HasFocus);
  376. Assert.False (v3.HasFocus);
  377. }
  378. [Fact]
  379. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  380. {
  381. var r = new View ();
  382. var v1 = new View () { CanFocus = true, TabStop = false };
  383. var v2 = new View () { CanFocus = false, TabStop = true };
  384. var v3 = new View () { CanFocus = false, TabStop = false };
  385. r.Add (v1, v2, v3);
  386. r.FocusNext ();
  387. Assert.False (v1.HasFocus);
  388. Assert.False (v2.HasFocus);
  389. Assert.False (v3.HasFocus);
  390. r.FocusNext ();
  391. Assert.False (v1.HasFocus);
  392. Assert.False (v2.HasFocus);
  393. Assert.False (v3.HasFocus);
  394. r.FocusNext ();
  395. Assert.False (v1.HasFocus);
  396. Assert.False (v2.HasFocus);
  397. Assert.False (v3.HasFocus);
  398. }
  399. [Fact]
  400. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  401. {
  402. var r = new View ();
  403. var v1 = new View ();
  404. var v2 = new View ();
  405. var v3 = new View ();
  406. r.Add (v1, v2, v3);
  407. r.FocusNext ();
  408. Assert.False (v1.HasFocus);
  409. Assert.False (v2.HasFocus);
  410. Assert.False (v3.HasFocus);
  411. v1.CanFocus = true;
  412. r.FocusNext ();
  413. Assert.True (v1.HasFocus);
  414. Assert.False (v2.HasFocus);
  415. Assert.False (v3.HasFocus);
  416. v2.CanFocus = true;
  417. r.FocusNext ();
  418. Assert.False (v1.HasFocus);
  419. Assert.True (v2.HasFocus);
  420. Assert.False (v3.HasFocus);
  421. v3.CanFocus = true;
  422. r.FocusNext ();
  423. Assert.False (v1.HasFocus);
  424. Assert.False (v2.HasFocus);
  425. Assert.True (v3.HasFocus);
  426. }
  427. [Fact]
  428. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  429. {
  430. var r = new View ();
  431. var v1 = new View () { CanFocus = true, TabStop = false };
  432. var v2 = new View () { CanFocus = true, TabStop = false };
  433. var v3 = new View () { CanFocus = true, TabStop = false };
  434. r.Add (v1, v2, v3);
  435. r.FocusNext ();
  436. Assert.False (v1.HasFocus);
  437. Assert.False (v2.HasFocus);
  438. Assert.False (v3.HasFocus);
  439. v1.TabStop = true;
  440. r.FocusNext ();
  441. Assert.True (v1.HasFocus);
  442. Assert.False (v2.HasFocus);
  443. Assert.False (v3.HasFocus);
  444. v2.TabStop = true;
  445. r.FocusNext ();
  446. Assert.False (v1.HasFocus);
  447. Assert.True (v2.HasFocus);
  448. Assert.False (v3.HasFocus);
  449. v3.TabStop = true;
  450. r.FocusNext ();
  451. Assert.False (v1.HasFocus);
  452. Assert.False (v2.HasFocus);
  453. Assert.True (v3.HasFocus);
  454. }
  455. [Fact]
  456. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  457. {
  458. var r = new View ();
  459. var v1 = new View ("1");
  460. var v2 = new View ("2");
  461. var v3 = new View ("3");
  462. r.Add (v1, v2, v3);
  463. v2.CanFocus = true;
  464. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  465. Assert.Equal (0, v2.TabIndex);
  466. Assert.True (v2.TabStop);
  467. v1.CanFocus = true;
  468. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  469. Assert.Equal (1, v1.TabIndex);
  470. Assert.True (v1.TabStop);
  471. v1.TabIndex = 2;
  472. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  473. Assert.Equal (1, v1.TabIndex);
  474. v3.CanFocus = true;
  475. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  476. Assert.Equal (1, v1.TabIndex);
  477. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  478. Assert.Equal (2, v3.TabIndex);
  479. Assert.True (v3.TabStop);
  480. v2.CanFocus = false;
  481. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  482. Assert.Equal (1, v1.TabIndex);
  483. Assert.True (v1.TabStop);
  484. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  485. Assert.Equal (-1, v2.TabIndex);
  486. Assert.False (v2.TabStop);
  487. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  488. Assert.Equal (2, v3.TabIndex);
  489. Assert.True (v3.TabStop);
  490. }
  491. [Fact]
  492. public void Initialized_Event_Comparing_With_Added_Event ()
  493. {
  494. Application.Init (new FakeDriver ());
  495. var t = new Toplevel () { Id = "0", };
  496. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  497. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  498. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  499. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  500. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  501. w.Added += (e) => {
  502. Assert.Equal (e.Frame.Width, w.Frame.Width);
  503. Assert.Equal (e.Frame.Height, w.Frame.Height);
  504. };
  505. v1.Added += (e) => {
  506. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  507. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  508. };
  509. v2.Added += (e) => {
  510. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  511. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  512. };
  513. sv1.Added += (e) => {
  514. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  515. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  516. };
  517. t.Initialized += (s, e) => {
  518. tc++;
  519. Assert.Equal (1, tc);
  520. Assert.Equal (1, wc);
  521. Assert.Equal (1, v1c);
  522. Assert.Equal (1, v2c);
  523. Assert.Equal (1, sv1c);
  524. Assert.True (t.CanFocus);
  525. Assert.True (w.CanFocus);
  526. Assert.False (v1.CanFocus);
  527. Assert.False (v2.CanFocus);
  528. Assert.False (sv1.CanFocus);
  529. Application.Refresh ();
  530. };
  531. w.Initialized += (s, e) => {
  532. wc++;
  533. Assert.Equal (t.Frame.Width, w.Frame.Width);
  534. Assert.Equal (t.Frame.Height, w.Frame.Height);
  535. };
  536. v1.Initialized += (s, e) => {
  537. v1c++;
  538. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  539. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  540. };
  541. v2.Initialized += (s, e) => {
  542. v2c++;
  543. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  544. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  545. };
  546. sv1.Initialized += (s, e) => {
  547. sv1c++;
  548. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  549. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  550. Assert.False (sv1.CanFocus);
  551. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  552. Assert.False (sv1.CanFocus);
  553. };
  554. v1.Add (sv1);
  555. w.Add (v1, v2);
  556. t.Add (w);
  557. Application.Iteration = () => {
  558. Application.Refresh ();
  559. t.Running = false;
  560. };
  561. Application.Run (t);
  562. Application.Shutdown ();
  563. Assert.Equal (1, tc);
  564. Assert.Equal (1, wc);
  565. Assert.Equal (1, v1c);
  566. Assert.Equal (1, v2c);
  567. Assert.Equal (1, sv1c);
  568. Assert.True (t.CanFocus);
  569. Assert.True (w.CanFocus);
  570. Assert.False (v1.CanFocus);
  571. Assert.False (v2.CanFocus);
  572. Assert.False (sv1.CanFocus);
  573. v1.CanFocus = true;
  574. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  575. }
  576. [Fact]
  577. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  578. {
  579. Application.Init (new FakeDriver ());
  580. var t = new Toplevel () { Id = "0", };
  581. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  582. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  583. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  584. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  585. t.Initialized += (s, e) => {
  586. tc++;
  587. Assert.Equal (1, tc);
  588. Assert.Equal (1, wc);
  589. Assert.Equal (1, v1c);
  590. Assert.Equal (1, v2c);
  591. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  592. Assert.True (t.CanFocus);
  593. Assert.True (w.CanFocus);
  594. Assert.False (v1.CanFocus);
  595. Assert.False (v2.CanFocus);
  596. Application.Refresh ();
  597. };
  598. w.Initialized += (s, e) => {
  599. wc++;
  600. Assert.Equal (t.Frame.Width, w.Frame.Width);
  601. Assert.Equal (t.Frame.Height, w.Frame.Height);
  602. };
  603. v1.Initialized += (s, e) => {
  604. v1c++;
  605. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  606. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  607. };
  608. v2.Initialized += (s, e) => {
  609. v2c++;
  610. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  611. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  612. };
  613. w.Add (v1, v2);
  614. t.Add (w);
  615. Application.Iteration = () => {
  616. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  617. sv1.Initialized += (s, e) => {
  618. sv1c++;
  619. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  620. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  621. Assert.False (sv1.CanFocus);
  622. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  623. Assert.False (sv1.CanFocus);
  624. };
  625. v1.Add (sv1);
  626. Application.Refresh ();
  627. t.Running = false;
  628. };
  629. Application.Run (t);
  630. Application.Shutdown ();
  631. Assert.Equal (1, tc);
  632. Assert.Equal (1, wc);
  633. Assert.Equal (1, v1c);
  634. Assert.Equal (1, v2c);
  635. Assert.Equal (1, sv1c);
  636. Assert.True (t.CanFocus);
  637. Assert.True (w.CanFocus);
  638. Assert.False (v1.CanFocus);
  639. Assert.False (v2.CanFocus);
  640. }
  641. [Fact]
  642. [AutoInitShutdown]
  643. public void CanFocus_Faced_With_Container ()
  644. {
  645. var t = new Toplevel ();
  646. var w = new Window ();
  647. var f = new FrameView ();
  648. var v = new View () { CanFocus = true };
  649. f.Add (v);
  650. w.Add (f);
  651. t.Add (w);
  652. Assert.True (t.CanFocus);
  653. Assert.True (w.CanFocus);
  654. Assert.True (f.CanFocus);
  655. Assert.True (v.CanFocus);
  656. f.CanFocus = false;
  657. Assert.False (f.CanFocus);
  658. Assert.True (v.CanFocus);
  659. v.CanFocus = false;
  660. Assert.False (f.CanFocus);
  661. Assert.False (v.CanFocus);
  662. v.CanFocus = true;
  663. Assert.False (f.CanFocus);
  664. Assert.True (v.CanFocus);
  665. }
  666. [Fact]
  667. public void CanFocus_Faced_With_Container_Before_Run ()
  668. {
  669. Application.Init (new FakeDriver ());
  670. var t = Application.Top;
  671. var w = new Window ("w");
  672. var f = new FrameView ("f");
  673. var v = new View ("v") { CanFocus = true };
  674. f.Add (v);
  675. w.Add (f);
  676. t.Add (w);
  677. Assert.True (t.CanFocus);
  678. Assert.True (w.CanFocus);
  679. Assert.True (f.CanFocus);
  680. Assert.True (v.CanFocus);
  681. f.CanFocus = false;
  682. Assert.False (f.CanFocus);
  683. Assert.True (v.CanFocus);
  684. v.CanFocus = false;
  685. Assert.False (f.CanFocus);
  686. Assert.False (v.CanFocus);
  687. v.CanFocus = true;
  688. Assert.False (f.CanFocus);
  689. Assert.True (v.CanFocus);
  690. Application.Iteration += () => Application.RequestStop ();
  691. Application.Run ();
  692. Application.Shutdown ();
  693. }
  694. [Fact]
  695. public void CanFocus_Faced_With_Container_After_Run ()
  696. {
  697. Application.Init (new FakeDriver ());
  698. var t = Application.Top;
  699. var w = new Window ("w");
  700. var f = new FrameView ("f");
  701. var v = new View ("v") { CanFocus = true };
  702. f.Add (v);
  703. w.Add (f);
  704. t.Add (w);
  705. t.Ready += () => {
  706. Assert.True (t.CanFocus);
  707. Assert.True (w.CanFocus);
  708. Assert.True (f.CanFocus);
  709. Assert.True (v.CanFocus);
  710. f.CanFocus = false;
  711. Assert.False (f.CanFocus);
  712. Assert.False (v.CanFocus);
  713. v.CanFocus = false;
  714. Assert.False (f.CanFocus);
  715. Assert.False (v.CanFocus);
  716. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  717. Assert.False (f.CanFocus);
  718. Assert.False (v.CanFocus);
  719. f.CanFocus = true;
  720. Assert.True (f.CanFocus);
  721. Assert.True (v.CanFocus);
  722. };
  723. Application.Iteration += () => Application.RequestStop ();
  724. Application.Run ();
  725. Application.Shutdown ();
  726. }
  727. [Fact]
  728. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  729. {
  730. Application.Init (new FakeDriver ());
  731. var t = Application.Top;
  732. var w = new Window ("w");
  733. var f = new FrameView ("f");
  734. var v1 = new View ("v1") { CanFocus = true };
  735. var v2 = new View ("v2") { CanFocus = true };
  736. f.Add (v1, v2);
  737. w.Add (f);
  738. t.Add (w);
  739. t.Ready += () => {
  740. Assert.True (t.CanFocus);
  741. Assert.True (w.CanFocus);
  742. Assert.True (f.CanFocus);
  743. Assert.True (v1.CanFocus);
  744. Assert.True (v2.CanFocus);
  745. w.CanFocus = false;
  746. Assert.False (w.CanFocus);
  747. Assert.False (f.CanFocus);
  748. Assert.False (v1.CanFocus);
  749. Assert.False (v2.CanFocus);
  750. };
  751. Application.Iteration += () => Application.RequestStop ();
  752. Application.Run ();
  753. Application.Shutdown ();
  754. }
  755. [Fact]
  756. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  757. {
  758. Application.Init (new FakeDriver ());
  759. var t = Application.Top;
  760. var w = new Window ("w");
  761. var f = new FrameView ("f");
  762. var v1 = new View ("v1");
  763. var v2 = new View ("v2") { CanFocus = true };
  764. f.Add (v1, v2);
  765. w.Add (f);
  766. t.Add (w);
  767. t.Ready += () => {
  768. Assert.True (t.CanFocus);
  769. Assert.True (w.CanFocus);
  770. Assert.True (f.CanFocus);
  771. Assert.False (v1.CanFocus);
  772. Assert.True (v2.CanFocus);
  773. w.CanFocus = false;
  774. Assert.False (w.CanFocus);
  775. Assert.False (f.CanFocus);
  776. Assert.False (v1.CanFocus);
  777. Assert.False (v2.CanFocus);
  778. w.CanFocus = true;
  779. Assert.True (w.CanFocus);
  780. Assert.True (f.CanFocus);
  781. Assert.False (v1.CanFocus);
  782. Assert.True (v2.CanFocus);
  783. };
  784. Application.Iteration += () => Application.RequestStop ();
  785. Application.Run ();
  786. Application.Shutdown ();
  787. }
  788. [Fact]
  789. public void Navigation_With_Null_Focused_View ()
  790. {
  791. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  792. Application.Init (new FakeDriver ());
  793. Application.Top.Ready += () => {
  794. Assert.Null (Application.Top.Focused);
  795. };
  796. // Keyboard navigation with tab
  797. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  798. Application.Iteration += () => Application.RequestStop ();
  799. Application.Run ();
  800. Application.Shutdown ();
  801. }
  802. [Fact]
  803. public void Multi_Thread_Toplevels ()
  804. {
  805. Application.Init (new FakeDriver ());
  806. var t = Application.Top;
  807. var w = new Window ();
  808. t.Add (w);
  809. int count = 0, count1 = 0, count2 = 0;
  810. bool log = false, log1 = false, log2 = false;
  811. bool fromTopStillKnowFirstIsRunning = false;
  812. bool fromTopStillKnowSecondIsRunning = false;
  813. bool fromFirstStillKnowSecondIsRunning = false;
  814. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  815. count++;
  816. if (count1 == 5) {
  817. log1 = true;
  818. }
  819. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  820. fromTopStillKnowFirstIsRunning = true;
  821. }
  822. if (count1 == 7 && count2 == 7 && count == 8) {
  823. fromTopStillKnowSecondIsRunning = true;
  824. }
  825. if (count == 30) {
  826. Assert.Equal (30, count);
  827. Assert.Equal (20, count1);
  828. Assert.Equal (10, count2);
  829. Assert.True (log);
  830. Assert.True (log1);
  831. Assert.True (log2);
  832. Assert.True (fromTopStillKnowFirstIsRunning);
  833. Assert.True (fromTopStillKnowSecondIsRunning);
  834. Assert.True (fromFirstStillKnowSecondIsRunning);
  835. Application.RequestStop ();
  836. return false;
  837. }
  838. return true;
  839. });
  840. t.Ready += FirstDialogToplevel;
  841. void FirstDialogToplevel ()
  842. {
  843. var od = new OpenDialog ();
  844. od.Ready += SecoundDialogToplevel;
  845. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  846. count1++;
  847. if (count2 == 5) {
  848. log2 = true;
  849. }
  850. if (count2 == 4 && count1 == 5 && count == 5) {
  851. fromFirstStillKnowSecondIsRunning = true;
  852. }
  853. if (count1 == 20) {
  854. Assert.Equal (20, count1);
  855. Application.RequestStop ();
  856. return false;
  857. }
  858. return true;
  859. });
  860. Application.Run (od);
  861. }
  862. void SecoundDialogToplevel ()
  863. {
  864. var d = new Dialog ();
  865. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  866. count2++;
  867. if (count < 30) {
  868. log = true;
  869. }
  870. if (count2 == 10) {
  871. Assert.Equal (10, count2);
  872. Application.RequestStop ();
  873. return false;
  874. }
  875. return true;
  876. });
  877. Application.Run (d);
  878. }
  879. Application.Run ();
  880. Application.Shutdown ();
  881. }
  882. [Fact]
  883. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  884. {
  885. // Object Initializer
  886. var view = new View () {
  887. X = 1,
  888. Y = 2,
  889. Width = 3,
  890. Height = 4
  891. };
  892. Assert.Equal (1, view.X);
  893. Assert.Equal (2, view.Y);
  894. Assert.Equal (3, view.Width);
  895. Assert.Equal (4, view.Height);
  896. Assert.False (view.Frame.IsEmpty);
  897. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  898. Assert.False (view.Bounds.IsEmpty);
  899. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  900. view.LayoutSubviews ();
  901. Assert.Equal (1, view.X);
  902. Assert.Equal (2, view.Y);
  903. Assert.Equal (3, view.Width);
  904. Assert.Equal (4, view.Height);
  905. Assert.False (view.Frame.IsEmpty);
  906. Assert.False (view.Bounds.IsEmpty);
  907. // Default Constructor
  908. view = new View ();
  909. Assert.Null (view.X);
  910. Assert.Null (view.Y);
  911. Assert.Null (view.Width);
  912. Assert.Null (view.Height);
  913. Assert.True (view.Frame.IsEmpty);
  914. Assert.True (view.Bounds.IsEmpty);
  915. // Constructor
  916. view = new View (1, 2, "");
  917. Assert.Null (view.X);
  918. Assert.Null (view.Y);
  919. Assert.Null (view.Width);
  920. Assert.Null (view.Height);
  921. Assert.False (view.Frame.IsEmpty);
  922. Assert.True (view.Bounds.IsEmpty);
  923. // Default Constructor and post assignment equivalent to Object Initializer
  924. view = new View ();
  925. view.X = 1;
  926. view.Y = 2;
  927. view.Width = 3;
  928. view.Height = 4;
  929. Assert.Equal (1, view.X);
  930. Assert.Equal (2, view.Y);
  931. Assert.Equal (3, view.Width);
  932. Assert.Equal (4, view.Height);
  933. Assert.False (view.Frame.IsEmpty);
  934. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  935. Assert.False (view.Bounds.IsEmpty);
  936. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  937. }
  938. [Fact]
  939. public void FocusNearestView_Ensure_Focus_Ordered ()
  940. {
  941. var top = new Toplevel ();
  942. var win = new Window ();
  943. var winSubview = new View ("WindowSubview") {
  944. CanFocus = true
  945. };
  946. win.Add (winSubview);
  947. top.Add (win);
  948. var frm = new FrameView ();
  949. var frmSubview = new View ("FrameSubview") {
  950. CanFocus = true
  951. };
  952. frm.Add (frmSubview);
  953. top.Add (frm);
  954. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  955. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  956. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  957. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  958. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  959. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  960. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  961. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  962. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  963. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  964. }
  965. [Fact]
  966. public void KeyPress_Handled_To_True_Prevents_Changes ()
  967. {
  968. Application.Init (new FakeDriver ());
  969. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  970. var top = Application.Top;
  971. var text = new TextField ("");
  972. text.KeyPress += (e) => {
  973. e.Handled = true;
  974. Assert.True (e.Handled);
  975. Assert.Equal (Key.N, e.KeyEvent.Key);
  976. };
  977. top.Add (text);
  978. Application.Iteration += () => {
  979. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  980. Assert.Equal ("", text.Text);
  981. Application.RequestStop ();
  982. };
  983. Application.Run ();
  984. // Shutdown must be called to safely clean up Application if Init has been called
  985. Application.Shutdown ();
  986. }
  987. [Theory]
  988. [InlineData (1)]
  989. [InlineData (2)]
  990. [InlineData (3)]
  991. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  992. {
  993. var driver = new FakeDriver ();
  994. Application.Init (driver);
  995. try {
  996. // Create a label with a short text
  997. Label lbl;
  998. var text = "test";
  999. if (choice == 1) {
  1000. // An object initializer should call the default constructor.
  1001. lbl = new Label { Text = text };
  1002. } else if (choice == 2) {
  1003. // Calling the default constructor followed by the object initializer.
  1004. lbl = new Label () { Text = text };
  1005. } else {
  1006. // Calling the Text constructor.
  1007. lbl = new Label (text);
  1008. }
  1009. Application.Top.Add (lbl);
  1010. Application.Top.Redraw (Application.Top.Bounds);
  1011. // should have the initial text
  1012. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  1013. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  1014. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  1015. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  1016. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  1017. } finally {
  1018. Application.Shutdown ();
  1019. }
  1020. }
  1021. [Fact]
  1022. [AutoInitShutdown]
  1023. public void Internal_Tests ()
  1024. {
  1025. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1026. Enum.GetValues (typeof (View.Direction)));
  1027. var rect = new Rect (1, 1, 10, 1);
  1028. var view = new View (rect);
  1029. var top = Application.Top;
  1030. top.Add (view);
  1031. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1032. view.FocusDirection = View.Direction.Backward;
  1033. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1034. Assert.Empty (view.InternalSubviews);
  1035. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1036. Assert.True (view.LayoutNeeded);
  1037. Assert.False (view.ChildNeedsDisplay);
  1038. Assert.False (view.addingView);
  1039. view.addingView = true;
  1040. Assert.True (view.addingView);
  1041. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1042. Assert.Equal (1, rcol);
  1043. Assert.Equal (1, rrow);
  1044. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1045. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1046. view.Width = Dim.Fill ();
  1047. view.Height = Dim.Fill ();
  1048. Assert.Equal (10, view.Bounds.Width);
  1049. Assert.Equal (1, view.Bounds.Height);
  1050. view.SetRelativeLayout (top.Bounds);
  1051. Assert.Equal (79, view.Bounds.Width);
  1052. Assert.Equal (24, view.Bounds.Height);
  1053. view.X = 0;
  1054. view.Y = 0;
  1055. view.SetRelativeLayout (top.Bounds);
  1056. Assert.Equal (80, view.Bounds.Width);
  1057. Assert.Equal (25, view.Bounds.Height);
  1058. bool layoutStarted = false;
  1059. view.LayoutStarted += (_) => layoutStarted = true;
  1060. view.OnLayoutStarted (null);
  1061. Assert.True (layoutStarted);
  1062. view.LayoutComplete += (_) => layoutStarted = false;
  1063. view.OnLayoutComplete (null);
  1064. Assert.False (layoutStarted);
  1065. view.X = Pos.Center () - 41;
  1066. view.Y = Pos.Center () - 13;
  1067. view.SetRelativeLayout (top.Bounds);
  1068. view.ViewToScreen (0, 0, out rcol, out rrow);
  1069. Assert.Equal (-41, rcol);
  1070. Assert.Equal (-13, rrow);
  1071. }
  1072. [Fact]
  1073. [AutoInitShutdown]
  1074. public void Enabled_False_Sets_HasFocus_To_False ()
  1075. {
  1076. var wasClicked = false;
  1077. var view = new Button ("Click Me");
  1078. view.Clicked += () => wasClicked = !wasClicked;
  1079. Application.Top.Add (view);
  1080. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1081. Assert.True (wasClicked);
  1082. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1083. Assert.False (wasClicked);
  1084. Assert.True (view.Enabled);
  1085. Assert.True (view.CanFocus);
  1086. Assert.True (view.HasFocus);
  1087. view.Enabled = false;
  1088. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1089. Assert.False (wasClicked);
  1090. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1091. Assert.False (wasClicked);
  1092. Assert.False (view.Enabled);
  1093. Assert.True (view.CanFocus);
  1094. Assert.False (view.HasFocus);
  1095. view.SetFocus ();
  1096. Assert.False (view.HasFocus);
  1097. }
  1098. [Fact]
  1099. [AutoInitShutdown]
  1100. public void Enabled_Sets_Also_Sets_Subviews ()
  1101. {
  1102. var wasClicked = false;
  1103. var button = new Button ("Click Me");
  1104. button.Clicked += () => wasClicked = !wasClicked;
  1105. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1106. win.Add (button);
  1107. Application.Top.Add (win);
  1108. var iterations = 0;
  1109. Application.Iteration += () => {
  1110. iterations++;
  1111. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1112. Assert.True (wasClicked);
  1113. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1114. Assert.False (wasClicked);
  1115. Assert.True (button.Enabled);
  1116. Assert.True (button.CanFocus);
  1117. Assert.True (button.HasFocus);
  1118. Assert.True (win.Enabled);
  1119. Assert.True (win.CanFocus);
  1120. Assert.True (win.HasFocus);
  1121. win.Enabled = false;
  1122. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1123. Assert.False (wasClicked);
  1124. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1125. Assert.False (wasClicked);
  1126. Assert.False (button.Enabled);
  1127. Assert.True (button.CanFocus);
  1128. Assert.False (button.HasFocus);
  1129. Assert.False (win.Enabled);
  1130. Assert.True (win.CanFocus);
  1131. Assert.False (win.HasFocus);
  1132. button.SetFocus ();
  1133. Assert.False (button.HasFocus);
  1134. Assert.False (win.HasFocus);
  1135. win.SetFocus ();
  1136. Assert.False (button.HasFocus);
  1137. Assert.False (win.HasFocus);
  1138. win.Enabled = true;
  1139. win.FocusFirst ();
  1140. Assert.True (button.HasFocus);
  1141. Assert.True (win.HasFocus);
  1142. Application.RequestStop ();
  1143. };
  1144. Application.Run ();
  1145. Assert.Equal (1, iterations);
  1146. }
  1147. [Fact]
  1148. [AutoInitShutdown]
  1149. public void Visible_Sets_Also_Sets_Subviews ()
  1150. {
  1151. var button = new Button ("Click Me");
  1152. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1153. win.Add (button);
  1154. var top = Application.Top;
  1155. top.Add (win);
  1156. var iterations = 0;
  1157. Application.Iteration += () => {
  1158. iterations++;
  1159. Assert.True (button.Visible);
  1160. Assert.True (button.CanFocus);
  1161. Assert.True (button.HasFocus);
  1162. Assert.True (win.Visible);
  1163. Assert.True (win.CanFocus);
  1164. Assert.True (win.HasFocus);
  1165. Assert.True (RunesCount () > 0);
  1166. win.Visible = false;
  1167. Assert.True (button.Visible);
  1168. Assert.True (button.CanFocus);
  1169. Assert.False (button.HasFocus);
  1170. Assert.False (win.Visible);
  1171. Assert.True (win.CanFocus);
  1172. Assert.False (win.HasFocus);
  1173. button.SetFocus ();
  1174. Assert.False (button.HasFocus);
  1175. Assert.False (win.HasFocus);
  1176. win.SetFocus ();
  1177. Assert.False (button.HasFocus);
  1178. Assert.False (win.HasFocus);
  1179. top.Redraw (top.Bounds);
  1180. Assert.True (RunesCount () == 0);
  1181. win.Visible = true;
  1182. win.FocusFirst ();
  1183. Assert.True (button.HasFocus);
  1184. Assert.True (win.HasFocus);
  1185. top.Redraw (top.Bounds);
  1186. Assert.True (RunesCount () > 0);
  1187. Application.RequestStop ();
  1188. };
  1189. Application.Run ();
  1190. Assert.Equal (1, iterations);
  1191. int RunesCount ()
  1192. {
  1193. var contents = ((FakeDriver)Application.Driver).Contents;
  1194. var runesCount = 0;
  1195. for (int i = 0; i < Application.Driver.Rows; i++) {
  1196. for (int j = 0; j < Application.Driver.Cols; j++) {
  1197. if (contents [i, j, 0] != ' ') {
  1198. runesCount++;
  1199. }
  1200. }
  1201. }
  1202. return runesCount;
  1203. }
  1204. }
  1205. [Fact]
  1206. [AutoInitShutdown]
  1207. public void GetTopSuperView_Test ()
  1208. {
  1209. var v1 = new View ();
  1210. var fv1 = new FrameView ();
  1211. fv1.Add (v1);
  1212. var tf1 = new TextField ();
  1213. var w1 = new Window ();
  1214. w1.Add (fv1, tf1);
  1215. var top1 = new Toplevel ();
  1216. top1.Add (w1);
  1217. var v2 = new View ();
  1218. var fv2 = new FrameView ();
  1219. fv2.Add (v2);
  1220. var tf2 = new TextField ();
  1221. var w2 = new Window ();
  1222. w2.Add (fv2, tf2);
  1223. var top2 = new Toplevel ();
  1224. top2.Add (w2);
  1225. Assert.Equal (top1, v1.GetTopSuperView ());
  1226. Assert.Equal (top2, v2.GetTopSuperView ());
  1227. }
  1228. [Fact]
  1229. [AutoInitShutdown]
  1230. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1231. {
  1232. var view = new View () { CanFocus = true };
  1233. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1234. win.Add (view);
  1235. Application.Top.Add (win);
  1236. Application.Begin (Application.Top);
  1237. Assert.True (view.CanFocus);
  1238. Assert.True (view.HasFocus);
  1239. view.CanFocus = false;
  1240. Assert.False (view.CanFocus);
  1241. Assert.False (view.HasFocus);
  1242. Assert.Null (Application.Current.Focused);
  1243. Assert.Null (Application.Current.MostFocused);
  1244. }
  1245. [Fact]
  1246. [AutoInitShutdown]
  1247. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1248. {
  1249. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1250. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1251. win1.Add (view1);
  1252. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1253. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1254. win2.Add (view2);
  1255. Application.Top.Add (win1, win2);
  1256. Application.Begin (Application.Top);
  1257. Assert.True (view1.CanFocus);
  1258. Assert.True (view1.HasFocus);
  1259. Assert.True (view2.CanFocus);
  1260. Assert.True (view2.HasFocus);
  1261. view1.CanFocus = false;
  1262. Assert.False (view1.CanFocus);
  1263. Assert.False (view1.HasFocus);
  1264. Assert.Equal (win2, Application.Current.Focused);
  1265. Assert.Equal (view2, Application.Current.MostFocused);
  1266. }
  1267. [Fact]
  1268. [AutoInitShutdown]
  1269. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1270. {
  1271. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1272. var view12 = new View () { Y = 5, Width = 10, Height = 1, CanFocus = true };
  1273. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1274. win1.Add (view1, view12);
  1275. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1276. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1277. win2.Add (view2);
  1278. Application.Top.Add (win1, win2);
  1279. Application.Begin (Application.Top);
  1280. Assert.True (view1.CanFocus);
  1281. Assert.True (view1.HasFocus);
  1282. Assert.True (view2.CanFocus);
  1283. Assert.True (view2.HasFocus);
  1284. view1.CanFocus = false;
  1285. Assert.False (view1.CanFocus);
  1286. Assert.False (view1.HasFocus);
  1287. Assert.Equal (win1, Application.Current.Focused);
  1288. Assert.Equal (view12, Application.Current.MostFocused);
  1289. }
  1290. [Fact]
  1291. [AutoInitShutdown]
  1292. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1293. {
  1294. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1295. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1296. win1.Add (view1);
  1297. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1298. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1299. win2.Add (view2);
  1300. Application.Top.Add (win1, win2);
  1301. Application.Begin (Application.Top);
  1302. Assert.True (view1.CanFocus);
  1303. Assert.True (view1.HasFocus);
  1304. Assert.True (view2.CanFocus);
  1305. Assert.True (view2.HasFocus);
  1306. win1.CanFocus = false;
  1307. Assert.False (view1.CanFocus);
  1308. Assert.False (view1.HasFocus);
  1309. Assert.False (win1.CanFocus);
  1310. Assert.False (win1.HasFocus);
  1311. Assert.Equal (win2, Application.Current.Focused);
  1312. Assert.Equal (view2, Application.Current.MostFocused);
  1313. }
  1314. [Fact]
  1315. [AutoInitShutdown]
  1316. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1317. {
  1318. var sbQuiting = false;
  1319. var tfQuiting = false;
  1320. var topQuiting = false;
  1321. var sb = new StatusBar (new StatusItem [] {
  1322. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1323. });
  1324. var tf = new TextField ();
  1325. tf.KeyPress += Tf_KeyPress;
  1326. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1327. {
  1328. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1329. obj.Handled = tfQuiting = true;
  1330. }
  1331. }
  1332. var win = new Window ();
  1333. win.Add (sb, tf);
  1334. var top = Application.Top;
  1335. top.KeyPress += Top_KeyPress;
  1336. void Top_KeyPress (View.KeyEventEventArgs obj)
  1337. {
  1338. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1339. obj.Handled = topQuiting = true;
  1340. }
  1341. }
  1342. top.Add (win);
  1343. Application.Begin (top);
  1344. Assert.False (sbQuiting);
  1345. Assert.False (tfQuiting);
  1346. Assert.False (topQuiting);
  1347. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1348. Assert.False (sbQuiting);
  1349. Assert.True (tfQuiting);
  1350. Assert.False (topQuiting);
  1351. tf.KeyPress -= Tf_KeyPress;
  1352. tfQuiting = false;
  1353. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1354. Application.MainLoop.MainIteration ();
  1355. Assert.True (sbQuiting);
  1356. Assert.False (tfQuiting);
  1357. Assert.False (topQuiting);
  1358. sb.RemoveItem (0);
  1359. sbQuiting = false;
  1360. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1361. Application.MainLoop.MainIteration ();
  1362. Assert.False (sbQuiting);
  1363. Assert.False (tfQuiting);
  1364. Assert.True (topQuiting);
  1365. }
  1366. [Fact]
  1367. [AutoInitShutdown]
  1368. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1369. {
  1370. var sbQuiting = false;
  1371. var tfQuiting = false;
  1372. var sb = new StatusBar (new StatusItem [] {
  1373. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1374. });
  1375. var tf = new TextField ();
  1376. tf.KeyPress += Tf_KeyPress;
  1377. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1378. {
  1379. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1380. obj.Handled = tfQuiting = true;
  1381. }
  1382. }
  1383. var win = new Window ();
  1384. win.Add (sb, tf);
  1385. var top = Application.Top;
  1386. top.Add (win);
  1387. Application.Begin (top);
  1388. Assert.False (sbQuiting);
  1389. Assert.False (tfQuiting);
  1390. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1391. Assert.False (sbQuiting);
  1392. Assert.True (tfQuiting);
  1393. tf.KeyPress -= Tf_KeyPress;
  1394. tfQuiting = false;
  1395. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1396. Application.MainLoop.MainIteration ();
  1397. Assert.True (sbQuiting);
  1398. Assert.False (tfQuiting);
  1399. }
  1400. [Fact]
  1401. [AutoInitShutdown]
  1402. public void WindowDispose_CanFocusProblem ()
  1403. {
  1404. // Arrange
  1405. Application.Init ();
  1406. using var top = Toplevel.Create ();
  1407. using var view = new View (
  1408. x: 0,
  1409. y: 1,
  1410. text: nameof (WindowDispose_CanFocusProblem));
  1411. using var window = new Window ();
  1412. top.Add (window);
  1413. window.Add (view);
  1414. // Act
  1415. Application.Begin (top);
  1416. Application.Shutdown ();
  1417. // Assert does Not throw NullReferenceException
  1418. top.SetFocus ();
  1419. }
  1420. [Fact, AutoInitShutdown]
  1421. public void DrawFrame_With_Positive_Positions ()
  1422. {
  1423. var view = new View (new Rect (0, 0, 8, 4));
  1424. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1425. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1426. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1427. Application.Top.Add (view);
  1428. Application.Begin (Application.Top);
  1429. var expected = @"
  1430. ┌──────┐
  1431. │ │
  1432. │ │
  1433. └──────┘
  1434. ";
  1435. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1436. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1437. }
  1438. [Fact, AutoInitShutdown]
  1439. public void DrawFrame_With_Minimum_Size ()
  1440. {
  1441. var view = new View (new Rect (0, 0, 2, 2));
  1442. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1443. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1444. Assert.Equal (new Size (2, 2), new Size (view.Frame.Width, view.Frame.Height));
  1445. Application.Top.Add (view);
  1446. Application.Begin (Application.Top);
  1447. var expected = @"
  1448. ┌┐
  1449. └┘
  1450. ";
  1451. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1452. Assert.Equal (new Rect (0, 0, 2, 2), pos);
  1453. }
  1454. [Fact, AutoInitShutdown]
  1455. public void DrawFrame_With_Negative_Positions ()
  1456. {
  1457. var view = new View (new Rect (-1, 0, 8, 4));
  1458. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1459. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1460. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1461. Application.Top.Add (view);
  1462. Application.Begin (Application.Top);
  1463. var expected = @"
  1464. ──────┐
  1465. ──────┘
  1466. ";
  1467. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1468. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1469. view.Frame = new Rect (-1, -1, 8, 4);
  1470. Application.Refresh ();
  1471. expected = @"
  1472. ──────┘
  1473. ";
  1474. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1475. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1476. view.Frame = new Rect (0, 0, 8, 4);
  1477. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1478. expected = @"
  1479. ┌──────
  1480. └──────
  1481. ";
  1482. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1483. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1484. view.Frame = new Rect (0, 0, 8, 4);
  1485. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1486. }
  1487. [Fact, AutoInitShutdown]
  1488. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1489. {
  1490. var view = new View (new Rect (0, 0, 20, 20));
  1491. view.Add (new Label ("0123456789abcdefghij"));
  1492. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1493. view.Add (new Button (1, 1, "Press me!"));
  1494. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1495. ContentSize = new Size (40, 40),
  1496. ShowHorizontalScrollIndicator = true,
  1497. ShowVerticalScrollIndicator = true
  1498. };
  1499. scrollView.Add (view);
  1500. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1501. win.Add (scrollView);
  1502. Application.Top.Add (win);
  1503. Application.Begin (Application.Top);
  1504. var expected = @"
  1505. ┌ Test ────────────┐
  1506. │ │
  1507. │ 0123456789abcd▲ │
  1508. │ 1[ Press me! ]┬ │
  1509. │ 2 │ │
  1510. │ 3 ┴ │
  1511. │ 4 ░ │
  1512. │ 5 ░ │
  1513. │ 6 ░ │
  1514. │ 7 ░ │
  1515. │ 8 ▼ │
  1516. │ ◄├───┤░░░░░░░► │
  1517. │ │
  1518. └──────────────────┘
  1519. ";
  1520. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1521. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1522. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1523. Application.Top.Redraw (Application.Top.Bounds);
  1524. expected = @"
  1525. ┌ Test ────────────┐
  1526. │ │
  1527. │ 123456789abcde▲ │
  1528. │ [ Press me! ] ┬ │
  1529. │ │ │
  1530. │ ┴ │
  1531. │ ░ │
  1532. │ ░ │
  1533. │ ░ │
  1534. │ ░ │
  1535. │ ▼ │
  1536. │ ◄├───┤░░░░░░░► │
  1537. │ │
  1538. └──────────────────┘
  1539. ";
  1540. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1541. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1542. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1543. Application.Top.Redraw (Application.Top.Bounds);
  1544. expected = @"
  1545. ┌ Test ────────────┐
  1546. │ │
  1547. │ 23456789abcdef▲ │
  1548. │ Press me! ] ┬ │
  1549. │ │ │
  1550. │ ┴ │
  1551. │ ░ │
  1552. │ ░ │
  1553. │ ░ │
  1554. │ ░ │
  1555. │ ▼ │
  1556. │ ◄├────┤░░░░░░► │
  1557. │ │
  1558. └──────────────────┘
  1559. ";
  1560. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1561. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1562. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1563. Application.Top.Redraw (Application.Top.Bounds);
  1564. expected = @"
  1565. ┌ Test ────────────┐
  1566. │ │
  1567. │ 3456789abcdefg▲ │
  1568. │ Press me! ] ┬ │
  1569. │ │ │
  1570. │ ┴ │
  1571. │ ░ │
  1572. │ ░ │
  1573. │ ░ │
  1574. │ ░ │
  1575. │ ▼ │
  1576. │ ◄├────┤░░░░░░► │
  1577. │ │
  1578. └──────────────────┘
  1579. ";
  1580. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1581. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1582. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1583. Application.Top.Redraw (Application.Top.Bounds);
  1584. expected = @"
  1585. ┌ Test ────────────┐
  1586. │ │
  1587. │ 456789abcdefgh▲ │
  1588. │ ress me! ] ┬ │
  1589. │ │ │
  1590. │ ┴ │
  1591. │ ░ │
  1592. │ ░ │
  1593. │ ░ │
  1594. │ ░ │
  1595. │ ▼ │
  1596. │ ◄░├───┤░░░░░░► │
  1597. │ │
  1598. └──────────────────┘
  1599. ";
  1600. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1601. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1602. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1603. Application.Top.Redraw (Application.Top.Bounds);
  1604. expected = @"
  1605. ┌ Test ────────────┐
  1606. │ │
  1607. │ 56789abcdefghi▲ │
  1608. │ ess me! ] ┬ │
  1609. │ │ │
  1610. │ ┴ │
  1611. │ ░ │
  1612. │ ░ │
  1613. │ ░ │
  1614. │ ░ │
  1615. │ ▼ │
  1616. │ ◄░├────┤░░░░░► │
  1617. │ │
  1618. └──────────────────┘
  1619. ";
  1620. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1621. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1622. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1623. Application.Top.Redraw (Application.Top.Bounds);
  1624. expected = @"
  1625. ┌ Test ────────────┐
  1626. │ │
  1627. │ 6789abcdefghij▲ │
  1628. │ ss me! ] ┬ │
  1629. │ │ │
  1630. │ ┴ │
  1631. │ ░ │
  1632. │ ░ │
  1633. │ ░ │
  1634. │ ░ │
  1635. │ ▼ │
  1636. │ ◄░├────┤░░░░░► │
  1637. │ │
  1638. └──────────────────┘
  1639. ";
  1640. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1641. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1642. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1643. Application.Top.Redraw (Application.Top.Bounds);
  1644. expected = @"
  1645. ┌ Test ────────────┐
  1646. │ │
  1647. │ 789abcdefghij ▲ │
  1648. │ s me! ] ┬ │
  1649. │ │ │
  1650. │ ┴ │
  1651. │ ░ │
  1652. │ ░ │
  1653. │ ░ │
  1654. │ ░ │
  1655. │ ▼ │
  1656. │ ◄░░├───┤░░░░░► │
  1657. │ │
  1658. └──────────────────┘
  1659. ";
  1660. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1661. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1662. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1663. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1664. Application.Top.Redraw (Application.Top.Bounds);
  1665. expected = @"
  1666. ┌ Test ────────────┐
  1667. │ │
  1668. │ 1[ Press me! ]▲ │
  1669. │ 2 ┬ │
  1670. │ 3 │ │
  1671. │ 4 ┴ │
  1672. │ 5 ░ │
  1673. │ 6 ░ │
  1674. │ 7 ░ │
  1675. │ 8 ░ │
  1676. │ 9 ▼ │
  1677. │ ◄├───┤░░░░░░░► │
  1678. │ │
  1679. └──────────────────┘
  1680. ";
  1681. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1682. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1683. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1684. Application.Top.Redraw (Application.Top.Bounds);
  1685. expected = @"
  1686. ┌ Test ────────────┐
  1687. │ │
  1688. │ 2 ▲ │
  1689. │ 3 ┬ │
  1690. │ 4 │ │
  1691. │ 5 ┴ │
  1692. │ 6 ░ │
  1693. │ 7 ░ │
  1694. │ 8 ░ │
  1695. │ 9 ░ │
  1696. │ 0 ▼ │
  1697. │ ◄├───┤░░░░░░░► │
  1698. │ │
  1699. └──────────────────┘
  1700. ";
  1701. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1702. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1703. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1704. Application.Top.Redraw (Application.Top.Bounds);
  1705. expected = @"
  1706. ┌ Test ────────────┐
  1707. │ │
  1708. │ 3 ▲ │
  1709. │ 4 ┬ │
  1710. │ 5 │ │
  1711. │ 6 ┴ │
  1712. │ 7 ░ │
  1713. │ 8 ░ │
  1714. │ 9 ░ │
  1715. │ 0 ░ │
  1716. │ ▼ │
  1717. │ ◄├───┤░░░░░░░► │
  1718. │ │
  1719. └──────────────────┘
  1720. ";
  1721. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1722. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1723. }
  1724. [Fact, AutoInitShutdown]
  1725. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1726. {
  1727. var view = new View () {
  1728. Width = Dim.Fill (),
  1729. Height = Dim.Fill ()
  1730. };
  1731. view.DrawContent += e => {
  1732. view.DrawFrame (view.Bounds);
  1733. var savedClip = Application.Driver.Clip;
  1734. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1735. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1736. Application.Driver.Move (1, row + 1);
  1737. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1738. Application.Driver.AddStr ($"{col}");
  1739. }
  1740. }
  1741. Application.Driver.Clip = savedClip;
  1742. };
  1743. Application.Top.Add (view);
  1744. Application.Begin (Application.Top);
  1745. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1746. var expected = @"
  1747. ┌──────────────────┐
  1748. │012345678910111213│
  1749. │012345678910111213│
  1750. │012345678910111213│
  1751. │012345678910111213│
  1752. │012345678910111213│
  1753. │012345678910111213│
  1754. │012345678910111213│
  1755. │012345678910111213│
  1756. └──────────────────┘
  1757. ";
  1758. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1759. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1760. view.Clear ();
  1761. expected = @"
  1762. ";
  1763. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1764. Assert.Equal (Rect.Empty, pos);
  1765. }
  1766. [Fact, AutoInitShutdown]
  1767. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1768. {
  1769. var view = new View () {
  1770. Width = Dim.Fill (),
  1771. Height = Dim.Fill ()
  1772. };
  1773. view.DrawContent += e => {
  1774. view.DrawFrame (view.Bounds);
  1775. var savedClip = Application.Driver.Clip;
  1776. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1777. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1778. Application.Driver.Move (1, row + 1);
  1779. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1780. Application.Driver.AddStr ($"{col}");
  1781. }
  1782. }
  1783. Application.Driver.Clip = savedClip;
  1784. };
  1785. Application.Top.Add (view);
  1786. Application.Begin (Application.Top);
  1787. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1788. var expected = @"
  1789. ┌──────────────────┐
  1790. │012345678910111213│
  1791. │012345678910111213│
  1792. │012345678910111213│
  1793. │012345678910111213│
  1794. │012345678910111213│
  1795. │012345678910111213│
  1796. │012345678910111213│
  1797. │012345678910111213│
  1798. └──────────────────┘
  1799. ";
  1800. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1801. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1802. view.Clear (view.Bounds);
  1803. expected = @"
  1804. ";
  1805. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1806. Assert.Equal (Rect.Empty, pos);
  1807. }
  1808. [Fact]
  1809. public void GetTextFormatterBoundsSize_GetBoundsTextFormatterSize_HotKeySpecifier ()
  1810. {
  1811. var text = "Say Hello 你";
  1812. var horizontalView = new View () { Text = text, AutoSize = true, HotKeySpecifier = '_' };
  1813. var verticalView = new View () {
  1814. Text = text,
  1815. AutoSize = true,
  1816. HotKeySpecifier = '_',
  1817. TextDirection = TextDirection.TopBottom_LeftRight
  1818. };
  1819. Assert.True (horizontalView.AutoSize);
  1820. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1821. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  1822. Assert.Equal (new Size (12, 1), horizontalView.GetBoundsTextFormatterSize ());
  1823. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  1824. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  1825. Assert.True (verticalView.AutoSize);
  1826. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1827. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  1828. Assert.Equal (new Size (2, 11), verticalView.GetBoundsTextFormatterSize ());
  1829. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  1830. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  1831. text = "Say He_llo 你";
  1832. horizontalView.Text = text;
  1833. verticalView.Text = text;
  1834. Assert.True (horizontalView.AutoSize);
  1835. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1836. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  1837. Assert.Equal (new Size (13, 1), horizontalView.GetBoundsTextFormatterSize ());
  1838. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  1839. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  1840. Assert.True (verticalView.AutoSize);
  1841. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1842. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  1843. Assert.Equal (new Size (2, 12), verticalView.GetBoundsTextFormatterSize ());
  1844. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  1845. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  1846. }
  1847. [Fact]
  1848. public void IsAdded_Added_Removed ()
  1849. {
  1850. var top = new Toplevel ();
  1851. var view = new View ();
  1852. Assert.False (view.IsAdded);
  1853. top.Add (view);
  1854. Assert.True (view.IsAdded);
  1855. top.Remove (view);
  1856. Assert.False (view.IsAdded);
  1857. }
  1858. [Fact, AutoInitShutdown]
  1859. public void Visible_Clear_The_View_Output ()
  1860. {
  1861. var label = new Label ("Testing visibility.");
  1862. var win = new Window ();
  1863. win.Add (label);
  1864. var top = Application.Top;
  1865. top.Add (win);
  1866. Application.Begin (top);
  1867. Assert.True (label.Visible);
  1868. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1869. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1870. ┌────────────────────────────┐
  1871. │Testing visibility. │
  1872. │ │
  1873. │ │
  1874. └────────────────────────────┘
  1875. ", output);
  1876. label.Visible = false;
  1877. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1878. ┌────────────────────────────┐
  1879. │ │
  1880. │ │
  1881. │ │
  1882. └────────────────────────────┘
  1883. ", output);
  1884. }
  1885. [Fact, AutoInitShutdown]
  1886. public void ClearOnVisibleFalse_Gets_Sets ()
  1887. {
  1888. var text = "This is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test\nThis is a test";
  1889. var label = new Label (text);
  1890. Application.Top.Add (label);
  1891. var sbv = new ScrollBarView (label, true, false) {
  1892. Size = 100,
  1893. ClearOnVisibleFalse = false
  1894. };
  1895. Application.Begin (Application.Top);
  1896. Assert.True (sbv.Visible);
  1897. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1898. This is a tes▲
  1899. This is a tes┬
  1900. This is a tes┴
  1901. This is a tes░
  1902. This is a tes░
  1903. This is a tes▼
  1904. ", output);
  1905. sbv.Visible = false;
  1906. Assert.False (sbv.Visible);
  1907. Application.Top.Redraw (Application.Top.Bounds);
  1908. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1909. This is a test
  1910. This is a test
  1911. This is a test
  1912. This is a test
  1913. This is a test
  1914. This is a test
  1915. ", output);
  1916. sbv.Visible = true;
  1917. Assert.True (sbv.Visible);
  1918. Application.Top.Redraw (Application.Top.Bounds);
  1919. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1920. This is a tes▲
  1921. This is a tes┬
  1922. This is a tes┴
  1923. This is a tes░
  1924. This is a tes░
  1925. This is a tes▼
  1926. ", output);
  1927. sbv.ClearOnVisibleFalse = true;
  1928. sbv.Visible = false;
  1929. Assert.False (sbv.Visible);
  1930. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1931. This is a tes
  1932. This is a tes
  1933. This is a tes
  1934. This is a tes
  1935. This is a tes
  1936. This is a tes
  1937. ", output);
  1938. }
  1939. [Fact, AutoInitShutdown]
  1940. public void DrawContentComplete_Event_Is_Always_Called ()
  1941. {
  1942. var viewCalled = false;
  1943. var tvCalled = false;
  1944. var view = new View ("View") { Width = 10, Height = 10 };
  1945. view.DrawContentComplete += (e) => viewCalled = true;
  1946. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  1947. tv.DrawContentComplete += (e) => tvCalled = true;
  1948. Application.Top.Add (view, tv);
  1949. Application.Begin (Application.Top);
  1950. Assert.True (viewCalled);
  1951. Assert.True (tvCalled);
  1952. }
  1953. [Fact, AutoInitShutdown]
  1954. public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
  1955. {
  1956. var keyDown = false;
  1957. var keyPress = false;
  1958. var keyUp = false;
  1959. var view = new DerivedView ();
  1960. view.KeyDown += (e) => {
  1961. Assert.Equal (Key.a, e.KeyEvent.Key);
  1962. Assert.False (keyDown);
  1963. Assert.False (view.IsKeyDown);
  1964. e.Handled = true;
  1965. keyDown = true;
  1966. };
  1967. view.KeyPress += (e) => {
  1968. Assert.Equal (Key.a, e.KeyEvent.Key);
  1969. Assert.False (keyPress);
  1970. Assert.False (view.IsKeyPress);
  1971. e.Handled = true;
  1972. keyPress = true;
  1973. };
  1974. view.KeyUp += (e) => {
  1975. Assert.Equal (Key.a, e.KeyEvent.Key);
  1976. Assert.False (keyUp);
  1977. Assert.False (view.IsKeyUp);
  1978. e.Handled = true;
  1979. keyUp = true;
  1980. };
  1981. Application.Top.Add (view);
  1982. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
  1983. Application.Iteration += () => Application.RequestStop ();
  1984. Assert.True (view.CanFocus);
  1985. Application.Run ();
  1986. Application.Shutdown ();
  1987. Assert.True (keyDown);
  1988. Assert.True (keyPress);
  1989. Assert.True (keyUp);
  1990. Assert.False (view.IsKeyDown);
  1991. Assert.False (view.IsKeyPress);
  1992. Assert.False (view.IsKeyUp);
  1993. }
  1994. public class DerivedView : View {
  1995. public DerivedView ()
  1996. {
  1997. CanFocus = true;
  1998. }
  1999. public bool IsKeyDown { get; set; }
  2000. public bool IsKeyPress { get; set; }
  2001. public bool IsKeyUp { get; set; }
  2002. public override ustring Text { get; set; }
  2003. public override bool OnKeyDown (KeyEvent keyEvent)
  2004. {
  2005. IsKeyDown = true;
  2006. return true;
  2007. }
  2008. public override bool ProcessKey (KeyEvent keyEvent)
  2009. {
  2010. IsKeyPress = true;
  2011. return true;
  2012. }
  2013. public override bool OnKeyUp (KeyEvent keyEvent)
  2014. {
  2015. IsKeyUp = true;
  2016. return true;
  2017. }
  2018. public void CorrectRedraw (Rect bounds)
  2019. {
  2020. // Clear the old and new frame area
  2021. Clear (NeedDisplay);
  2022. DrawText ();
  2023. }
  2024. public void IncorrectRedraw (Rect bounds)
  2025. {
  2026. // Clear only the new frame area
  2027. Clear ();
  2028. DrawText ();
  2029. }
  2030. private void DrawText ()
  2031. {
  2032. var idx = 0;
  2033. for (int r = 0; r < Frame.Height; r++) {
  2034. for (int c = 0; c < Frame.Width; c++) {
  2035. if (idx < Text.Length) {
  2036. var rune = Text [idx];
  2037. if (rune != '\n') {
  2038. AddRune (c, r, Text [idx]);
  2039. }
  2040. idx++;
  2041. if (rune == '\n') {
  2042. break;
  2043. }
  2044. }
  2045. }
  2046. }
  2047. ClearLayoutNeeded ();
  2048. ClearNeedsDisplay ();
  2049. }
  2050. }
  2051. [Theory, AutoInitShutdown]
  2052. [InlineData (true, false, false)]
  2053. [InlineData (true, true, false)]
  2054. [InlineData (true, true, true)]
  2055. public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
  2056. {
  2057. var keyDown = false;
  2058. var keyPress = false;
  2059. var keyUp = false;
  2060. var view = new DerivedView ();
  2061. view.KeyDown += (e) => {
  2062. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2063. Assert.Equal (shift, e.KeyEvent.IsShift);
  2064. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2065. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2066. Assert.False (keyDown);
  2067. Assert.False (view.IsKeyDown);
  2068. keyDown = true;
  2069. };
  2070. view.KeyPress += (e) => {
  2071. keyPress = true;
  2072. };
  2073. view.KeyUp += (e) => {
  2074. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2075. Assert.Equal (shift, e.KeyEvent.IsShift);
  2076. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2077. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2078. Assert.False (keyUp);
  2079. Assert.False (view.IsKeyUp);
  2080. keyUp = true;
  2081. };
  2082. Application.Top.Add (view);
  2083. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
  2084. Application.Iteration += () => Application.RequestStop ();
  2085. Assert.True (view.CanFocus);
  2086. Application.Run ();
  2087. Application.Shutdown ();
  2088. Assert.True (keyDown);
  2089. Assert.False (keyPress);
  2090. Assert.True (keyUp);
  2091. Assert.True (view.IsKeyDown);
  2092. Assert.False (view.IsKeyPress);
  2093. Assert.True (view.IsKeyUp);
  2094. }
  2095. [Fact, AutoInitShutdown]
  2096. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  2097. {
  2098. var view1Leave = false;
  2099. var subView1Leave = false;
  2100. var subView1subView1Leave = false;
  2101. var top = Application.Top;
  2102. var view1 = new View { CanFocus = true };
  2103. var subView1 = new View { CanFocus = true };
  2104. var subView1subView1 = new View { CanFocus = true };
  2105. view1.Leave += (e) => {
  2106. view1Leave = true;
  2107. };
  2108. subView1.Leave += (e) => {
  2109. subView1.Remove (subView1subView1);
  2110. subView1Leave = true;
  2111. };
  2112. view1.Add (subView1);
  2113. subView1subView1.Leave += (e) => {
  2114. // This is never invoked
  2115. subView1subView1Leave = true;
  2116. };
  2117. subView1.Add (subView1subView1);
  2118. var view2 = new View { CanFocus = true };
  2119. top.Add (view1, view2);
  2120. Application.Begin (top);
  2121. view2.SetFocus ();
  2122. Assert.True (view1Leave);
  2123. Assert.True (subView1Leave);
  2124. Assert.False (subView1subView1Leave);
  2125. }
  2126. [Fact, AutoInitShutdown]
  2127. public void GetNormalColor_ColorScheme ()
  2128. {
  2129. var view = new View { ColorScheme = Colors.Base };
  2130. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  2131. view.Enabled = false;
  2132. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  2133. }
  2134. [Fact, AutoInitShutdown]
  2135. public void GetHotNormalColor_ColorScheme ()
  2136. {
  2137. var view = new View { ColorScheme = Colors.Base };
  2138. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  2139. view.Enabled = false;
  2140. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  2141. }
  2142. [Theory, AutoInitShutdown]
  2143. [InlineData (true)]
  2144. [InlineData (false)]
  2145. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  2146. {
  2147. var root = new View () { Width = 20, Height = 10 };
  2148. var v = label == true ?
  2149. new Label (new string ('c', 100)) {
  2150. Width = Dim.Fill ()
  2151. } :
  2152. (View)new TextView () {
  2153. Height = 1,
  2154. Text = new string ('c', 100),
  2155. Width = Dim.Fill ()
  2156. };
  2157. root.Add (v);
  2158. Application.Top.Add (root);
  2159. Application.Begin (Application.Top);
  2160. if (label) {
  2161. Assert.True (v.AutoSize);
  2162. Assert.False (v.CanFocus);
  2163. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  2164. } else {
  2165. Assert.False (v.AutoSize);
  2166. Assert.True (v.CanFocus);
  2167. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  2168. }
  2169. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2170. cccccccccccccccccccc", output);
  2171. var attributes = new Attribute [] {
  2172. Colors.TopLevel.Normal,
  2173. Colors.TopLevel.Focus,
  2174. };
  2175. if (label) {
  2176. TestHelpers.AssertDriverColorsAre (@"
  2177. 000000000000000000000", attributes);
  2178. } else {
  2179. TestHelpers.AssertDriverColorsAre (@"
  2180. 111111111111111111110", attributes);
  2181. }
  2182. if (label) {
  2183. root.CanFocus = true;
  2184. v.CanFocus = true;
  2185. Assert.False (v.HasFocus);
  2186. v.SetFocus ();
  2187. Assert.True (v.HasFocus);
  2188. Application.Refresh ();
  2189. TestHelpers.AssertDriverColorsAre (@"
  2190. 111111111111111111110", attributes);
  2191. }
  2192. }
  2193. [Fact, AutoInitShutdown]
  2194. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2195. {
  2196. var label = new Label ("At 0,0");
  2197. var view = new DerivedView () {
  2198. X = 2,
  2199. Y = 2,
  2200. Width = 30,
  2201. Height = 2,
  2202. Text = "A text with some long width\n and also with two lines."
  2203. };
  2204. var top = Application.Top;
  2205. top.Add (label, view);
  2206. Application.Begin (top);
  2207. view.CorrectRedraw (view.Bounds);
  2208. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2209. At 0,0
  2210. A text with some long width
  2211. and also with two lines. ", output);
  2212. view.Frame = new Rect (1, 1, 10, 1);
  2213. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2214. Assert.Equal (new Rect (1, 1, 31, 3), view.NeedDisplay);
  2215. view.CorrectRedraw (view.Bounds);
  2216. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2217. At 0,0
  2218. A text wit", output);
  2219. }
  2220. [Fact, AutoInitShutdown]
  2221. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2222. {
  2223. var label = new Label ("At 0,0");
  2224. var view = new DerivedView () {
  2225. X = 2,
  2226. Y = 2,
  2227. Width = 30,
  2228. Height = 2,
  2229. Text = "A text with some long width\n and also with two lines."
  2230. };
  2231. var top = Application.Top;
  2232. top.Add (label, view);
  2233. Application.Begin (top);
  2234. view.CorrectRedraw (view.Bounds);
  2235. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2236. At 0,0
  2237. A text with some long width
  2238. and also with two lines. ", output);
  2239. view.X = 1;
  2240. view.Y = 1;
  2241. view.Width = 10;
  2242. view.Height = 1;
  2243. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2244. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2245. Assert.Equal (new Rect (1, 1, 31, 3), view.NeedDisplay);
  2246. view.CorrectRedraw (view.Bounds);
  2247. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2248. At 0,0
  2249. A text wit", output);
  2250. }
  2251. [Fact, AutoInitShutdown]
  2252. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2253. {
  2254. var label = new Label ("At 0,0");
  2255. var view = new DerivedView () {
  2256. X = 2,
  2257. Y = 2,
  2258. Width = 30,
  2259. Height = 2,
  2260. Text = "A text with some long width\n and also with two lines."
  2261. };
  2262. var top = Application.Top;
  2263. top.Add (label, view);
  2264. Application.Begin (top);
  2265. view.IncorrectRedraw (view.Bounds);
  2266. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2267. At 0,0
  2268. A text with some long width
  2269. and also with two lines. ", output);
  2270. view.Frame = new Rect (1, 1, 10, 1);
  2271. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2272. Assert.Equal (new Rect (1, 1, 31, 3), view.NeedDisplay);
  2273. view.IncorrectRedraw (view.Bounds);
  2274. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2275. At 0,0
  2276. A text wit
  2277. A text with some long width
  2278. and also with two lines. ", output);
  2279. }
  2280. [Fact, AutoInitShutdown]
  2281. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2282. {
  2283. var label = new Label ("At 0,0");
  2284. var view = new DerivedView () {
  2285. X = 2,
  2286. Y = 2,
  2287. Width = 30,
  2288. Height = 2,
  2289. Text = "A text with some long width\n and also with two lines."
  2290. };
  2291. var top = Application.Top;
  2292. top.Add (label, view);
  2293. Application.Begin (top);
  2294. view.IncorrectRedraw (view.Bounds);
  2295. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2296. At 0,0
  2297. A text with some long width
  2298. and also with two lines. ", output);
  2299. view.X = 1;
  2300. view.Y = 1;
  2301. view.Width = 10;
  2302. view.Height = 1;
  2303. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2304. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2305. Assert.Equal (new Rect (1, 1, 31, 3), view.NeedDisplay);
  2306. view.IncorrectRedraw (view.Bounds);
  2307. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2308. At 0,0
  2309. A text wit
  2310. A text with some long width
  2311. and also with two lines. ", output);
  2312. }
  2313. [Fact, AutoInitShutdown]
  2314. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2315. {
  2316. var label = new Label ("At 0,0");
  2317. var view = new DerivedView () {
  2318. X = 2,
  2319. Y = 2,
  2320. Width = 30,
  2321. Height = 2,
  2322. Text = "A text with some long width\n and also with two lines."
  2323. };
  2324. var top = Application.Top;
  2325. top.Add (label, view);
  2326. Application.Begin (top);
  2327. view.CorrectRedraw (view.Bounds);
  2328. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2329. At 0,0
  2330. A text with some long width
  2331. and also with two lines. ", output);
  2332. view.Frame = new Rect (3, 3, 10, 1);
  2333. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2334. Assert.Equal (new Rect (2, 2, 30, 2), view.NeedDisplay);
  2335. view.CorrectRedraw (view.Bounds);
  2336. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2337. At 0,0
  2338. A text wit", output);
  2339. }
  2340. [Fact, AutoInitShutdown]
  2341. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2342. {
  2343. var label = new Label ("At 0,0");
  2344. var view = new DerivedView () {
  2345. X = 2,
  2346. Y = 2,
  2347. Width = 30,
  2348. Height = 2,
  2349. Text = "A text with some long width\n and also with two lines."
  2350. };
  2351. var top = Application.Top;
  2352. top.Add (label, view);
  2353. Application.Begin (top);
  2354. view.CorrectRedraw (view.Bounds);
  2355. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2356. At 0,0
  2357. A text with some long width
  2358. and also with two lines. ", output);
  2359. view.X = 3;
  2360. view.Y = 3;
  2361. view.Width = 10;
  2362. view.Height = 1;
  2363. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2364. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2365. Assert.Equal (new Rect (2, 2, 30, 2), view.NeedDisplay);
  2366. view.CorrectRedraw (view.Bounds);
  2367. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2368. At 0,0
  2369. A text wit", output);
  2370. }
  2371. [Fact, AutoInitShutdown]
  2372. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2373. {
  2374. var label = new Label ("At 0,0");
  2375. var view = new DerivedView () {
  2376. X = 2,
  2377. Y = 2,
  2378. Width = 30,
  2379. Height = 2,
  2380. Text = "A text with some long width\n and also with two lines."
  2381. };
  2382. var top = Application.Top;
  2383. top.Add (label, view);
  2384. Application.Begin (top);
  2385. view.IncorrectRedraw (view.Bounds);
  2386. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2387. At 0,0
  2388. A text with some long width
  2389. and also with two lines. ", output);
  2390. view.Frame = new Rect (3, 3, 10, 1);
  2391. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2392. Assert.Equal (new Rect (2, 2, 30, 2), view.NeedDisplay);
  2393. view.IncorrectRedraw (view.Bounds);
  2394. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2395. At 0,0
  2396. A text with some long width
  2397. A text witith two lines. ", output);
  2398. }
  2399. [Fact, AutoInitShutdown]
  2400. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2401. {
  2402. var label = new Label ("At 0,0");
  2403. var view = new DerivedView () {
  2404. X = 2,
  2405. Y = 2,
  2406. Width = 30,
  2407. Height = 2,
  2408. Text = "A text with some long width\n and also with two lines."
  2409. };
  2410. var top = Application.Top;
  2411. top.Add (label, view);
  2412. Application.Begin (top);
  2413. view.IncorrectRedraw (view.Bounds);
  2414. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2415. At 0,0
  2416. A text with some long width
  2417. and also with two lines. ", output);
  2418. view.X = 3;
  2419. view.Y = 3;
  2420. view.Width = 10;
  2421. view.Height = 1;
  2422. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2423. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2424. Assert.Equal (new Rect (2, 2, 30, 2), view.NeedDisplay);
  2425. view.IncorrectRedraw (view.Bounds);
  2426. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2427. At 0,0
  2428. A text with some long width
  2429. A text witith two lines. ", output);
  2430. }
  2431. [Fact, AutoInitShutdown]
  2432. public void Test_Nested_Views_With_Height_Equal_To_One ()
  2433. {
  2434. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  2435. var top = new View () { Width = Dim.Fill (), Height = 1 };
  2436. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  2437. top.Add (new Label ("111"));
  2438. v.Add (top);
  2439. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  2440. bottom.Add (new Label ("222"));
  2441. v.Add (bottom);
  2442. v.LayoutSubviews ();
  2443. v.Redraw (v.Bounds);
  2444. string looksLike =
  2445. @"
  2446. 111
  2447. ───────────
  2448. 222";
  2449. TestHelpers.AssertDriverContentsAre (looksLike, output);
  2450. }
  2451. }
  2452. }