ViewTests.cs 131 KB

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