ViewTests.cs 88 KB

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