ViewTests.cs 130 KB

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