ViewTests.cs 136 KB

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