ViewTests.cs 129 KB

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