ViewTests.cs 83 KB

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