ViewTests.cs 118 KB

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