ViewTests.cs 130 KB

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