ViewTests.cs 133 KB

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