ViewTests.cs 131 KB

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