ViewTests.cs 136 KB

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