ViewTests.cs 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.Core {
  8. public class ViewTests {
  9. readonly ITestOutputHelper output;
  10. public ViewTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact]
  15. public void New_Initializes ()
  16. {
  17. // Parameterless
  18. var r = new View ();
  19. Assert.NotNull (r);
  20. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  21. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  22. Assert.False (r.CanFocus);
  23. Assert.False (r.HasFocus);
  24. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  25. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  26. Assert.Null (r.Focused);
  27. Assert.Null (r.ColorScheme);
  28. Assert.Null (r.Width);
  29. Assert.Null (r.Height);
  30. Assert.Null (r.X);
  31. Assert.Null (r.Y);
  32. Assert.False (r.IsCurrentTop);
  33. Assert.Empty (r.Id);
  34. Assert.Empty (r.Subviews);
  35. Assert.False (r.WantContinuousButtonPressed);
  36. Assert.False (r.WantMousePositionReports);
  37. Assert.Null (r.SuperView);
  38. Assert.Null (r.MostFocused);
  39. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  40. // Empty Rect
  41. r = new View (Rect.Empty);
  42. Assert.NotNull (r);
  43. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  44. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  45. Assert.False (r.CanFocus);
  46. Assert.False (r.HasFocus);
  47. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  48. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  49. Assert.Null (r.Focused);
  50. Assert.Null (r.ColorScheme);
  51. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  52. Assert.Null (r.Height); // avoiding Dim errors.
  53. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  54. Assert.Null (r.Y); // avoiding Pos errors.
  55. Assert.False (r.IsCurrentTop);
  56. Assert.Empty (r.Id);
  57. Assert.Empty (r.Subviews);
  58. Assert.False (r.WantContinuousButtonPressed);
  59. Assert.False (r.WantMousePositionReports);
  60. Assert.Null (r.SuperView);
  61. Assert.Null (r.MostFocused);
  62. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  63. // Rect with values
  64. r = new View (new Rect (1, 2, 3, 4));
  65. Assert.NotNull (r);
  66. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  67. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  68. Assert.False (r.CanFocus);
  69. Assert.False (r.HasFocus);
  70. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  71. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  72. Assert.Null (r.Focused);
  73. Assert.Null (r.ColorScheme);
  74. Assert.Null (r.Width);
  75. Assert.Null (r.Height);
  76. Assert.Null (r.X);
  77. Assert.Null (r.Y);
  78. Assert.False (r.IsCurrentTop);
  79. Assert.Empty (r.Id);
  80. Assert.Empty (r.Subviews);
  81. Assert.False (r.WantContinuousButtonPressed);
  82. Assert.False (r.WantMousePositionReports);
  83. Assert.Null (r.SuperView);
  84. Assert.Null (r.MostFocused);
  85. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  86. // Initializes a view with a vertical direction
  87. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  88. Assert.NotNull (r);
  89. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  90. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  91. Assert.False (r.CanFocus);
  92. Assert.False (r.HasFocus);
  93. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  94. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  95. Assert.Null (r.Focused);
  96. Assert.Null (r.ColorScheme);
  97. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  98. Assert.Null (r.Height); // avoiding Dim errors.
  99. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  100. Assert.Null (r.Y); // avoiding Pos errors.
  101. Assert.False (r.IsCurrentTop);
  102. Assert.Empty (r.Id);
  103. Assert.Empty (r.Subviews);
  104. Assert.False (r.WantContinuousButtonPressed);
  105. Assert.False (r.WantMousePositionReports);
  106. Assert.Null (r.SuperView);
  107. Assert.Null (r.MostFocused);
  108. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  109. }
  110. [Fact]
  111. public void New_Methods_Return_False ()
  112. {
  113. var r = new View ();
  114. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  115. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  116. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  120. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  121. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  122. Assert.False (r.OnEnter (new View ()));
  123. Assert.False (r.OnLeave (new View ()));
  124. // TODO: Add more
  125. }
  126. [Fact]
  127. public void TopologicalSort_Missing_Add ()
  128. {
  129. var root = new View ();
  130. var sub1 = new View ();
  131. root.Add (sub1);
  132. var sub2 = new View ();
  133. sub1.Width = Dim.Width (sub2);
  134. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  135. sub2.Width = Dim.Width (sub1);
  136. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  137. }
  138. [Fact]
  139. public void TopologicalSort_Recursive_Ref ()
  140. {
  141. var root = new View ();
  142. var sub1 = new View ();
  143. root.Add (sub1);
  144. var sub2 = new View ();
  145. root.Add (sub2);
  146. sub2.Width = Dim.Width (sub2);
  147. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  148. }
  149. [Fact]
  150. public void Added_Removed ()
  151. {
  152. var v = new View (new Rect (0, 0, 10, 24));
  153. var t = new View ();
  154. v.Added += (View e) => {
  155. Assert.True (v.SuperView == e);
  156. };
  157. v.Removed += (View e) => {
  158. Assert.True (v.SuperView == null);
  159. };
  160. t.Add (v);
  161. Assert.True (t.Subviews.Count == 1);
  162. t.Remove (v);
  163. Assert.True (t.Subviews.Count == 0);
  164. }
  165. [Fact]
  166. public void Subviews_TabIndexes_AreEqual ()
  167. {
  168. var r = new View ();
  169. var v1 = new View () { CanFocus = true };
  170. var v2 = new View () { CanFocus = true };
  171. var v3 = new View () { CanFocus = true };
  172. r.Add (v1, v2, v3);
  173. Assert.True (r.Subviews.IndexOf (v1) == 0);
  174. Assert.True (r.Subviews.IndexOf (v2) == 1);
  175. Assert.True (r.Subviews.IndexOf (v3) == 2);
  176. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  177. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  178. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  179. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  180. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  181. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  182. }
  183. [Fact]
  184. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  185. {
  186. var r = new View ();
  187. var v1 = new View () { CanFocus = true };
  188. var v2 = new View () { CanFocus = true };
  189. var v3 = new View () { CanFocus = true };
  190. r.Add (v1, v2, v3);
  191. r.BringSubviewToFront (v1);
  192. Assert.True (r.Subviews.IndexOf (v1) == 2);
  193. Assert.True (r.Subviews.IndexOf (v2) == 0);
  194. Assert.True (r.Subviews.IndexOf (v3) == 1);
  195. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  196. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  197. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  198. }
  199. [Fact]
  200. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  201. {
  202. var r = new View ();
  203. var v1 = new View () { CanFocus = true };
  204. var v2 = new View () { CanFocus = true };
  205. var v3 = new View () { CanFocus = true };
  206. r.Add (v1, v2, v3);
  207. r.BringSubviewForward (v1);
  208. Assert.True (r.Subviews.IndexOf (v1) == 1);
  209. Assert.True (r.Subviews.IndexOf (v2) == 0);
  210. Assert.True (r.Subviews.IndexOf (v3) == 2);
  211. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  212. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  213. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  214. }
  215. [Fact]
  216. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  217. {
  218. var r = new View ();
  219. var v1 = new View () { CanFocus = true };
  220. var v2 = new View () { CanFocus = true };
  221. var v3 = new View () { CanFocus = true };
  222. r.Add (v1, v2, v3);
  223. r.SendSubviewToBack (v3);
  224. Assert.True (r.Subviews.IndexOf (v1) == 1);
  225. Assert.True (r.Subviews.IndexOf (v2) == 2);
  226. Assert.True (r.Subviews.IndexOf (v3) == 0);
  227. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  228. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  229. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  230. }
  231. [Fact]
  232. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  233. {
  234. var r = new View ();
  235. var v1 = new View () { CanFocus = true };
  236. var v2 = new View () { CanFocus = true };
  237. var v3 = new View () { CanFocus = true };
  238. r.Add (v1, v2, v3);
  239. r.SendSubviewBackwards (v3);
  240. Assert.True (r.Subviews.IndexOf (v1) == 0);
  241. Assert.True (r.Subviews.IndexOf (v2) == 2);
  242. Assert.True (r.Subviews.IndexOf (v3) == 1);
  243. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  244. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  245. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  246. }
  247. [Fact]
  248. public void TabIndex_Set_CanFocus_ValidValues ()
  249. {
  250. var r = new View ();
  251. var v1 = new View () { CanFocus = true };
  252. var v2 = new View () { CanFocus = true };
  253. var v3 = new View () { CanFocus = true };
  254. r.Add (v1, v2, v3);
  255. v1.TabIndex = 1;
  256. Assert.True (r.Subviews.IndexOf (v1) == 0);
  257. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  258. v1.TabIndex = 2;
  259. Assert.True (r.Subviews.IndexOf (v1) == 0);
  260. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  261. }
  262. [Fact]
  263. public void TabIndex_Set_CanFocus_HigherValues ()
  264. {
  265. var r = new View ();
  266. var v1 = new View () { CanFocus = true };
  267. var v2 = new View () { CanFocus = true };
  268. var v3 = new View () { CanFocus = true };
  269. r.Add (v1, v2, v3);
  270. v1.TabIndex = 3;
  271. Assert.True (r.Subviews.IndexOf (v1) == 0);
  272. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  273. }
  274. [Fact]
  275. public void TabIndex_Set_CanFocus_LowerValues ()
  276. {
  277. var r = new View ();
  278. var v1 = new View () { CanFocus = true };
  279. var v2 = new View () { CanFocus = true };
  280. var v3 = new View () { CanFocus = true };
  281. r.Add (v1, v2, v3);
  282. v1.TabIndex = -1;
  283. Assert.True (r.Subviews.IndexOf (v1) == 0);
  284. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  285. }
  286. [Fact]
  287. public void TabIndex_Set_CanFocus_False ()
  288. {
  289. var r = new View ();
  290. var v1 = new View () { CanFocus = true };
  291. var v2 = new View () { CanFocus = true };
  292. var v3 = new View () { CanFocus = true };
  293. r.Add (v1, v2, v3);
  294. v1.CanFocus = false;
  295. v1.TabIndex = 0;
  296. Assert.True (r.Subviews.IndexOf (v1) == 0);
  297. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  298. Assert.Equal (-1, v1.TabIndex);
  299. }
  300. [Fact]
  301. public void TabIndex_Set_CanFocus_False_To_True ()
  302. {
  303. var r = new View ();
  304. var v1 = new View ();
  305. var v2 = new View () { CanFocus = true };
  306. var v3 = new View () { CanFocus = true };
  307. r.Add (v1, v2, v3);
  308. v1.CanFocus = true;
  309. v1.TabIndex = 1;
  310. Assert.True (r.Subviews.IndexOf (v1) == 0);
  311. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  312. }
  313. [Fact]
  314. public void TabStop_And_CanFocus_Are_All_True ()
  315. {
  316. var r = new View ();
  317. var v1 = new View () { CanFocus = true };
  318. var v2 = new View () { CanFocus = true };
  319. var v3 = new View () { CanFocus = true };
  320. r.Add (v1, v2, v3);
  321. r.FocusNext ();
  322. Assert.True (v1.HasFocus);
  323. Assert.False (v2.HasFocus);
  324. Assert.False (v3.HasFocus);
  325. r.FocusNext ();
  326. Assert.False (v1.HasFocus);
  327. Assert.True (v2.HasFocus);
  328. Assert.False (v3.HasFocus);
  329. r.FocusNext ();
  330. Assert.False (v1.HasFocus);
  331. Assert.False (v2.HasFocus);
  332. Assert.True (v3.HasFocus);
  333. }
  334. [Fact]
  335. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  336. {
  337. var r = new View ();
  338. var v1 = new View ();
  339. var v2 = new View ();
  340. var v3 = new View ();
  341. r.Add (v1, v2, v3);
  342. r.FocusNext ();
  343. Assert.False (v1.HasFocus);
  344. Assert.False (v2.HasFocus);
  345. Assert.False (v3.HasFocus);
  346. r.FocusNext ();
  347. Assert.False (v1.HasFocus);
  348. Assert.False (v2.HasFocus);
  349. Assert.False (v3.HasFocus);
  350. r.FocusNext ();
  351. Assert.False (v1.HasFocus);
  352. Assert.False (v2.HasFocus);
  353. Assert.False (v3.HasFocus);
  354. }
  355. [Fact]
  356. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  357. {
  358. var r = new View ();
  359. var v1 = new View () { CanFocus = true, TabStop = false };
  360. var v2 = new View () { CanFocus = true, TabStop = false };
  361. var v3 = new View () { CanFocus = true, TabStop = false };
  362. r.Add (v1, v2, v3);
  363. r.FocusNext ();
  364. Assert.False (v1.HasFocus);
  365. Assert.False (v2.HasFocus);
  366. Assert.False (v3.HasFocus);
  367. r.FocusNext ();
  368. Assert.False (v1.HasFocus);
  369. Assert.False (v2.HasFocus);
  370. Assert.False (v3.HasFocus);
  371. r.FocusNext ();
  372. Assert.False (v1.HasFocus);
  373. Assert.False (v2.HasFocus);
  374. Assert.False (v3.HasFocus);
  375. }
  376. [Fact]
  377. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  378. {
  379. var r = new View ();
  380. var v1 = new View () { CanFocus = true, TabStop = false };
  381. var v2 = new View () { CanFocus = false, TabStop = true };
  382. var v3 = new View () { CanFocus = false, TabStop = false };
  383. r.Add (v1, v2, v3);
  384. r.FocusNext ();
  385. Assert.False (v1.HasFocus);
  386. Assert.False (v2.HasFocus);
  387. Assert.False (v3.HasFocus);
  388. r.FocusNext ();
  389. Assert.False (v1.HasFocus);
  390. Assert.False (v2.HasFocus);
  391. Assert.False (v3.HasFocus);
  392. r.FocusNext ();
  393. Assert.False (v1.HasFocus);
  394. Assert.False (v2.HasFocus);
  395. Assert.False (v3.HasFocus);
  396. }
  397. [Fact]
  398. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  399. {
  400. var r = new View ();
  401. var v1 = new View ();
  402. var v2 = new View ();
  403. var v3 = new View ();
  404. r.Add (v1, v2, v3);
  405. r.FocusNext ();
  406. Assert.False (v1.HasFocus);
  407. Assert.False (v2.HasFocus);
  408. Assert.False (v3.HasFocus);
  409. v1.CanFocus = true;
  410. r.FocusNext ();
  411. Assert.True (v1.HasFocus);
  412. Assert.False (v2.HasFocus);
  413. Assert.False (v3.HasFocus);
  414. v2.CanFocus = true;
  415. r.FocusNext ();
  416. Assert.False (v1.HasFocus);
  417. Assert.True (v2.HasFocus);
  418. Assert.False (v3.HasFocus);
  419. v3.CanFocus = true;
  420. r.FocusNext ();
  421. Assert.False (v1.HasFocus);
  422. Assert.False (v2.HasFocus);
  423. Assert.True (v3.HasFocus);
  424. }
  425. [Fact]
  426. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  427. {
  428. var r = new View ();
  429. var v1 = new View () { CanFocus = true, TabStop = false };
  430. var v2 = new View () { CanFocus = true, TabStop = false };
  431. var v3 = new View () { CanFocus = true, TabStop = false };
  432. r.Add (v1, v2, v3);
  433. r.FocusNext ();
  434. Assert.False (v1.HasFocus);
  435. Assert.False (v2.HasFocus);
  436. Assert.False (v3.HasFocus);
  437. v1.TabStop = true;
  438. r.FocusNext ();
  439. Assert.True (v1.HasFocus);
  440. Assert.False (v2.HasFocus);
  441. Assert.False (v3.HasFocus);
  442. v2.TabStop = true;
  443. r.FocusNext ();
  444. Assert.False (v1.HasFocus);
  445. Assert.True (v2.HasFocus);
  446. Assert.False (v3.HasFocus);
  447. v3.TabStop = true;
  448. r.FocusNext ();
  449. Assert.False (v1.HasFocus);
  450. Assert.False (v2.HasFocus);
  451. Assert.True (v3.HasFocus);
  452. }
  453. [Fact]
  454. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  455. {
  456. var r = new View ();
  457. var v1 = new View ("1");
  458. var v2 = new View ("2");
  459. var v3 = new View ("3");
  460. r.Add (v1, v2, v3);
  461. v2.CanFocus = true;
  462. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  463. Assert.Equal (0, v2.TabIndex);
  464. Assert.True (v2.TabStop);
  465. v1.CanFocus = true;
  466. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  467. Assert.Equal (1, v1.TabIndex);
  468. Assert.True (v1.TabStop);
  469. v1.TabIndex = 2;
  470. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  471. Assert.Equal (1, v1.TabIndex);
  472. v3.CanFocus = true;
  473. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  474. Assert.Equal (1, v1.TabIndex);
  475. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  476. Assert.Equal (2, v3.TabIndex);
  477. Assert.True (v3.TabStop);
  478. v2.CanFocus = false;
  479. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  480. Assert.Equal (1, v1.TabIndex);
  481. Assert.True (v1.TabStop);
  482. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  483. Assert.Equal (-1, v2.TabIndex);
  484. Assert.False (v2.TabStop);
  485. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  486. Assert.Equal (2, v3.TabIndex);
  487. Assert.True (v3.TabStop);
  488. }
  489. [Fact]
  490. public void Initialized_Event_Comparing_With_Added_Event ()
  491. {
  492. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  493. var t = new Toplevel () { Id = "0", };
  494. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  495. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  496. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  497. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  498. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  499. w.Added += (e) => {
  500. Assert.Equal (e.Frame.Width, w.Frame.Width);
  501. Assert.Equal (e.Frame.Height, w.Frame.Height);
  502. };
  503. v1.Added += (e) => {
  504. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  505. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  506. };
  507. v2.Added += (e) => {
  508. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  509. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  510. };
  511. sv1.Added += (e) => {
  512. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  513. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  514. };
  515. t.Initialized += (s, e) => {
  516. tc++;
  517. Assert.Equal (1, tc);
  518. Assert.Equal (1, wc);
  519. Assert.Equal (1, v1c);
  520. Assert.Equal (1, v2c);
  521. Assert.Equal (1, sv1c);
  522. Assert.True (t.CanFocus);
  523. Assert.True (w.CanFocus);
  524. Assert.False (v1.CanFocus);
  525. Assert.False (v2.CanFocus);
  526. Assert.False (sv1.CanFocus);
  527. Application.Refresh ();
  528. };
  529. w.Initialized += (s, e) => {
  530. wc++;
  531. Assert.Equal (t.Frame.Width, w.Frame.Width);
  532. Assert.Equal (t.Frame.Height, w.Frame.Height);
  533. };
  534. v1.Initialized += (s, e) => {
  535. v1c++;
  536. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  537. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  538. };
  539. v2.Initialized += (s, e) => {
  540. v2c++;
  541. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  542. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  543. };
  544. sv1.Initialized += (s, e) => {
  545. sv1c++;
  546. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  547. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  548. Assert.False (sv1.CanFocus);
  549. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  550. Assert.False (sv1.CanFocus);
  551. };
  552. v1.Add (sv1);
  553. w.Add (v1, v2);
  554. t.Add (w);
  555. Application.Iteration = () => {
  556. Application.Refresh ();
  557. t.Running = false;
  558. };
  559. Application.Run (t);
  560. Application.Shutdown ();
  561. Assert.Equal (1, tc);
  562. Assert.Equal (1, wc);
  563. Assert.Equal (1, v1c);
  564. Assert.Equal (1, v2c);
  565. Assert.Equal (1, sv1c);
  566. Assert.True (t.CanFocus);
  567. Assert.True (w.CanFocus);
  568. Assert.False (v1.CanFocus);
  569. Assert.False (v2.CanFocus);
  570. Assert.False (sv1.CanFocus);
  571. v1.CanFocus = true;
  572. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  573. }
  574. [Fact]
  575. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  576. {
  577. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  578. var t = new Toplevel () { Id = "0", };
  579. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  580. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  581. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  582. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  583. t.Initialized += (s, e) => {
  584. tc++;
  585. Assert.Equal (1, tc);
  586. Assert.Equal (1, wc);
  587. Assert.Equal (1, v1c);
  588. Assert.Equal (1, v2c);
  589. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  590. Assert.True (t.CanFocus);
  591. Assert.True (w.CanFocus);
  592. Assert.False (v1.CanFocus);
  593. Assert.False (v2.CanFocus);
  594. Application.Refresh ();
  595. };
  596. w.Initialized += (s, e) => {
  597. wc++;
  598. Assert.Equal (t.Frame.Width, w.Frame.Width);
  599. Assert.Equal (t.Frame.Height, w.Frame.Height);
  600. };
  601. v1.Initialized += (s, e) => {
  602. v1c++;
  603. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  604. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  605. };
  606. v2.Initialized += (s, e) => {
  607. v2c++;
  608. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  609. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  610. };
  611. w.Add (v1, v2);
  612. t.Add (w);
  613. Application.Iteration = () => {
  614. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  615. sv1.Initialized += (s, e) => {
  616. sv1c++;
  617. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  618. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  619. Assert.False (sv1.CanFocus);
  620. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  621. Assert.False (sv1.CanFocus);
  622. };
  623. v1.Add (sv1);
  624. Application.Refresh ();
  625. t.Running = false;
  626. };
  627. Application.Run (t);
  628. Application.Shutdown ();
  629. Assert.Equal (1, tc);
  630. Assert.Equal (1, wc);
  631. Assert.Equal (1, v1c);
  632. Assert.Equal (1, v2c);
  633. Assert.Equal (1, sv1c);
  634. Assert.True (t.CanFocus);
  635. Assert.True (w.CanFocus);
  636. Assert.False (v1.CanFocus);
  637. Assert.False (v2.CanFocus);
  638. }
  639. [Fact]
  640. [AutoInitShutdown]
  641. public void CanFocus_Faced_With_Container ()
  642. {
  643. var t = new Toplevel ();
  644. var w = new Window ();
  645. var f = new FrameView ();
  646. var v = new View () { CanFocus = true };
  647. f.Add (v);
  648. w.Add (f);
  649. t.Add (w);
  650. Assert.True (t.CanFocus);
  651. Assert.True (w.CanFocus);
  652. Assert.True (f.CanFocus);
  653. Assert.True (v.CanFocus);
  654. f.CanFocus = false;
  655. Assert.False (f.CanFocus);
  656. Assert.True (v.CanFocus);
  657. v.CanFocus = false;
  658. Assert.False (f.CanFocus);
  659. Assert.False (v.CanFocus);
  660. v.CanFocus = true;
  661. Assert.False (f.CanFocus);
  662. Assert.True (v.CanFocus);
  663. }
  664. [Fact]
  665. public void CanFocus_Faced_With_Container_Before_Run ()
  666. {
  667. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  668. var t = Application.Top;
  669. var w = new Window ("w");
  670. var f = new FrameView ("f");
  671. var v = new View ("v") { CanFocus = true };
  672. f.Add (v);
  673. w.Add (f);
  674. t.Add (w);
  675. Assert.True (t.CanFocus);
  676. Assert.True (w.CanFocus);
  677. Assert.True (f.CanFocus);
  678. Assert.True (v.CanFocus);
  679. f.CanFocus = false;
  680. Assert.False (f.CanFocus);
  681. Assert.True (v.CanFocus);
  682. v.CanFocus = false;
  683. Assert.False (f.CanFocus);
  684. Assert.False (v.CanFocus);
  685. v.CanFocus = true;
  686. Assert.False (f.CanFocus);
  687. Assert.True (v.CanFocus);
  688. Application.Iteration += () => Application.RequestStop ();
  689. Application.Run ();
  690. Application.Shutdown ();
  691. }
  692. [Fact]
  693. public void CanFocus_Faced_With_Container_After_Run ()
  694. {
  695. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  696. var t = Application.Top;
  697. var w = new Window ("w");
  698. var f = new FrameView ("f");
  699. var v = new View ("v") { CanFocus = true };
  700. f.Add (v);
  701. w.Add (f);
  702. t.Add (w);
  703. t.Ready += () => {
  704. Assert.True (t.CanFocus);
  705. Assert.True (w.CanFocus);
  706. Assert.True (f.CanFocus);
  707. Assert.True (v.CanFocus);
  708. f.CanFocus = false;
  709. Assert.False (f.CanFocus);
  710. Assert.False (v.CanFocus);
  711. v.CanFocus = false;
  712. Assert.False (f.CanFocus);
  713. Assert.False (v.CanFocus);
  714. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  715. Assert.False (f.CanFocus);
  716. Assert.False (v.CanFocus);
  717. f.CanFocus = true;
  718. Assert.True (f.CanFocus);
  719. Assert.True (v.CanFocus);
  720. };
  721. Application.Iteration += () => Application.RequestStop ();
  722. Application.Run ();
  723. Application.Shutdown ();
  724. }
  725. [Fact]
  726. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  727. {
  728. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  729. var t = Application.Top;
  730. var w = new Window ("w");
  731. var f = new FrameView ("f");
  732. var v1 = new View ("v1") { CanFocus = true };
  733. var v2 = new View ("v2") { CanFocus = true };
  734. f.Add (v1, v2);
  735. w.Add (f);
  736. t.Add (w);
  737. t.Ready += () => {
  738. Assert.True (t.CanFocus);
  739. Assert.True (w.CanFocus);
  740. Assert.True (f.CanFocus);
  741. Assert.True (v1.CanFocus);
  742. Assert.True (v2.CanFocus);
  743. w.CanFocus = false;
  744. Assert.False (w.CanFocus);
  745. Assert.False (f.CanFocus);
  746. Assert.False (v1.CanFocus);
  747. Assert.False (v2.CanFocus);
  748. };
  749. Application.Iteration += () => Application.RequestStop ();
  750. Application.Run ();
  751. Application.Shutdown ();
  752. }
  753. [Fact]
  754. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  755. {
  756. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  757. var t = Application.Top;
  758. var w = new Window ("w");
  759. var f = new FrameView ("f");
  760. var v1 = new View ("v1");
  761. var v2 = new View ("v2") { CanFocus = true };
  762. f.Add (v1, v2);
  763. w.Add (f);
  764. t.Add (w);
  765. t.Ready += () => {
  766. Assert.True (t.CanFocus);
  767. Assert.True (w.CanFocus);
  768. Assert.True (f.CanFocus);
  769. Assert.False (v1.CanFocus);
  770. Assert.True (v2.CanFocus);
  771. w.CanFocus = false;
  772. Assert.False (w.CanFocus);
  773. Assert.False (f.CanFocus);
  774. Assert.False (v1.CanFocus);
  775. Assert.False (v2.CanFocus);
  776. w.CanFocus = true;
  777. Assert.True (w.CanFocus);
  778. Assert.True (f.CanFocus);
  779. Assert.False (v1.CanFocus);
  780. Assert.True (v2.CanFocus);
  781. };
  782. Application.Iteration += () => Application.RequestStop ();
  783. Application.Run ();
  784. Application.Shutdown ();
  785. }
  786. [Fact]
  787. public void Navigation_With_Null_Focused_View ()
  788. {
  789. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  790. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  791. Application.Top.Ready += () => {
  792. Assert.Null (Application.Top.Focused);
  793. };
  794. // Keyboard navigation with tab
  795. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  796. Application.Iteration += () => Application.RequestStop ();
  797. Application.Run ();
  798. Application.Shutdown ();
  799. }
  800. [Fact]
  801. public void Multi_Thread_Toplevels ()
  802. {
  803. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  804. var t = Application.Top;
  805. var w = new Window ();
  806. t.Add (w);
  807. int count = 0, count1 = 0, count2 = 0;
  808. bool log = false, log1 = false, log2 = false;
  809. bool fromTopStillKnowFirstIsRunning = false;
  810. bool fromTopStillKnowSecondIsRunning = false;
  811. bool fromFirstStillKnowSecondIsRunning = false;
  812. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  813. count++;
  814. if (count1 == 5) {
  815. log1 = true;
  816. }
  817. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  818. fromTopStillKnowFirstIsRunning = true;
  819. }
  820. if (count1 == 7 && count2 == 7 && count == 8) {
  821. fromTopStillKnowSecondIsRunning = true;
  822. }
  823. if (count == 30) {
  824. Assert.Equal (30, count);
  825. Assert.Equal (20, count1);
  826. Assert.Equal (10, count2);
  827. Assert.True (log);
  828. Assert.True (log1);
  829. Assert.True (log2);
  830. Assert.True (fromTopStillKnowFirstIsRunning);
  831. Assert.True (fromTopStillKnowSecondIsRunning);
  832. Assert.True (fromFirstStillKnowSecondIsRunning);
  833. Application.RequestStop ();
  834. return false;
  835. }
  836. return true;
  837. });
  838. t.Ready += FirstDialogToplevel;
  839. void FirstDialogToplevel ()
  840. {
  841. var od = new OpenDialog ();
  842. od.Ready += SecoundDialogToplevel;
  843. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  844. count1++;
  845. if (count2 == 5) {
  846. log2 = true;
  847. }
  848. if (count2 == 4 && count1 == 5 && count == 5) {
  849. fromFirstStillKnowSecondIsRunning = true;
  850. }
  851. if (count1 == 20) {
  852. Assert.Equal (20, count1);
  853. Application.RequestStop ();
  854. return false;
  855. }
  856. return true;
  857. });
  858. Application.Run (od);
  859. }
  860. void SecoundDialogToplevel ()
  861. {
  862. var d = new Dialog ();
  863. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  864. count2++;
  865. if (count < 30) {
  866. log = true;
  867. }
  868. if (count2 == 10) {
  869. Assert.Equal (10, count2);
  870. Application.RequestStop ();
  871. return false;
  872. }
  873. return true;
  874. });
  875. Application.Run (d);
  876. }
  877. Application.Run ();
  878. Application.Shutdown ();
  879. }
  880. [Fact]
  881. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  882. {
  883. // Object Initializer
  884. var view = new View () {
  885. X = 1,
  886. Y = 2,
  887. Width = 3,
  888. Height = 4
  889. };
  890. Assert.Equal (1, view.X);
  891. Assert.Equal (2, view.Y);
  892. Assert.Equal (3, view.Width);
  893. Assert.Equal (4, view.Height);
  894. Assert.False (view.Frame.IsEmpty);
  895. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  896. Assert.False (view.Bounds.IsEmpty);
  897. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  898. view.LayoutSubviews ();
  899. Assert.Equal (1, view.X);
  900. Assert.Equal (2, view.Y);
  901. Assert.Equal (3, view.Width);
  902. Assert.Equal (4, view.Height);
  903. Assert.False (view.Frame.IsEmpty);
  904. Assert.False (view.Bounds.IsEmpty);
  905. // Default Constructor
  906. view = new View ();
  907. Assert.Null (view.X);
  908. Assert.Null (view.Y);
  909. Assert.Null (view.Width);
  910. Assert.Null (view.Height);
  911. Assert.True (view.Frame.IsEmpty);
  912. Assert.True (view.Bounds.IsEmpty);
  913. // Constructor
  914. view = new View (1, 2, "");
  915. Assert.Null (view.X);
  916. Assert.Null (view.Y);
  917. Assert.Null (view.Width);
  918. Assert.Null (view.Height);
  919. Assert.False (view.Frame.IsEmpty);
  920. Assert.True (view.Bounds.IsEmpty);
  921. // Default Constructor and post assignment equivalent to Object Initializer
  922. view = new View ();
  923. view.X = 1;
  924. view.Y = 2;
  925. view.Width = 3;
  926. view.Height = 4;
  927. Assert.Equal (1, view.X);
  928. Assert.Equal (2, view.Y);
  929. Assert.Equal (3, view.Width);
  930. Assert.Equal (4, view.Height);
  931. Assert.False (view.Frame.IsEmpty);
  932. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  933. Assert.False (view.Bounds.IsEmpty);
  934. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  935. }
  936. [Fact]
  937. public void FocusNearestView_Ensure_Focus_Ordered ()
  938. {
  939. var top = new Toplevel ();
  940. var win = new Window ();
  941. var winSubview = new View ("WindowSubview") {
  942. CanFocus = true
  943. };
  944. win.Add (winSubview);
  945. top.Add (win);
  946. var frm = new FrameView ();
  947. var frmSubview = new View ("FrameSubview") {
  948. CanFocus = true
  949. };
  950. frm.Add (frmSubview);
  951. top.Add (frm);
  952. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  953. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  954. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  955. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  956. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  957. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  958. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  959. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  960. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  961. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  962. }
  963. [Fact]
  964. public void KeyPress_Handled_To_True_Prevents_Changes ()
  965. {
  966. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  967. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  968. var top = Application.Top;
  969. var text = new TextField ("");
  970. text.KeyPress += (e) => {
  971. e.Handled = true;
  972. Assert.True (e.Handled);
  973. Assert.Equal (Key.N, e.KeyEvent.Key);
  974. };
  975. top.Add (text);
  976. Application.Iteration += () => {
  977. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  978. Assert.Equal ("", text.Text);
  979. Application.RequestStop ();
  980. };
  981. Application.Run ();
  982. // Shutdown must be called to safely clean up Application if Init has been called
  983. Application.Shutdown ();
  984. }
  985. [Fact, AutoInitShutdown]
  986. public void SetWidth_CanSetWidth_ForceValidatePosDim ()
  987. {
  988. var top = new View () {
  989. X = 0,
  990. Y = 0,
  991. Width = 80,
  992. };
  993. var v = new View () {
  994. Width = Dim.Fill (),
  995. ForceValidatePosDim = true
  996. };
  997. top.Add (v);
  998. Assert.False (v.SetWidth (70, out int rWidth));
  999. Assert.Equal (70, rWidth);
  1000. v.Width = Dim.Fill (1);
  1001. Assert.False (v.SetWidth (70, out rWidth));
  1002. Assert.Equal (69, rWidth);
  1003. v.Width = null;
  1004. Assert.True (v.SetWidth (70, out rWidth));
  1005. Assert.Equal (70, rWidth);
  1006. Assert.False (v.IsInitialized);
  1007. Application.Top.Add (top);
  1008. Application.Begin (Application.Top);
  1009. Assert.True (v.IsInitialized);
  1010. v.Width = Dim.Fill (1);
  1011. Assert.Throws<ArgumentException> (() => v.Width = 75);
  1012. v.LayoutStyle = LayoutStyle.Absolute;
  1013. v.Width = 75;
  1014. Assert.True (v.SetWidth (60, out rWidth));
  1015. Assert.Equal (60, rWidth);
  1016. }
  1017. [Fact, AutoInitShutdown]
  1018. public void SetHeight_CanSetHeight_ForceValidatePosDim ()
  1019. {
  1020. var top = new View () {
  1021. X = 0,
  1022. Y = 0,
  1023. Height = 20
  1024. };
  1025. var v = new View () {
  1026. Height = Dim.Fill (),
  1027. ForceValidatePosDim = true
  1028. };
  1029. top.Add (v);
  1030. Assert.False (v.SetHeight (10, out int rHeight));
  1031. Assert.Equal (10, rHeight);
  1032. v.Height = Dim.Fill (1);
  1033. Assert.False (v.SetHeight (10, out rHeight));
  1034. Assert.Equal (9, rHeight);
  1035. v.Height = null;
  1036. Assert.True (v.SetHeight (10, out rHeight));
  1037. Assert.Equal (10, rHeight);
  1038. Assert.False (v.IsInitialized);
  1039. Application.Top.Add (top);
  1040. Application.Begin (Application.Top);
  1041. Assert.True (v.IsInitialized);
  1042. v.Height = Dim.Fill (1);
  1043. Assert.Throws<ArgumentException> (() => v.Height = 15);
  1044. v.LayoutStyle = LayoutStyle.Absolute;
  1045. v.Height = 15;
  1046. Assert.True (v.SetHeight (5, out rHeight));
  1047. Assert.Equal (5, rHeight);
  1048. }
  1049. [Fact]
  1050. public void GetCurrentWidth_CanSetWidth ()
  1051. {
  1052. var top = new View () {
  1053. X = 0,
  1054. Y = 0,
  1055. Width = 80,
  1056. };
  1057. var v = new View () {
  1058. Width = Dim.Fill ()
  1059. };
  1060. top.Add (v);
  1061. Assert.False (v.AutoSize);
  1062. Assert.True (v.GetCurrentWidth (out int cWidth));
  1063. Assert.Equal (80, cWidth);
  1064. v.Width = Dim.Fill (1);
  1065. Assert.True (v.GetCurrentWidth (out cWidth));
  1066. Assert.Equal (79, cWidth);
  1067. v.AutoSize = true;
  1068. Assert.True (v.GetCurrentWidth (out cWidth));
  1069. Assert.Equal (79, cWidth);
  1070. }
  1071. [Fact]
  1072. public void GetCurrentHeight_CanSetHeight ()
  1073. {
  1074. var top = new View () {
  1075. X = 0,
  1076. Y = 0,
  1077. Height = 20
  1078. };
  1079. var v = new View () {
  1080. Height = Dim.Fill ()
  1081. };
  1082. top.Add (v);
  1083. Assert.False (v.AutoSize);
  1084. Assert.True (v.GetCurrentHeight (out int cHeight));
  1085. Assert.Equal (20, cHeight);
  1086. v.Height = Dim.Fill (1);
  1087. Assert.True (v.GetCurrentHeight (out cHeight));
  1088. Assert.Equal (19, cHeight);
  1089. v.AutoSize = true;
  1090. Assert.True (v.GetCurrentHeight (out cHeight));
  1091. Assert.Equal (19, cHeight);
  1092. }
  1093. [Fact]
  1094. public void AutoSize_False_If_Text_Emmpty ()
  1095. {
  1096. var view1 = new View ();
  1097. var view2 = new View ("");
  1098. var view3 = new View () { Text = "" };
  1099. Assert.False (view1.AutoSize);
  1100. Assert.False (view2.AutoSize);
  1101. Assert.False (view3.AutoSize);
  1102. }
  1103. [Fact]
  1104. public void AutoSize_False_If_Text_Is_Not_Emmpty ()
  1105. {
  1106. var view1 = new View ();
  1107. view1.Text = "Hello World";
  1108. var view2 = new View ("Hello World");
  1109. var view3 = new View () { Text = "Hello World" };
  1110. Assert.False (view1.AutoSize);
  1111. Assert.False (view2.AutoSize);
  1112. Assert.False (view3.AutoSize);
  1113. }
  1114. [Fact]
  1115. public void AutoSize_True_Label_If_Text_Emmpty ()
  1116. {
  1117. var label1 = new Label ();
  1118. var label2 = new Label ("");
  1119. var label3 = new Label () { Text = "" };
  1120. Assert.True (label1.AutoSize);
  1121. Assert.True (label2.AutoSize);
  1122. Assert.True (label3.AutoSize);
  1123. }
  1124. [Fact]
  1125. public void AutoSize_True_Label_If_Text_Is_Not_Emmpty ()
  1126. {
  1127. var label1 = new Label ();
  1128. label1.Text = "Hello World";
  1129. var label2 = new Label ("Hello World");
  1130. var label3 = new Label () { Text = "Hello World" };
  1131. Assert.True (label1.AutoSize);
  1132. Assert.True (label2.AutoSize);
  1133. Assert.True (label3.AutoSize);
  1134. }
  1135. [Fact]
  1136. public void AutoSize_False_ResizeView_Is_Always_False ()
  1137. {
  1138. var label = new Label () { AutoSize = false };
  1139. label.Text = "New text";
  1140. Assert.False (label.AutoSize);
  1141. Assert.Equal ("{X=0,Y=0,Width=0,Height=1}", label.Bounds.ToString ());
  1142. }
  1143. [Fact]
  1144. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1145. {
  1146. var label = new Label ();
  1147. label.Text = "New text";
  1148. Assert.True (label.AutoSize);
  1149. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  1150. }
  1151. [Fact, AutoInitShutdown]
  1152. public void AutoSize_False_ResizeView_With_Dim_Fill_After_IsInitialized ()
  1153. {
  1154. var win = new Window (new Rect (0, 0, 30, 80), "");
  1155. var label = new Label () { AutoSize = false, Width = Dim.Fill (), Height = Dim.Fill () };
  1156. win.Add (label);
  1157. Application.Top.Add (win);
  1158. // Text is empty so height=0
  1159. Assert.False (label.AutoSize);
  1160. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1161. label.Text = "New text\nNew line";
  1162. Application.Top.LayoutSubviews ();
  1163. Assert.False (label.AutoSize);
  1164. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  1165. Assert.False (label.IsInitialized);
  1166. Application.Begin (Application.Top);
  1167. Assert.True (label.IsInitialized);
  1168. Assert.False (label.AutoSize);
  1169. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  1170. }
  1171. [Fact, AutoInitShutdown]
  1172. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_After_IsAdded_And_IsInitialized ()
  1173. {
  1174. var win = new Window (new Rect (0, 0, 30, 80), "");
  1175. var label = new Label () { Width = Dim.Fill () };
  1176. win.Add (label);
  1177. Application.Top.Add (win);
  1178. Assert.True (label.IsAdded);
  1179. // Text is empty so height=0
  1180. Assert.True (label.AutoSize);
  1181. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1182. label.Text = "First line\nSecond line";
  1183. Application.Top.LayoutSubviews ();
  1184. Assert.True (label.AutoSize);
  1185. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1186. Assert.False (label.IsInitialized);
  1187. Application.Begin (Application.Top);
  1188. Assert.True (label.AutoSize);
  1189. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1190. Assert.True (label.IsInitialized);
  1191. label.AutoSize = false;
  1192. Application.Refresh ();
  1193. Assert.False (label.AutoSize);
  1194. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  1195. }
  1196. [Fact, AutoInitShutdown]
  1197. public void AutoSize_False_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute_With_Initialization ()
  1198. {
  1199. var win = new Window (new Rect (0, 0, 30, 80), "");
  1200. var label = new Label () { Width = Dim.Fill () };
  1201. win.Add (label);
  1202. Application.Top.Add (win);
  1203. // Text is empty so height=0
  1204. Assert.True (label.AutoSize);
  1205. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1206. Application.Begin (Application.Top);
  1207. Assert.True (label.AutoSize);
  1208. Assert.Equal ("{X=0,Y=0,Width=28,Height=0}", label.Bounds.ToString ());
  1209. label.Text = "First line\nSecond line";
  1210. Application.Refresh ();
  1211. // Here the AutoSize ensuring the right size
  1212. Assert.True (label.AutoSize);
  1213. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1214. label.AutoSize = false;
  1215. Application.Refresh ();
  1216. // Here the SetMinWidthHeight ensuring the minimum height
  1217. Assert.False (label.AutoSize);
  1218. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  1219. label.Text = "First changed line\nSecond changed line\nNew line";
  1220. Application.Refresh ();
  1221. Assert.False (label.AutoSize);
  1222. Assert.Equal ("{X=0,Y=0,Width=28,Height=1}", label.Bounds.ToString ());
  1223. label.AutoSize = true;
  1224. Application.Refresh ();
  1225. Assert.True (label.AutoSize);
  1226. Assert.Equal ("{X=0,Y=0,Width=28,Height=3}", label.Bounds.ToString ());
  1227. }
  1228. [Fact, AutoInitShutdown]
  1229. public void AutoSize_True_Setting_With_Height_Horizontal ()
  1230. {
  1231. var label = new Label ("Hello") { Width = 10, Height = 2 };
  1232. var viewX = new View ("X") { X = Pos.Right (label) };
  1233. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  1234. Application.Top.Add (label, viewX, viewY);
  1235. Application.Begin (Application.Top);
  1236. Assert.True (label.AutoSize);
  1237. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  1238. var expected = @"
  1239. Hello X
  1240. Y
  1241. ";
  1242. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1243. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  1244. label.AutoSize = false;
  1245. Application.Refresh ();
  1246. Assert.False (label.AutoSize);
  1247. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  1248. expected = @"
  1249. Hello X
  1250. Y
  1251. ";
  1252. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1253. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  1254. }
  1255. [Fact, AutoInitShutdown]
  1256. public void AutoSize_True_Setting_With_Height_Vertical ()
  1257. {
  1258. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  1259. var viewX = new View ("X") { X = Pos.Right (label) };
  1260. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  1261. Application.Top.Add (label, viewX, viewY);
  1262. Application.Begin (Application.Top);
  1263. Assert.True (label.AutoSize);
  1264. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  1265. var expected = @"
  1266. H X
  1267. e
  1268. l
  1269. l
  1270. o
  1271. Y
  1272. ";
  1273. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1274. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  1275. label.AutoSize = false;
  1276. Application.Refresh ();
  1277. Assert.False (label.AutoSize);
  1278. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  1279. expected = @"
  1280. H X
  1281. e
  1282. l
  1283. l
  1284. o
  1285. Y
  1286. ";
  1287. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1288. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  1289. }
  1290. [Theory]
  1291. [InlineData (1)]
  1292. [InlineData (2)]
  1293. [InlineData (3)]
  1294. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  1295. {
  1296. var driver = new FakeDriver ();
  1297. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  1298. try {
  1299. // Create a label with a short text
  1300. Label lbl;
  1301. var text = "test";
  1302. if (choice == 1) {
  1303. // An object initializer should call the default constructor.
  1304. lbl = new Label { Text = text };
  1305. } else if (choice == 2) {
  1306. // Calling the default constructor followed by the object initializer.
  1307. lbl = new Label () { Text = text };
  1308. } else {
  1309. // Calling the Text constructor.
  1310. lbl = new Label (text);
  1311. }
  1312. lbl.ColorScheme = new ColorScheme ();
  1313. lbl.Redraw (lbl.Bounds);
  1314. // should have the initial text
  1315. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  1316. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  1317. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  1318. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  1319. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  1320. } finally {
  1321. Application.Shutdown ();
  1322. }
  1323. }
  1324. [Fact]
  1325. [AutoInitShutdown]
  1326. public void Internal_Tests ()
  1327. {
  1328. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1329. Enum.GetValues (typeof (View.Direction)));
  1330. var rect = new Rect (1, 1, 10, 1);
  1331. var view = new View (rect);
  1332. var top = Application.Top;
  1333. top.Add (view);
  1334. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1335. view.FocusDirection = View.Direction.Backward;
  1336. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1337. Assert.Empty (view.InternalSubviews);
  1338. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1339. Assert.True (view.LayoutNeeded);
  1340. Assert.False (view.ChildNeedsDisplay);
  1341. Assert.False (view.addingView);
  1342. view.addingView = true;
  1343. Assert.True (view.addingView);
  1344. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1345. Assert.Equal (1, rcol);
  1346. Assert.Equal (1, rrow);
  1347. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1348. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1349. view.Width = Dim.Fill ();
  1350. view.Height = Dim.Fill ();
  1351. Assert.Equal (10, view.Bounds.Width);
  1352. Assert.Equal (1, view.Bounds.Height);
  1353. view.SetRelativeLayout (top.Bounds);
  1354. Assert.Equal (79, view.Bounds.Width);
  1355. Assert.Equal (24, view.Bounds.Height);
  1356. view.X = 0;
  1357. view.Y = 0;
  1358. view.SetRelativeLayout (top.Bounds);
  1359. Assert.Equal (80, view.Bounds.Width);
  1360. Assert.Equal (25, view.Bounds.Height);
  1361. bool layoutStarted = false;
  1362. view.LayoutStarted += (_) => layoutStarted = true;
  1363. view.OnLayoutStarted (null);
  1364. Assert.True (layoutStarted);
  1365. view.LayoutComplete += (_) => layoutStarted = false;
  1366. view.OnLayoutComplete (null);
  1367. Assert.False (layoutStarted);
  1368. view.X = Pos.Center () - 41;
  1369. view.Y = Pos.Center () - 13;
  1370. view.SetRelativeLayout (top.Bounds);
  1371. view.ViewToScreen (0, 0, out rcol, out rrow);
  1372. Assert.Equal (-1, rcol);
  1373. Assert.Equal (-1, rrow);
  1374. }
  1375. [Fact]
  1376. [AutoInitShutdown]
  1377. public void Enabled_False_Sets_HasFocus_To_False ()
  1378. {
  1379. var wasClicked = false;
  1380. var view = new Button ("Click Me");
  1381. view.Clicked += () => wasClicked = !wasClicked;
  1382. Application.Top.Add (view);
  1383. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1384. Assert.True (wasClicked);
  1385. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1386. Assert.False (wasClicked);
  1387. Assert.True (view.Enabled);
  1388. Assert.True (view.CanFocus);
  1389. Assert.True (view.HasFocus);
  1390. view.Enabled = false;
  1391. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1392. Assert.False (wasClicked);
  1393. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1394. Assert.False (wasClicked);
  1395. Assert.False (view.Enabled);
  1396. Assert.True (view.CanFocus);
  1397. Assert.False (view.HasFocus);
  1398. view.SetFocus ();
  1399. Assert.False (view.HasFocus);
  1400. }
  1401. [Fact]
  1402. [AutoInitShutdown]
  1403. public void Enabled_Sets_Also_Sets_Subviews ()
  1404. {
  1405. var wasClicked = false;
  1406. var button = new Button ("Click Me");
  1407. button.Clicked += () => wasClicked = !wasClicked;
  1408. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1409. win.Add (button);
  1410. Application.Top.Add (win);
  1411. var iterations = 0;
  1412. Application.Iteration += () => {
  1413. iterations++;
  1414. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1415. Assert.True (wasClicked);
  1416. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1417. Assert.False (wasClicked);
  1418. Assert.True (button.Enabled);
  1419. Assert.True (button.CanFocus);
  1420. Assert.True (button.HasFocus);
  1421. Assert.True (win.Enabled);
  1422. Assert.True (win.CanFocus);
  1423. Assert.True (win.HasFocus);
  1424. win.Enabled = false;
  1425. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1426. Assert.False (wasClicked);
  1427. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1428. Assert.False (wasClicked);
  1429. Assert.False (button.Enabled);
  1430. Assert.True (button.CanFocus);
  1431. Assert.False (button.HasFocus);
  1432. Assert.False (win.Enabled);
  1433. Assert.True (win.CanFocus);
  1434. Assert.False (win.HasFocus);
  1435. button.SetFocus ();
  1436. Assert.False (button.HasFocus);
  1437. Assert.False (win.HasFocus);
  1438. win.SetFocus ();
  1439. Assert.False (button.HasFocus);
  1440. Assert.False (win.HasFocus);
  1441. win.Enabled = true;
  1442. win.FocusFirst ();
  1443. Assert.True (button.HasFocus);
  1444. Assert.True (win.HasFocus);
  1445. Application.RequestStop ();
  1446. };
  1447. Application.Run ();
  1448. Assert.Equal (1, iterations);
  1449. }
  1450. [Fact]
  1451. [AutoInitShutdown]
  1452. public void Visible_Sets_Also_Sets_Subviews ()
  1453. {
  1454. var button = new Button ("Click Me");
  1455. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1456. win.Add (button);
  1457. var top = Application.Top;
  1458. top.Add (win);
  1459. var iterations = 0;
  1460. Application.Iteration += () => {
  1461. iterations++;
  1462. Assert.True (button.Visible);
  1463. Assert.True (button.CanFocus);
  1464. Assert.True (button.HasFocus);
  1465. Assert.True (win.Visible);
  1466. Assert.True (win.CanFocus);
  1467. Assert.True (win.HasFocus);
  1468. Assert.True (RunesCount () > 0);
  1469. win.Visible = false;
  1470. Assert.True (button.Visible);
  1471. Assert.True (button.CanFocus);
  1472. Assert.False (button.HasFocus);
  1473. Assert.False (win.Visible);
  1474. Assert.True (win.CanFocus);
  1475. Assert.False (win.HasFocus);
  1476. button.SetFocus ();
  1477. Assert.False (button.HasFocus);
  1478. Assert.False (win.HasFocus);
  1479. win.SetFocus ();
  1480. Assert.False (button.HasFocus);
  1481. Assert.False (win.HasFocus);
  1482. top.Redraw (top.Bounds);
  1483. Assert.True (RunesCount () == 0);
  1484. win.Visible = true;
  1485. win.FocusFirst ();
  1486. Assert.True (button.HasFocus);
  1487. Assert.True (win.HasFocus);
  1488. top.Redraw (top.Bounds);
  1489. Assert.True (RunesCount () > 0);
  1490. Application.RequestStop ();
  1491. };
  1492. Application.Run ();
  1493. Assert.Equal (1, iterations);
  1494. int RunesCount ()
  1495. {
  1496. var contents = ((FakeDriver)Application.Driver).Contents;
  1497. var runesCount = 0;
  1498. for (int i = 0; i < Application.Driver.Rows; i++) {
  1499. for (int j = 0; j < Application.Driver.Cols; j++) {
  1500. if (contents [i, j, 0] != ' ') {
  1501. runesCount++;
  1502. }
  1503. }
  1504. }
  1505. return runesCount;
  1506. }
  1507. }
  1508. [Fact]
  1509. [AutoInitShutdown]
  1510. public void GetTopSuperView_Test ()
  1511. {
  1512. var v1 = new View ();
  1513. var fv1 = new FrameView ();
  1514. fv1.Add (v1);
  1515. var tf1 = new TextField ();
  1516. var w1 = new Window ();
  1517. w1.Add (fv1, tf1);
  1518. var top1 = new Toplevel ();
  1519. top1.Add (w1);
  1520. var v2 = new View ();
  1521. var fv2 = new FrameView ();
  1522. fv2.Add (v2);
  1523. var tf2 = new TextField ();
  1524. var w2 = new Window ();
  1525. w2.Add (fv2, tf2);
  1526. var top2 = new Toplevel ();
  1527. top2.Add (w2);
  1528. Assert.Equal (top1, v1.GetTopSuperView ());
  1529. Assert.Equal (top2, v2.GetTopSuperView ());
  1530. }
  1531. [Fact]
  1532. [AutoInitShutdown]
  1533. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1534. {
  1535. var lbl = new Label ("123");
  1536. Application.Top.Add (lbl);
  1537. Application.Begin (Application.Top);
  1538. Assert.True (lbl.AutoSize);
  1539. Assert.Equal ("123 ", GetContents ());
  1540. lbl.Text = "12";
  1541. lbl.SuperView.Redraw (lbl.SuperView.NeedDisplay);
  1542. Assert.Equal ("12 ", GetContents ());
  1543. string GetContents ()
  1544. {
  1545. var text = "";
  1546. for (int i = 0; i < 4; i++) {
  1547. text += (char)Application.Driver.Contents [0, i, 0];
  1548. }
  1549. return text;
  1550. }
  1551. }
  1552. [Fact]
  1553. [AutoInitShutdown]
  1554. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1555. {
  1556. var view = new View () { CanFocus = true };
  1557. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1558. win.Add (view);
  1559. Application.Top.Add (win);
  1560. Application.Begin (Application.Top);
  1561. Assert.True (view.CanFocus);
  1562. Assert.True (view.HasFocus);
  1563. view.CanFocus = false;
  1564. Assert.False (view.CanFocus);
  1565. Assert.False (view.HasFocus);
  1566. Assert.Null (Application.Current.Focused);
  1567. Assert.Null (Application.Current.MostFocused);
  1568. }
  1569. [Fact]
  1570. [AutoInitShutdown]
  1571. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1572. {
  1573. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1574. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1575. win1.Add (view1);
  1576. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1577. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1578. win2.Add (view2);
  1579. Application.Top.Add (win1, win2);
  1580. Application.Begin (Application.Top);
  1581. Assert.True (view1.CanFocus);
  1582. Assert.True (view1.HasFocus);
  1583. Assert.True (view2.CanFocus);
  1584. Assert.True (view2.HasFocus);
  1585. view1.CanFocus = false;
  1586. Assert.False (view1.CanFocus);
  1587. Assert.False (view1.HasFocus);
  1588. Assert.Equal (win2, Application.Current.Focused);
  1589. Assert.Equal (view2, Application.Current.MostFocused);
  1590. }
  1591. [Fact]
  1592. [AutoInitShutdown]
  1593. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1594. {
  1595. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1596. var view12 = new View () { Y = 5, Width = 10, Height = 1, CanFocus = true };
  1597. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1598. win1.Add (view1, view12);
  1599. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1600. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1601. win2.Add (view2);
  1602. Application.Top.Add (win1, win2);
  1603. Application.Begin (Application.Top);
  1604. Assert.True (view1.CanFocus);
  1605. Assert.True (view1.HasFocus);
  1606. Assert.True (view2.CanFocus);
  1607. Assert.True (view2.HasFocus);
  1608. view1.CanFocus = false;
  1609. Assert.False (view1.CanFocus);
  1610. Assert.False (view1.HasFocus);
  1611. Assert.Equal (win1, Application.Current.Focused);
  1612. Assert.Equal (view12, Application.Current.MostFocused);
  1613. }
  1614. [Fact]
  1615. [AutoInitShutdown]
  1616. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1617. {
  1618. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1619. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1620. win1.Add (view1);
  1621. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1622. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1623. win2.Add (view2);
  1624. Application.Top.Add (win1, win2);
  1625. Application.Begin (Application.Top);
  1626. Assert.True (view1.CanFocus);
  1627. Assert.True (view1.HasFocus);
  1628. Assert.True (view2.CanFocus);
  1629. Assert.True (view2.HasFocus);
  1630. win1.CanFocus = false;
  1631. Assert.False (view1.CanFocus);
  1632. Assert.False (view1.HasFocus);
  1633. Assert.False (win1.CanFocus);
  1634. Assert.False (win1.HasFocus);
  1635. Assert.Equal (win2, Application.Current.Focused);
  1636. Assert.Equal (view2, Application.Current.MostFocused);
  1637. }
  1638. [Fact]
  1639. [AutoInitShutdown]
  1640. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1641. {
  1642. var sbQuiting = false;
  1643. var tfQuiting = false;
  1644. var topQuiting = false;
  1645. var sb = new StatusBar (new StatusItem [] {
  1646. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1647. });
  1648. var tf = new TextField ();
  1649. tf.KeyPress += Tf_KeyPress;
  1650. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1651. {
  1652. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1653. obj.Handled = tfQuiting = true;
  1654. }
  1655. }
  1656. var win = new Window ();
  1657. win.Add (sb, tf);
  1658. var top = Application.Top;
  1659. top.KeyPress += Top_KeyPress;
  1660. void Top_KeyPress (View.KeyEventEventArgs obj)
  1661. {
  1662. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1663. obj.Handled = topQuiting = true;
  1664. }
  1665. }
  1666. top.Add (win);
  1667. Application.Begin (top);
  1668. Assert.False (sbQuiting);
  1669. Assert.False (tfQuiting);
  1670. Assert.False (topQuiting);
  1671. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1672. Assert.False (sbQuiting);
  1673. Assert.True (tfQuiting);
  1674. Assert.False (topQuiting);
  1675. tf.KeyPress -= Tf_KeyPress;
  1676. tfQuiting = false;
  1677. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1678. Application.MainLoop.MainIteration ();
  1679. Assert.True (sbQuiting);
  1680. Assert.False (tfQuiting);
  1681. Assert.False (topQuiting);
  1682. sb.RemoveItem (0);
  1683. sbQuiting = false;
  1684. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1685. Application.MainLoop.MainIteration ();
  1686. Assert.False (sbQuiting);
  1687. Assert.False (tfQuiting);
  1688. Assert.True (topQuiting);
  1689. }
  1690. [Fact]
  1691. [AutoInitShutdown]
  1692. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1693. {
  1694. var sbQuiting = false;
  1695. var tfQuiting = false;
  1696. var sb = new StatusBar (new StatusItem [] {
  1697. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1698. });
  1699. var tf = new TextField ();
  1700. tf.KeyPress += Tf_KeyPress;
  1701. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1702. {
  1703. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1704. obj.Handled = tfQuiting = true;
  1705. }
  1706. }
  1707. var win = new Window ();
  1708. win.Add (sb, tf);
  1709. var top = Application.Top;
  1710. top.Add (win);
  1711. Application.Begin (top);
  1712. Assert.False (sbQuiting);
  1713. Assert.False (tfQuiting);
  1714. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1715. Assert.False (sbQuiting);
  1716. Assert.True (tfQuiting);
  1717. tf.KeyPress -= Tf_KeyPress;
  1718. tfQuiting = false;
  1719. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1720. Application.MainLoop.MainIteration ();
  1721. Assert.True (sbQuiting);
  1722. Assert.False (tfQuiting);
  1723. }
  1724. [Fact]
  1725. [AutoInitShutdown]
  1726. public void WindowDispose_CanFocusProblem ()
  1727. {
  1728. // Arrange
  1729. Application.Init ();
  1730. using var top = Toplevel.Create ();
  1731. using var view = new View (
  1732. x: 0,
  1733. y: 1,
  1734. text: nameof (WindowDispose_CanFocusProblem));
  1735. using var window = new Window ();
  1736. top.Add (window);
  1737. window.Add (view);
  1738. // Act
  1739. Application.Begin (top);
  1740. Application.Shutdown ();
  1741. // Assert does Not throw NullReferenceException
  1742. top.SetFocus ();
  1743. }
  1744. [Fact, AutoInitShutdown]
  1745. public void DrawFrame_With_Positive_Positions ()
  1746. {
  1747. var view = new View (new Rect (0, 0, 8, 4));
  1748. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1749. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1750. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1751. Application.Top.Add (view);
  1752. Application.Begin (Application.Top);
  1753. var expected = @"
  1754. ┌──────┐
  1755. │ │
  1756. │ │
  1757. └──────┘
  1758. ";
  1759. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1760. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1761. }
  1762. [Fact, AutoInitShutdown]
  1763. public void DrawFrame_With_Negative_Positions ()
  1764. {
  1765. var view = new View (new Rect (-1, 0, 8, 4));
  1766. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1767. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1768. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1769. Application.Top.Add (view);
  1770. Application.Begin (Application.Top);
  1771. var expected = @"
  1772. ──────┐
  1773. ──────┘
  1774. ";
  1775. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1776. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1777. view.Frame = new Rect (-1, -1, 8, 4);
  1778. Application.Refresh ();
  1779. expected = @"
  1780. ──────┘
  1781. ";
  1782. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1783. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1784. view.Frame = new Rect (0, 0, 8, 4);
  1785. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1786. expected = @"
  1787. ┌──────
  1788. └──────
  1789. ";
  1790. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1791. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1792. view.Frame = new Rect (0, 0, 8, 4);
  1793. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1794. }
  1795. [Fact, AutoInitShutdown]
  1796. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1797. {
  1798. var view = new View (new Rect (0, 0, 20, 20));
  1799. view.Add (new Label ("0123456789abcdefghij"));
  1800. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1801. view.Add (new Button (1, 1, "Press me!"));
  1802. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1803. ContentSize = new Size (40, 40),
  1804. ShowHorizontalScrollIndicator = true,
  1805. ShowVerticalScrollIndicator = true
  1806. };
  1807. scrollView.Add (view);
  1808. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1809. win.Add (scrollView);
  1810. Application.Top.Add (win);
  1811. Application.Begin (Application.Top);
  1812. var expected = @"
  1813. ┌ Test ────────────┐
  1814. │ │
  1815. │ 0123456789abcd▲ │
  1816. │ 1[ Press me! ]┬ │
  1817. │ 2 │ │
  1818. │ 3 ┴ │
  1819. │ 4 ░ │
  1820. │ 5 ░ │
  1821. │ 6 ░ │
  1822. │ 7 ░ │
  1823. │ 8 ▼ │
  1824. │ ◄├───┤░░░░░░░► │
  1825. │ │
  1826. └──────────────────┘
  1827. ";
  1828. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1829. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1830. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1831. Application.Top.Redraw (Application.Top.Bounds);
  1832. expected = @"
  1833. ┌ Test ────────────┐
  1834. │ │
  1835. │ 123456789abcde▲ │
  1836. │ [ Press me! ] ┬ │
  1837. │ │ │
  1838. │ ┴ │
  1839. │ ░ │
  1840. │ ░ │
  1841. │ ░ │
  1842. │ ░ │
  1843. │ ▼ │
  1844. │ ◄├───┤░░░░░░░► │
  1845. │ │
  1846. └──────────────────┘
  1847. ";
  1848. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1849. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1850. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1851. Application.Top.Redraw (Application.Top.Bounds);
  1852. expected = @"
  1853. ┌ Test ────────────┐
  1854. │ │
  1855. │ 23456789abcdef▲ │
  1856. │ Press me! ] ┬ │
  1857. │ │ │
  1858. │ ┴ │
  1859. │ ░ │
  1860. │ ░ │
  1861. │ ░ │
  1862. │ ░ │
  1863. │ ▼ │
  1864. │ ◄├────┤░░░░░░► │
  1865. │ │
  1866. └──────────────────┘
  1867. ";
  1868. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1869. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1870. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1871. Application.Top.Redraw (Application.Top.Bounds);
  1872. expected = @"
  1873. ┌ Test ────────────┐
  1874. │ │
  1875. │ 3456789abcdefg▲ │
  1876. │ Press me! ] ┬ │
  1877. │ │ │
  1878. │ ┴ │
  1879. │ ░ │
  1880. │ ░ │
  1881. │ ░ │
  1882. │ ░ │
  1883. │ ▼ │
  1884. │ ◄├────┤░░░░░░► │
  1885. │ │
  1886. └──────────────────┘
  1887. ";
  1888. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1889. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1890. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1891. Application.Top.Redraw (Application.Top.Bounds);
  1892. expected = @"
  1893. ┌ Test ────────────┐
  1894. │ │
  1895. │ 456789abcdefgh▲ │
  1896. │ ress me! ] ┬ │
  1897. │ │ │
  1898. │ ┴ │
  1899. │ ░ │
  1900. │ ░ │
  1901. │ ░ │
  1902. │ ░ │
  1903. │ ▼ │
  1904. │ ◄░├───┤░░░░░░► │
  1905. │ │
  1906. └──────────────────┘
  1907. ";
  1908. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1909. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1910. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1911. Application.Top.Redraw (Application.Top.Bounds);
  1912. expected = @"
  1913. ┌ Test ────────────┐
  1914. │ │
  1915. │ 56789abcdefghi▲ │
  1916. │ ess me! ] ┬ │
  1917. │ │ │
  1918. │ ┴ │
  1919. │ ░ │
  1920. │ ░ │
  1921. │ ░ │
  1922. │ ░ │
  1923. │ ▼ │
  1924. │ ◄░├────┤░░░░░► │
  1925. │ │
  1926. └──────────────────┘
  1927. ";
  1928. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1929. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1930. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1931. Application.Top.Redraw (Application.Top.Bounds);
  1932. expected = @"
  1933. ┌ Test ────────────┐
  1934. │ │
  1935. │ 6789abcdefghij▲ │
  1936. │ ss me! ] ┬ │
  1937. │ │ │
  1938. │ ┴ │
  1939. │ ░ │
  1940. │ ░ │
  1941. │ ░ │
  1942. │ ░ │
  1943. │ ▼ │
  1944. │ ◄░├────┤░░░░░► │
  1945. │ │
  1946. └──────────────────┘
  1947. ";
  1948. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1949. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1950. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1951. Application.Top.Redraw (Application.Top.Bounds);
  1952. expected = @"
  1953. ┌ Test ────────────┐
  1954. │ │
  1955. │ 789abcdefghij ▲ │
  1956. │ s me! ] ┬ │
  1957. │ │ │
  1958. │ ┴ │
  1959. │ ░ │
  1960. │ ░ │
  1961. │ ░ │
  1962. │ ░ │
  1963. │ ▼ │
  1964. │ ◄░░├───┤░░░░░► │
  1965. │ │
  1966. └──────────────────┘
  1967. ";
  1968. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1969. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1970. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1971. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1972. Application.Top.Redraw (Application.Top.Bounds);
  1973. expected = @"
  1974. ┌ Test ────────────┐
  1975. │ │
  1976. │ 1[ Press me! ]▲ │
  1977. │ 2 ┬ │
  1978. │ 3 │ │
  1979. │ 4 ┴ │
  1980. │ 5 ░ │
  1981. │ 6 ░ │
  1982. │ 7 ░ │
  1983. │ 8 ░ │
  1984. │ 9 ▼ │
  1985. │ ◄├───┤░░░░░░░► │
  1986. │ │
  1987. └──────────────────┘
  1988. ";
  1989. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1990. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1991. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1992. Application.Top.Redraw (Application.Top.Bounds);
  1993. expected = @"
  1994. ┌ Test ────────────┐
  1995. │ │
  1996. │ 2 ▲ │
  1997. │ 3 ┬ │
  1998. │ 4 │ │
  1999. │ 5 ┴ │
  2000. │ 6 ░ │
  2001. │ 7 ░ │
  2002. │ 8 ░ │
  2003. │ 9 ░ │
  2004. │ 0 ▼ │
  2005. │ ◄├───┤░░░░░░░► │
  2006. │ │
  2007. └──────────────────┘
  2008. ";
  2009. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2010. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  2011. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  2012. Application.Top.Redraw (Application.Top.Bounds);
  2013. expected = @"
  2014. ┌ Test ────────────┐
  2015. │ │
  2016. │ 3 ▲ │
  2017. │ 4 ┬ │
  2018. │ 5 │ │
  2019. │ 6 ┴ │
  2020. │ 7 ░ │
  2021. │ 8 ░ │
  2022. │ 9 ░ │
  2023. │ 0 ░ │
  2024. │ ▼ │
  2025. │ ◄├───┤░░░░░░░► │
  2026. │ │
  2027. └──────────────────┘
  2028. ";
  2029. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2030. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  2031. }
  2032. [Fact, AutoInitShutdown]
  2033. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  2034. {
  2035. var view = new View () {
  2036. Width = Dim.Fill (),
  2037. Height = Dim.Fill ()
  2038. };
  2039. view.LayoutComplete += e => {
  2040. view.DrawFrame (view.Bounds);
  2041. var savedClip = Application.Driver.Clip;
  2042. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  2043. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  2044. Application.Driver.Move (1, row + 1);
  2045. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  2046. Application.Driver.AddStr ($"{col}");
  2047. }
  2048. }
  2049. Application.Driver.Clip = savedClip;
  2050. };
  2051. Application.Top.Add (view);
  2052. Application.Begin (Application.Top);
  2053. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  2054. var expected = @"
  2055. ┌──────────────────┐
  2056. │012345678910111213│
  2057. │012345678910111213│
  2058. │012345678910111213│
  2059. │012345678910111213│
  2060. │012345678910111213│
  2061. │012345678910111213│
  2062. │012345678910111213│
  2063. │012345678910111213│
  2064. └──────────────────┘
  2065. ";
  2066. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2067. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  2068. view.Clear ();
  2069. expected = @"
  2070. ";
  2071. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2072. Assert.Equal (Rect.Empty, pos);
  2073. }
  2074. [Fact, AutoInitShutdown]
  2075. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  2076. {
  2077. var view = new View () {
  2078. Width = Dim.Fill (),
  2079. Height = Dim.Fill ()
  2080. };
  2081. view.LayoutComplete += e => {
  2082. view.DrawFrame (view.Bounds);
  2083. var savedClip = Application.Driver.Clip;
  2084. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  2085. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  2086. Application.Driver.Move (1, row + 1);
  2087. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  2088. Application.Driver.AddStr ($"{col}");
  2089. }
  2090. }
  2091. Application.Driver.Clip = savedClip;
  2092. };
  2093. Application.Top.Add (view);
  2094. Application.Begin (Application.Top);
  2095. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  2096. var expected = @"
  2097. ┌──────────────────┐
  2098. │012345678910111213│
  2099. │012345678910111213│
  2100. │012345678910111213│
  2101. │012345678910111213│
  2102. │012345678910111213│
  2103. │012345678910111213│
  2104. │012345678910111213│
  2105. │012345678910111213│
  2106. └──────────────────┘
  2107. ";
  2108. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2109. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  2110. view.Clear (view.Bounds);
  2111. expected = @"
  2112. ";
  2113. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2114. Assert.Equal (Rect.Empty, pos);
  2115. }
  2116. [Fact, AutoInitShutdown]
  2117. public void Width_Height_SetMinWidthHeight_Narrow_Wide_Runes ()
  2118. {
  2119. var text = $"First line{Environment.NewLine}Second line";
  2120. var horizontalView = new View () {
  2121. Width = 20,
  2122. Text = text
  2123. };
  2124. var verticalView = new View () {
  2125. Y = 3,
  2126. Height = 20,
  2127. Text = text,
  2128. TextDirection = TextDirection.TopBottom_LeftRight
  2129. };
  2130. var win = new Window () {
  2131. Width = Dim.Fill (),
  2132. Height = Dim.Fill (),
  2133. Text = "Window"
  2134. };
  2135. win.Add (horizontalView, verticalView);
  2136. Application.Top.Add (win);
  2137. Application.Begin (Application.Top);
  2138. ((FakeDriver)Application.Driver).SetBufferSize (32, 32);
  2139. Assert.False (horizontalView.AutoSize);
  2140. Assert.False (verticalView.AutoSize);
  2141. Assert.Equal (new Rect (0, 0, 20, 1), horizontalView.Frame);
  2142. Assert.Equal (new Rect (0, 3, 1, 20), verticalView.Frame);
  2143. var expected = @"
  2144. ┌──────────────────────────────┐
  2145. │First line Second li │
  2146. │ │
  2147. │ │
  2148. │F │
  2149. │i │
  2150. │r │
  2151. │s │
  2152. │t │
  2153. │ │
  2154. │l │
  2155. │i │
  2156. │n │
  2157. │e │
  2158. │ │
  2159. │S │
  2160. │e │
  2161. │c │
  2162. │o │
  2163. │n │
  2164. │d │
  2165. │ │
  2166. │l │
  2167. │i │
  2168. │ │
  2169. │ │
  2170. │ │
  2171. │ │
  2172. │ │
  2173. │ │
  2174. │ │
  2175. └──────────────────────────────┘
  2176. ";
  2177. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2178. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  2179. verticalView.Text = $"最初の行{Environment.NewLine}二行目";
  2180. Application.Top.Redraw (Application.Top.Bounds);
  2181. Assert.Equal (new Rect (0, 3, 2, 20), verticalView.Frame);
  2182. expected = @"
  2183. ┌──────────────────────────────┐
  2184. │First line Second li │
  2185. │ │
  2186. │ │
  2187. │最 │
  2188. │初 │
  2189. │の │
  2190. │行 │
  2191. │ │
  2192. │二 │
  2193. │行 │
  2194. │目 │
  2195. │ │
  2196. │ │
  2197. │ │
  2198. │ │
  2199. │ │
  2200. │ │
  2201. │ │
  2202. │ │
  2203. │ │
  2204. │ │
  2205. │ │
  2206. │ │
  2207. │ │
  2208. │ │
  2209. │ │
  2210. │ │
  2211. │ │
  2212. │ │
  2213. │ │
  2214. └──────────────────────────────┘
  2215. ";
  2216. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2217. Assert.Equal (new Rect (0, 0, 32, 32), pos);
  2218. }
  2219. [Fact, AutoInitShutdown]
  2220. public void TextDirection_Toggle ()
  2221. {
  2222. var view = new View ();
  2223. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  2224. win.Add (view);
  2225. Application.Top.Add (win);
  2226. Application.Begin (Application.Top);
  2227. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  2228. Assert.False (view.AutoSize);
  2229. Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection);
  2230. Assert.Equal (Rect.Empty, view.Frame);
  2231. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2232. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2233. Assert.Equal ("Dim.Absolute(0)", view.Width.ToString ());
  2234. Assert.Equal ("Dim.Absolute(0)", view.Height.ToString ());
  2235. var expected = @"
  2236. ┌────────────────────┐
  2237. │ │
  2238. │ │
  2239. │ │
  2240. │ │
  2241. │ │
  2242. │ │
  2243. │ │
  2244. │ │
  2245. │ │
  2246. │ │
  2247. │ │
  2248. │ │
  2249. │ │
  2250. │ │
  2251. │ │
  2252. │ │
  2253. │ │
  2254. │ │
  2255. │ │
  2256. │ │
  2257. └────────────────────┘
  2258. ";
  2259. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2260. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2261. view.Text = "Hello World";
  2262. view.Width = 11;
  2263. Application.Refresh ();
  2264. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  2265. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2266. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2267. Assert.Equal ("Dim.Absolute(11)", view.Width.ToString ());
  2268. Assert.Equal ("Dim.Absolute(0)", view.Height.ToString ());
  2269. expected = @"
  2270. ┌────────────────────┐
  2271. │Hello World │
  2272. │ │
  2273. │ │
  2274. │ │
  2275. │ │
  2276. │ │
  2277. │ │
  2278. │ │
  2279. │ │
  2280. │ │
  2281. │ │
  2282. │ │
  2283. │ │
  2284. │ │
  2285. │ │
  2286. │ │
  2287. │ │
  2288. │ │
  2289. │ │
  2290. │ │
  2291. └────────────────────┘
  2292. ";
  2293. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2294. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2295. view.AutoSize = true;
  2296. view.Text = "Hello Worlds";
  2297. Application.Refresh ();
  2298. Assert.Equal (new Rect (0, 0, 12, 1), view.Frame);
  2299. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2300. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2301. Assert.Equal ("Dim.Absolute(11)", view.Width.ToString ());
  2302. Assert.Equal ("Dim.Absolute(0)", view.Height.ToString ());
  2303. expected = @"
  2304. ┌────────────────────┐
  2305. │Hello Worlds │
  2306. │ │
  2307. │ │
  2308. │ │
  2309. │ │
  2310. │ │
  2311. │ │
  2312. │ │
  2313. │ │
  2314. │ │
  2315. │ │
  2316. │ │
  2317. │ │
  2318. │ │
  2319. │ │
  2320. │ │
  2321. │ │
  2322. │ │
  2323. │ │
  2324. │ │
  2325. └────────────────────┘
  2326. ";
  2327. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2328. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2329. view.TextDirection = TextDirection.TopBottom_LeftRight;
  2330. Application.Refresh ();
  2331. Assert.Equal (new Rect (0, 0, 11, 12), view.Frame);
  2332. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2333. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2334. Assert.Equal ("Dim.Absolute(11)", view.Width.ToString ());
  2335. Assert.Equal ("Dim.Absolute(0)", view.Height.ToString ());
  2336. expected = @"
  2337. ┌────────────────────┐
  2338. │H │
  2339. │e │
  2340. │l │
  2341. │l │
  2342. │o │
  2343. │ │
  2344. │W │
  2345. │o │
  2346. │r │
  2347. │l │
  2348. │d │
  2349. │s │
  2350. │ │
  2351. │ │
  2352. │ │
  2353. │ │
  2354. │ │
  2355. │ │
  2356. │ │
  2357. │ │
  2358. └────────────────────┘
  2359. ";
  2360. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2361. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2362. view.AutoSize = false;
  2363. view.Height = 1;
  2364. Application.Refresh ();
  2365. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  2366. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2367. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2368. Assert.Equal ("Dim.Absolute(11)", view.Width.ToString ());
  2369. Assert.Equal ("Dim.Absolute(1)", view.Height.ToString ());
  2370. expected = @"
  2371. ┌────────────────────┐
  2372. │HelloWorlds │
  2373. │ │
  2374. │ │
  2375. │ │
  2376. │ │
  2377. │ │
  2378. │ │
  2379. │ │
  2380. │ │
  2381. │ │
  2382. │ │
  2383. │ │
  2384. │ │
  2385. │ │
  2386. │ │
  2387. │ │
  2388. │ │
  2389. │ │
  2390. │ │
  2391. │ │
  2392. └────────────────────┘
  2393. ";
  2394. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2395. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2396. view.PreserveTrailingSpaces = true;
  2397. Application.Refresh ();
  2398. Assert.Equal (new Rect (0, 0, 11, 1), view.Frame);
  2399. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2400. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2401. Assert.Equal ("Dim.Absolute(11)", view.Width.ToString ());
  2402. Assert.Equal ("Dim.Absolute(1)", view.Height.ToString ());
  2403. expected = @"
  2404. ┌────────────────────┐
  2405. │Hello World │
  2406. │ │
  2407. │ │
  2408. │ │
  2409. │ │
  2410. │ │
  2411. │ │
  2412. │ │
  2413. │ │
  2414. │ │
  2415. │ │
  2416. │ │
  2417. │ │
  2418. │ │
  2419. │ │
  2420. │ │
  2421. │ │
  2422. │ │
  2423. │ │
  2424. │ │
  2425. └────────────────────┘
  2426. ";
  2427. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2428. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2429. view.PreserveTrailingSpaces = false;
  2430. var f = view.Frame;
  2431. view.Width = f.Height;
  2432. view.Height = f.Width;
  2433. view.TextDirection = TextDirection.TopBottom_LeftRight;
  2434. Application.Refresh ();
  2435. Assert.Equal (new Rect (0, 0, 1, 11), view.Frame);
  2436. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2437. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2438. Assert.Equal ("Dim.Absolute(1)", view.Width.ToString ());
  2439. Assert.Equal ("Dim.Absolute(11)", view.Height.ToString ());
  2440. expected = @"
  2441. ┌────────────────────┐
  2442. │H │
  2443. │e │
  2444. │l │
  2445. │l │
  2446. │o │
  2447. │ │
  2448. │W │
  2449. │o │
  2450. │r │
  2451. │l │
  2452. │d │
  2453. │ │
  2454. │ │
  2455. │ │
  2456. │ │
  2457. │ │
  2458. │ │
  2459. │ │
  2460. │ │
  2461. │ │
  2462. └────────────────────┘
  2463. ";
  2464. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2465. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2466. view.AutoSize = true;
  2467. Application.Refresh ();
  2468. Assert.Equal (new Rect (0, 0, 1, 12), view.Frame);
  2469. Assert.Equal ("Pos.Absolute(0)", view.X.ToString ());
  2470. Assert.Equal ("Pos.Absolute(0)", view.Y.ToString ());
  2471. Assert.Equal ("Dim.Absolute(1)", view.Width.ToString ());
  2472. Assert.Equal ("Dim.Absolute(11)", view.Height.ToString ());
  2473. expected = @"
  2474. ┌────────────────────┐
  2475. │H │
  2476. │e │
  2477. │l │
  2478. │l │
  2479. │o │
  2480. │ │
  2481. │W │
  2482. │o │
  2483. │r │
  2484. │l │
  2485. │d │
  2486. │s │
  2487. │ │
  2488. │ │
  2489. │ │
  2490. │ │
  2491. │ │
  2492. │ │
  2493. │ │
  2494. │ │
  2495. └────────────────────┘
  2496. ";
  2497. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2498. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2499. }
  2500. [Fact, AutoInitShutdown]
  2501. public void Width_Height_AutoSize_True_Stay_True_If_TextFormatter_Size_Fit ()
  2502. {
  2503. var text = $"Fi_nish 終";
  2504. var horizontalView = new View () {
  2505. AutoSize = true,
  2506. HotKeySpecifier = '_',
  2507. Text = text
  2508. };
  2509. var verticalView = new View () {
  2510. Y = 3,
  2511. AutoSize = true,
  2512. HotKeySpecifier = '_',
  2513. Text = text,
  2514. TextDirection = TextDirection.TopBottom_LeftRight
  2515. };
  2516. var win = new Window () {
  2517. Width = Dim.Fill (),
  2518. Height = Dim.Fill (),
  2519. Text = "Window"
  2520. };
  2521. win.Add (horizontalView, verticalView);
  2522. Application.Top.Add (win);
  2523. Application.Begin (Application.Top);
  2524. ((FakeDriver)Application.Driver).SetBufferSize (22, 22);
  2525. Assert.True (horizontalView.AutoSize);
  2526. Assert.True (verticalView.AutoSize);
  2527. Assert.Equal (new Size (10, 1), horizontalView.TextFormatter.Size);
  2528. Assert.Equal (new Size (2, 9), verticalView.TextFormatter.Size);
  2529. Assert.Equal (new Rect (0, 0, 9, 1), horizontalView.Frame);
  2530. Assert.Equal ("Pos.Absolute(0)", horizontalView.X.ToString ());
  2531. Assert.Equal ("Pos.Absolute(0)", horizontalView.Y.ToString ());
  2532. Assert.Equal ("Dim.Absolute(9)", horizontalView.Width.ToString ());
  2533. Assert.Equal ("Dim.Absolute(1)", horizontalView.Height.ToString ());
  2534. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  2535. Assert.Equal ("Pos.Absolute(0)", verticalView.X.ToString ());
  2536. Assert.Equal ("Pos.Absolute(3)", verticalView.Y.ToString ());
  2537. Assert.Equal ("Dim.Absolute(2)", verticalView.Width.ToString ());
  2538. Assert.Equal ("Dim.Absolute(8)", verticalView.Height.ToString ());
  2539. var expected = @"
  2540. ┌────────────────────┐
  2541. │Finish 終 │
  2542. │ │
  2543. │ │
  2544. │F │
  2545. │i │
  2546. │n │
  2547. │i │
  2548. │s │
  2549. │h │
  2550. │ │
  2551. │終 │
  2552. │ │
  2553. │ │
  2554. │ │
  2555. │ │
  2556. │ │
  2557. │ │
  2558. │ │
  2559. │ │
  2560. │ │
  2561. └────────────────────┘
  2562. ";
  2563. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2564. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2565. verticalView.Text = $"最初_の行二行目";
  2566. Application.Top.Redraw (Application.Top.Bounds);
  2567. Assert.True (horizontalView.AutoSize);
  2568. Assert.True (verticalView.AutoSize);
  2569. // height was initialized with 8 and is kept as minimum
  2570. Assert.Equal (new Rect (0, 3, 2, 8), verticalView.Frame);
  2571. Assert.Equal ("Pos.Absolute(0)", verticalView.X.ToString ());
  2572. Assert.Equal ("Pos.Absolute(3)", verticalView.Y.ToString ());
  2573. Assert.Equal ("Dim.Absolute(2)", verticalView.Width.ToString ());
  2574. Assert.Equal ("Dim.Absolute(8)", verticalView.Height.ToString ());
  2575. expected = @"
  2576. ┌────────────────────┐
  2577. │Finish 終 │
  2578. │ │
  2579. │ │
  2580. │最 │
  2581. │初 │
  2582. │の │
  2583. │行 │
  2584. │二 │
  2585. │行 │
  2586. │目 │
  2587. │ │
  2588. │ │
  2589. │ │
  2590. │ │
  2591. │ │
  2592. │ │
  2593. │ │
  2594. │ │
  2595. │ │
  2596. │ │
  2597. └────────────────────┘
  2598. ";
  2599. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2600. Assert.Equal (new Rect (0, 0, 22, 22), pos);
  2601. }
  2602. [Fact, AutoInitShutdown]
  2603. public void AutoSize_Stays_True_Center_HotKeySpecifier ()
  2604. {
  2605. var btn = new Button () {
  2606. X = Pos.Center (),
  2607. Y = Pos.Center (),
  2608. Text = "Say He_llo 你"
  2609. };
  2610. var win = new Window () {
  2611. Width = Dim.Fill (),
  2612. Height = Dim.Fill (),
  2613. Title = "Test Demo 你"
  2614. };
  2615. win.Add (btn);
  2616. Application.Top.Add (win);
  2617. Assert.True (btn.AutoSize);
  2618. Application.Begin (Application.Top);
  2619. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  2620. var expected = @"
  2621. ┌ Test Demo 你 ──────────────┐
  2622. │ │
  2623. │ [ Say Hello 你 ] │
  2624. │ │
  2625. └────────────────────────────┘
  2626. ";
  2627. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2628. Assert.True (btn.AutoSize);
  2629. btn.Text = "Say He_llo 你 changed";
  2630. Assert.True (btn.AutoSize);
  2631. Application.Refresh ();
  2632. expected = @"
  2633. ┌ Test Demo 你 ──────────────┐
  2634. │ │
  2635. │ [ Say Hello 你 changed ] │
  2636. │ │
  2637. └────────────────────────────┘
  2638. ";
  2639. GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  2640. }
  2641. [Fact]
  2642. public void GetTextFormatterBoundsSize_GetBoundsTextFormatterSize_HotKeySpecifier ()
  2643. {
  2644. var text = "Say Hello 你";
  2645. var horizontalView = new View () { Text = text, AutoSize = true, HotKeySpecifier = '_' };
  2646. var verticalView = new View () {
  2647. Text = text,
  2648. AutoSize = true,
  2649. HotKeySpecifier = '_',
  2650. TextDirection = TextDirection.TopBottom_LeftRight
  2651. };
  2652. Assert.True (horizontalView.AutoSize);
  2653. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  2654. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  2655. Assert.Equal (new Size (12, 1), horizontalView.GetBoundsTextFormatterSize ());
  2656. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  2657. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  2658. Assert.True (verticalView.AutoSize);
  2659. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  2660. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  2661. Assert.Equal (new Size (2, 11), verticalView.GetBoundsTextFormatterSize ());
  2662. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  2663. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  2664. text = "Say He_llo 你";
  2665. horizontalView.Text = text;
  2666. verticalView.Text = text;
  2667. Assert.True (horizontalView.AutoSize);
  2668. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  2669. Assert.Equal (new Size (12, 1), horizontalView.GetTextFormatterBoundsSize ());
  2670. Assert.Equal (new Size (13, 1), horizontalView.GetBoundsTextFormatterSize ());
  2671. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetBoundsTextFormatterSize ());
  2672. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetTextFormatterBoundsSize ());
  2673. Assert.True (verticalView.AutoSize);
  2674. Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  2675. Assert.Equal (new Size (2, 11), verticalView.GetTextFormatterBoundsSize ());
  2676. Assert.Equal (new Size (2, 12), verticalView.GetBoundsTextFormatterSize ());
  2677. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetBoundsTextFormatterSize ());
  2678. Assert.Equal (verticalView.Frame.Size, verticalView.GetTextFormatterBoundsSize ());
  2679. }
  2680. [Fact, AutoInitShutdown]
  2681. public void AutoSize_False_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  2682. {
  2683. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = false, Width = 10, Height = 5 };
  2684. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = false };
  2685. var view3 = new View () { AutoSize = false, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  2686. var view4 = new View () {
  2687. Text = "Say Hello view4 你",
  2688. AutoSize = false,
  2689. Width = 10,
  2690. Height = 5,
  2691. TextDirection = TextDirection.TopBottom_LeftRight
  2692. };
  2693. var view5 = new View () {
  2694. Text = "Say Hello view5 你",
  2695. Width = 10,
  2696. Height = 5,
  2697. AutoSize = false,
  2698. TextDirection = TextDirection.TopBottom_LeftRight
  2699. };
  2700. var view6 = new View () {
  2701. AutoSize = false,
  2702. Width = 10,
  2703. Height = 5,
  2704. TextDirection = TextDirection.TopBottom_LeftRight,
  2705. Text = "Say Hello view6 你",
  2706. };
  2707. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  2708. Assert.False (view1.IsInitialized);
  2709. Assert.False (view2.IsInitialized);
  2710. Assert.False (view3.IsInitialized);
  2711. Assert.False (view4.IsInitialized);
  2712. Assert.False (view5.IsInitialized);
  2713. Assert.False (view1.AutoSize);
  2714. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  2715. Assert.Equal ("Dim.Absolute(10)", view1.Width.ToString ());
  2716. Assert.Equal ("Dim.Absolute(5)", view1.Height.ToString ());
  2717. Assert.False (view2.AutoSize);
  2718. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  2719. Assert.Equal ("Dim.Absolute(10)", view2.Width.ToString ());
  2720. Assert.Equal ("Dim.Absolute(5)", view2.Height.ToString ());
  2721. Assert.False (view3.AutoSize);
  2722. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  2723. Assert.Equal ("Dim.Absolute(10)", view3.Width.ToString ());
  2724. Assert.Equal ("Dim.Absolute(5)", view3.Height.ToString ());
  2725. Assert.False (view4.AutoSize);
  2726. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  2727. Assert.Equal ("Dim.Absolute(10)", view4.Width.ToString ());
  2728. Assert.Equal ("Dim.Absolute(5)", view4.Height.ToString ());
  2729. Assert.False (view5.AutoSize);
  2730. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  2731. Assert.Equal ("Dim.Absolute(10)", view5.Width.ToString ());
  2732. Assert.Equal ("Dim.Absolute(5)", view5.Height.ToString ());
  2733. Assert.False (view6.AutoSize);
  2734. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  2735. Assert.Equal ("Dim.Absolute(10)", view6.Width.ToString ());
  2736. Assert.Equal ("Dim.Absolute(5)", view6.Height.ToString ());
  2737. Application.Begin (Application.Top);
  2738. Assert.True (view1.IsInitialized);
  2739. Assert.True (view2.IsInitialized);
  2740. Assert.True (view3.IsInitialized);
  2741. Assert.True (view4.IsInitialized);
  2742. Assert.True (view5.IsInitialized);
  2743. Assert.False (view1.AutoSize);
  2744. Assert.Equal (new Rect (0, 0, 10, 5), view1.Frame);
  2745. Assert.Equal ("Dim.Absolute(10)", view1.Width.ToString ());
  2746. Assert.Equal ("Dim.Absolute(5)", view1.Height.ToString ());
  2747. Assert.False (view2.AutoSize);
  2748. Assert.Equal (new Rect (0, 0, 10, 5), view2.Frame);
  2749. Assert.Equal ("Dim.Absolute(10)", view2.Width.ToString ());
  2750. Assert.Equal ("Dim.Absolute(5)", view2.Height.ToString ());
  2751. Assert.False (view3.AutoSize);
  2752. Assert.Equal (new Rect (0, 0, 10, 5), view3.Frame);
  2753. Assert.Equal ("Dim.Absolute(10)", view3.Width.ToString ());
  2754. Assert.Equal ("Dim.Absolute(5)", view3.Height.ToString ());
  2755. Assert.False (view4.AutoSize);
  2756. Assert.Equal (new Rect (0, 0, 10, 5), view4.Frame);
  2757. Assert.Equal ("Dim.Absolute(10)", view4.Width.ToString ());
  2758. Assert.Equal ("Dim.Absolute(5)", view4.Height.ToString ());
  2759. Assert.False (view5.AutoSize);
  2760. Assert.Equal (new Rect (0, 0, 10, 5), view5.Frame);
  2761. Assert.Equal ("Dim.Absolute(10)", view5.Width.ToString ());
  2762. Assert.Equal ("Dim.Absolute(5)", view5.Height.ToString ());
  2763. Assert.False (view6.AutoSize);
  2764. Assert.Equal (new Rect (0, 0, 10, 5), view6.Frame);
  2765. Assert.Equal ("Dim.Absolute(10)", view6.Width.ToString ());
  2766. Assert.Equal ("Dim.Absolute(5)", view6.Height.ToString ());
  2767. }
  2768. [Fact, AutoInitShutdown]
  2769. public void AutoSize_True_Equal_Before_And_After_IsInitialized_With_Differents_Orders ()
  2770. {
  2771. var view1 = new View () { Text = "Say Hello view1 你", AutoSize = true, Width = 10, Height = 5 };
  2772. var view2 = new View () { Text = "Say Hello view2 你", Width = 10, Height = 5, AutoSize = true };
  2773. var view3 = new View () { AutoSize = true, Width = 10, Height = 5, Text = "Say Hello view3 你" };
  2774. var view4 = new View () {
  2775. Text = "Say Hello view4 你",
  2776. AutoSize = true,
  2777. Width = 10,
  2778. Height = 5,
  2779. TextDirection = TextDirection.TopBottom_LeftRight
  2780. };
  2781. var view5 = new View () {
  2782. Text = "Say Hello view5 你",
  2783. Width = 10,
  2784. Height = 5,
  2785. AutoSize = true,
  2786. TextDirection = TextDirection.TopBottom_LeftRight
  2787. };
  2788. var view6 = new View () {
  2789. AutoSize = true,
  2790. Width = 10,
  2791. Height = 5,
  2792. TextDirection = TextDirection.TopBottom_LeftRight,
  2793. Text = "Say Hello view6 你",
  2794. };
  2795. Application.Top.Add (view1, view2, view3, view4, view5, view6);
  2796. Assert.False (view1.IsInitialized);
  2797. Assert.False (view2.IsInitialized);
  2798. Assert.False (view3.IsInitialized);
  2799. Assert.False (view4.IsInitialized);
  2800. Assert.False (view5.IsInitialized);
  2801. Assert.True (view1.AutoSize);
  2802. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  2803. Assert.Equal ("Dim.Absolute(10)", view1.Width.ToString ());
  2804. Assert.Equal ("Dim.Absolute(5)", view1.Height.ToString ());
  2805. Assert.True (view2.AutoSize);
  2806. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  2807. Assert.Equal ("Dim.Absolute(10)", view2.Width.ToString ());
  2808. Assert.Equal ("Dim.Absolute(5)", view2.Height.ToString ());
  2809. Assert.True (view3.AutoSize);
  2810. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  2811. Assert.Equal ("Dim.Absolute(10)", view3.Width.ToString ());
  2812. Assert.Equal ("Dim.Absolute(5)", view3.Height.ToString ());
  2813. Assert.True (view4.AutoSize);
  2814. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  2815. Assert.Equal ("Dim.Absolute(10)", view4.Width.ToString ());
  2816. Assert.Equal ("Dim.Absolute(5)", view4.Height.ToString ());
  2817. Assert.True (view5.AutoSize);
  2818. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  2819. Assert.Equal ("Dim.Absolute(10)", view5.Width.ToString ());
  2820. Assert.Equal ("Dim.Absolute(5)", view5.Height.ToString ());
  2821. Assert.True (view6.AutoSize);
  2822. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  2823. Assert.Equal ("Dim.Absolute(10)", view6.Width.ToString ());
  2824. Assert.Equal ("Dim.Absolute(5)", view6.Height.ToString ());
  2825. Application.Begin (Application.Top);
  2826. Assert.True (view1.IsInitialized);
  2827. Assert.True (view2.IsInitialized);
  2828. Assert.True (view3.IsInitialized);
  2829. Assert.True (view4.IsInitialized);
  2830. Assert.True (view5.IsInitialized);
  2831. Assert.True (view1.AutoSize);
  2832. Assert.Equal (new Rect (0, 0, 18, 5), view1.Frame);
  2833. Assert.Equal ("Dim.Absolute(10)", view1.Width.ToString ());
  2834. Assert.Equal ("Dim.Absolute(5)", view1.Height.ToString ());
  2835. Assert.True (view2.AutoSize);
  2836. Assert.Equal (new Rect (0, 0, 18, 5), view2.Frame);
  2837. Assert.Equal ("Dim.Absolute(10)", view2.Width.ToString ());
  2838. Assert.Equal ("Dim.Absolute(5)", view2.Height.ToString ());
  2839. Assert.True (view3.AutoSize);
  2840. Assert.Equal (new Rect (0, 0, 18, 5), view3.Frame);
  2841. Assert.Equal ("Dim.Absolute(10)", view3.Width.ToString ());
  2842. Assert.Equal ("Dim.Absolute(5)", view3.Height.ToString ());
  2843. Assert.True (view4.AutoSize);
  2844. Assert.Equal (new Rect (0, 0, 10, 17), view4.Frame);
  2845. Assert.Equal ("Dim.Absolute(10)", view4.Width.ToString ());
  2846. Assert.Equal ("Dim.Absolute(5)", view4.Height.ToString ());
  2847. Assert.True (view5.AutoSize);
  2848. Assert.Equal (new Rect (0, 0, 10, 17), view5.Frame);
  2849. Assert.Equal ("Dim.Absolute(10)", view5.Width.ToString ());
  2850. Assert.Equal ("Dim.Absolute(5)", view5.Height.ToString ());
  2851. Assert.True (view6.AutoSize);
  2852. Assert.Equal (new Rect (0, 0, 10, 17), view6.Frame);
  2853. Assert.Equal ("Dim.Absolute(10)", view6.Width.ToString ());
  2854. Assert.Equal ("Dim.Absolute(5)", view6.Height.ToString ());
  2855. }
  2856. [Fact, AutoInitShutdown]
  2857. public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
  2858. {
  2859. var view1 = new View (new Rect (0, 0, 10, 0)) { Text = "Say Hello view1 你", AutoSize = true };
  2860. var view2 = new View (new Rect (0, 0, 0, 10)) {
  2861. Text = "Say Hello view2 你",
  2862. AutoSize = true,
  2863. TextDirection = TextDirection.TopBottom_LeftRight
  2864. };
  2865. Application.Top.Add (view1, view2);
  2866. var rs = Application.Begin (Application.Top);
  2867. Assert.True (view1.AutoSize);
  2868. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  2869. Assert.Equal (new Rect (0, 0, 18, 1), view1.Frame);
  2870. Assert.Equal ("Pos.Absolute(0)", view1.X.ToString ());
  2871. Assert.Equal ("Pos.Absolute(0)", view1.Y.ToString ());
  2872. Assert.Equal ("Dim.Absolute(18)", view1.Width.ToString ());
  2873. Assert.Equal ("Dim.Absolute(1)", view1.Height.ToString ());
  2874. Assert.True (view2.AutoSize);
  2875. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  2876. Assert.Equal (new Rect (0, 0, 2, 17), view2.Frame);
  2877. Assert.Equal ("Pos.Absolute(0)", view2.X.ToString ());
  2878. Assert.Equal ("Pos.Absolute(0)", view2.Y.ToString ());
  2879. Assert.Equal ("Dim.Absolute(2)", view2.Width.ToString ());
  2880. Assert.Equal ("Dim.Absolute(17)", view2.Height.ToString ());
  2881. view1.Frame = new Rect (0, 0, 25, 4);
  2882. bool firstIteration = false;
  2883. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  2884. Assert.True (view1.AutoSize);
  2885. Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
  2886. Assert.Equal (new Rect (0, 0, 25, 4), view1.Frame);
  2887. Assert.Equal ("Pos.Absolute(0)", view1.X.ToString ());
  2888. Assert.Equal ("Pos.Absolute(0)", view1.Y.ToString ());
  2889. Assert.Equal ("Dim.Absolute(18)", view1.Width.ToString ());
  2890. Assert.Equal ("Dim.Absolute(1)", view1.Height.ToString ());
  2891. view2.Frame = new Rect (0, 0, 1, 25);
  2892. Application.RunMainLoopIteration (ref rs, true, ref firstIteration);
  2893. Assert.True (view2.AutoSize);
  2894. Assert.Equal (LayoutStyle.Absolute, view2.LayoutStyle);
  2895. Assert.Equal (new Rect (0, 0, 1, 25), view2.Frame);
  2896. Assert.Equal ("Pos.Absolute(0)", view2.X.ToString ());
  2897. Assert.Equal ("Pos.Absolute(0)", view2.Y.ToString ());
  2898. Assert.Equal ("Dim.Absolute(2)", view2.Width.ToString ());
  2899. Assert.Equal ("Dim.Absolute(17)", view2.Height.ToString ());
  2900. }
  2901. [Fact, AutoInitShutdown]
  2902. public void Pos_Dim_Are_Null_If_Not_Initialized_On_Constructor_IsAdded_False ()
  2903. {
  2904. var top = Application.Top;
  2905. var view1 = new View ();
  2906. Assert.False (view1.IsAdded);
  2907. Assert.Null (view1.X);
  2908. Assert.Null (view1.Y);
  2909. Assert.Null (view1.Width);
  2910. Assert.Null (view1.Height);
  2911. top.Add (view1);
  2912. Assert.True (view1.IsAdded);
  2913. Assert.Equal ("Pos.Absolute(0)", view1.X.ToString ());
  2914. Assert.Equal ("Pos.Absolute(0)", view1.Y.ToString ());
  2915. Assert.Equal ("Dim.Absolute(0)", view1.Width.ToString ());
  2916. Assert.Equal ("Dim.Absolute(0)", view1.Height.ToString ());
  2917. var view2 = new View () {
  2918. X = Pos.Center (),
  2919. Y = Pos.Center (),
  2920. Width = Dim.Fill (),
  2921. Height = Dim.Fill ()
  2922. };
  2923. Assert.False (view2.IsAdded);
  2924. Assert.Equal ("Pos.Center", view2.X.ToString ());
  2925. Assert.Equal ("Pos.Center", view2.Y.ToString ());
  2926. Assert.Equal ("Dim.Fill(margin=0)", view2.Width.ToString ());
  2927. Assert.Equal ("Dim.Fill(margin=0)", view2.Height.ToString ());
  2928. top.Add (view2);
  2929. Assert.True (view2.IsAdded);
  2930. Assert.Equal ("Pos.Center", view2.X.ToString ());
  2931. Assert.Equal ("Pos.Center", view2.Y.ToString ());
  2932. Assert.Equal ("Dim.Fill(margin=0)", view2.Width.ToString ());
  2933. Assert.Equal ("Dim.Fill(margin=0)", view2.Height.ToString ());
  2934. }
  2935. [Fact]
  2936. public void IsAdded_Added_Removed ()
  2937. {
  2938. var top = new Toplevel ();
  2939. var view = new View ();
  2940. Assert.False (view.IsAdded);
  2941. top.Add (view);
  2942. Assert.True (view.IsAdded);
  2943. top.Remove (view);
  2944. Assert.False (view.IsAdded);
  2945. }
  2946. [Fact]
  2947. public void AutoSize_Layout_Absolute_Without_Add_Horizontal_Narrow ()
  2948. {
  2949. var view = new View (new Rect (0, 0, 10, 1)) {
  2950. Text = "Test"
  2951. };
  2952. Assert.False (view.IsAdded);
  2953. Assert.False (view.AutoSize);
  2954. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  2955. Assert.Equal ("Test", view.TextFormatter.Text);
  2956. view.Text = "First line\nSecond line";
  2957. Assert.False (view.AutoSize);
  2958. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  2959. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  2960. view.AutoSize = true;
  2961. Assert.True (view.AutoSize);
  2962. Assert.Equal (new Rect (0, 0, 11, 2), view.Frame);
  2963. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  2964. view.AutoSize = false;
  2965. Assert.False (view.AutoSize);
  2966. Assert.Equal (new Rect (0, 0, 11, 2), view.Frame);
  2967. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  2968. }
  2969. [Fact, AutoInitShutdown]
  2970. public void AutoSize_Layout_Absolute_With_Add_Horizontal_Narrow ()
  2971. {
  2972. var view = new View (new Rect (0, 0, 10, 1)) {
  2973. Text = "Test"
  2974. };
  2975. Application.Top.Add (view);
  2976. Application.Begin (Application.Top);
  2977. Assert.True (view.IsAdded);
  2978. Assert.False (view.AutoSize);
  2979. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  2980. Assert.Equal ("Test", view.TextFormatter.Text);
  2981. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  2982. Test
  2983. ", output);
  2984. view.Text = "First line\nSecond line";
  2985. Assert.False (view.AutoSize);
  2986. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  2987. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  2988. Application.Refresh ();
  2989. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  2990. First line
  2991. ", output);
  2992. view.AutoSize = true;
  2993. Assert.True (view.AutoSize);
  2994. Assert.Equal (new Rect (0, 0, 11, 2), view.Frame);
  2995. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  2996. Application.Refresh ();
  2997. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  2998. First line
  2999. Second line
  3000. ", output);
  3001. view.AutoSize = false;
  3002. Assert.False (view.AutoSize);
  3003. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3004. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3005. Application.Refresh ();
  3006. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3007. First line
  3008. ", output);
  3009. }
  3010. [Fact]
  3011. public void AutoSize_Layout_Absolute_Without_Add_Vertical_Narrow ()
  3012. {
  3013. var view = new View (new Rect (0, 0, 1, 10)) {
  3014. Text = "Test",
  3015. TextDirection = TextDirection.TopBottom_LeftRight
  3016. };
  3017. Assert.False (view.IsAdded);
  3018. Assert.False (view.AutoSize);
  3019. Assert.Equal (new Rect (0, 0, 1, 10), view.Frame);
  3020. Assert.Equal ("Test", view.TextFormatter.Text);
  3021. view.Text = "First line\nSecond line";
  3022. Assert.False (view.AutoSize);
  3023. Assert.Equal (new Rect (0, 0, 1, 10), view.Frame);
  3024. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3025. view.AutoSize = true;
  3026. Assert.True (view.AutoSize);
  3027. Assert.Equal (new Rect (0, 0, 2, 11), view.Frame);
  3028. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3029. view.AutoSize = false;
  3030. Assert.False (view.AutoSize);
  3031. Assert.Equal (new Rect (0, 0, 2, 11), view.Frame);
  3032. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3033. }
  3034. [Fact, AutoInitShutdown]
  3035. public void AutoSize_Layout_Absolute_With_Add_Vertical_Narrow ()
  3036. {
  3037. var view = new View (new Rect (0, 0, 1, 10)) {
  3038. Text = "Test",
  3039. TextDirection = TextDirection.TopBottom_LeftRight
  3040. };
  3041. Application.Top.Add (view);
  3042. Application.Begin (Application.Top);
  3043. Assert.True (view.IsAdded);
  3044. Assert.False (view.AutoSize);
  3045. Assert.Equal (new Rect (0, 0, 1, 10), view.Frame);
  3046. Assert.Equal ("Test", view.TextFormatter.Text);
  3047. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3048. T
  3049. e
  3050. s
  3051. t
  3052. ", output);
  3053. view.Text = "First line\nSecond line";
  3054. Assert.False (view.AutoSize);
  3055. Assert.Equal (new Rect (0, 0, 1, 10), view.Frame);
  3056. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3057. Application.Refresh ();
  3058. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3059. F
  3060. i
  3061. r
  3062. s
  3063. t
  3064. l
  3065. i
  3066. n
  3067. e
  3068. ", output);
  3069. view.AutoSize = true;
  3070. Assert.True (view.AutoSize);
  3071. Assert.Equal (new Rect (0, 0, 2, 11), view.Frame);
  3072. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3073. Application.Refresh ();
  3074. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3075. FS
  3076. ie
  3077. rc
  3078. so
  3079. tn
  3080. d
  3081. l
  3082. il
  3083. ni
  3084. en
  3085. e
  3086. ", output);
  3087. view.AutoSize = false;
  3088. Assert.False (view.AutoSize);
  3089. Assert.Equal (new Rect (0, 0, 1, 10), view.Frame);
  3090. Assert.Equal ("First line\nSecond line", view.TextFormatter.Text);
  3091. Application.Refresh ();
  3092. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3093. F
  3094. i
  3095. r
  3096. s
  3097. t
  3098. l
  3099. i
  3100. n
  3101. e
  3102. ", output);
  3103. }
  3104. [Fact]
  3105. public void AutoSize_Layout_Absolute_Without_Add_Horizontal_Wide ()
  3106. {
  3107. var view = new View (new Rect (0, 0, 10, 1)) {
  3108. Text = "Test 你"
  3109. };
  3110. Assert.False (view.IsAdded);
  3111. Assert.False (view.AutoSize);
  3112. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3113. Assert.Equal ("Test 你", view.TextFormatter.Text);
  3114. view.Text = "First line 你\nSecond line 你";
  3115. Assert.False (view.AutoSize);
  3116. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3117. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3118. view.AutoSize = true;
  3119. Assert.True (view.AutoSize);
  3120. Assert.Equal (new Rect (0, 0, 14, 2), view.Frame);
  3121. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3122. view.AutoSize = false;
  3123. Assert.False (view.AutoSize);
  3124. Assert.Equal (new Rect (0, 0, 14, 2), view.Frame);
  3125. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3126. }
  3127. [Fact, AutoInitShutdown]
  3128. public void AutoSize_Layout_Absolute_With_Add_Horizontal_Wide ()
  3129. {
  3130. var view = new View (new Rect (0, 0, 10, 1)) {
  3131. Text = "Test 你"
  3132. };
  3133. Application.Top.Add (view);
  3134. Application.Begin (Application.Top);
  3135. Assert.True (view.IsAdded);
  3136. Assert.False (view.AutoSize);
  3137. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3138. Assert.Equal ("Test 你", view.TextFormatter.Text);
  3139. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3140. Test 你
  3141. ", output);
  3142. view.Text = "First line 你\nSecond line 你";
  3143. Assert.False (view.AutoSize);
  3144. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3145. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3146. Application.Refresh ();
  3147. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3148. First line
  3149. ", output);
  3150. view.AutoSize = true;
  3151. Assert.True (view.AutoSize);
  3152. Assert.Equal (new Rect (0, 0, 14, 2), view.Frame);
  3153. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3154. Application.Refresh ();
  3155. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3156. First line 你
  3157. Second line 你
  3158. ", output);
  3159. view.AutoSize = false;
  3160. Assert.False (view.AutoSize);
  3161. Assert.Equal (new Rect (0, 0, 10, 1), view.Frame);
  3162. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3163. Application.Refresh ();
  3164. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3165. First line
  3166. ", output);
  3167. }
  3168. [Fact]
  3169. public void AutoSize_Layout_Absolute_Without_Add_Vertical_Wide ()
  3170. {
  3171. var view = new View (new Rect (0, 0, 1, 10)) {
  3172. Text = "Test 你",
  3173. TextDirection = TextDirection.TopBottom_LeftRight
  3174. };
  3175. Assert.False (view.IsAdded);
  3176. Assert.False (view.AutoSize);
  3177. // SetMinWidthHeight ensuring the minimum width for the wide char
  3178. Assert.Equal (new Rect (0, 0, 2, 10), view.Frame);
  3179. Assert.Equal ("Test 你", view.TextFormatter.Text);
  3180. view.Text = "First line 你\nSecond line 你";
  3181. Assert.False (view.AutoSize);
  3182. Assert.Equal (new Rect (0, 0, 2, 10), view.Frame);
  3183. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3184. view.AutoSize = true;
  3185. Assert.True (view.AutoSize);
  3186. Assert.Equal (new Rect (0, 0, 4, 13), view.Frame);
  3187. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3188. view.AutoSize = false;
  3189. Assert.False (view.AutoSize);
  3190. Assert.Equal (new Rect (0, 0, 4, 13), view.Frame);
  3191. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3192. }
  3193. [Fact, AutoInitShutdown]
  3194. public void AutoSize_Layout_Absolute_With_Add_Vertical_Wide ()
  3195. {
  3196. var view = new View (new Rect (0, 0, 1, 10)) {
  3197. Text = "Test 你",
  3198. TextDirection = TextDirection.TopBottom_LeftRight
  3199. };
  3200. Application.Top.Add (view);
  3201. Application.Begin (Application.Top);
  3202. Assert.True (view.IsAdded);
  3203. Assert.False (view.AutoSize);
  3204. // SetMinWidthHeight ensuring the minimum width for the wide char
  3205. Assert.Equal (new Rect (0, 0, 2, 10), view.Frame);
  3206. Assert.Equal ("Test 你", view.TextFormatter.Text);
  3207. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3208. T
  3209. e
  3210. s
  3211. t
  3212. ", output);
  3213. view.Text = "First line 你\nSecond line 你";
  3214. Assert.False (view.AutoSize);
  3215. Assert.Equal (new Rect (0, 0, 2, 10), view.Frame);
  3216. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3217. Application.Refresh ();
  3218. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3219. F
  3220. i
  3221. r
  3222. s
  3223. t
  3224. l
  3225. i
  3226. n
  3227. e
  3228. ", output);
  3229. view.AutoSize = true;
  3230. Assert.True (view.AutoSize);
  3231. Assert.Equal (new Rect (0, 0, 4, 13), view.Frame);
  3232. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3233. Application.Refresh ();
  3234. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3235. F S
  3236. i e
  3237. r c
  3238. s o
  3239. t n
  3240. d
  3241. l
  3242. i l
  3243. n i
  3244. e n
  3245. e
  3246. ", output);
  3247. view.AutoSize = false;
  3248. Assert.False (view.AutoSize);
  3249. Assert.Equal (new Rect (0, 0, 2, 10), view.Frame);
  3250. Assert.Equal ("First line 你\nSecond line 你", view.TextFormatter.Text);
  3251. Application.Refresh ();
  3252. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3253. F
  3254. i
  3255. r
  3256. s
  3257. t
  3258. l
  3259. i
  3260. n
  3261. e
  3262. ", output);
  3263. }
  3264. [Fact, AutoInitShutdown]
  3265. public void Visible_Clear_The_View_Output ()
  3266. {
  3267. var label = new Label ("Testing visibility.");
  3268. var win = new Window ();
  3269. win.Add (label);
  3270. var top = Application.Top;
  3271. top.Add (win);
  3272. Application.Begin (top);
  3273. Assert.True (label.Visible);
  3274. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  3275. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3276. ┌────────────────────────────┐
  3277. │Testing visibility. │
  3278. │ │
  3279. │ │
  3280. └────────────────────────────┘
  3281. ", output);
  3282. label.Visible = false;
  3283. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3284. ┌────────────────────────────┐
  3285. │ │
  3286. │ │
  3287. │ │
  3288. └────────────────────────────┘
  3289. ", output);
  3290. }
  3291. [Fact, AutoInitShutdown]
  3292. public void ClearOnVisibleFalse_Gets_Sets ()
  3293. {
  3294. 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";
  3295. var label = new Label (text);
  3296. Application.Top.Add (label);
  3297. var sbv = new ScrollBarView (label, true, false) {
  3298. Size = 100,
  3299. ClearOnVisibleFalse = false
  3300. };
  3301. Application.Begin (Application.Top);
  3302. Assert.True (sbv.Visible);
  3303. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3304. This is a tes▲
  3305. This is a tes┬
  3306. This is a tes┴
  3307. This is a tes░
  3308. This is a tes░
  3309. This is a tes▼
  3310. ", output);
  3311. sbv.Visible = false;
  3312. Assert.False (sbv.Visible);
  3313. Application.Top.Redraw (Application.Top.Bounds);
  3314. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3315. This is a test
  3316. This is a test
  3317. This is a test
  3318. This is a test
  3319. This is a test
  3320. This is a test
  3321. ", output);
  3322. sbv.Visible = true;
  3323. Assert.True (sbv.Visible);
  3324. Application.Top.Redraw (Application.Top.Bounds);
  3325. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3326. This is a tes▲
  3327. This is a tes┬
  3328. This is a tes┴
  3329. This is a tes░
  3330. This is a tes░
  3331. This is a tes▼
  3332. ", output);
  3333. sbv.ClearOnVisibleFalse = true;
  3334. sbv.Visible = false;
  3335. Assert.False (sbv.Visible);
  3336. GraphViewTests.AssertDriverContentsWithFrameAre (@"
  3337. This is a tes
  3338. This is a tes
  3339. This is a tes
  3340. This is a tes
  3341. This is a tes
  3342. This is a tes
  3343. ", output);
  3344. }
  3345. [Fact, AutoInitShutdown]
  3346. public void DrawContentComplete_Event_Is_Always_Called ()
  3347. {
  3348. var viewCalled = false;
  3349. var tvCalled = false;
  3350. var view = new View ("View") { Width = 10, Height = 10 };
  3351. view.DrawContentComplete += (e) => viewCalled = true;
  3352. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  3353. tv.DrawContentComplete += (e) => tvCalled = true;
  3354. Application.Top.Add (view, tv);
  3355. Application.Begin (Application.Top);
  3356. Assert.True (viewCalled);
  3357. Assert.True (tvCalled);
  3358. }
  3359. [Fact, AutoInitShutdown]
  3360. public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
  3361. {
  3362. var keyDown = false;
  3363. var keyPress = false;
  3364. var keyUp = false;
  3365. var view = new DerivedView ();
  3366. view.KeyDown += (e) => {
  3367. Assert.Equal (Key.a, e.KeyEvent.Key);
  3368. Assert.False (keyDown);
  3369. Assert.False (view.IsKeyDown);
  3370. e.Handled = true;
  3371. keyDown = true;
  3372. };
  3373. view.KeyPress += (e) => {
  3374. Assert.Equal (Key.a, e.KeyEvent.Key);
  3375. Assert.False (keyPress);
  3376. Assert.False (view.IsKeyPress);
  3377. e.Handled = true;
  3378. keyPress = true;
  3379. };
  3380. view.KeyUp += (e) => {
  3381. Assert.Equal (Key.a, e.KeyEvent.Key);
  3382. Assert.False (keyUp);
  3383. Assert.False (view.IsKeyUp);
  3384. e.Handled = true;
  3385. keyUp = true;
  3386. };
  3387. Application.Top.Add (view);
  3388. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
  3389. Application.Iteration += () => Application.RequestStop ();
  3390. Assert.True (view.CanFocus);
  3391. Application.Run ();
  3392. Application.Shutdown ();
  3393. Assert.True (keyDown);
  3394. Assert.True (keyPress);
  3395. Assert.True (keyUp);
  3396. Assert.False (view.IsKeyDown);
  3397. Assert.False (view.IsKeyPress);
  3398. Assert.False (view.IsKeyUp);
  3399. }
  3400. public class DerivedView : View {
  3401. public DerivedView ()
  3402. {
  3403. CanFocus = true;
  3404. }
  3405. public bool IsKeyDown { get; set; }
  3406. public bool IsKeyPress { get; set; }
  3407. public bool IsKeyUp { get; set; }
  3408. public override bool OnKeyDown (KeyEvent keyEvent)
  3409. {
  3410. IsKeyDown = true;
  3411. return true;
  3412. }
  3413. public override bool ProcessKey (KeyEvent keyEvent)
  3414. {
  3415. IsKeyPress = true;
  3416. return true;
  3417. }
  3418. public override bool OnKeyUp (KeyEvent keyEvent)
  3419. {
  3420. IsKeyUp = true;
  3421. return true;
  3422. }
  3423. }
  3424. [Theory, AutoInitShutdown]
  3425. [InlineData (true, false, false)]
  3426. [InlineData (true, true, false)]
  3427. [InlineData (true, true, true)]
  3428. public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
  3429. {
  3430. var keyDown = false;
  3431. var keyPress = false;
  3432. var keyUp = false;
  3433. var view = new DerivedView ();
  3434. view.KeyDown += (e) => {
  3435. Assert.Equal (-1, e.KeyEvent.KeyValue);
  3436. Assert.Equal (shift, e.KeyEvent.IsShift);
  3437. Assert.Equal (alt, e.KeyEvent.IsAlt);
  3438. Assert.Equal (control, e.KeyEvent.IsCtrl);
  3439. Assert.False (keyDown);
  3440. Assert.False (view.IsKeyDown);
  3441. keyDown = true;
  3442. };
  3443. view.KeyPress += (e) => {
  3444. keyPress = true;
  3445. };
  3446. view.KeyUp += (e) => {
  3447. Assert.Equal (-1, e.KeyEvent.KeyValue);
  3448. Assert.Equal (shift, e.KeyEvent.IsShift);
  3449. Assert.Equal (alt, e.KeyEvent.IsAlt);
  3450. Assert.Equal (control, e.KeyEvent.IsCtrl);
  3451. Assert.False (keyUp);
  3452. Assert.False (view.IsKeyUp);
  3453. keyUp = true;
  3454. };
  3455. Application.Top.Add (view);
  3456. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
  3457. Application.Iteration += () => Application.RequestStop ();
  3458. Assert.True (view.CanFocus);
  3459. Application.Run ();
  3460. Application.Shutdown ();
  3461. Assert.True (keyDown);
  3462. Assert.False (keyPress);
  3463. Assert.True (keyUp);
  3464. Assert.True (view.IsKeyDown);
  3465. Assert.False (view.IsKeyPress);
  3466. Assert.True (view.IsKeyUp);
  3467. }
  3468. }
  3469. }