ViewTests.cs 134 KB

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