ViewTests.cs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034
  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. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view._needsDisplay);
  1028. Assert.True (view.LayoutNeeded);
  1029. Assert.False (view._childNeedsDisplay);
  1030. Assert.False (view.addingView);
  1031. view.addingView = true;
  1032. Assert.True (view.addingView);
  1033. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1034. Assert.Equal (1, rcol);
  1035. Assert.Equal (1, rrow);
  1036. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1037. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1038. view.Width = Dim.Fill ();
  1039. view.Height = Dim.Fill ();
  1040. Assert.Equal (10, view.Bounds.Width);
  1041. Assert.Equal (1, view.Bounds.Height);
  1042. view.SetRelativeLayout (top.Bounds);
  1043. Assert.Equal (79, view.Bounds.Width);
  1044. Assert.Equal (24, view.Bounds.Height);
  1045. view.X = 0;
  1046. view.Y = 0;
  1047. view.SetRelativeLayout (top.Bounds);
  1048. Assert.Equal (80, view.Bounds.Width);
  1049. Assert.Equal (25, view.Bounds.Height);
  1050. bool layoutStarted = false;
  1051. view.LayoutStarted += (s, e) => layoutStarted = true;
  1052. view.OnLayoutStarted (null);
  1053. Assert.True (layoutStarted);
  1054. view.LayoutComplete += (s, e) => layoutStarted = false;
  1055. view.OnLayoutComplete (null);
  1056. Assert.False (layoutStarted);
  1057. view.X = Pos.Center () - 41;
  1058. view.Y = Pos.Center () - 13;
  1059. view.SetRelativeLayout (top.Bounds);
  1060. view.ViewToScreen (0, 0, out rcol, out rrow);
  1061. Assert.Equal (-41, rcol);
  1062. Assert.Equal (-13, rrow);
  1063. }
  1064. [Fact]
  1065. [AutoInitShutdown]
  1066. public void Enabled_False_Sets_HasFocus_To_False ()
  1067. {
  1068. var wasClicked = false;
  1069. var view = new Button ("Click Me");
  1070. view.Clicked += (s, e) => wasClicked = !wasClicked;
  1071. Application.Top.Add (view);
  1072. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1073. Assert.True (wasClicked);
  1074. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1075. Assert.False (wasClicked);
  1076. Assert.True (view.Enabled);
  1077. Assert.True (view.CanFocus);
  1078. Assert.True (view.HasFocus);
  1079. view.Enabled = false;
  1080. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1081. Assert.False (wasClicked);
  1082. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1083. Assert.False (wasClicked);
  1084. Assert.False (view.Enabled);
  1085. Assert.True (view.CanFocus);
  1086. Assert.False (view.HasFocus);
  1087. view.SetFocus ();
  1088. Assert.False (view.HasFocus);
  1089. }
  1090. [Fact]
  1091. [AutoInitShutdown]
  1092. public void Enabled_Sets_Also_Sets_Subviews ()
  1093. {
  1094. var wasClicked = false;
  1095. var button = new Button ("Click Me");
  1096. button.Clicked += (s, e) => wasClicked = !wasClicked;
  1097. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1098. win.Add (button);
  1099. Application.Top.Add (win);
  1100. var iterations = 0;
  1101. Application.Iteration += () => {
  1102. iterations++;
  1103. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1104. Assert.True (wasClicked);
  1105. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1106. Assert.False (wasClicked);
  1107. Assert.True (button.Enabled);
  1108. Assert.True (button.CanFocus);
  1109. Assert.True (button.HasFocus);
  1110. Assert.True (win.Enabled);
  1111. Assert.True (win.CanFocus);
  1112. Assert.True (win.HasFocus);
  1113. win.Enabled = false;
  1114. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1115. Assert.False (wasClicked);
  1116. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1117. Assert.False (wasClicked);
  1118. Assert.False (button.Enabled);
  1119. Assert.True (button.CanFocus);
  1120. Assert.False (button.HasFocus);
  1121. Assert.False (win.Enabled);
  1122. Assert.True (win.CanFocus);
  1123. Assert.False (win.HasFocus);
  1124. button.SetFocus ();
  1125. Assert.False (button.HasFocus);
  1126. Assert.False (win.HasFocus);
  1127. win.SetFocus ();
  1128. Assert.False (button.HasFocus);
  1129. Assert.False (win.HasFocus);
  1130. win.Enabled = true;
  1131. win.FocusFirst ();
  1132. Assert.True (button.HasFocus);
  1133. Assert.True (win.HasFocus);
  1134. Application.RequestStop ();
  1135. };
  1136. Application.Run ();
  1137. Assert.Equal (1, iterations);
  1138. }
  1139. [Fact]
  1140. [AutoInitShutdown]
  1141. public void Visible_Sets_Also_Sets_Subviews ()
  1142. {
  1143. var button = new Button ("Click Me");
  1144. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1145. win.Add (button);
  1146. var top = Application.Top;
  1147. top.Add (win);
  1148. var iterations = 0;
  1149. Application.Iteration += () => {
  1150. iterations++;
  1151. Assert.True (button.Visible);
  1152. Assert.True (button.CanFocus);
  1153. Assert.True (button.HasFocus);
  1154. Assert.True (win.Visible);
  1155. Assert.True (win.CanFocus);
  1156. Assert.True (win.HasFocus);
  1157. Assert.True (RunesCount () > 0);
  1158. win.Visible = false;
  1159. Assert.True (button.Visible);
  1160. Assert.True (button.CanFocus);
  1161. Assert.False (button.HasFocus);
  1162. Assert.False (win.Visible);
  1163. Assert.True (win.CanFocus);
  1164. Assert.False (win.HasFocus);
  1165. button.SetFocus ();
  1166. Assert.False (button.HasFocus);
  1167. Assert.False (win.HasFocus);
  1168. win.SetFocus ();
  1169. Assert.False (button.HasFocus);
  1170. Assert.False (win.HasFocus);
  1171. top.Redraw (top.Bounds);
  1172. Assert.True (RunesCount () == 0);
  1173. win.Visible = true;
  1174. win.FocusFirst ();
  1175. Assert.True (button.HasFocus);
  1176. Assert.True (win.HasFocus);
  1177. top.Redraw (top.Bounds);
  1178. Assert.True (RunesCount () > 0);
  1179. Application.RequestStop ();
  1180. };
  1181. Application.Run ();
  1182. Assert.Equal (1, iterations);
  1183. int RunesCount ()
  1184. {
  1185. var contents = ((FakeDriver)Application.Driver).Contents;
  1186. var runesCount = 0;
  1187. for (int i = 0; i < Application.Driver.Rows; i++) {
  1188. for (int j = 0; j < Application.Driver.Cols; j++) {
  1189. if (contents [i, j, 0] != ' ') {
  1190. runesCount++;
  1191. }
  1192. }
  1193. }
  1194. return runesCount;
  1195. }
  1196. }
  1197. [Fact]
  1198. [AutoInitShutdown]
  1199. public void GetTopSuperView_Test ()
  1200. {
  1201. var v1 = new View ();
  1202. var fv1 = new FrameView ();
  1203. fv1.Add (v1);
  1204. var tf1 = new TextField ();
  1205. var w1 = new Window ();
  1206. w1.Add (fv1, tf1);
  1207. var top1 = new Toplevel ();
  1208. top1.Add (w1);
  1209. var v2 = new View ();
  1210. var fv2 = new FrameView ();
  1211. fv2.Add (v2);
  1212. var tf2 = new TextField ();
  1213. var w2 = new Window ();
  1214. w2.Add (fv2, tf2);
  1215. var top2 = new Toplevel ();
  1216. top2.Add (w2);
  1217. Assert.Equal (top1, v1.GetTopSuperView ());
  1218. Assert.Equal (top2, v2.GetTopSuperView ());
  1219. }
  1220. [Fact]
  1221. [AutoInitShutdown]
  1222. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1223. {
  1224. var view = new View () { CanFocus = true };
  1225. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1226. win.Add (view);
  1227. Application.Top.Add (win);
  1228. Application.Begin (Application.Top);
  1229. Assert.True (view.CanFocus);
  1230. Assert.True (view.HasFocus);
  1231. view.CanFocus = false;
  1232. Assert.False (view.CanFocus);
  1233. Assert.False (view.HasFocus);
  1234. Assert.Null (Application.Current.Focused);
  1235. Assert.Null (Application.Current.MostFocused);
  1236. }
  1237. [Fact]
  1238. [AutoInitShutdown]
  1239. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1240. {
  1241. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1242. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1243. win1.Add (view1);
  1244. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  1245. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1246. win2.Add (view2);
  1247. Application.Top.Add (win1, win2);
  1248. Application.Begin (Application.Top);
  1249. Assert.True (view1.CanFocus);
  1250. Assert.True (view1.HasFocus);
  1251. Assert.True (view2.CanFocus);
  1252. Assert.False (view2.HasFocus);
  1253. view1.CanFocus = false;
  1254. Assert.False (view1.CanFocus);
  1255. Assert.False (view1.HasFocus);
  1256. Assert.Equal (win2, Application.Current.Focused);
  1257. Assert.Equal (view2, Application.Current.MostFocused);
  1258. }
  1259. [Fact]
  1260. [AutoInitShutdown]
  1261. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1262. {
  1263. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1264. var view12 = new View () { Id = "view12", Y = 5, Width = 10, Height = 1, CanFocus = true };
  1265. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1266. win1.Add (view1, view12);
  1267. var view2 = new View () { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  1268. var win2 = new Window () { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1269. win2.Add (view2);
  1270. Application.Top.Add (win1, win2);
  1271. Application.Begin (Application.Top);
  1272. Assert.True (view1.CanFocus);
  1273. Assert.True (view1.HasFocus);
  1274. Assert.True (view2.CanFocus);
  1275. Assert.False (view2.HasFocus);
  1276. view1.CanFocus = false;
  1277. Assert.False (view1.CanFocus);
  1278. Assert.False (view1.HasFocus);
  1279. Assert.Equal (win1, Application.Current.Focused);
  1280. Assert.Equal (view12, Application.Current.MostFocused);
  1281. }
  1282. [Fact]
  1283. [AutoInitShutdown]
  1284. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1285. {
  1286. var view1 = new View () { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  1287. var win1 = new Window () { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  1288. win1.Add (view1);
  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. win1.CanFocus = false;
  1299. Assert.False (view1.CanFocus);
  1300. Assert.False (view1.HasFocus);
  1301. Assert.False (win1.CanFocus);
  1302. Assert.False (win1.HasFocus);
  1303. Assert.Equal (win2, Application.Current.Focused);
  1304. Assert.Equal (view2, Application.Current.MostFocused);
  1305. }
  1306. [Fact]
  1307. [AutoInitShutdown]
  1308. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1309. {
  1310. var sbQuiting = false;
  1311. var tfQuiting = false;
  1312. var topQuiting = false;
  1313. var sb = new StatusBar (new StatusItem [] {
  1314. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1315. });
  1316. var tf = new TextField ();
  1317. tf.KeyPress += Tf_KeyPress;
  1318. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  1319. {
  1320. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1321. obj.Handled = tfQuiting = true;
  1322. }
  1323. }
  1324. var win = new Window ();
  1325. win.Add (sb, tf);
  1326. var top = Application.Top;
  1327. top.KeyPress += Top_KeyPress;
  1328. void Top_KeyPress (object sender, KeyEventEventArgs obj)
  1329. {
  1330. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1331. obj.Handled = topQuiting = true;
  1332. }
  1333. }
  1334. top.Add (win);
  1335. Application.Begin (top);
  1336. Assert.False (sbQuiting);
  1337. Assert.False (tfQuiting);
  1338. Assert.False (topQuiting);
  1339. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1340. Assert.False (sbQuiting);
  1341. Assert.True (tfQuiting);
  1342. Assert.False (topQuiting);
  1343. tf.KeyPress -= Tf_KeyPress;
  1344. tfQuiting = false;
  1345. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1346. Application.MainLoop.MainIteration ();
  1347. Assert.True (sbQuiting);
  1348. Assert.False (tfQuiting);
  1349. Assert.False (topQuiting);
  1350. sb.RemoveItem (0);
  1351. sbQuiting = false;
  1352. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1353. Application.MainLoop.MainIteration ();
  1354. Assert.False (sbQuiting);
  1355. Assert.False (tfQuiting);
  1356. Assert.True (topQuiting);
  1357. }
  1358. [Fact]
  1359. [AutoInitShutdown]
  1360. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1361. {
  1362. var sbQuiting = false;
  1363. var tfQuiting = false;
  1364. var sb = new StatusBar (new StatusItem [] {
  1365. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1366. });
  1367. var tf = new TextField ();
  1368. tf.KeyPress += Tf_KeyPress;
  1369. void Tf_KeyPress (object sender, KeyEventEventArgs obj)
  1370. {
  1371. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1372. obj.Handled = tfQuiting = true;
  1373. }
  1374. }
  1375. var win = new Window ();
  1376. win.Add (sb, tf);
  1377. var top = Application.Top;
  1378. top.Add (win);
  1379. Application.Begin (top);
  1380. Assert.False (sbQuiting);
  1381. Assert.False (tfQuiting);
  1382. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1383. Assert.False (sbQuiting);
  1384. Assert.True (tfQuiting);
  1385. tf.KeyPress -= Tf_KeyPress;
  1386. tfQuiting = false;
  1387. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1388. Application.MainLoop.MainIteration ();
  1389. Assert.True (sbQuiting);
  1390. Assert.False (tfQuiting);
  1391. }
  1392. [Fact]
  1393. [AutoInitShutdown]
  1394. public void WindowDispose_CanFocusProblem ()
  1395. {
  1396. // Arrange
  1397. Application.Init ();
  1398. using var top = Toplevel.Create ();
  1399. using var view = new View (
  1400. x: 0,
  1401. y: 1,
  1402. text: nameof (WindowDispose_CanFocusProblem));
  1403. using var window = new Window ();
  1404. top.Add (window);
  1405. window.Add (view);
  1406. // Act
  1407. Application.Begin (top);
  1408. Application.Shutdown ();
  1409. // Assert does Not throw NullReferenceException
  1410. top.SetFocus ();
  1411. }
  1412. [Fact, AutoInitShutdown]
  1413. public void DrawFrame_With_Positive_Positions ()
  1414. {
  1415. var view = new View (new Rect (0, 0, 8, 4));
  1416. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1417. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1418. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1419. Application.Top.Add (view);
  1420. Application.Begin (Application.Top);
  1421. var expected = @"
  1422. ┌──────┐
  1423. │ │
  1424. │ │
  1425. └──────┘
  1426. ";
  1427. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1428. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1429. }
  1430. [Fact, AutoInitShutdown]
  1431. public void DrawFrame_With_Minimum_Size ()
  1432. {
  1433. var view = new View (new Rect (0, 0, 2, 2));
  1434. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1435. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1436. Assert.Equal (new Size (2, 2), new Size (view.Frame.Width, view.Frame.Height));
  1437. Application.Top.Add (view);
  1438. Application.Begin (Application.Top);
  1439. var expected = @"
  1440. ┌┐
  1441. └┘
  1442. ";
  1443. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1444. Assert.Equal (new Rect (0, 0, 2, 2), pos);
  1445. }
  1446. [Fact, AutoInitShutdown]
  1447. public void DrawFrame_With_Negative_Positions ()
  1448. {
  1449. var view = new View (new Rect (-1, 0, 8, 4));
  1450. view.DrawContent += (s, e) => view.DrawFrame (view.Bounds, 0, true);
  1451. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1452. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1453. Application.Top.Add (view);
  1454. Application.Begin (Application.Top);
  1455. var expected = @"
  1456. ──────┐
  1457. ──────┘
  1458. ";
  1459. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1460. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1461. view.Frame = new Rect (-1, -1, 8, 4);
  1462. Application.Refresh ();
  1463. expected = @"
  1464. ──────┘
  1465. ";
  1466. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1467. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1468. view.Frame = new Rect (0, 0, 8, 4);
  1469. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1470. expected = @"
  1471. ┌──────
  1472. └──────
  1473. ";
  1474. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1475. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1476. view.Frame = new Rect (0, 0, 8, 4);
  1477. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1478. }
  1479. [Fact, AutoInitShutdown]
  1480. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1481. {
  1482. // BUGBUG: v2 - scrollview is broken. Disabling test for now
  1483. return;
  1484. var view = new View (new Rect (0, 0, 20, 20));
  1485. view.Add (new Label ("0123456789abcdefghij"));
  1486. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1487. view.Add (new Button (1, 1, "Press me!"));
  1488. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1489. ContentSize = new Size (40, 40),
  1490. ShowHorizontalScrollIndicator = true,
  1491. ShowVerticalScrollIndicator = true
  1492. };
  1493. scrollView.Add (view);
  1494. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1495. win.Add (scrollView);
  1496. Application.Top.Add (win);
  1497. Application.Begin (Application.Top);
  1498. var expected = @"
  1499. ┌┤Test├────────────┐
  1500. │ │
  1501. │ 0123456789abcd▲ │
  1502. │ 1[ Press me! ]┬ │
  1503. │ 2 │ │
  1504. │ 3 ┴ │
  1505. │ 4 ░ │
  1506. │ 5 ░ │
  1507. │ 6 ░ │
  1508. │ 7 ░ │
  1509. │ 8 ▼ │
  1510. │ ◄├───┤░░░░░░░► │
  1511. │ │
  1512. └──────────────────┘
  1513. ";
  1514. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1515. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1516. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1517. Application.Top.Redraw (Application.Top.Bounds);
  1518. expected = @"
  1519. ┌┤Test├────────────┐
  1520. │ │
  1521. │ 123456789abcde▲ │
  1522. │ [ Press me! ] ┬ │
  1523. │ │ │
  1524. │ ┴ │
  1525. │ ░ │
  1526. │ ░ │
  1527. │ ░ │
  1528. │ ░ │
  1529. │ ▼ │
  1530. │ ◄├───┤░░░░░░░► │
  1531. │ │
  1532. └──────────────────┘
  1533. ";
  1534. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1535. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1536. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1537. Application.Top.Redraw (Application.Top.Bounds);
  1538. expected = @"
  1539. ┌┤Test├────────────┐
  1540. │ │
  1541. │ 23456789abcdef▲ │
  1542. │ Press me! ] ┬ │
  1543. │ │ │
  1544. │ ┴ │
  1545. │ ░ │
  1546. │ ░ │
  1547. │ ░ │
  1548. │ ░ │
  1549. │ ▼ │
  1550. │ ◄├────┤░░░░░░► │
  1551. │ │
  1552. └──────────────────┘
  1553. ";
  1554. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1555. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1556. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1557. Application.Top.Redraw (Application.Top.Bounds);
  1558. expected = @"
  1559. ┌┤Test├────────────┐
  1560. │ │
  1561. │ 3456789abcdefg▲ │
  1562. │ Press me! ] ┬ │
  1563. │ │ │
  1564. │ ┴ │
  1565. │ ░ │
  1566. │ ░ │
  1567. │ ░ │
  1568. │ ░ │
  1569. │ ▼ │
  1570. │ ◄├────┤░░░░░░► │
  1571. │ │
  1572. └──────────────────┘
  1573. ";
  1574. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1575. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1576. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1577. Application.Top.Redraw (Application.Top.Bounds);
  1578. expected = @"
  1579. ┌┤Test├────────────┐
  1580. │ │
  1581. │ 456789abcdefgh▲ │
  1582. │ ress me! ] ┬ │
  1583. │ │ │
  1584. │ ┴ │
  1585. │ ░ │
  1586. │ ░ │
  1587. │ ░ │
  1588. │ ░ │
  1589. │ ▼ │
  1590. │ ◄░├───┤░░░░░░► │
  1591. │ │
  1592. └──────────────────┘
  1593. ";
  1594. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1595. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1596. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1597. Application.Top.Redraw (Application.Top.Bounds);
  1598. expected = @"
  1599. ┌┤Test├────────────┐
  1600. │ │
  1601. │ 56789abcdefghi▲ │
  1602. │ ess me! ] ┬ │
  1603. │ │ │
  1604. │ ┴ │
  1605. │ ░ │
  1606. │ ░ │
  1607. │ ░ │
  1608. │ ░ │
  1609. │ ▼ │
  1610. │ ◄░├────┤░░░░░► │
  1611. │ │
  1612. └──────────────────┘
  1613. ";
  1614. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1615. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1616. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1617. Application.Top.Redraw (Application.Top.Bounds);
  1618. expected = @"
  1619. ┌┤Test├────────────┐
  1620. │ │
  1621. │ 6789abcdefghij▲ │
  1622. │ ss me! ] ┬ │
  1623. │ │ │
  1624. │ ┴ │
  1625. │ ░ │
  1626. │ ░ │
  1627. │ ░ │
  1628. │ ░ │
  1629. │ ▼ │
  1630. │ ◄░├────┤░░░░░► │
  1631. │ │
  1632. └──────────────────┘
  1633. ";
  1634. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1635. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1636. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1637. Application.Top.Redraw (Application.Top.Bounds);
  1638. expected = @"
  1639. ┌┤Test├────────────┐
  1640. │ │
  1641. │ 789abcdefghij ▲ │
  1642. │ s me! ] ┬ │
  1643. │ │ │
  1644. │ ┴ │
  1645. │ ░ │
  1646. │ ░ │
  1647. │ ░ │
  1648. │ ░ │
  1649. │ ▼ │
  1650. │ ◄░░├───┤░░░░░► │
  1651. │ │
  1652. └──────────────────┘
  1653. ";
  1654. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1655. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1656. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1657. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1658. Application.Top.Redraw (Application.Top.Bounds);
  1659. expected = @"
  1660. ┌┤Test├────────────┐
  1661. │ │
  1662. │ 1[ Press me! ]▲ │
  1663. │ 2 ┬ │
  1664. │ 3 │ │
  1665. │ 4 ┴ │
  1666. │ 5 ░ │
  1667. │ 6 ░ │
  1668. │ 7 ░ │
  1669. │ 8 ░ │
  1670. │ 9 ▼ │
  1671. │ ◄├───┤░░░░░░░► │
  1672. │ │
  1673. └──────────────────┘
  1674. ";
  1675. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1676. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1677. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1678. Application.Top.Redraw (Application.Top.Bounds);
  1679. expected = @"
  1680. ┌┤Test├────────────┐
  1681. │ │
  1682. │ 2 ▲ │
  1683. │ 3 ┬ │
  1684. │ 4 │ │
  1685. │ 5 ┴ │
  1686. │ 6 ░ │
  1687. │ 7 ░ │
  1688. │ 8 ░ │
  1689. │ 9 ░ │
  1690. │ 0 ▼ │
  1691. │ ◄├───┤░░░░░░░► │
  1692. │ │
  1693. └──────────────────┘
  1694. ";
  1695. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1696. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1697. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1698. Application.Top.Redraw (Application.Top.Bounds);
  1699. expected = @"
  1700. ┌┤Test├────────────┐
  1701. │ │
  1702. │ 3 ▲ │
  1703. │ 4 ┬ │
  1704. │ 5 │ │
  1705. │ 6 ┴ │
  1706. │ 7 ░ │
  1707. │ 8 ░ │
  1708. │ 9 ░ │
  1709. │ 0 ░ │
  1710. │ ▼ │
  1711. │ ◄├───┤░░░░░░░► │
  1712. │ │
  1713. └──────────────────┘
  1714. ";
  1715. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1716. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1717. }
  1718. [Fact, AutoInitShutdown]
  1719. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1720. {
  1721. var view = new View () {
  1722. Width = Dim.Fill (),
  1723. Height = Dim.Fill ()
  1724. };
  1725. view.DrawContent += (s, e) => {
  1726. view.DrawFrame (view.Bounds);
  1727. var savedClip = Application.Driver.Clip;
  1728. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1729. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1730. Application.Driver.Move (1, row + 1);
  1731. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1732. Application.Driver.AddStr ($"{col}");
  1733. }
  1734. }
  1735. Application.Driver.Clip = savedClip;
  1736. };
  1737. Application.Top.Add (view);
  1738. Application.Begin (Application.Top);
  1739. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1740. var expected = @"
  1741. ┌──────────────────┐
  1742. │012345678910111213│
  1743. │012345678910111213│
  1744. │012345678910111213│
  1745. │012345678910111213│
  1746. │012345678910111213│
  1747. │012345678910111213│
  1748. │012345678910111213│
  1749. │012345678910111213│
  1750. └──────────────────┘
  1751. ";
  1752. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1753. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1754. view.Clear ();
  1755. expected = @"
  1756. ";
  1757. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1758. Assert.Equal (Rect.Empty, pos);
  1759. }
  1760. [Fact, AutoInitShutdown]
  1761. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1762. {
  1763. var view = new View () {
  1764. Width = Dim.Fill (),
  1765. Height = Dim.Fill ()
  1766. };
  1767. view.DrawContent += (s, e) => {
  1768. view.DrawFrame (view.Bounds);
  1769. var savedClip = Application.Driver.Clip;
  1770. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1771. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1772. Application.Driver.Move (1, row + 1);
  1773. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1774. Application.Driver.AddStr ($"{col}");
  1775. }
  1776. }
  1777. Application.Driver.Clip = savedClip;
  1778. };
  1779. Application.Top.Add (view);
  1780. Application.Begin (Application.Top);
  1781. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1782. var expected = @"
  1783. ┌──────────────────┐
  1784. │012345678910111213│
  1785. │012345678910111213│
  1786. │012345678910111213│
  1787. │012345678910111213│
  1788. │012345678910111213│
  1789. │012345678910111213│
  1790. │012345678910111213│
  1791. │012345678910111213│
  1792. └──────────────────┘
  1793. ";
  1794. var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1795. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1796. view.Clear (view.Bounds);
  1797. expected = @"
  1798. ";
  1799. pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  1800. Assert.Equal (Rect.Empty, pos);
  1801. }
  1802. [Fact, AutoInitShutdown]
  1803. public void GetTextFormatterBoundsSize_GetSizeNeededForText_HotKeySpecifier ()
  1804. {
  1805. var text = "Say Hello 你";
  1806. var horizontalView = new View () {
  1807. Text = text,
  1808. AutoSize = true,
  1809. HotKeySpecifier = '_' };
  1810. var verticalView = new View () {
  1811. Text = text,
  1812. AutoSize = true,
  1813. HotKeySpecifier = '_',
  1814. TextDirection = TextDirection.TopBottom_LeftRight
  1815. };
  1816. Application.Top.Add (horizontalView, verticalView);
  1817. Application.Begin (Application.Top);
  1818. ((FakeDriver)Application.Driver).SetBufferSize (50, 50);
  1819. Assert.True (horizontalView.AutoSize);
  1820. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1821. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1822. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  1823. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  1824. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1825. Assert.True (verticalView.AutoSize);
  1826. // BUGBUG: v2 - Autosize is broken; disabling this test
  1827. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1828. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  1829. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextAndHotKey ());
  1830. Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  1831. Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  1832. text = "Say He_llo 你";
  1833. horizontalView.Text = text;
  1834. verticalView.Text = text;
  1835. Assert.True (horizontalView.AutoSize);
  1836. Assert.Equal (new Rect (0, 0, 12, 1), horizontalView.Frame);
  1837. Assert.Equal (new Size (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1838. Assert.Equal (new Size (13, 1), horizontalView.GetSizeNeededForTextAndHotKey ());
  1839. Assert.Equal (horizontalView.TextFormatter.Size, horizontalView.GetSizeNeededForTextAndHotKey ());
  1840. Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
  1841. Assert.True (verticalView.AutoSize);
  1842. // BUGBUG: v2 - Autosize is broken; disabling this test
  1843. //Assert.Equal (new Rect (0, 0, 2, 11), verticalView.Frame);
  1844. //Assert.Equal (new Size (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
  1845. //Assert.Equal (new Size (2, 12), verticalView.GetSizeNeededForTextAndHotKey ());
  1846. //Assert.Equal (verticalView.TextFormatter.Size, verticalView.GetSizeNeededForTextAndHotKey ());
  1847. //Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
  1848. }
  1849. [Fact]
  1850. public void IsAdded_Added_Removed ()
  1851. {
  1852. var top = new Toplevel ();
  1853. var view = new View ();
  1854. Assert.False (view.IsAdded);
  1855. top.Add (view);
  1856. Assert.True (view.IsAdded);
  1857. top.Remove (view);
  1858. Assert.False (view.IsAdded);
  1859. }
  1860. [Fact, AutoInitShutdown]
  1861. public void Visible_Clear_The_View_Output ()
  1862. {
  1863. var label = new Label ("Testing visibility.");
  1864. var win = new Window ();
  1865. win.Add (label);
  1866. var top = Application.Top;
  1867. top.Add (win);
  1868. Application.Begin (top);
  1869. Assert.True (label.Visible);
  1870. ((FakeDriver)Application.Driver).SetBufferSize (30, 5);
  1871. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1872. ┌────────────────────────────┐
  1873. │Testing visibility. │
  1874. │ │
  1875. │ │
  1876. └────────────────────────────┘
  1877. ", output);
  1878. label.Visible = false;
  1879. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1880. ┌────────────────────────────┐
  1881. │ │
  1882. │ │
  1883. │ │
  1884. └────────────────────────────┘
  1885. ", output);
  1886. }
  1887. [Fact, AutoInitShutdown]
  1888. public void ClearOnVisibleFalse_Gets_Sets ()
  1889. {
  1890. // BUGBUG: v2 - scrollview is broken. Disabling test for now
  1891. return;
  1892. 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";
  1893. var label = new Label (text);
  1894. Application.Top.Add (label);
  1895. var sbv = new ScrollBarView (label, true, false) {
  1896. Size = 100,
  1897. ClearOnVisibleFalse = false
  1898. };
  1899. Application.Begin (Application.Top);
  1900. Assert.True (sbv.Visible);
  1901. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1902. This is a tes▲
  1903. This is a tes┬
  1904. This is a tes┴
  1905. This is a tes░
  1906. This is a tes░
  1907. This is a tes▼
  1908. ", output);
  1909. sbv.Visible = false;
  1910. Assert.False (sbv.Visible);
  1911. Application.Top.Redraw (Application.Top.Bounds);
  1912. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1913. This is a test
  1914. This is a test
  1915. This is a test
  1916. This is a test
  1917. This is a test
  1918. This is a test
  1919. ", output);
  1920. sbv.Visible = true;
  1921. Assert.True (sbv.Visible);
  1922. Application.Top.Redraw (Application.Top.Bounds);
  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.ClearOnVisibleFalse = true;
  1932. sbv.Visible = false;
  1933. Assert.False (sbv.Visible);
  1934. TestHelpers.AssertDriverContentsWithFrameAre (@"
  1935. This is a tes
  1936. This is a tes
  1937. This is a tes
  1938. This is a tes
  1939. This is a tes
  1940. This is a tes
  1941. ", output);
  1942. }
  1943. [Fact, AutoInitShutdown]
  1944. public void DrawContentComplete_Event_Is_Always_Called ()
  1945. {
  1946. var viewCalled = false;
  1947. var tvCalled = false;
  1948. var view = new View ("View") { Width = 10, Height = 10 };
  1949. view.DrawContentComplete += (s, e) => viewCalled = true;
  1950. var tv = new TextView () { Y = 11, Width = 10, Height = 10 };
  1951. tv.DrawContentComplete += (s, e) => tvCalled = true;
  1952. Application.Top.Add (view, tv);
  1953. Application.Begin (Application.Top);
  1954. Assert.True (viewCalled);
  1955. Assert.True (tvCalled);
  1956. }
  1957. [Fact, AutoInitShutdown]
  1958. public void KeyDown_And_KeyUp_Events_Must_Called_Before_OnKeyDown_And_OnKeyUp ()
  1959. {
  1960. var keyDown = false;
  1961. var keyPress = false;
  1962. var keyUp = false;
  1963. var view = new DerivedView ();
  1964. view.KeyDown += (s, e) => {
  1965. Assert.Equal (Key.a, e.KeyEvent.Key);
  1966. Assert.False (keyDown);
  1967. Assert.False (view.IsKeyDown);
  1968. e.Handled = true;
  1969. keyDown = true;
  1970. };
  1971. view.KeyPress += (s, e) => {
  1972. Assert.Equal (Key.a, e.KeyEvent.Key);
  1973. Assert.False (keyPress);
  1974. Assert.False (view.IsKeyPress);
  1975. e.Handled = true;
  1976. keyPress = true;
  1977. };
  1978. view.KeyUp += (s, e) => {
  1979. Assert.Equal (Key.a, e.KeyEvent.Key);
  1980. Assert.False (keyUp);
  1981. Assert.False (view.IsKeyUp);
  1982. e.Handled = true;
  1983. keyUp = true;
  1984. };
  1985. Application.Top.Add (view);
  1986. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('a', ConsoleKey.A, false, false, false));
  1987. Application.Iteration += () => Application.RequestStop ();
  1988. Assert.True (view.CanFocus);
  1989. Application.Run ();
  1990. Application.Shutdown ();
  1991. Assert.True (keyDown);
  1992. Assert.True (keyPress);
  1993. Assert.True (keyUp);
  1994. Assert.False (view.IsKeyDown);
  1995. Assert.False (view.IsKeyPress);
  1996. Assert.False (view.IsKeyUp);
  1997. }
  1998. public class DerivedView : View {
  1999. public DerivedView ()
  2000. {
  2001. CanFocus = true;
  2002. }
  2003. public bool IsKeyDown { get; set; }
  2004. public bool IsKeyPress { get; set; }
  2005. public bool IsKeyUp { get; set; }
  2006. public override ustring Text { get; set; }
  2007. public override bool OnKeyDown (KeyEvent keyEvent)
  2008. {
  2009. IsKeyDown = true;
  2010. return true;
  2011. }
  2012. public override bool ProcessKey (KeyEvent keyEvent)
  2013. {
  2014. IsKeyPress = true;
  2015. return true;
  2016. }
  2017. public override bool OnKeyUp (KeyEvent keyEvent)
  2018. {
  2019. IsKeyUp = true;
  2020. return true;
  2021. }
  2022. public override void Redraw (Rect bounds)
  2023. {
  2024. var idx = 0;
  2025. // BUGBUG: v2 - this should use Boudns, not Frame
  2026. for (int r = 0; r < Frame.Height; r++) {
  2027. for (int c = 0; c < Frame.Width; c++) {
  2028. if (idx < Text.Length) {
  2029. var rune = Text [idx];
  2030. if (rune != '\n') {
  2031. AddRune (c, r, Text [idx]);
  2032. }
  2033. idx++;
  2034. if (rune == '\n') {
  2035. break;
  2036. }
  2037. }
  2038. }
  2039. }
  2040. ClearLayoutNeeded ();
  2041. ClearNeedsDisplay ();
  2042. }
  2043. }
  2044. [Theory, AutoInitShutdown]
  2045. [InlineData (true, false, false)]
  2046. [InlineData (true, true, false)]
  2047. [InlineData (true, true, true)]
  2048. public void KeyDown_And_KeyUp_Events_With_Only_Key_Modifiers (bool shift, bool alt, bool control)
  2049. {
  2050. var keyDown = false;
  2051. var keyPress = false;
  2052. var keyUp = false;
  2053. var view = new DerivedView ();
  2054. view.KeyDown += (s, e) => {
  2055. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2056. Assert.Equal (shift, e.KeyEvent.IsShift);
  2057. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2058. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2059. Assert.False (keyDown);
  2060. Assert.False (view.IsKeyDown);
  2061. keyDown = true;
  2062. };
  2063. view.KeyPress += (s, e) => {
  2064. keyPress = true;
  2065. };
  2066. view.KeyUp += (s, e) => {
  2067. Assert.Equal (-1, e.KeyEvent.KeyValue);
  2068. Assert.Equal (shift, e.KeyEvent.IsShift);
  2069. Assert.Equal (alt, e.KeyEvent.IsAlt);
  2070. Assert.Equal (control, e.KeyEvent.IsCtrl);
  2071. Assert.False (keyUp);
  2072. Assert.False (view.IsKeyUp);
  2073. keyUp = true;
  2074. };
  2075. Application.Top.Add (view);
  2076. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\0', (ConsoleKey)'\0', shift, alt, control));
  2077. Application.Iteration += () => Application.RequestStop ();
  2078. Assert.True (view.CanFocus);
  2079. Application.Run ();
  2080. Application.Shutdown ();
  2081. Assert.True (keyDown);
  2082. Assert.False (keyPress);
  2083. Assert.True (keyUp);
  2084. Assert.True (view.IsKeyDown);
  2085. Assert.False (view.IsKeyPress);
  2086. Assert.True (view.IsKeyUp);
  2087. }
  2088. [Fact, AutoInitShutdown]
  2089. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  2090. {
  2091. var view1Leave = false;
  2092. var subView1Leave = false;
  2093. var subView1subView1Leave = false;
  2094. var top = Application.Top;
  2095. var view1 = new View { CanFocus = true };
  2096. var subView1 = new View { CanFocus = true };
  2097. var subView1subView1 = new View { CanFocus = true };
  2098. view1.Leave += (s, e) => {
  2099. view1Leave = true;
  2100. };
  2101. subView1.Leave += (s, e) => {
  2102. subView1.Remove (subView1subView1);
  2103. subView1Leave = true;
  2104. };
  2105. view1.Add (subView1);
  2106. subView1subView1.Leave += (s, e) => {
  2107. // This is never invoked
  2108. subView1subView1Leave = true;
  2109. };
  2110. subView1.Add (subView1subView1);
  2111. var view2 = new View { CanFocus = true };
  2112. top.Add (view1, view2);
  2113. Application.Begin (top);
  2114. view2.SetFocus ();
  2115. Assert.True (view1Leave);
  2116. Assert.True (subView1Leave);
  2117. Assert.False (subView1subView1Leave);
  2118. }
  2119. [Fact, AutoInitShutdown]
  2120. public void GetNormalColor_ColorScheme ()
  2121. {
  2122. var view = new View { ColorScheme = Colors.Base };
  2123. Assert.Equal (view.ColorScheme.Normal, view.GetNormalColor ());
  2124. view.Enabled = false;
  2125. Assert.Equal (view.ColorScheme.Disabled, view.GetNormalColor ());
  2126. }
  2127. [Fact, AutoInitShutdown]
  2128. public void GetHotNormalColor_ColorScheme ()
  2129. {
  2130. var view = new View { ColorScheme = Colors.Base };
  2131. Assert.Equal (view.ColorScheme.HotNormal, view.GetHotNormalColor ());
  2132. view.Enabled = false;
  2133. Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ());
  2134. }
  2135. [Theory, AutoInitShutdown]
  2136. [InlineData (true)]
  2137. [InlineData (false)]
  2138. public void Clear_Does_Not_Spillover_Its_Parent (bool label)
  2139. {
  2140. var root = new View () { Width = 20, Height = 10, ColorScheme = Colors.Base };
  2141. var v = label == true ?
  2142. new Label (new string ('c', 100)) {
  2143. Width = Dim.Fill (),
  2144. } :
  2145. (View)new TextView () {
  2146. Height = 1,
  2147. Text = new string ('c', 100),
  2148. Width = Dim.Fill ()
  2149. };
  2150. root.Add (v);
  2151. Application.Top.Add (root);
  2152. Application.Begin (Application.Top);
  2153. if (label) {
  2154. Assert.True (v.AutoSize);
  2155. Assert.False (v.CanFocus);
  2156. Assert.Equal (new Rect (0, 0, 100, 1), v.Frame);
  2157. } else {
  2158. Assert.False (v.AutoSize);
  2159. Assert.True (v.CanFocus);
  2160. Assert.Equal (new Rect (0, 0, 20, 1), v.Frame);
  2161. }
  2162. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2163. cccccccccccccccccccc", output);
  2164. var attributes = new Attribute [] {
  2165. Colors.TopLevel.Normal,
  2166. Colors.Base.Normal,
  2167. Colors.Base.Focus
  2168. };
  2169. if (label) {
  2170. TestHelpers.AssertDriverColorsAre (@"
  2171. 111111111111111111110
  2172. 111111111111111111110", attributes);
  2173. } else {
  2174. TestHelpers.AssertDriverColorsAre (@"
  2175. 222222222222222222220
  2176. 111111111111111111110", attributes);
  2177. }
  2178. if (label) {
  2179. root.CanFocus = true;
  2180. v.CanFocus = true;
  2181. Assert.False (v.HasFocus);
  2182. v.SetFocus ();
  2183. Assert.True (v.HasFocus);
  2184. Application.Refresh ();
  2185. TestHelpers.AssertDriverColorsAre (@"
  2186. 222222222222222222220
  2187. 111111111111111111110", attributes);
  2188. }
  2189. }
  2190. [Fact, AutoInitShutdown]
  2191. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2192. {
  2193. var label = new Label ("At 0,0");
  2194. var view = new DerivedView () {
  2195. X = 2,
  2196. Y = 2,
  2197. Width = 30,
  2198. Height = 2,
  2199. Text = "A text with some long width\n and also with two lines."
  2200. };
  2201. var top = Application.Top;
  2202. top.Add (label, view);
  2203. Application.Begin (top);
  2204. top.Redraw (top.Bounds);
  2205. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2206. At 0,0
  2207. A text with some long width
  2208. and also with two lines. ", output);
  2209. view.Frame = new Rect (1, 1, 10, 1);
  2210. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2211. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2212. view.LayoutStyle = LayoutStyle.Absolute;
  2213. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2214. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2215. top.Redraw (top.Bounds);
  2216. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2217. At 0,0
  2218. A text wit", output);
  2219. }
  2220. [Fact, AutoInitShutdown]
  2221. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2222. {
  2223. var label = new Label ("At 0,0");
  2224. var view = new DerivedView () {
  2225. X = 2,
  2226. Y = 2,
  2227. Width = 30,
  2228. Height = 2,
  2229. Text = "A text with some long width\n and also with two lines."
  2230. };
  2231. var top = Application.Top;
  2232. top.Add (label, view);
  2233. Application.Begin (top);
  2234. top.Redraw (top.Bounds);
  2235. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2236. At 0,0
  2237. A text with some long width
  2238. and also with two lines. ", output);
  2239. view.X = 1;
  2240. view.Y = 1;
  2241. view.Width = 10;
  2242. view.Height = 1;
  2243. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2244. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2245. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2246. top.Redraw (top.Bounds);
  2247. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2248. At 0,0
  2249. A text wit", output);
  2250. }
  2251. [Fact, AutoInitShutdown]
  2252. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Frame ()
  2253. {
  2254. var label = new Label ("At 0,0");
  2255. var view = new DerivedView () {
  2256. X = 2,
  2257. Y = 2,
  2258. Width = 30,
  2259. Height = 2,
  2260. Text = "A text with some long width\n and also with two lines."
  2261. };
  2262. var top = Application.Top;
  2263. top.Add (label, view);
  2264. Application.Begin (top);
  2265. view.Redraw (view.Bounds);
  2266. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2267. At 0,0
  2268. A text with some long width
  2269. and also with two lines. ", output);
  2270. view.Frame = new Rect (1, 1, 10, 1);
  2271. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2272. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2273. view.LayoutStyle = LayoutStyle.Absolute;
  2274. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2275. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2276. view.Redraw (view.Bounds);
  2277. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2278. At 0,0
  2279. A text wit
  2280. A text with some long width
  2281. and also with two lines. ", output);
  2282. }
  2283. [Fact, AutoInitShutdown]
  2284. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Up_Left_Using_Pos_Dim ()
  2285. {
  2286. var label = new Label ("At 0,0");
  2287. var view = new DerivedView () {
  2288. X = 2,
  2289. Y = 2,
  2290. Width = 30,
  2291. Height = 2,
  2292. Text = "A text with some long width\n and also with two lines."
  2293. };
  2294. var top = Application.Top;
  2295. top.Add (label, view);
  2296. Application.Begin (top);
  2297. view.Redraw (view.Bounds);
  2298. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2299. At 0,0
  2300. A text with some long width
  2301. and also with two lines. ", output);
  2302. view.X = 1;
  2303. view.Y = 1;
  2304. view.Width = 10;
  2305. view.Height = 1;
  2306. Assert.Equal (new Rect (1, 1, 10, 1), view.Frame);
  2307. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2308. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2309. view.Redraw (view.Bounds);
  2310. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2311. At 0,0
  2312. A text wit
  2313. A text with some long width
  2314. and also with two lines. ", output);
  2315. }
  2316. [Fact, AutoInitShutdown]
  2317. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2318. {
  2319. var label = new Label ("At 0,0");
  2320. var view = new DerivedView () {
  2321. X = 2,
  2322. Y = 2,
  2323. Width = 30,
  2324. Height = 2,
  2325. Text = "A text with some long width\n and also with two lines."
  2326. };
  2327. var top = Application.Top;
  2328. top.Add (label, view);
  2329. Application.Begin (top);
  2330. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2331. At 0,0
  2332. A text with some long width
  2333. and also with two lines. ", output);
  2334. view.Frame = new Rect (3, 3, 10, 1);
  2335. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2336. Assert.Equal (LayoutStyle.Computed, view.LayoutStyle);
  2337. view.LayoutStyle = LayoutStyle.Absolute;
  2338. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2339. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2340. top.Redraw (top.Bounds);
  2341. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2342. At 0,0
  2343. A text wit", output);
  2344. }
  2345. [Fact, AutoInitShutdown]
  2346. public void Correct_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2347. {
  2348. var label = new Label ("At 0,0");
  2349. var view = new DerivedView () {
  2350. X = 2,
  2351. Y = 2,
  2352. Width = 30,
  2353. Height = 2,
  2354. Text = "A text with some long width\n and also with two lines."
  2355. };
  2356. var top = Application.Top;
  2357. top.Add (label, view);
  2358. Application.Begin (top);
  2359. top.Redraw (top.Bounds);
  2360. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2361. At 0,0
  2362. A text with some long width
  2363. and also with two lines. ", output);
  2364. view.X = 3;
  2365. view.Y = 3;
  2366. view.Width = 10;
  2367. view.Height = 1;
  2368. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2369. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2370. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2371. top.Redraw (top.Bounds);
  2372. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2373. At 0,0
  2374. A text wit", output);
  2375. }
  2376. [Fact, AutoInitShutdown]
  2377. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Frame ()
  2378. {
  2379. var label = new Label ("At 0,0");
  2380. var view = new DerivedView () {
  2381. X = 2,
  2382. Y = 2,
  2383. Width = 30,
  2384. Height = 2,
  2385. Text = "A text with some long width\n and also with two lines."
  2386. };
  2387. var top = Application.Top;
  2388. top.Add (label, view);
  2389. Application.Begin (top);
  2390. view.Redraw (view.Bounds);
  2391. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2392. At 0,0
  2393. A text with some long width
  2394. and also with two lines. ", output);
  2395. view.Frame = new Rect (3, 3, 10, 1);
  2396. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2397. Assert.Equal (new Rect (0, 0, 10, 1), view._needsDisplay);
  2398. view.Redraw (view.Bounds);
  2399. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2400. At 0,0
  2401. A text with some long width
  2402. A text witith two lines. ", output);
  2403. }
  2404. [Fact, AutoInitShutdown]
  2405. public void Incorrect_Redraw_Bounds_NeedDisplay_On_Shrink_And_Move_Down_Right_Using_Pos_Dim ()
  2406. {
  2407. var label = new Label ("At 0,0");
  2408. var view = new DerivedView () {
  2409. X = 2,
  2410. Y = 2,
  2411. Width = 30,
  2412. Height = 2,
  2413. Text = "A text with some long width\n and also with two lines."
  2414. };
  2415. var top = Application.Top;
  2416. top.Add (label, view);
  2417. Application.Begin (top);
  2418. view.Redraw (view.Bounds);
  2419. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2420. At 0,0
  2421. A text with some long width
  2422. and also with two lines. ", output);
  2423. view.X = 3;
  2424. view.Y = 3;
  2425. view.Width = 10;
  2426. view.Height = 1;
  2427. Assert.Equal (new Rect (3, 3, 10, 1), view.Frame);
  2428. Assert.Equal (new Rect (0, 0, 10, 1), view.Bounds);
  2429. Assert.Equal (new Rect (0, 0, 30, 2), view._needsDisplay);
  2430. view.Redraw (view.Bounds);
  2431. TestHelpers.AssertDriverContentsWithFrameAre (@"
  2432. At 0,0
  2433. A text with some long width
  2434. A text witith two lines. ", output);
  2435. }
  2436. [Fact, AutoInitShutdown]
  2437. public void Test_Nested_Views_With_Height_Equal_To_One ()
  2438. {
  2439. var v = new View () { Width = 11, Height = 3, ColorScheme = new ColorScheme () };
  2440. var top = new View () { Width = Dim.Fill (), Height = 1 };
  2441. var bottom = new View () { Width = Dim.Fill (), Height = 1, Y = 2 };
  2442. top.Add (new Label ("111"));
  2443. v.Add (top);
  2444. v.Add (new LineView (Orientation.Horizontal) { Y = 1 });
  2445. bottom.Add (new Label ("222"));
  2446. v.Add (bottom);
  2447. v.LayoutSubviews ();
  2448. v.Redraw (v.Bounds);
  2449. string looksLike =
  2450. @"
  2451. 111
  2452. ───────────
  2453. 222";
  2454. TestHelpers.AssertDriverContentsAre (looksLike, output);
  2455. }
  2456. [Fact]
  2457. [AutoInitShutdown]
  2458. public void Frame_Set_After_Initialze_Update_NeededDisplay ()
  2459. {
  2460. var frame = new FrameView ();
  2461. var label = new Label ("This should be the first line.") {
  2462. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  2463. ColorScheme = Colors.Menu,
  2464. Width = Dim.Fill (),
  2465. X = Pos.Center (),
  2466. Y = Pos.Center () - 2 // center minus 2 minus two lines top and bottom borders equal to zero (4-2-2=0)
  2467. };
  2468. var button = new Button ("Press me!") {
  2469. X = Pos.Center (),
  2470. Y = Pos.Center ()
  2471. };
  2472. frame.Add (label, button);
  2473. frame.X = Pos.Center ();
  2474. frame.Y = Pos.Center ();
  2475. frame.Width = 40;
  2476. frame.Height = 8;
  2477. var top = Application.Top;
  2478. top.Add (frame);
  2479. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2480. Assert.Equal (new Rect (0, 0, 40, 8), frame.Frame);
  2481. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2482. frame.Frame.Left, frame.Frame.Top,
  2483. frame.Frame.Right, frame.Frame.Bottom));
  2484. Assert.Equal (new Rect (0, 0, 30, 1), label.Frame);
  2485. Assert.Equal (new Rect (0, 0, 13, 1), button.Frame);
  2486. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  2487. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  2488. Assert.Equal (new Rect (0, 0, 40, 8), new Rect (
  2489. frame._needsDisplay.Left, frame._needsDisplay.Top,
  2490. frame._needsDisplay.Right, frame._needsDisplay.Bottom));
  2491. Assert.Equal (new Rect (0, 0, 30, 1), label._needsDisplay);
  2492. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  2493. top.LayoutComplete += (s, e) => {
  2494. Assert.Equal (new Rect (0, 0, 80, 25), top._needsDisplay);
  2495. };
  2496. frame.LayoutComplete += (s, e) => {
  2497. Assert.Equal (new Rect (0, 0, 40, 8), frame._needsDisplay);
  2498. };
  2499. label.LayoutComplete += (s, e) => {
  2500. Assert.Equal (new Rect (0, 0, 38, 1), label._needsDisplay);
  2501. };
  2502. button.LayoutComplete += (s, e) => {
  2503. Assert.Equal (new Rect (0, 0, 13, 1), button._needsDisplay);
  2504. };
  2505. Application.Begin (top);
  2506. Assert.True (label.AutoSize);
  2507. Assert.Equal (new Rect (0, 0, 80, 25), top.Frame);
  2508. Assert.Equal (new Rect (20, 8, 40, 8), frame.Frame);
  2509. Assert.Equal (new Rect (20, 8, 60, 16), new Rect (
  2510. frame.Frame.Left, frame.Frame.Top,
  2511. frame.Frame.Right, frame.Frame.Bottom));
  2512. Assert.Equal (new Rect (0, 0, 38, 1), label.Frame);
  2513. Assert.Equal (new Rect (12, 2, 13, 1), button.Frame);
  2514. var expected = @"
  2515. ┌──────────────────────────────────────┐
  2516. │ This should be the first line. │
  2517. │ │
  2518. │ [ Press me! ] │
  2519. │ │
  2520. │ │
  2521. │ │
  2522. └──────────────────────────────────────┘
  2523. ";
  2524. TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
  2525. }
  2526. [Fact]
  2527. public void Set_Title_Fires_TitleChanging ()
  2528. {
  2529. var r = new View ();
  2530. Assert.Equal (ustring.Empty, r.Title);
  2531. string expectedOld = null;
  2532. string expectedDuring = null;
  2533. string expectedAfter = null;
  2534. bool cancel = false;
  2535. r.TitleChanging += (s, args) => {
  2536. Assert.Equal (expectedOld, args.OldTitle);
  2537. Assert.Equal (expectedDuring, args.NewTitle);
  2538. args.Cancel = cancel;
  2539. };
  2540. expectedOld = string.Empty;
  2541. r.Title = expectedDuring = expectedAfter = "title";
  2542. Assert.Equal (expectedAfter, r.Title.ToString ());
  2543. expectedOld = r.Title.ToString ();
  2544. r.Title = expectedDuring = expectedAfter = "a different title";
  2545. Assert.Equal (expectedAfter, r.Title.ToString ());
  2546. // Now setup cancelling the change and change it back to "title"
  2547. cancel = true;
  2548. expectedOld = r.Title.ToString ();
  2549. r.Title = expectedDuring = "title";
  2550. Assert.Equal (expectedAfter, r.Title.ToString ());
  2551. r.Dispose ();
  2552. }
  2553. [Fact]
  2554. public void Set_Title_Fires_TitleChanged ()
  2555. {
  2556. var r = new View ();
  2557. Assert.Equal (ustring.Empty, r.Title);
  2558. string expectedOld = null;
  2559. string expected = null;
  2560. r.TitleChanged += (s, args) => {
  2561. Assert.Equal (expectedOld, args.OldTitle);
  2562. Assert.Equal (r.Title, args.NewTitle);
  2563. };
  2564. expected = "title";
  2565. expectedOld = r.Title.ToString ();
  2566. r.Title = expected;
  2567. Assert.Equal (expected, r.Title.ToString ());
  2568. expected = "another title";
  2569. expectedOld = r.Title.ToString ();
  2570. r.Title = expected;
  2571. Assert.Equal (expected, r.Title.ToString ());
  2572. r.Dispose ();
  2573. }
  2574. }
  2575. }