ViewTests.cs 87 KB

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