ViewTests.cs 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using Terminal.Gui.Graphs;
  5. using Xunit;
  6. using Xunit.Abstractions;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. using Console = Terminal.Gui.FakeConsole;
  9. namespace Terminal.Gui.CoreTests {
  10. public class ViewTests {
  11. readonly ITestOutputHelper output;
  12. public ViewTests (ITestOutputHelper output)
  13. {
  14. this.output = output;
  15. }
  16. [Fact]
  17. public void New_Initializes ()
  18. {
  19. // Parameterless
  20. var r = new View ();
  21. Assert.NotNull (r);
  22. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  23. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  24. Assert.False (r.CanFocus);
  25. Assert.False (r.HasFocus);
  26. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  27. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  28. Assert.Null (r.Focused);
  29. Assert.Null (r.ColorScheme);
  30. Assert.Null (r.Width);
  31. Assert.Null (r.Height);
  32. Assert.Null (r.X);
  33. Assert.Null (r.Y);
  34. Assert.False (r.IsCurrentTop);
  35. Assert.Empty (r.Id);
  36. Assert.Empty (r.Subviews);
  37. Assert.False (r.WantContinuousButtonPressed);
  38. Assert.False (r.WantMousePositionReports);
  39. Assert.Null (r.SuperView);
  40. Assert.Null (r.MostFocused);
  41. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  42. // Empty Rect
  43. r = new View (Rect.Empty);
  44. Assert.NotNull (r);
  45. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  46. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  47. Assert.False (r.CanFocus);
  48. Assert.False (r.HasFocus);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  50. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  51. Assert.Null (r.Focused);
  52. Assert.Null (r.ColorScheme);
  53. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  54. Assert.Null (r.Height); // avoiding Dim errors.
  55. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  56. Assert.Null (r.Y); // avoiding Pos errors.
  57. Assert.False (r.IsCurrentTop);
  58. Assert.Empty (r.Id);
  59. Assert.Empty (r.Subviews);
  60. Assert.False (r.WantContinuousButtonPressed);
  61. Assert.False (r.WantMousePositionReports);
  62. Assert.Null (r.SuperView);
  63. Assert.Null (r.MostFocused);
  64. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  65. // Rect with values
  66. r = new View (new Rect (1, 2, 3, 4));
  67. Assert.NotNull (r);
  68. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  69. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  70. Assert.False (r.CanFocus);
  71. Assert.False (r.HasFocus);
  72. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  73. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  74. Assert.Null (r.Focused);
  75. Assert.Null (r.ColorScheme);
  76. Assert.Null (r.Width);
  77. Assert.Null (r.Height);
  78. Assert.Null (r.X);
  79. Assert.Null (r.Y);
  80. Assert.False (r.IsCurrentTop);
  81. Assert.Empty (r.Id);
  82. Assert.Empty (r.Subviews);
  83. Assert.False (r.WantContinuousButtonPressed);
  84. Assert.False (r.WantMousePositionReports);
  85. Assert.Null (r.SuperView);
  86. Assert.Null (r.MostFocused);
  87. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  88. // Initializes a view with a vertical direction
  89. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  90. Assert.NotNull (r);
  91. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  92. Assert.Equal ("View(Vertical View)({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  93. Assert.False (r.CanFocus);
  94. Assert.False (r.HasFocus);
  95. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  96. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  97. Assert.Null (r.Focused);
  98. Assert.Null (r.ColorScheme);
  99. Assert.Null (r.Width); // All view Dim are initialized now in the IsAdded setter,
  100. Assert.Null (r.Height); // avoiding Dim errors.
  101. Assert.Null (r.X); // All view Pos are initialized now in the IsAdded setter,
  102. Assert.Null (r.Y); // avoiding Pos errors.
  103. Assert.False (r.IsCurrentTop);
  104. Assert.Equal ("Vertical View", r.Id);
  105. Assert.Empty (r.Subviews);
  106. Assert.False (r.WantContinuousButtonPressed);
  107. Assert.False (r.WantMousePositionReports);
  108. Assert.Null (r.SuperView);
  109. Assert.Null (r.MostFocused);
  110. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  111. }
  112. [Fact]
  113. public void New_Methods_Return_False ()
  114. {
  115. var r = new View ();
  116. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  120. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  121. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  122. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  123. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  124. Assert.False (r.OnEnter (new View ()));
  125. Assert.False (r.OnLeave (new View ()));
  126. // TODO: Add more
  127. }
  128. [Fact]
  129. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  130. {
  131. // Object Initializer
  132. var view = new View () {
  133. X = 1,
  134. Y = 2,
  135. Width = 3,
  136. Height = 4
  137. };
  138. Assert.Equal (1, view.X);
  139. Assert.Equal (2, view.Y);
  140. Assert.Equal (3, view.Width);
  141. Assert.Equal (4, view.Height);
  142. Assert.False (view.Frame.IsEmpty);
  143. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  144. Assert.False (view.Bounds.IsEmpty);
  145. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  146. view.LayoutSubviews ();
  147. Assert.Equal (1, view.X);
  148. Assert.Equal (2, view.Y);
  149. Assert.Equal (3, view.Width);
  150. Assert.Equal (4, view.Height);
  151. Assert.False (view.Frame.IsEmpty);
  152. Assert.False (view.Bounds.IsEmpty);
  153. // Default Constructor
  154. view = new View ();
  155. Assert.Null (view.X);
  156. Assert.Null (view.Y);
  157. Assert.Null (view.Width);
  158. Assert.Null (view.Height);
  159. Assert.True (view.Frame.IsEmpty);
  160. Assert.True (view.Bounds.IsEmpty);
  161. // Constructor
  162. view = new View (1, 2, "");
  163. Assert.Null (view.X);
  164. Assert.Null (view.Y);
  165. Assert.Null (view.Width);
  166. Assert.Null (view.Height);
  167. Assert.False (view.Frame.IsEmpty);
  168. Assert.True (view.Bounds.IsEmpty);
  169. // Default Constructor and post assignment equivalent to Object Initializer
  170. view = new View ();
  171. view.X = 1;
  172. view.Y = 2;
  173. view.Width = 3;
  174. view.Height = 4;
  175. Assert.Equal (1, view.X);
  176. Assert.Equal (2, view.Y);
  177. Assert.Equal (3, view.Width);
  178. Assert.Equal (4, view.Height);
  179. Assert.False (view.Frame.IsEmpty);
  180. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  181. Assert.False (view.Bounds.IsEmpty);
  182. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  183. }
  184. [Fact]
  185. public void FocusNearestView_Ensure_Focus_Ordered ()
  186. {
  187. var top = new Toplevel ();
  188. var win = new Window ();
  189. var winSubview = new View ("WindowSubview") {
  190. CanFocus = true
  191. };
  192. win.Add (winSubview);
  193. top.Add (win);
  194. var frm = new FrameView ();
  195. var frmSubview = new View ("FrameSubview") {
  196. CanFocus = true
  197. };
  198. frm.Add (frmSubview);
  199. top.Add (frm);
  200. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  201. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  202. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  203. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  204. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  205. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  206. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  207. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  208. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  209. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  210. }
  211. [Fact]
  212. public void Added_Removed ()
  213. {
  214. var v = new View (new Rect (0, 0, 10, 24));
  215. var t = new View ();
  216. v.Added += (s, e) => {
  217. Assert.Same (v.SuperView, e.Parent);
  218. Assert.Same (t, e.Parent);
  219. Assert.Same (v, e.Child);
  220. };
  221. v.Removed += (s, e) => {
  222. Assert.Same (t, e.Parent);
  223. Assert.Same (v, e.Child);
  224. Assert.True (v.SuperView == null);
  225. };
  226. t.Add (v);
  227. Assert.True (t.Subviews.Count == 1);
  228. t.Remove (v);
  229. Assert.True (t.Subviews.Count == 0);
  230. }
  231. [Fact]
  232. public void Subviews_TabIndexes_AreEqual ()
  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. Assert.True (r.Subviews.IndexOf (v1) == 0);
  240. Assert.True (r.Subviews.IndexOf (v2) == 1);
  241. Assert.True (r.Subviews.IndexOf (v3) == 2);
  242. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  243. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  244. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  245. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  246. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  247. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  248. }
  249. [Fact]
  250. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  251. {
  252. var r = new View ();
  253. var v1 = new View () { CanFocus = true };
  254. var v2 = new View () { CanFocus = true };
  255. var v3 = new View () { CanFocus = true };
  256. r.Add (v1, v2, v3);
  257. r.BringSubviewToFront (v1);
  258. Assert.True (r.Subviews.IndexOf (v1) == 2);
  259. Assert.True (r.Subviews.IndexOf (v2) == 0);
  260. Assert.True (r.Subviews.IndexOf (v3) == 1);
  261. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  262. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  263. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  264. }
  265. [Fact]
  266. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  267. {
  268. var r = new View ();
  269. var v1 = new View () { CanFocus = true };
  270. var v2 = new View () { CanFocus = true };
  271. var v3 = new View () { CanFocus = true };
  272. r.Add (v1, v2, v3);
  273. r.BringSubviewForward (v1);
  274. Assert.True (r.Subviews.IndexOf (v1) == 1);
  275. Assert.True (r.Subviews.IndexOf (v2) == 0);
  276. Assert.True (r.Subviews.IndexOf (v3) == 2);
  277. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  278. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  279. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  280. }
  281. [Fact]
  282. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  283. {
  284. var r = new View ();
  285. var v1 = new View () { CanFocus = true };
  286. var v2 = new View () { CanFocus = true };
  287. var v3 = new View () { CanFocus = true };
  288. r.Add (v1, v2, v3);
  289. r.SendSubviewToBack (v3);
  290. Assert.True (r.Subviews.IndexOf (v1) == 1);
  291. Assert.True (r.Subviews.IndexOf (v2) == 2);
  292. Assert.True (r.Subviews.IndexOf (v3) == 0);
  293. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  294. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  295. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  296. }
  297. [Fact]
  298. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  299. {
  300. var r = new View ();
  301. var v1 = new View () { CanFocus = true };
  302. var v2 = new View () { CanFocus = true };
  303. var v3 = new View () { CanFocus = true };
  304. r.Add (v1, v2, v3);
  305. r.SendSubviewBackwards (v3);
  306. Assert.True (r.Subviews.IndexOf (v1) == 0);
  307. Assert.True (r.Subviews.IndexOf (v2) == 2);
  308. Assert.True (r.Subviews.IndexOf (v3) == 1);
  309. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  310. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  311. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  312. }
  313. [Fact]
  314. public void TabIndex_Set_CanFocus_ValidValues ()
  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. v1.TabIndex = 1;
  322. Assert.True (r.Subviews.IndexOf (v1) == 0);
  323. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  324. v1.TabIndex = 2;
  325. Assert.True (r.Subviews.IndexOf (v1) == 0);
  326. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  327. }
  328. [Fact]
  329. public void TabIndex_Set_CanFocus_HigherValues ()
  330. {
  331. var r = new View ();
  332. var v1 = new View () { CanFocus = true };
  333. var v2 = new View () { CanFocus = true };
  334. var v3 = new View () { CanFocus = true };
  335. r.Add (v1, v2, v3);
  336. v1.TabIndex = 3;
  337. Assert.True (r.Subviews.IndexOf (v1) == 0);
  338. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  339. }
  340. [Fact]
  341. public void TabIndex_Set_CanFocus_LowerValues ()
  342. {
  343. var r = new View ();
  344. var v1 = new View () { CanFocus = true };
  345. var v2 = new View () { CanFocus = true };
  346. var v3 = new View () { CanFocus = true };
  347. r.Add (v1, v2, v3);
  348. v1.TabIndex = -1;
  349. Assert.True (r.Subviews.IndexOf (v1) == 0);
  350. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  351. }
  352. [Fact]
  353. public void TabIndex_Set_CanFocus_False ()
  354. {
  355. var r = new View ();
  356. var v1 = new View () { CanFocus = true };
  357. var v2 = new View () { CanFocus = true };
  358. var v3 = new View () { CanFocus = true };
  359. r.Add (v1, v2, v3);
  360. v1.CanFocus = false;
  361. v1.TabIndex = 0;
  362. Assert.True (r.Subviews.IndexOf (v1) == 0);
  363. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  364. Assert.Equal (-1, v1.TabIndex);
  365. }
  366. [Fact]
  367. public void TabIndex_Set_CanFocus_False_To_True ()
  368. {
  369. var r = new View ();
  370. var v1 = new View ();
  371. var v2 = new View () { CanFocus = true };
  372. var v3 = new View () { CanFocus = true };
  373. r.Add (v1, v2, v3);
  374. v1.CanFocus = true;
  375. v1.TabIndex = 1;
  376. Assert.True (r.Subviews.IndexOf (v1) == 0);
  377. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  378. }
  379. [Fact]
  380. public void TabStop_And_CanFocus_Are_All_True ()
  381. {
  382. var r = new View ();
  383. var v1 = new View () { CanFocus = true };
  384. var v2 = new View () { CanFocus = true };
  385. var v3 = new View () { CanFocus = true };
  386. r.Add (v1, v2, v3);
  387. r.FocusNext ();
  388. Assert.True (v1.HasFocus);
  389. Assert.False (v2.HasFocus);
  390. Assert.False (v3.HasFocus);
  391. r.FocusNext ();
  392. Assert.False (v1.HasFocus);
  393. Assert.True (v2.HasFocus);
  394. Assert.False (v3.HasFocus);
  395. r.FocusNext ();
  396. Assert.False (v1.HasFocus);
  397. Assert.False (v2.HasFocus);
  398. Assert.True (v3.HasFocus);
  399. }
  400. [Fact]
  401. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  402. {
  403. var r = new View ();
  404. var v1 = new View ();
  405. var v2 = new View ();
  406. var v3 = new View ();
  407. r.Add (v1, v2, v3);
  408. r.FocusNext ();
  409. Assert.False (v1.HasFocus);
  410. Assert.False (v2.HasFocus);
  411. Assert.False (v3.HasFocus);
  412. r.FocusNext ();
  413. Assert.False (v1.HasFocus);
  414. Assert.False (v2.HasFocus);
  415. Assert.False (v3.HasFocus);
  416. r.FocusNext ();
  417. Assert.False (v1.HasFocus);
  418. Assert.False (v2.HasFocus);
  419. Assert.False (v3.HasFocus);
  420. }
  421. [Fact]
  422. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  423. {
  424. var r = new View ();
  425. var v1 = new View () { CanFocus = true, TabStop = false };
  426. var v2 = new View () { CanFocus = true, TabStop = false };
  427. var v3 = new View () { CanFocus = true, TabStop = false };
  428. r.Add (v1, v2, v3);
  429. r.FocusNext ();
  430. Assert.False (v1.HasFocus);
  431. Assert.False (v2.HasFocus);
  432. Assert.False (v3.HasFocus);
  433. r.FocusNext ();
  434. Assert.False (v1.HasFocus);
  435. Assert.False (v2.HasFocus);
  436. Assert.False (v3.HasFocus);
  437. r.FocusNext ();
  438. Assert.False (v1.HasFocus);
  439. Assert.False (v2.HasFocus);
  440. Assert.False (v3.HasFocus);
  441. }
  442. [Fact]
  443. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  444. {
  445. var r = new View ();
  446. var v1 = new View () { CanFocus = true, TabStop = false };
  447. var v2 = new View () { CanFocus = false, TabStop = true };
  448. var v3 = new View () { CanFocus = false, TabStop = false };
  449. r.Add (v1, v2, v3);
  450. r.FocusNext ();
  451. Assert.False (v1.HasFocus);
  452. Assert.False (v2.HasFocus);
  453. Assert.False (v3.HasFocus);
  454. r.FocusNext ();
  455. Assert.False (v1.HasFocus);
  456. Assert.False (v2.HasFocus);
  457. Assert.False (v3.HasFocus);
  458. r.FocusNext ();
  459. Assert.False (v1.HasFocus);
  460. Assert.False (v2.HasFocus);
  461. Assert.False (v3.HasFocus);
  462. }
  463. [Fact]
  464. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  465. {
  466. var r = new View ();
  467. var v1 = new View ();
  468. var v2 = new View ();
  469. var v3 = new View ();
  470. r.Add (v1, v2, v3);
  471. r.FocusNext ();
  472. Assert.False (v1.HasFocus);
  473. Assert.False (v2.HasFocus);
  474. Assert.False (v3.HasFocus);
  475. v1.CanFocus = true;
  476. r.FocusNext ();
  477. Assert.True (v1.HasFocus);
  478. Assert.False (v2.HasFocus);
  479. Assert.False (v3.HasFocus);
  480. v2.CanFocus = true;
  481. r.FocusNext ();
  482. Assert.False (v1.HasFocus);
  483. Assert.True (v2.HasFocus);
  484. Assert.False (v3.HasFocus);
  485. v3.CanFocus = true;
  486. r.FocusNext ();
  487. Assert.False (v1.HasFocus);
  488. Assert.False (v2.HasFocus);
  489. Assert.True (v3.HasFocus);
  490. }
  491. [Fact]
  492. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  493. {
  494. var r = new View ();
  495. var v1 = new View () { CanFocus = true, TabStop = false };
  496. var v2 = new View () { CanFocus = true, TabStop = false };
  497. var v3 = new View () { CanFocus = true, TabStop = false };
  498. r.Add (v1, v2, v3);
  499. r.FocusNext ();
  500. Assert.False (v1.HasFocus);
  501. Assert.False (v2.HasFocus);
  502. Assert.False (v3.HasFocus);
  503. v1.TabStop = true;
  504. r.FocusNext ();
  505. Assert.True (v1.HasFocus);
  506. Assert.False (v2.HasFocus);
  507. Assert.False (v3.HasFocus);
  508. v2.TabStop = true;
  509. r.FocusNext ();
  510. Assert.False (v1.HasFocus);
  511. Assert.True (v2.HasFocus);
  512. Assert.False (v3.HasFocus);
  513. v3.TabStop = true;
  514. r.FocusNext ();
  515. Assert.False (v1.HasFocus);
  516. Assert.False (v2.HasFocus);
  517. Assert.True (v3.HasFocus);
  518. }
  519. [Fact]
  520. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  521. {
  522. var r = new View ();
  523. var v1 = new View ("1");
  524. var v2 = new View ("2");
  525. var v3 = new View ("3");
  526. r.Add (v1, v2, v3);
  527. v2.CanFocus = true;
  528. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  529. Assert.Equal (0, v2.TabIndex);
  530. Assert.True (v2.TabStop);
  531. v1.CanFocus = true;
  532. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  533. Assert.Equal (1, v1.TabIndex);
  534. Assert.True (v1.TabStop);
  535. v1.TabIndex = 2;
  536. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  537. Assert.Equal (1, v1.TabIndex);
  538. v3.CanFocus = true;
  539. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  540. Assert.Equal (1, v1.TabIndex);
  541. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  542. Assert.Equal (2, v3.TabIndex);
  543. Assert.True (v3.TabStop);
  544. v2.CanFocus = false;
  545. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  546. Assert.Equal (1, v1.TabIndex);
  547. Assert.True (v1.TabStop);
  548. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  549. Assert.Equal (-1, v2.TabIndex);
  550. Assert.False (v2.TabStop);
  551. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  552. Assert.Equal (2, v3.TabIndex);
  553. Assert.True (v3.TabStop);
  554. }
  555. [Fact]
  556. public void Initialized_Event_Comparing_With_Added_Event ()
  557. {
  558. Application.Init (new FakeDriver ());
  559. var t = new Toplevel () { Id = "0", };
  560. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  561. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  562. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  563. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  564. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  565. w.Added += (s, e) => {
  566. Assert.Equal (e.Parent.Frame.Width, w.Frame.Width);
  567. Assert.Equal (e.Parent.Frame.Height, w.Frame.Height);
  568. };
  569. v1.Added += (s, e) => {
  570. Assert.Equal (e.Parent.Frame.Width, v1.Frame.Width);
  571. Assert.Equal (e.Parent.Frame.Height, v1.Frame.Height);
  572. };
  573. v2.Added += (s, e) => {
  574. Assert.Equal (e.Parent.Frame.Width, v2.Frame.Width);
  575. Assert.Equal (e.Parent.Frame.Height, v2.Frame.Height);
  576. };
  577. sv1.Added += (s, e) => {
  578. Assert.Equal (e.Parent.Frame.Width, sv1.Frame.Width);
  579. Assert.Equal (e.Parent.Frame.Height, sv1.Frame.Height);
  580. };
  581. t.Initialized += (s, e) => {
  582. tc++;
  583. Assert.Equal (1, tc);
  584. Assert.Equal (1, wc);
  585. Assert.Equal (1, v1c);
  586. Assert.Equal (1, v2c);
  587. Assert.Equal (1, sv1c);
  588. Assert.True (t.CanFocus);
  589. Assert.True (w.CanFocus);
  590. Assert.False (v1.CanFocus);
  591. Assert.False (v2.CanFocus);
  592. Assert.False (sv1.CanFocus);
  593. Application.Refresh ();
  594. };
  595. w.Initialized += (s, e) => {
  596. wc++;
  597. Assert.Equal (t.Frame.Width, w.Frame.Width);
  598. Assert.Equal (t.Frame.Height, w.Frame.Height);
  599. };
  600. v1.Initialized += (s, e) => {
  601. v1c++;
  602. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  603. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  604. };
  605. v2.Initialized += (s, e) => {
  606. v2c++;
  607. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  608. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  609. };
  610. sv1.Initialized += (s, e) => {
  611. sv1c++;
  612. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  613. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  614. Assert.False (sv1.CanFocus);
  615. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  616. Assert.False (sv1.CanFocus);
  617. };
  618. v1.Add (sv1);
  619. w.Add (v1, v2);
  620. t.Add (w);
  621. Application.Iteration = () => {
  622. Application.Refresh ();
  623. t.Running = false;
  624. };
  625. Application.Run (t);
  626. Application.Shutdown ();
  627. Assert.Equal (1, tc);
  628. Assert.Equal (1, wc);
  629. Assert.Equal (1, v1c);
  630. Assert.Equal (1, v2c);
  631. Assert.Equal (1, sv1c);
  632. Assert.True (t.CanFocus);
  633. Assert.True (w.CanFocus);
  634. Assert.False (v1.CanFocus);
  635. Assert.False (v2.CanFocus);
  636. Assert.False (sv1.CanFocus);
  637. v1.CanFocus = true;
  638. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  639. }
  640. [Fact]
  641. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  642. {
  643. Application.Init (new FakeDriver ());
  644. var t = new Toplevel () { Id = "0", };
  645. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  646. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  647. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  648. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  649. t.Initialized += (s, e) => {
  650. tc++;
  651. Assert.Equal (1, tc);
  652. Assert.Equal (1, wc);
  653. Assert.Equal (1, v1c);
  654. Assert.Equal (1, v2c);
  655. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  656. Assert.True (t.CanFocus);
  657. Assert.True (w.CanFocus);
  658. Assert.False (v1.CanFocus);
  659. Assert.False (v2.CanFocus);
  660. Application.Refresh ();
  661. };
  662. w.Initialized += (s, e) => {
  663. wc++;
  664. Assert.Equal (t.Frame.Width, w.Frame.Width);
  665. Assert.Equal (t.Frame.Height, w.Frame.Height);
  666. };
  667. v1.Initialized += (s, e) => {
  668. v1c++;
  669. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  670. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  671. };
  672. v2.Initialized += (s, e) => {
  673. v2c++;
  674. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  675. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  676. };
  677. w.Add (v1, v2);
  678. t.Add (w);
  679. Application.Iteration = () => {
  680. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  681. sv1.Initialized += (s, e) => {
  682. sv1c++;
  683. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  684. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  685. Assert.False (sv1.CanFocus);
  686. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  687. Assert.False (sv1.CanFocus);
  688. };
  689. v1.Add (sv1);
  690. Application.Refresh ();
  691. t.Running = false;
  692. };
  693. Application.Run (t);
  694. Application.Shutdown ();
  695. Assert.Equal (1, tc);
  696. Assert.Equal (1, wc);
  697. Assert.Equal (1, v1c);
  698. Assert.Equal (1, v2c);
  699. Assert.Equal (1, sv1c);
  700. Assert.True (t.CanFocus);
  701. Assert.True (w.CanFocus);
  702. Assert.False (v1.CanFocus);
  703. Assert.False (v2.CanFocus);
  704. }
  705. [Fact]
  706. [AutoInitShutdown]
  707. public void CanFocus_Faced_With_Container ()
  708. {
  709. var t = new Toplevel ();
  710. var w = new Window ();
  711. var f = new FrameView ();
  712. var v = new View () { CanFocus = true };
  713. f.Add (v);
  714. w.Add (f);
  715. t.Add (w);
  716. Assert.True (t.CanFocus);
  717. Assert.True (w.CanFocus);
  718. Assert.True (f.CanFocus);
  719. Assert.True (v.CanFocus);
  720. f.CanFocus = false;
  721. Assert.False (f.CanFocus);
  722. Assert.True (v.CanFocus);
  723. v.CanFocus = false;
  724. Assert.False (f.CanFocus);
  725. Assert.False (v.CanFocus);
  726. v.CanFocus = true;
  727. Assert.False (f.CanFocus);
  728. Assert.True (v.CanFocus);
  729. }
  730. [Fact]
  731. public void CanFocus_Faced_With_Container_Before_Run ()
  732. {
  733. Application.Init (new FakeDriver ());
  734. var t = Application.Top;
  735. var w = new Window ("w");
  736. var f = new FrameView ("f");
  737. var v = new View ("v") { CanFocus = true };
  738. f.Add (v);
  739. w.Add (f);
  740. t.Add (w);
  741. Assert.True (t.CanFocus);
  742. Assert.True (w.CanFocus);
  743. Assert.True (f.CanFocus);
  744. Assert.True (v.CanFocus);
  745. f.CanFocus = false;
  746. Assert.False (f.CanFocus);
  747. Assert.True (v.CanFocus);
  748. v.CanFocus = false;
  749. Assert.False (f.CanFocus);
  750. Assert.False (v.CanFocus);
  751. v.CanFocus = true;
  752. Assert.False (f.CanFocus);
  753. Assert.True (v.CanFocus);
  754. Application.Iteration += () => Application.RequestStop ();
  755. Application.Run ();
  756. Application.Shutdown ();
  757. }
  758. [Fact]
  759. public void CanFocus_Faced_With_Container_After_Run ()
  760. {
  761. Application.Init (new FakeDriver ());
  762. var t = Application.Top;
  763. var w = new Window ("w");
  764. var f = new FrameView ("f");
  765. var v = new View ("v") { CanFocus = true };
  766. f.Add (v);
  767. w.Add (f);
  768. t.Add (w);
  769. t.Ready += (s, e) => {
  770. Assert.True (t.CanFocus);
  771. Assert.True (w.CanFocus);
  772. Assert.True (f.CanFocus);
  773. Assert.True (v.CanFocus);
  774. f.CanFocus = false;
  775. Assert.False (f.CanFocus);
  776. Assert.False (v.CanFocus);
  777. v.CanFocus = false;
  778. Assert.False (f.CanFocus);
  779. Assert.False (v.CanFocus);
  780. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  781. Assert.False (f.CanFocus);
  782. Assert.False (v.CanFocus);
  783. f.CanFocus = true;
  784. Assert.True (f.CanFocus);
  785. Assert.True (v.CanFocus);
  786. };
  787. Application.Iteration += () => Application.RequestStop ();
  788. Application.Run ();
  789. Application.Shutdown ();
  790. }
  791. [Fact]
  792. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  793. {
  794. Application.Init (new FakeDriver ());
  795. var t = Application.Top;
  796. var w = new Window ("w");
  797. var f = new FrameView ("f");
  798. var v1 = new View ("v1") { CanFocus = true };
  799. var v2 = new View ("v2") { CanFocus = true };
  800. f.Add (v1, v2);
  801. w.Add (f);
  802. t.Add (w);
  803. t.Ready += (s, e) => {
  804. Assert.True (t.CanFocus);
  805. Assert.True (w.CanFocus);
  806. Assert.True (f.CanFocus);
  807. Assert.True (v1.CanFocus);
  808. Assert.True (v2.CanFocus);
  809. w.CanFocus = false;
  810. Assert.False (w.CanFocus);
  811. Assert.False (f.CanFocus);
  812. Assert.False (v1.CanFocus);
  813. Assert.False (v2.CanFocus);
  814. };
  815. Application.Iteration += () => Application.RequestStop ();
  816. Application.Run ();
  817. Application.Shutdown ();
  818. }
  819. [Fact]
  820. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  821. {
  822. Application.Init (new FakeDriver ());
  823. var t = Application.Top;
  824. var w = new Window ("w");
  825. var f = new FrameView ("f");
  826. var v1 = new View ("v1");
  827. var v2 = new View ("v2") { CanFocus = true };
  828. f.Add (v1, v2);
  829. w.Add (f);
  830. t.Add (w);
  831. t.Ready += (s, e) => {
  832. Assert.True (t.CanFocus);
  833. Assert.True (w.CanFocus);
  834. Assert.True (f.CanFocus);
  835. Assert.False (v1.CanFocus);
  836. Assert.True (v2.CanFocus);
  837. w.CanFocus = false;
  838. Assert.False (w.CanFocus);
  839. Assert.False (f.CanFocus);
  840. Assert.False (v1.CanFocus);
  841. Assert.False (v2.CanFocus);
  842. w.CanFocus = true;
  843. Assert.True (w.CanFocus);
  844. Assert.True (f.CanFocus);
  845. Assert.False (v1.CanFocus);
  846. Assert.True (v2.CanFocus);
  847. };
  848. Application.Iteration += () => Application.RequestStop ();
  849. Application.Run ();
  850. Application.Shutdown ();
  851. }
  852. [Fact]
  853. public void Navigation_With_Null_Focused_View ()
  854. {
  855. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  856. Application.Init (new FakeDriver ());
  857. Application.Top.Ready += (s, e) => {
  858. Assert.Null (Application.Top.Focused);
  859. };
  860. // Keyboard navigation with tab
  861. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  862. Application.Iteration += () => Application.RequestStop ();
  863. Application.Run ();
  864. Application.Shutdown ();
  865. }
  866. [Fact]
  867. public void Multi_Thread_Toplevels ()
  868. {
  869. Application.Init (new FakeDriver ());
  870. var t = Application.Top;
  871. var w = new Window ();
  872. t.Add (w);
  873. int count = 0, count1 = 0, count2 = 0;
  874. bool log = false, log1 = false, log2 = false;
  875. bool fromTopStillKnowFirstIsRunning = false;
  876. bool fromTopStillKnowSecondIsRunning = false;
  877. bool fromFirstStillKnowSecondIsRunning = false;
  878. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  879. count++;
  880. if (count1 == 5) {
  881. log1 = true;
  882. }
  883. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  884. fromTopStillKnowFirstIsRunning = true;
  885. }
  886. if (count1 == 7 && count2 == 7 && count == 8) {
  887. fromTopStillKnowSecondIsRunning = true;
  888. }
  889. if (count == 30) {
  890. Assert.Equal (30, count);
  891. Assert.Equal (20, count1);
  892. Assert.Equal (10, count2);
  893. Assert.True (log);
  894. Assert.True (log1);
  895. Assert.True (log2);
  896. Assert.True (fromTopStillKnowFirstIsRunning);
  897. Assert.True (fromTopStillKnowSecondIsRunning);
  898. Assert.True (fromFirstStillKnowSecondIsRunning);
  899. Application.RequestStop ();
  900. return false;
  901. }
  902. return true;
  903. });
  904. t.Ready += FirstDialogToplevel;
  905. void FirstDialogToplevel (object sender, EventArgs args)
  906. {
  907. var od = new OpenDialog ();
  908. od.Ready += SecondDialogToplevel;
  909. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  910. count1++;
  911. if (count2 == 5) {
  912. log2 = true;
  913. }
  914. if (count2 == 4 && count1 == 5 && count == 5) {
  915. fromFirstStillKnowSecondIsRunning = true;
  916. }
  917. if (count1 == 20) {
  918. Assert.Equal (20, count1);
  919. Application.RequestStop ();
  920. return false;
  921. }
  922. return true;
  923. });
  924. Application.Run (od);
  925. }
  926. void SecondDialogToplevel (object sender, EventArgs args)
  927. {
  928. var d = new Dialog ();
  929. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  930. count2++;
  931. if (count < 30) {
  932. log = true;
  933. }
  934. if (count2 == 10) {
  935. Assert.Equal (10, count2);
  936. Application.RequestStop ();
  937. return false;
  938. }
  939. return true;
  940. });
  941. Application.Run (d);
  942. }
  943. Application.Run ();
  944. Application.Shutdown ();
  945. }
  946. [Fact]
  947. public void KeyPress_Handled_To_True_Prevents_Changes ()
  948. {
  949. Application.Init (new FakeDriver ());
  950. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  951. var top = Application.Top;
  952. var text = new TextField ("");
  953. text.KeyPress += (s, e) => {
  954. e.Handled = true;
  955. Assert.True (e.Handled);
  956. Assert.Equal (Key.N, e.KeyEvent.Key);
  957. };
  958. top.Add (text);
  959. Application.Iteration += () => {
  960. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  961. Assert.Equal ("", text.Text);
  962. Application.RequestStop ();
  963. };
  964. Application.Run ();
  965. // Shutdown must be called to safely clean up Application if Init has been called
  966. Application.Shutdown ();
  967. }
  968. [Theory]
  969. [InlineData (1)]
  970. [InlineData (2)]
  971. [InlineData (3)]
  972. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  973. {
  974. var driver = new FakeDriver ();
  975. Application.Init (driver);
  976. try {
  977. // Create a label with a short text
  978. Label lbl;
  979. var text = "test";
  980. if (choice == 1) {
  981. // An object initializer should call the default constructor.
  982. lbl = new Label { Text = text };
  983. } else if (choice == 2) {
  984. // Calling the default constructor followed by the object initializer.
  985. lbl = new Label () { Text = text };
  986. } else {
  987. // Calling the Text constructor.
  988. lbl = new Label (text);
  989. }
  990. Application.Top.Add (lbl);
  991. Application.Top.LayoutSubviews ();
  992. Application.Top.Redraw (Application.Top.Bounds);
  993. // should have the initial text
  994. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  995. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  996. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  997. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  998. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  999. } finally {
  1000. Application.Shutdown ();
  1001. }
  1002. }
  1003. [Fact]
  1004. [AutoInitShutdown]
  1005. public void Internal_Tests ()
  1006. {
  1007. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1008. Enum.GetValues (typeof (View.Direction)));
  1009. var rect = new Rect (1, 1, 10, 1);
  1010. var view = new View (rect);
  1011. var top = Application.Top;
  1012. top.Add (view);
  1013. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1014. view.FocusDirection = View.Direction.Backward;
  1015. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1016. Assert.Empty (view.InternalSubviews);
  1017. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view._needsDisplay);
  1018. Assert.True (view.LayoutNeeded);
  1019. Assert.False (view._childNeedsDisplay);
  1020. Assert.False (view.addingView);
  1021. view.addingView = true;
  1022. Assert.True (view.addingView);
  1023. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1024. Assert.Equal (1, rcol);
  1025. Assert.Equal (1, rrow);
  1026. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1027. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1028. view.Width = Dim.Fill ();
  1029. view.Height = Dim.Fill ();
  1030. Assert.Equal (10, view.Bounds.Width);
  1031. Assert.Equal (1, view.Bounds.Height);
  1032. view.SetRelativeLayout (top.Bounds);
  1033. Assert.Equal (79, view.Bounds.Width);
  1034. Assert.Equal (24, view.Bounds.Height);
  1035. view.X = 0;
  1036. view.Y = 0;
  1037. view.SetRelativeLayout (top.Bounds);
  1038. Assert.Equal (80, view.Bounds.Width);
  1039. Assert.Equal (25, view.Bounds.Height);
  1040. bool layoutStarted = false;
  1041. view.LayoutStarted += (s, e) => layoutStarted = true;
  1042. view.OnLayoutStarted (null);
  1043. Assert.True (layoutStarted);
  1044. view.LayoutComplete += (s, e) => layoutStarted = false;
  1045. view.OnLayoutComplete (null);
  1046. Assert.False (layoutStarted);
  1047. view.X = Pos.Center () - 41;
  1048. view.Y = Pos.Center () - 13;
  1049. view.SetRelativeLayout (top.Bounds);
  1050. view.ViewToScreen (0, 0, out rcol, out rrow);
  1051. Assert.Equal (-41, rcol);
  1052. Assert.Equal (-13, rrow);
  1053. }
  1054. [Fact]
  1055. [AutoInitShutdown]
  1056. public void Enabled_False_Sets_HasFocus_To_False ()
  1057. {
  1058. var wasClicked = false;
  1059. var view = new Button ("Click Me");
  1060. view.Clicked += (s, e) => wasClicked = !wasClicked;
  1061. Application.Top.Add (view);
  1062. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1063. Assert.True (wasClicked);
  1064. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1065. Assert.False (wasClicked);
  1066. Assert.True (view.Enabled);
  1067. Assert.True (view.CanFocus);
  1068. Assert.True (view.HasFocus);
  1069. view.Enabled = false;
  1070. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1071. Assert.False (wasClicked);
  1072. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1073. Assert.False (wasClicked);
  1074. Assert.False (view.Enabled);
  1075. Assert.True (view.CanFocus);
  1076. Assert.False (view.HasFocus);
  1077. view.SetFocus ();
  1078. Assert.False (view.HasFocus);
  1079. }
  1080. [Fact]
  1081. [AutoInitShutdown]
  1082. public void Enabled_Sets_Also_Sets_Subviews ()
  1083. {
  1084. var wasClicked = false;
  1085. var button = new Button ("Click Me");
  1086. button.Clicked += (s, e) => wasClicked = !wasClicked;
  1087. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1088. win.Add (button);
  1089. Application.Top.Add (win);
  1090. var iterations = 0;
  1091. Application.Iteration += () => {
  1092. iterations++;
  1093. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1094. Assert.True (wasClicked);
  1095. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1096. Assert.False (wasClicked);
  1097. Assert.True (button.Enabled);
  1098. Assert.True (button.CanFocus);
  1099. Assert.True (button.HasFocus);
  1100. Assert.True (win.Enabled);
  1101. Assert.True (win.CanFocus);
  1102. Assert.True (win.HasFocus);
  1103. win.Enabled = false;
  1104. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1105. Assert.False (wasClicked);
  1106. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1107. Assert.False (wasClicked);
  1108. Assert.False (button.Enabled);
  1109. Assert.True (button.CanFocus);
  1110. Assert.False (button.HasFocus);
  1111. Assert.False (win.Enabled);
  1112. Assert.True (win.CanFocus);
  1113. Assert.False (win.HasFocus);
  1114. button.SetFocus ();
  1115. Assert.False (button.HasFocus);
  1116. Assert.False (win.HasFocus);
  1117. win.SetFocus ();
  1118. Assert.False (button.HasFocus);
  1119. Assert.False (win.HasFocus);
  1120. win.Enabled = true;
  1121. win.FocusFirst ();
  1122. Assert.True (button.HasFocus);
  1123. Assert.True (win.HasFocus);
  1124. Application.RequestStop ();
  1125. };
  1126. Application.Run ();
  1127. Assert.Equal (1, iterations);
  1128. }
  1129. [Fact]
  1130. [AutoInitShutdown]
  1131. public void Visible_Sets_Also_Sets_Subviews ()
  1132. {
  1133. var button = new Button ("Click Me");
  1134. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1135. win.Add (button);
  1136. var top = Application.Top;
  1137. top.Add (win);
  1138. var iterations = 0;
  1139. Application.Iteration += () => {
  1140. iterations++;
  1141. Assert.True (button.Visible);
  1142. Assert.True (button.CanFocus);
  1143. Assert.True (button.HasFocus);
  1144. Assert.True (win.Visible);
  1145. Assert.True (win.CanFocus);
  1146. Assert.True (win.HasFocus);
  1147. Assert.True (RunesCount () > 0);
  1148. win.Visible = false;
  1149. Assert.True (button.Visible);
  1150. Assert.True (button.CanFocus);
  1151. Assert.False (button.HasFocus);
  1152. Assert.False (win.Visible);
  1153. Assert.True (win.CanFocus);
  1154. Assert.False (win.HasFocus);
  1155. button.SetFocus ();
  1156. Assert.False (button.HasFocus);
  1157. Assert.False (win.HasFocus);
  1158. win.SetFocus ();
  1159. Assert.False (button.HasFocus);
  1160. Assert.False (win.HasFocus);
  1161. top.Redraw (top.Bounds);
  1162. Assert.True (RunesCount () == 0);
  1163. win.Visible = true;
  1164. win.FocusFirst ();
  1165. Assert.True (button.HasFocus);
  1166. Assert.True (win.HasFocus);
  1167. top.Redraw (top.Bounds);
  1168. Assert.True (RunesCount () > 0);
  1169. Application.RequestStop ();
  1170. };
  1171. Application.Run ();
  1172. Assert.Equal (1, iterations);
  1173. int RunesCount ()
  1174. {
  1175. var contents = ((FakeDriver)Application.Driver).Contents;
  1176. var runesCount = 0;
  1177. for (int i = 0; i < Application.Driver.Rows; i++) {
  1178. for (int j = 0; j < Application.Driver.Cols; j++) {
  1179. if (contents [i, j, 0] != ' ') {
  1180. runesCount++;
  1181. }
  1182. }
  1183. }
  1184. return runesCount;
  1185. }
  1186. }
  1187. [Fact]
  1188. [AutoInitShutdown]
  1189. public void GetTopSuperView_Test ()
  1190. {
  1191. var v1 = new View ();
  1192. var fv1 = new FrameView ();
  1193. fv1.Add (v1);
  1194. var tf1 = new TextField ();
  1195. var w1 = new Window ();
  1196. w1.Add (fv1, tf1);
  1197. var top1 = new Toplevel ();
  1198. top1.Add (w1);
  1199. var v2 = new View ();
  1200. var fv2 = new FrameView ();
  1201. fv2.Add (v2);
  1202. var tf2 = new TextField ();
  1203. var w2 = new Window ();
  1204. w2.Add (fv2, tf2);
  1205. var top2 = new Toplevel ();
  1206. top2.Add (w2);
  1207. Assert.Equal (top1, v1.GetTopSuperView ());
  1208. Assert.Equal (top2, v2.GetTopSuperView ());
  1209. }
  1210. [Fact]
  1211. [AutoInitShutdown]
  1212. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1213. {
  1214. var view = new View () { CanFocus = true };
  1215. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1216. win.Add (view);
  1217. Application.Top.Add (win);
  1218. Application.Begin (Application.Top);
  1219. Assert.True (view.CanFocus);
  1220. Assert.True (view.HasFocus);
  1221. view.CanFocus = false;
  1222. Assert.False (view.CanFocus);
  1223. Assert.False (view.HasFocus);
  1224. Assert.Null (Application.Current.Focused);
  1225. Assert.Null (Application.Current.MostFocused);
  1226. }
  1227. [Fact]
  1228. [AutoInitShutdown]
  1229. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1230. {
  1231. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1232. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1233. win1.Add (view1);
  1234. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  1235. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1236. win2.Add (view2);
  1237. Application.Top.Add (win1, win2);
  1238. Application.Begin (Application.Top);
  1239. Assert.True (view1.CanFocus);
  1240. Assert.True (view1.HasFocus);
  1241. Assert.True (view2.CanFocus);
  1242. Assert.False (view2.HasFocus);
  1243. view1.CanFocus = false;
  1244. Assert.False (view1.CanFocus);
  1245. Assert.False (view1.HasFocus);
  1246. Assert.Equal (win2, Application.Current.Focused);
  1247. Assert.Equal (view2, Application.Current.MostFocused);
  1248. }
  1249. [Fact]
  1250. [AutoInitShutdown]
  1251. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1252. {
  1253. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1254. var view12 = new View () { Id = "view12", Y = 5, Width = 10, Height = 1, CanFocus = true };
  1255. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1256. win1.Add (view1, view12);
  1257. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  1258. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1259. win2.Add (view2);
  1260. Application.Top.Add (win1, win2);
  1261. Application.Begin (Application.Top);
  1262. Assert.True (view1.CanFocus);
  1263. Assert.True (view1.HasFocus);
  1264. Assert.True (view2.CanFocus);
  1265. Assert.False (view2.HasFocus);
  1266. view1.CanFocus = false;
  1267. Assert.False (view1.CanFocus);
  1268. Assert.False (view1.HasFocus);
  1269. Assert.Equal (win1, Application.Current.Focused);
  1270. Assert.Equal (view12, Application.Current.MostFocused);
  1271. }
  1272. [Fact]
  1273. [AutoInitShutdown]
  1274. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1275. {
  1276. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1277. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1278. win1.Add (view1);
  1279. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  1280. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1281. win2.Add (view2);
  1282. Application.Top.Add (win1, win2);
  1283. Application.Begin (Application.Top);
  1284. Assert.True (view1.CanFocus);
  1285. Assert.True (view1.HasFocus);
  1286. Assert.True (view2.CanFocus);
  1287. Assert.False (view2.HasFocus);
  1288. win1.CanFocus = false;
  1289. Assert.False (view1.CanFocus);
  1290. Assert.False (view1.HasFocus);
  1291. Assert.False (win1.CanFocus);
  1292. Assert.False (win1.HasFocus);
  1293. Assert.Equal (win2, Application.Current.Focused);
  1294. Assert.Equal (view2, Application.Current.MostFocused);
  1295. }
  1296. [Fact]
  1297. [AutoInitShutdown]
  1298. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1299. {
  1300. var sbQuiting = false;
  1301. var tfQuiting = false;
  1302. var topQuiting = false;
  1303. var sb = new StatusBar (new StatusItem [] {
  1304. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1305. });
  1306. var tf = new TextField ();
  1307. tf.KeyPress += Tf_KeyPress;
  1308. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  1309. {
  1310. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1311. obj.Handled = tfQuiting = true;
  1312. }
  1313. }
  1314. var win = new Window ();
  1315. win.Add (sb, tf);
  1316. var top = Application.Top;
  1317. top.KeyPress += Top_KeyPress;
  1318. void Top_KeyPress (object sender, KeyEventEventArgs obj)
  1319. {
  1320. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1321. obj.Handled = topQuiting = true;
  1322. }
  1323. }
  1324. top.Add (win);
  1325. Application.Begin (top);
  1326. Assert.False (sbQuiting);
  1327. Assert.False (tfQuiting);
  1328. Assert.False (topQuiting);
  1329. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1330. Assert.False (sbQuiting);
  1331. Assert.True (tfQuiting);
  1332. Assert.False (topQuiting);
  1333. tf.KeyPress -= Tf_KeyPress;
  1334. tfQuiting = false;
  1335. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1336. Application.MainLoop.MainIteration ();
  1337. Assert.True (sbQuiting);
  1338. Assert.False (tfQuiting);
  1339. Assert.False (topQuiting);
  1340. sb.RemoveItem (0);
  1341. sbQuiting = false;
  1342. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1343. Application.MainLoop.MainIteration ();
  1344. Assert.False (sbQuiting);
  1345. Assert.False (tfQuiting);
  1346. Assert.True (topQuiting);
  1347. }
  1348. [Fact]
  1349. [AutoInitShutdown]
  1350. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1351. {
  1352. var sbQuiting = false;
  1353. var tfQuiting = false;
  1354. var sb = new StatusBar (new StatusItem [] {
  1355. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1356. });
  1357. var tf = new TextField ();
  1358. tf.KeyPress += Tf_KeyPress;
  1359. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  1360. {
  1361. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1362. obj.Handled = tfQuiting = true;
  1363. }
  1364. }
  1365. var win = new Window ();
  1366. win.Add (sb, tf);
  1367. var top = Application.Top;
  1368. top.Add (win);
  1369. Application.Begin (top);
  1370. Assert.False (sbQuiting);
  1371. Assert.False (tfQuiting);
  1372. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1373. Assert.False (sbQuiting);
  1374. Assert.True (tfQuiting);
  1375. tf.KeyPress -= Tf_KeyPress;
  1376. tfQuiting = false;
  1377. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1378. Application.MainLoop.MainIteration ();
  1379. Assert.True (sbQuiting);
  1380. Assert.False (tfQuiting);
  1381. }
  1382. [Fact]
  1383. [AutoInitShutdown]
  1384. public void WindowDispose_CanFocusProblem ()
  1385. {
  1386. // Arrange
  1387. Application.Init ();
  1388. using var top = Toplevel.Create ();
  1389. using var view = new View (
  1390. x: 0,
  1391. y: 1,
  1392. text: nameof (WindowDispose_CanFocusProblem));
  1393. using var window = new Window ();
  1394. top.Add (window);
  1395. window.Add (view);
  1396. // Act
  1397. Application.Begin (top);
  1398. Application.Shutdown ();
  1399. // Assert does Not throw NullReferenceException
  1400. top.SetFocus ();
  1401. }
  1402. [Fact, AutoInitShutdown]
  1403. public void DrawFrame_With_Positive_Positions ()
  1404. {
  1405. var view = new View (new Rect (0, 0, 8, 4));
  1406. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1407. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1408. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1409. Application.Top.Add (view);
  1410. Application.Begin (Application.Top);
  1411. var expected = @"
  1412. ┌──────┐
  1413. │ │
  1414. │ │
  1415. └──────┘
  1416. ";
  1417. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1418. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1419. }
  1420. [Fact, AutoInitShutdown]
  1421. public void DrawFrame_With_Minimum_Size ()
  1422. {
  1423. var view = new View (new Rect (0, 0, 2, 2));
  1424. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1425. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1426. Assert.Equal (new Size (2, 2), new Size (view.Frame.Width, view.Frame.Height));
  1427. Application.Top.Add (view);
  1428. Application.Begin (Application.Top);
  1429. var expected = @"
  1430. ┌┐
  1431. └┘
  1432. ";
  1433. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1434. Assert.Equal (new Rect (0, 0, 2, 2), pos);
  1435. }
  1436. [Fact, AutoInitShutdown]
  1437. public void DrawFrame_With_Negative_Positions ()
  1438. {
  1439. var view = new View (new Rect (-1, 0, 8, 4));
  1440. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1441. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1442. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1443. Application.Top.Add (view);
  1444. Application.Begin (Application.Top);
  1445. var expected = @"
  1446. ──────┐
  1447. ──────┘
  1448. ";
  1449. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1450. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1451. view.Frame = new Rect (-1, -1, 8, 4);
  1452. Application.Refresh ();
  1453. expected = @"
  1454. ──────┘
  1455. ";
  1456. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1457. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1458. view.Frame = new Rect (0, 0, 8, 4);
  1459. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1460. expected = @"
  1461. ┌──────
  1462. └──────
  1463. ";
  1464. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1465. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1466. view.Frame = new Rect (0, 0, 8, 4);
  1467. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1468. }
  1469. [Fact, AutoInitShutdown]
  1470. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1471. {
  1472. // BUGBUG: v2 - scrollview is broken. Disabling test for now
  1473. return;
  1474. var view = new View (new Rect (0, 0, 20, 20));
  1475. view.Add (new Label ("0123456789abcdefghij"));
  1476. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1477. view.Add (new Button (1, 1, "Press me!"));
  1478. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1479. ContentSize = new Size (40, 40),
  1480. ShowHorizontalScrollIndicator = true,
  1481. ShowVerticalScrollIndicator = true
  1482. };
  1483. scrollView.Add (view);
  1484. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1485. win.Add (scrollView);
  1486. Application.Top.Add (win);
  1487. Application.Begin (Application.Top);
  1488. var expected = @"
  1489. ┌┤Test├────────────┐
  1490. │ │
  1491. │ 0123456789abcd▲ │
  1492. │ 1[ Press me! ]┬ │
  1493. │ 2 │ │
  1494. │ 3 ┴ │
  1495. │ 4 ░ │
  1496. │ 5 ░ │
  1497. │ 6 ░ │
  1498. │ 7 ░ │
  1499. │ 8 ▼ │
  1500. │ ◄├───┤░░░░░░░► │
  1501. │ │
  1502. └──────────────────┘
  1503. ";
  1504. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1505. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1506. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1507. Application.Top.Redraw (Application.Top.Bounds);
  1508. expected = @"
  1509. ┌┤Test├────────────┐
  1510. │ │
  1511. │ 123456789abcde▲ │
  1512. │ [ Press me! ] ┬ │
  1513. │ │ │
  1514. │ ┴ │
  1515. │ ░ │
  1516. │ ░ │
  1517. │ ░ │
  1518. │ ░ │
  1519. │ ▼ │
  1520. │ ◄├───┤░░░░░░░► │
  1521. │ │
  1522. └──────────────────┘
  1523. ";
  1524. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1525. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1526. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1527. Application.Top.Redraw (Application.Top.Bounds);
  1528. expected = @"
  1529. ┌┤Test├────────────┐
  1530. │ │
  1531. │ 23456789abcdef▲ │
  1532. │ Press me! ] ┬ │
  1533. │ │ │
  1534. │ ┴ │
  1535. │ ░ │
  1536. │ ░ │
  1537. │ ░ │
  1538. │ ░ │
  1539. │ ▼ │
  1540. │ ◄├────┤░░░░░░► │
  1541. │ │
  1542. └──────────────────┘
  1543. ";
  1544. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1545. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1546. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1547. Application.Top.Redraw (Application.Top.Bounds);
  1548. expected = @"
  1549. ┌┤Test├────────────┐
  1550. │ │
  1551. │ 3456789abcdefg▲ │
  1552. │ Press me! ] ┬ │
  1553. │ │ │
  1554. │ ┴ │
  1555. │ ░ │
  1556. │ ░ │
  1557. │ ░ │
  1558. │ ░ │
  1559. │ ▼ │
  1560. │ ◄├────┤░░░░░░► │
  1561. │ │
  1562. └──────────────────┘
  1563. ";
  1564. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1565. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1566. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1567. Application.Top.Redraw (Application.Top.Bounds);
  1568. expected = @"
  1569. ┌┤Test├────────────┐
  1570. │ │
  1571. │ 456789abcdefgh▲ │
  1572. │ ress me! ] ┬ │
  1573. │ │ │
  1574. │ ┴ │
  1575. │ ░ │
  1576. │ ░ │
  1577. │ ░ │
  1578. │ ░ │
  1579. │ ▼ │
  1580. │ ◄░├───┤░░░░░░► │
  1581. │ │
  1582. └──────────────────┘
  1583. ";
  1584. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1585. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1586. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1587. Application.Top.Redraw (Application.Top.Bounds);
  1588. expected = @"
  1589. ┌┤Test├────────────┐
  1590. │ │
  1591. │ 56789abcdefghi▲ │
  1592. │ ess me! ] ┬ │
  1593. │ │ │
  1594. │ ┴ │
  1595. │ ░ │
  1596. │ ░ │
  1597. │ ░ │
  1598. │ ░ │
  1599. │ ▼ │
  1600. │ ◄░├────┤░░░░░► │
  1601. │ │
  1602. └──────────────────┘
  1603. ";
  1604. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1605. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1606. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1607. Application.Top.Redraw (Application.Top.Bounds);
  1608. expected = @"
  1609. ┌┤Test├────────────┐
  1610. │ │
  1611. │ 6789abcdefghij▲ │
  1612. │ ss me! ] ┬ │
  1613. │ │ │
  1614. │ ┴ │
  1615. │ ░ │
  1616. │ ░ │
  1617. │ ░ │
  1618. │ ░ │
  1619. │ ▼ │
  1620. │ ◄░├────┤░░░░░► │
  1621. │ │
  1622. └──────────────────┘
  1623. ";
  1624. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1625. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1626. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1627. Application.Top.Redraw (Application.Top.Bounds);
  1628. expected = @"
  1629. ┌┤Test├────────────┐
  1630. │ │
  1631. │ 789abcdefghij ▲ │
  1632. │ s me! ] ┬ │
  1633. │ │ │
  1634. │ ┴ │
  1635. │ ░ │
  1636. │ ░ │
  1637. │ ░ │
  1638. │ ░ │
  1639. │ ▼ │
  1640. │ ◄░░├───┤░░░░░► │
  1641. │ │
  1642. └──────────────────┘
  1643. ";
  1644. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1645. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1646. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1647. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1648. Application.Top.Redraw (Application.Top.Bounds);
  1649. expected = @"
  1650. ┌┤Test├────────────┐
  1651. │ │
  1652. │ 1[ Press me! ]▲ │
  1653. │ 2 ┬ │
  1654. │ 3 │ │
  1655. │ 4 ┴ │
  1656. │ 5 ░ │
  1657. │ 6 ░ │
  1658. │ 7 ░ │
  1659. │ 8 ░ │
  1660. │ 9 ▼ │
  1661. │ ◄├───┤░░░░░░░► │
  1662. │ │
  1663. └──────────────────┘
  1664. ";
  1665. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1666. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1667. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1668. Application.Top.Redraw (Application.Top.Bounds);
  1669. expected = @"
  1670. ┌┤Test├────────────┐
  1671. │ │
  1672. │ 2 ▲ │
  1673. │ 3 ┬ │
  1674. │ 4 │ │
  1675. │ 5 ┴ │
  1676. │ 6 ░ │
  1677. │ 7 ░ │
  1678. │ 8 ░ │
  1679. │ 9 ░ │
  1680. │ 0 ▼ │
  1681. │ ◄├───┤░░░░░░░► │
  1682. │ │
  1683. └──────────────────┘
  1684. ";
  1685. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1686. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1687. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1688. Application.Top.Redraw (Application.Top.Bounds);
  1689. expected = @"
  1690. ┌┤Test├────────────┐
  1691. │ │
  1692. │ 3 ▲ │
  1693. │ 4 ┬ │
  1694. │ 5 │ │
  1695. │ 6 ┴ │
  1696. │ 7 ░ │
  1697. │ 8 ░ │
  1698. │ 9 ░ │
  1699. │ 0 ░ │
  1700. │ ▼ │
  1701. │ ◄├───┤░░░░░░░► │
  1702. │ │
  1703. └──────────────────┘
  1704. ";
  1705. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1706. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1707. }
  1708. [Fact, AutoInitShutdown]
  1709. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1710. {
  1711. var view = new View () {
  1712. Width = Dim.Fill (),
  1713. Height = Dim.Fill ()
  1714. };
  1715. view.DrawContent += (s, e) => {
  1716. view.DrawFrame (view.Bounds);
  1717. var savedClip = Application.Driver.Clip;
  1718. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1719. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1720. Application.Driver.Move (1, row + 1);
  1721. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1722. Application.Driver.AddStr ($"{col}");
  1723. }
  1724. }
  1725. Application.Driver.Clip = savedClip;
  1726. };
  1727. Application.Top.Add (view);
  1728. Application.Begin (Application.Top);
  1729. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1730. var expected = @"
  1731. ┌──────────────────┐
  1732. │012345678910111213│
  1733. │012345678910111213│
  1734. │012345678910111213│
  1735. │012345678910111213│
  1736. │012345678910111213│
  1737. │012345678910111213│
  1738. │012345678910111213│
  1739. │012345678910111213│
  1740. └──────────────────┘
  1741. ";
  1742. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1743. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1744. view.Clear ();
  1745. expected = @"
  1746. ";
  1747. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1748. Assert.Equal (Rect.Empty, pos);
  1749. }
  1750. [Fact, AutoInitShutdown]
  1751. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1752. {
  1753. var view = new View () {
  1754. Width = Dim.Fill (),
  1755. Height = Dim.Fill ()
  1756. };
  1757. view.DrawContent += (s, e) => {
  1758. view.DrawFrame (view.Bounds);
  1759. var savedClip = Application.Driver.Clip;
  1760. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1761. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1762. Application.Driver.Move (1, row + 1);
  1763. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1764. Application.Driver.AddStr ($"{col}");
  1765. }
  1766. }
  1767. Application.Driver.Clip = savedClip;
  1768. };
  1769. Application.Top.Add (view);
  1770. Application.Begin (Application.Top);
  1771. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1772. var expected = @"
  1773. ┌──────────────────┐
  1774. │012345678910111213│
  1775. │012345678910111213│
  1776. │012345678910111213│
  1777. │012345678910111213│
  1778. │012345678910111213│
  1779. │012345678910111213│
  1780. │012345678910111213│
  1781. │012345678910111213│
  1782. └──────────────────┘
  1783. ";
  1784. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1785. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1786. view.Clear (view.Bounds);
  1787. expected = @"
  1788. ";
  1789. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1790. Assert.Equal (Rect.Empty, pos);
  1791. }
  1792. [Fact, AutoInitShutdown]
  1793. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  1794. {
  1795. var text = "Say Hello 你";
  1796. var horizontalView = new View () {
  1797. Text = text,
  1798. AutoSize = true,
  1799. HotKeySpecifier = '_' };
  1800. var verticalView = new View () {
  1801. Text = text,
  1802. AutoSize = true,
  1803. HotKeySpecifier = '_',
  1804. TextDirection = TextDirection.TopBottom_LeftRight
  1805. };
  1806. Application.Top.Add (horizontalView, verticalView);
  1807. Application.Begin (Application.Top);
  1808. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  1809. Assert.True (horizontalView.AutoSize);
  1810. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1811. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1812. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  1813. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  1814. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1815. Assert.True (verticalView.AutoSize);
  1816. // BUGBUG: v2 - Autosize is broken; disabling this test
  1817. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1818. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  1819. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  1820. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  1821. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  1822. text = "Say He_llo 你";
  1823. horizontalView.Text = text;
  1824. verticalView.Text = text;
  1825. Assert.True (horizontalView.AutoSize);
  1826. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1827. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1828. Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  1829. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  1830. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1831. Assert.True (verticalView.AutoSize);
  1832. // BUGBUG: v2 - Autosize is broken; disabling this test
  1833. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1834. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  1835. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  1836. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  1837. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  1838. }
  1839. [Fact]
  1840. public void IsAdded_Added_Removed ()
  1841. {
  1842. var top = new Toplevel ();
  1843. var view = new View ();
  1844. Assert.False (view.IsAdded);
  1845. top.Add (view);
  1846. Assert.True (view.IsAdded);
  1847. top.Remove (view);
  1848. Assert.False (view.IsAdded);
  1849. }
  1850. [Fact, AutoInitShutdown]
  1851. public void Visible_Clear_The_View_Output ()
  1852. {
  1853. var label = new Label ("Testing visibility.");
  1854. var win = new Window ();
  1855. win.Add (label);
  1856. var top = Application.Top;
  1857. top.Add (win);
  1858. Application.Begin (top);
  1859. Assert.True (label.Visible);
  1860. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1861. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1862. ┌────────────────────────────┐
  1863. │Testing visibility. │
  1864. │ │
  1865. │ │
  1866. └────────────────────────────┘
  1867. ", output);
  1868. label.Visible = false;
  1869. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1870. ┌────────────────────────────┐
  1871. │ │
  1872. │ │
  1873. │ │
  1874. └────────────────────────────┘
  1875. ", output);
  1876. }
  1877. [Fact, AutoInitShutdown]
  1878. public void ClearOnVisibleFalse_Gets_Sets ()
  1879. {
  1880. // BUGBUG: v2 - scrollview is broken. Disabling test for now
  1881. return;
  1882. 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";
  1883. var label = new Label (text);
  1884. Application.Top.Add (label);
  1885. var sbv = new ScrollBarView (label, true, false) {
  1886. Size = 100,
  1887. ClearOnVisibleFalse = false
  1888. };
  1889. Application.Begin (Application.Top);
  1890. Assert.True (sbv.Visible);
  1891. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1892. This is a tes▲
  1893. This is a tes┬
  1894. This is a tes┴
  1895. This is a tes░
  1896. This is a tes░
  1897. This is a tes▼
  1898. ", output);
  1899. sbv.Visible = false;
  1900. Assert.False (sbv.Visible);
  1901. Application.Top.Redraw (Application.Top.Bounds);
  1902. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1903. This is a test
  1904. This is a test
  1905. This is a test
  1906. This is a test
  1907. This is a test
  1908. This is a test
  1909. ", output);
  1910. sbv.Visible = true;
  1911. Assert.True (sbv.Visible);
  1912. Application.Top.Redraw (Application.Top.Bounds);
  1913. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1914. This is a tes▲
  1915. This is a tes┬
  1916. This is a tes┴
  1917. This is a tes░
  1918. This is a tes░
  1919. This is a tes▼
  1920. ", output);
  1921. sbv.ClearOnVisibleFalse = true;
  1922. sbv.Visible = false;
  1923. Assert.False (sbv.Visible);
  1924. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1925. This is a tes
  1926. This is a tes
  1927. This is a tes
  1928. This is a tes
  1929. This is a tes
  1930. This is a tes
  1931. ", output);
  1932. }
  1933. [Fact, AutoInitShutdown]
  1934. public void DrawContentComplete_Event_Is_Always_Called ()
  1935. {
  1936. var viewCalled = false;
  1937. var tvCalled = false;
  1938. var view = new View ("View") { Width = 10, Height = 10 };
  1939. view.DrawContentComplete += (s, e) => viewCalled = true;
  1940. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  1941. tv.DrawContentComplete += (s, e) => tvCalled = true;
  1942. Application.Top.Add (view, tv);
  1943. Application.Begin (Application.Top);
  1944. Assert.True (viewCalled);
  1945. Assert.True (tvCalled);
  1946. }
  1947. [Fact, AutoInitShutdown]
  1948. public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
  1949. {
  1950. var keyDown = false;
  1951. var keyPress = false;
  1952. var keyUp = false;
  1953. var view = new DerivedView ();
  1954. view.KeyDown += (s, e) => {
  1955. Assert.Equal (Key.a, e.KeyEvent.Key);
  1956. Assert.False (keyDown);
  1957. Assert.False (view.IsKeyDown);
  1958. e.Handled = true;
  1959. keyDown = true;
  1960. };
  1961. view.KeyPress += (s, e) => {
  1962. Assert.Equal (Key.a, e.KeyEvent.Key);
  1963. Assert.False (keyPress);
  1964. Assert.False (view.IsKeyPress);
  1965. e.Handled = true;
  1966. keyPress = true;
  1967. };
  1968. view.KeyUp += (s, e) => {
  1969. Assert.Equal (Key.a, e.KeyEvent.Key);
  1970. Assert.False (keyUp);
  1971. Assert.False (view.IsKeyUp);
  1972. e.Handled = true;
  1973. keyUp = true;
  1974. };
  1975. Application.Top.Add (view);
  1976. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
  1977. Application.Iteration += () => Application.RequestStop ();
  1978. Assert.True (view.CanFocus);
  1979. Application.Run ();
  1980. Application.Shutdown ();
  1981. Assert.True (keyDown);
  1982. Assert.True (keyPress);
  1983. Assert.True (keyUp);
  1984. Assert.False (view.IsKeyDown);
  1985. Assert.False (view.IsKeyPress);
  1986. Assert.False (view.IsKeyUp);
  1987. }
  1988. public class DerivedView : View {
  1989. public DerivedView ()
  1990. {
  1991. CanFocus = true;
  1992. }
  1993. public bool IsKeyDown { get; set; }
  1994. public bool IsKeyPress { get; set; }
  1995. public bool IsKeyUp { get; set; }
  1996. public override ustring Text { get; set; }
  1997. public override bool OnKeyDown (KeyEvent keyEvent)
  1998. {
  1999. IsKeyDown = true;
  2000. return true;
  2001. }
  2002. public override bool ProcessKey (KeyEvent keyEvent)
  2003. {
  2004. IsKeyPress = true;
  2005. return true;
  2006. }
  2007. public override bool OnKeyUp (KeyEvent keyEvent)
  2008. {
  2009. IsKeyUp = true;
  2010. return true;
  2011. }
  2012. public override void Redraw (Rect bounds)
  2013. {
  2014. var idx = 0;
  2015. // BUGBUG: v2 - this should use Boudns, not Frame
  2016. for (int r = 0; r < Frame.Height; r++) {
  2017. for (int c = 0; c < Frame.Width; c++) {
  2018. if (idx < Text.Length) {
  2019. var rune = Text [idx];
  2020. if (rune != '\n') {
  2021. AddRune (c, r, Text [idx]);
  2022. }
  2023. idx++;
  2024. if (rune == '\n') {
  2025. break;
  2026. }
  2027. }
  2028. }
  2029. }
  2030. ClearLayoutNeeded ();
  2031. ClearNeedsDisplay ();
  2032. }
  2033. }
  2034. [Theory, AutoInitShutdown]
  2035. [InlineData (true, false, false)]
  2036. [InlineData (true, true, false)]
  2037. [InlineData (true, true, true)]
  2038. public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
  2039. {
  2040. var keyDown = false;
  2041. var keyPress = false;
  2042. var keyUp = false;
  2043. var view = new DerivedView ();
  2044. view.KeyDown += (s, e) => {
  2045. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2046. Assert.Equal (shift, e.KeyEvent.IsShift);
  2047. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2048. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2049. Assert.False (keyDown);
  2050. Assert.False (view.IsKeyDown);
  2051. keyDown = true;
  2052. };
  2053. view.KeyPress += (s, e) => {
  2054. keyPress = true;
  2055. };
  2056. view.KeyUp += (s, e) => {
  2057. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2058. Assert.Equal (shift, e.KeyEvent.IsShift);
  2059. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2060. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2061. Assert.False (keyUp);
  2062. Assert.False (view.IsKeyUp);
  2063. keyUp = true;
  2064. };
  2065. Application.Top.Add (view);
  2066. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
  2067. Application.Iteration += () => Application.RequestStop ();
  2068. Assert.True (view.CanFocus);
  2069. Application.Run ();
  2070. Application.Shutdown ();
  2071. Assert.True (keyDown);
  2072. Assert.False (keyPress);
  2073. Assert.True (keyUp);
  2074. Assert.True (view.IsKeyDown);
  2075. Assert.False (view.IsKeyPress);
  2076. Assert.True (view.IsKeyUp);
  2077. }
  2078. [Fact, AutoInitShutdown]
  2079. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  2080. {
  2081. var view1Leave = false;
  2082. var subView1Leave = false;
  2083. var subView1subView1Leave = false;
  2084. var top = Application.Top;
  2085. var view1 = new View { CanFocus = true };
  2086. var subView1 = new View { CanFocus = true };
  2087. var subView1subView1 = new View { CanFocus = true };
  2088. view1.Leave += (s, e) => {
  2089. view1Leave = true;
  2090. };
  2091. subView1.Leave += (s, e) => {
  2092. subView1.Remove (subView1subView1);
  2093. subView1Leave = true;
  2094. };
  2095. view1.Add (subView1);
  2096. subView1subView1.Leave += (s, e) => {
  2097. // This is never invoked
  2098. subView1subView1Leave = true;
  2099. };
  2100. subView1.Add (subView1subView1);
  2101. var view2 = new View { CanFocus = true };
  2102. top.Add (view1, view2);
  2103. Application.Begin (top);
  2104. view2.SetFocus ();
  2105. Assert.True (view1Leave);
  2106. Assert.True (subView1Leave);
  2107. Assert.False (subView1subView1Leave);
  2108. }
  2109. [Fact, AutoInitShutdown]
  2110. public void GetNormalColor_ColorScheme ()
  2111. {
  2112. var view = new View { ColorScheme = Colors.Base };
  2113. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  2114. view.Enabled = false;
  2115. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  2116. }
  2117. [Fact, AutoInitShutdown]
  2118. public void GetHotNormalColor_ColorScheme ()
  2119. {
  2120. var view = new View { ColorScheme = Colors.Base };
  2121. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  2122. view.Enabled = false;
  2123. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  2124. }
  2125. [Theory, AutoInitShutdown]
  2126. [InlineData (true)]
  2127. [InlineData (false)]
  2128. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  2129. {
  2130. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  2131. var v = label == true ?
  2132. new Label (new string ('c', 100)) {
  2133. Width = Dim.Fill (),
  2134. } :
  2135. (View)new TextView () {
  2136. Height = 1,
  2137. Text = new string ('c', 100),
  2138. Width = Dim.Fill ()
  2139. };
  2140. root.Add (v);
  2141. Application.Top.Add (root);
  2142. Application.Begin (Application.Top);
  2143. if (label) {
  2144. Assert.True (v.AutoSize);
  2145. Assert.False (v.CanFocus);
  2146. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  2147. } else {
  2148. Assert.False (v.AutoSize);
  2149. Assert.True (v.CanFocus);
  2150. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  2151. }
  2152. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2153. cccccccccccccccccccc", output);
  2154. var attributes = new Attribute [] {
  2155. Colors.TopLevel.Normal,
  2156. Colors.Base.Normal,
  2157. Colors.Base.Focus
  2158. };
  2159. if (label) {
  2160. TestHelpers.AssertDriverColorsAre (@"
  2161. 111111111111111111110
  2162. 111111111111111111110", attributes);
  2163. } else {
  2164. TestHelpers.AssertDriverColorsAre (@"
  2165. 222222222222222222220
  2166. 111111111111111111110", attributes);
  2167. }
  2168. if (label) {
  2169. root.CanFocus = true;
  2170. v.CanFocus = true;
  2171. Assert.False (v.HasFocus);
  2172. v.SetFocus ();
  2173. Assert.True (v.HasFocus);
  2174. Application.Refresh ();
  2175. TestHelpers.AssertDriverColorsAre (@"
  2176. 222222222222222222220
  2177. 111111111111111111110", attributes);
  2178. }
  2179. }
  2180. [Fact, AutoInitShutdown]
  2181. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2182. {
  2183. var label = new Label ("At 0,0");
  2184. var view = new DerivedView () {
  2185. X = 2,
  2186. Y = 2,
  2187. Width = 30,
  2188. Height = 2,
  2189. Text = "A text with some long width\n and also with two lines."
  2190. };
  2191. var top = Application.Top;
  2192. top.Add (label, view);
  2193. Application.Begin (top);
  2194. top.Redraw (top.Bounds);
  2195. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2196. At 0,0
  2197. A text with some long width
  2198. and also with two lines. ", output);
  2199. view.Frame = new Rect (1, 1, 10, 1);
  2200. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2201. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2202. view.LayoutStyle = LayoutStyle.Absolute;
  2203. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2204. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2205. top.Redraw (top.Bounds);
  2206. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2207. At 0,0
  2208. A text wit", output);
  2209. }
  2210. [Fact, AutoInitShutdown]
  2211. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2212. {
  2213. var label = new Label ("At 0,0");
  2214. var view = new DerivedView () {
  2215. X = 2,
  2216. Y = 2,
  2217. Width = 30,
  2218. Height = 2,
  2219. Text = "A text with some long width\n and also with two lines."
  2220. };
  2221. var top = Application.Top;
  2222. top.Add (label, view);
  2223. Application.Begin (top);
  2224. top.Redraw (top.Bounds);
  2225. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2226. At 0,0
  2227. A text with some long width
  2228. and also with two lines. ", output);
  2229. view.X = 1;
  2230. view.Y = 1;
  2231. view.Width = 10;
  2232. view.Height = 1;
  2233. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2234. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2235. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2236. top.Redraw (top.Bounds);
  2237. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2238. At 0,0
  2239. A text wit", output);
  2240. }
  2241. [Fact, AutoInitShutdown]
  2242. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2243. {
  2244. var label = new Label ("At 0,0");
  2245. var view = new DerivedView () {
  2246. X = 2,
  2247. Y = 2,
  2248. Width = 30,
  2249. Height = 2,
  2250. Text = "A text with some long width\n and also with two lines."
  2251. };
  2252. var top = Application.Top;
  2253. top.Add (label, view);
  2254. Application.Begin (top);
  2255. view.Redraw (view.Bounds);
  2256. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2257. At 0,0
  2258. A text with some long width
  2259. and also with two lines. ", output);
  2260. view.Frame = new Rect (1, 1, 10, 1);
  2261. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2262. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2263. view.LayoutStyle = LayoutStyle.Absolute;
  2264. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2265. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2266. view.Redraw (view.Bounds);
  2267. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2268. At 0,0
  2269. A text wit
  2270. A text with some long width
  2271. and also with two lines. ", output);
  2272. }
  2273. [Fact, AutoInitShutdown]
  2274. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2275. {
  2276. var label = new Label ("At 0,0");
  2277. var view = new DerivedView () {
  2278. X = 2,
  2279. Y = 2,
  2280. Width = 30,
  2281. Height = 2,
  2282. Text = "A text with some long width\n and also with two lines."
  2283. };
  2284. var top = Application.Top;
  2285. top.Add (label, view);
  2286. Application.Begin (top);
  2287. view.Redraw (view.Bounds);
  2288. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2289. At 0,0
  2290. A text with some long width
  2291. and also with two lines. ", output);
  2292. view.X = 1;
  2293. view.Y = 1;
  2294. view.Width = 10;
  2295. view.Height = 1;
  2296. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2297. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2298. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2299. view.Redraw (view.Bounds);
  2300. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2301. At 0,0
  2302. A text wit
  2303. A text with some long width
  2304. and also with two lines. ", output);
  2305. }
  2306. [Fact, AutoInitShutdown]
  2307. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2308. {
  2309. var label = new Label ("At 0,0");
  2310. var view = new DerivedView () {
  2311. X = 2,
  2312. Y = 2,
  2313. Width = 30,
  2314. Height = 2,
  2315. Text = "A text with some long width\n and also with two lines."
  2316. };
  2317. var top = Application.Top;
  2318. top.Add (label, view);
  2319. Application.Begin (top);
  2320. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2321. At 0,0
  2322. A text with some long width
  2323. and also with two lines. ", output);
  2324. view.Frame = new Rect (3, 3, 10, 1);
  2325. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2326. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2327. view.LayoutStyle = LayoutStyle.Absolute;
  2328. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2329. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2330. top.Redraw (top.Bounds);
  2331. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2332. At 0,0
  2333. A text wit", output);
  2334. }
  2335. [Fact, AutoInitShutdown]
  2336. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2337. {
  2338. var label = new Label ("At 0,0");
  2339. var view = new DerivedView () {
  2340. X = 2,
  2341. Y = 2,
  2342. Width = 30,
  2343. Height = 2,
  2344. Text = "A text with some long width\n and also with two lines."
  2345. };
  2346. var top = Application.Top;
  2347. top.Add (label, view);
  2348. Application.Begin (top);
  2349. top.Redraw (top.Bounds);
  2350. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2351. At 0,0
  2352. A text with some long width
  2353. and also with two lines. ", output);
  2354. view.X = 3;
  2355. view.Y = 3;
  2356. view.Width = 10;
  2357. view.Height = 1;
  2358. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2359. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2360. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2361. top.Redraw (top.Bounds);
  2362. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2363. At 0,0
  2364. A text wit", output);
  2365. }
  2366. [Fact, AutoInitShutdown]
  2367. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2368. {
  2369. var label = new Label ("At 0,0");
  2370. var view = new DerivedView () {
  2371. X = 2,
  2372. Y = 2,
  2373. Width = 30,
  2374. Height = 2,
  2375. Text = "A text with some long width\n and also with two lines."
  2376. };
  2377. var top = Application.Top;
  2378. top.Add (label, view);
  2379. Application.Begin (top);
  2380. view.Redraw (view.Bounds);
  2381. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2382. At 0,0
  2383. A text with some long width
  2384. and also with two lines. ", output);
  2385. view.Frame = new Rect (3, 3, 10, 1);
  2386. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2387. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2388. view.Redraw (view.Bounds);
  2389. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2390. At 0,0
  2391. A text with some long width
  2392. A text witith two lines. ", output);
  2393. }
  2394. [Fact, AutoInitShutdown]
  2395. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2396. {
  2397. var label = new Label ("At 0,0");
  2398. var view = new DerivedView () {
  2399. X = 2,
  2400. Y = 2,
  2401. Width = 30,
  2402. Height = 2,
  2403. Text = "A text with some long width\n and also with two lines."
  2404. };
  2405. var top = Application.Top;
  2406. top.Add (label, view);
  2407. Application.Begin (top);
  2408. view.Redraw (view.Bounds);
  2409. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2410. At 0,0
  2411. A text with some long width
  2412. and also with two lines. ", output);
  2413. view.X = 3;
  2414. view.Y = 3;
  2415. view.Width = 10;
  2416. view.Height = 1;
  2417. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2418. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2419. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2420. view.Redraw (view.Bounds);
  2421. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2422. At 0,0
  2423. A text with some long width
  2424. A text witith two lines. ", output);
  2425. }
  2426. [Fact, AutoInitShutdown]
  2427. public void Test_Nested_Views_With_Height_Equal_To_One ()
  2428. {
  2429. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  2430. var top = new View () { Width = Dim.Fill (), Height = 1 };
  2431. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  2432. top.Add (new Label ("111"));
  2433. v.Add (top);
  2434. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  2435. bottom.Add (new Label ("222"));
  2436. v.Add (bottom);
  2437. v.LayoutSubviews ();
  2438. v.Redraw (v.Bounds);
  2439. string looksLike =
  2440. @"
  2441. 111
  2442. ───────────
  2443. 222";
  2444. TestHelpers.AssertDriverContentsAre (looksLike, output);
  2445. }
  2446. [Fact]
  2447. [AutoInitShutdown]
  2448. public void Frame_Set_After_Initialze_Update_NeededDisplay ()
  2449. {
  2450. var frame = new FrameView ();
  2451. var label = new Label ("This should be the first line.") {
  2452. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  2453. ColorScheme = Colors.Menu,
  2454. Width = Dim.Fill (),
  2455. X = Pos.Center (),
  2456. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  2457. };
  2458. var button = new Button ("Press me!") {
  2459. X = Pos.Center (),
  2460. Y = Pos.Center ()
  2461. };
  2462. frame.Add (label, button);
  2463. frame.X = Pos.Center ();
  2464. frame.Y = Pos.Center ();
  2465. frame.Width = 40;
  2466. frame.Height = 8;
  2467. var top = Application.Top;
  2468. top.Add (frame);
  2469. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2470. Assert.Equal (new Rect (0, 0, 40, 8), frame.Frame);
  2471. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2472. frame.Frame.Left, frame.Frame.Top,
  2473. frame.Frame.Right, frame.Frame.Bottom));
  2474. Assert.Equal (new Rect (0, 0, 30, 1), label.Frame);
  2475. Assert.Equal (new Rect (0, 0, 13, 1), button.Frame);
  2476. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  2477. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  2478. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2479. frame._needsDisplay.Left, frame._needsDisplay.Top,
  2480. frame._needsDisplay.Right, frame._needsDisplay.Bottom));
  2481. Assert.Equal (new Rect (0, 0, 30, 1), label._needsDisplay);
  2482. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  2483. top.LayoutComplete += (s, e) => {
  2484. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  2485. };
  2486. frame.LayoutComplete += (s, e) => {
  2487. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  2488. };
  2489. label.LayoutComplete += (s, e) => {
  2490. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplay);
  2491. };
  2492. button.LayoutComplete += (s, e) => {
  2493. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  2494. };
  2495. Application.Begin (top);
  2496. Assert.True (label.AutoSize);
  2497. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2498. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  2499. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  2500. frame.Frame.Left, frame.Frame.Top,
  2501. frame.Frame.Right, frame.Frame.Bottom));
  2502. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  2503. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  2504. var expected = @"
  2505. ┌──────────────────────────────────────┐
  2506. │ This should be the first line. │
  2507. │ │
  2508. │ [ Press me! ] │
  2509. │ │
  2510. │ │
  2511. │ │
  2512. └──────────────────────────────────────┘
  2513. ";
  2514. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  2515. }
  2516. }
  2517. }