ViewTests.cs 137 KB

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