ViewTests.cs 130 KB

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