ViewTests.cs 125 KB

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