ViewTests.cs 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  1. using System;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
  5. // Alias Console to MockConsole so we don't accidentally use Console
  6. using Console = Terminal.Gui.FakeConsole;
  7. namespace Terminal.Gui.Core {
  8. public class ViewTests {
  9. readonly ITestOutputHelper output;
  10. public ViewTests (ITestOutputHelper output)
  11. {
  12. this.output = output;
  13. }
  14. [Fact]
  15. public void New_Initializes ()
  16. {
  17. // Parameterless
  18. var r = new View ();
  19. Assert.NotNull (r);
  20. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  21. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  22. Assert.False (r.CanFocus);
  23. Assert.False (r.HasFocus);
  24. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  25. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  26. Assert.Null (r.Focused);
  27. Assert.Null (r.ColorScheme);
  28. Assert.Equal (Dim.Sized (0), r.Width);
  29. Assert.Equal (Dim.Sized (0), r.Height);
  30. // FIXED: Pos needs equality implemented
  31. Assert.Equal (Pos.At (0), r.X);
  32. Assert.Equal (Pos.At (0), r.Y);
  33. Assert.False (r.IsCurrentTop);
  34. Assert.Empty (r.Id);
  35. Assert.Empty (r.Subviews);
  36. Assert.False (r.WantContinuousButtonPressed);
  37. Assert.False (r.WantMousePositionReports);
  38. Assert.Null (r.SuperView);
  39. Assert.Null (r.MostFocused);
  40. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  41. // Empty Rect
  42. r = new View (Rect.Empty);
  43. Assert.NotNull (r);
  44. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  45. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  46. Assert.False (r.CanFocus);
  47. Assert.False (r.HasFocus);
  48. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  49. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  50. Assert.Null (r.Focused);
  51. Assert.Null (r.ColorScheme);
  52. Assert.NotNull (r.Width); // All view Dim are initialized now,
  53. Assert.NotNull (r.Height); // avoiding Dim errors.
  54. Assert.NotNull (r.X); // All view Pos are initialized now,
  55. Assert.NotNull (r.Y); // avoiding Pos errors.
  56. Assert.False (r.IsCurrentTop);
  57. Assert.Empty (r.Id);
  58. Assert.Empty (r.Subviews);
  59. Assert.False (r.WantContinuousButtonPressed);
  60. Assert.False (r.WantMousePositionReports);
  61. Assert.Null (r.SuperView);
  62. Assert.Null (r.MostFocused);
  63. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  64. // Rect with values
  65. r = new View (new Rect (1, 2, 3, 4));
  66. Assert.NotNull (r);
  67. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  68. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  69. Assert.False (r.CanFocus);
  70. Assert.False (r.HasFocus);
  71. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  72. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  73. Assert.Null (r.Focused);
  74. Assert.Null (r.ColorScheme);
  75. Assert.NotNull (r.Width);
  76. Assert.NotNull (r.Height);
  77. Assert.NotNull (r.X);
  78. Assert.NotNull (r.Y);
  79. Assert.False (r.IsCurrentTop);
  80. Assert.Empty (r.Id);
  81. Assert.Empty (r.Subviews);
  82. Assert.False (r.WantContinuousButtonPressed);
  83. Assert.False (r.WantMousePositionReports);
  84. Assert.Null (r.SuperView);
  85. Assert.Null (r.MostFocused);
  86. Assert.Equal (TextDirection.LeftRight_TopBottom, r.TextDirection);
  87. // Initializes a view with a vertical direction
  88. r = new View ("Vertical View", TextDirection.TopBottom_LeftRight);
  89. Assert.NotNull (r);
  90. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  91. Assert.Equal ("View()({X=0,Y=0,Width=1,Height=13})", r.ToString ());
  92. Assert.False (r.CanFocus);
  93. Assert.False (r.HasFocus);
  94. Assert.Equal (new Rect (0, 0, 1, 13), r.Bounds);
  95. Assert.Equal (new Rect (0, 0, 1, 13), r.Frame);
  96. Assert.Null (r.Focused);
  97. Assert.Null (r.ColorScheme);
  98. Assert.NotNull (r.Width); // All view Dim are initialized now,
  99. Assert.NotNull (r.Height); // avoiding Dim errors.
  100. Assert.NotNull (r.X); // All view Pos are initialized now,
  101. Assert.NotNull (r.Y); // avoiding Pos errors.
  102. Assert.False (r.IsCurrentTop);
  103. Assert.Empty (r.Id);
  104. Assert.Empty (r.Subviews);
  105. Assert.False (r.WantContinuousButtonPressed);
  106. Assert.False (r.WantMousePositionReports);
  107. Assert.Null (r.SuperView);
  108. Assert.Null (r.MostFocused);
  109. Assert.Equal (TextDirection.TopBottom_LeftRight, r.TextDirection);
  110. }
  111. [Fact]
  112. public void New_Methods_Return_False ()
  113. {
  114. var r = new View ();
  115. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  116. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  117. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  118. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  119. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  120. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  121. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  122. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  123. Assert.False (r.OnEnter (new View ()));
  124. Assert.False (r.OnLeave (new View ()));
  125. // TODO: Add more
  126. }
  127. [Fact]
  128. public void TopologicalSort_Missing_Add ()
  129. {
  130. var root = new View ();
  131. var sub1 = new View ();
  132. root.Add (sub1);
  133. var sub2 = new View ();
  134. sub1.Width = Dim.Width (sub2);
  135. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  136. sub2.Width = Dim.Width (sub1);
  137. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  138. }
  139. [Fact]
  140. public void TopologicalSort_Recursive_Ref ()
  141. {
  142. var root = new View ();
  143. var sub1 = new View ();
  144. root.Add (sub1);
  145. var sub2 = new View ();
  146. root.Add (sub2);
  147. sub2.Width = Dim.Width (sub2);
  148. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  149. }
  150. [Fact]
  151. public void Added_Removed ()
  152. {
  153. var v = new View (new Rect (0, 0, 10, 24));
  154. var t = new View ();
  155. v.Added += (View e) => {
  156. Assert.True (v.SuperView == e);
  157. };
  158. v.Removed += (View e) => {
  159. Assert.True (v.SuperView == null);
  160. };
  161. t.Add (v);
  162. Assert.True (t.Subviews.Count == 1);
  163. t.Remove (v);
  164. Assert.True (t.Subviews.Count == 0);
  165. }
  166. [Fact]
  167. public void Subviews_TabIndexes_AreEqual ()
  168. {
  169. var r = new View ();
  170. var v1 = new View () { CanFocus = true };
  171. var v2 = new View () { CanFocus = true };
  172. var v3 = new View () { CanFocus = true };
  173. r.Add (v1, v2, v3);
  174. Assert.True (r.Subviews.IndexOf (v1) == 0);
  175. Assert.True (r.Subviews.IndexOf (v2) == 1);
  176. Assert.True (r.Subviews.IndexOf (v3) == 2);
  177. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  178. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  179. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  180. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  181. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  182. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  183. }
  184. [Fact]
  185. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  186. {
  187. var r = new View ();
  188. var v1 = new View () { CanFocus = true };
  189. var v2 = new View () { CanFocus = true };
  190. var v3 = new View () { CanFocus = true };
  191. r.Add (v1, v2, v3);
  192. r.BringSubviewToFront (v1);
  193. Assert.True (r.Subviews.IndexOf (v1) == 2);
  194. Assert.True (r.Subviews.IndexOf (v2) == 0);
  195. Assert.True (r.Subviews.IndexOf (v3) == 1);
  196. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  197. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  198. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  199. }
  200. [Fact]
  201. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  202. {
  203. var r = new View ();
  204. var v1 = new View () { CanFocus = true };
  205. var v2 = new View () { CanFocus = true };
  206. var v3 = new View () { CanFocus = true };
  207. r.Add (v1, v2, v3);
  208. r.BringSubviewForward (v1);
  209. Assert.True (r.Subviews.IndexOf (v1) == 1);
  210. Assert.True (r.Subviews.IndexOf (v2) == 0);
  211. Assert.True (r.Subviews.IndexOf (v3) == 2);
  212. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  213. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  214. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  215. }
  216. [Fact]
  217. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  218. {
  219. var r = new View ();
  220. var v1 = new View () { CanFocus = true };
  221. var v2 = new View () { CanFocus = true };
  222. var v3 = new View () { CanFocus = true };
  223. r.Add (v1, v2, v3);
  224. r.SendSubviewToBack (v3);
  225. Assert.True (r.Subviews.IndexOf (v1) == 1);
  226. Assert.True (r.Subviews.IndexOf (v2) == 2);
  227. Assert.True (r.Subviews.IndexOf (v3) == 0);
  228. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  229. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  230. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  231. }
  232. [Fact]
  233. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  234. {
  235. var r = new View ();
  236. var v1 = new View () { CanFocus = true };
  237. var v2 = new View () { CanFocus = true };
  238. var v3 = new View () { CanFocus = true };
  239. r.Add (v1, v2, v3);
  240. r.SendSubviewBackwards (v3);
  241. Assert.True (r.Subviews.IndexOf (v1) == 0);
  242. Assert.True (r.Subviews.IndexOf (v2) == 2);
  243. Assert.True (r.Subviews.IndexOf (v3) == 1);
  244. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  245. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  246. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  247. }
  248. [Fact]
  249. public void TabIndex_Set_CanFocus_ValidValues ()
  250. {
  251. var r = new View ();
  252. var v1 = new View () { CanFocus = true };
  253. var v2 = new View () { CanFocus = true };
  254. var v3 = new View () { CanFocus = true };
  255. r.Add (v1, v2, v3);
  256. v1.TabIndex = 1;
  257. Assert.True (r.Subviews.IndexOf (v1) == 0);
  258. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  259. v1.TabIndex = 2;
  260. Assert.True (r.Subviews.IndexOf (v1) == 0);
  261. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  262. }
  263. [Fact]
  264. public void TabIndex_Set_CanFocus_HigherValues ()
  265. {
  266. var r = new View ();
  267. var v1 = new View () { CanFocus = true };
  268. var v2 = new View () { CanFocus = true };
  269. var v3 = new View () { CanFocus = true };
  270. r.Add (v1, v2, v3);
  271. v1.TabIndex = 3;
  272. Assert.True (r.Subviews.IndexOf (v1) == 0);
  273. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  274. }
  275. [Fact]
  276. public void TabIndex_Set_CanFocus_LowerValues ()
  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. v1.TabIndex = -1;
  284. Assert.True (r.Subviews.IndexOf (v1) == 0);
  285. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  286. }
  287. [Fact]
  288. public void TabIndex_Set_CanFocus_False ()
  289. {
  290. var r = new View ();
  291. var v1 = new View () { CanFocus = true };
  292. var v2 = new View () { CanFocus = true };
  293. var v3 = new View () { CanFocus = true };
  294. r.Add (v1, v2, v3);
  295. v1.CanFocus = false;
  296. v1.TabIndex = 0;
  297. Assert.True (r.Subviews.IndexOf (v1) == 0);
  298. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  299. Assert.Equal (-1, v1.TabIndex);
  300. }
  301. [Fact]
  302. public void TabIndex_Set_CanFocus_False_To_True ()
  303. {
  304. var r = new View ();
  305. var v1 = new View ();
  306. var v2 = new View () { CanFocus = true };
  307. var v3 = new View () { CanFocus = true };
  308. r.Add (v1, v2, v3);
  309. v1.CanFocus = true;
  310. v1.TabIndex = 1;
  311. Assert.True (r.Subviews.IndexOf (v1) == 0);
  312. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  313. }
  314. [Fact]
  315. public void TabStop_And_CanFocus_Are_All_True ()
  316. {
  317. var r = new View ();
  318. var v1 = new View () { CanFocus = true };
  319. var v2 = new View () { CanFocus = true };
  320. var v3 = new View () { CanFocus = true };
  321. r.Add (v1, v2, v3);
  322. r.FocusNext ();
  323. Assert.True (v1.HasFocus);
  324. Assert.False (v2.HasFocus);
  325. Assert.False (v3.HasFocus);
  326. r.FocusNext ();
  327. Assert.False (v1.HasFocus);
  328. Assert.True (v2.HasFocus);
  329. Assert.False (v3.HasFocus);
  330. r.FocusNext ();
  331. Assert.False (v1.HasFocus);
  332. Assert.False (v2.HasFocus);
  333. Assert.True (v3.HasFocus);
  334. }
  335. [Fact]
  336. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  337. {
  338. var r = new View ();
  339. var v1 = new View ();
  340. var v2 = new View ();
  341. var v3 = new View ();
  342. r.Add (v1, v2, v3);
  343. r.FocusNext ();
  344. Assert.False (v1.HasFocus);
  345. Assert.False (v2.HasFocus);
  346. Assert.False (v3.HasFocus);
  347. r.FocusNext ();
  348. Assert.False (v1.HasFocus);
  349. Assert.False (v2.HasFocus);
  350. Assert.False (v3.HasFocus);
  351. r.FocusNext ();
  352. Assert.False (v1.HasFocus);
  353. Assert.False (v2.HasFocus);
  354. Assert.False (v3.HasFocus);
  355. }
  356. [Fact]
  357. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  358. {
  359. var r = new View ();
  360. var v1 = new View () { CanFocus = true, TabStop = false };
  361. var v2 = new View () { CanFocus = true, TabStop = false };
  362. var v3 = new View () { CanFocus = true, TabStop = false };
  363. r.Add (v1, v2, v3);
  364. r.FocusNext ();
  365. Assert.False (v1.HasFocus);
  366. Assert.False (v2.HasFocus);
  367. Assert.False (v3.HasFocus);
  368. r.FocusNext ();
  369. Assert.False (v1.HasFocus);
  370. Assert.False (v2.HasFocus);
  371. Assert.False (v3.HasFocus);
  372. r.FocusNext ();
  373. Assert.False (v1.HasFocus);
  374. Assert.False (v2.HasFocus);
  375. Assert.False (v3.HasFocus);
  376. }
  377. [Fact]
  378. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  379. {
  380. var r = new View ();
  381. var v1 = new View () { CanFocus = true, TabStop = false };
  382. var v2 = new View () { CanFocus = false, TabStop = true };
  383. var v3 = new View () { CanFocus = false, TabStop = false };
  384. r.Add (v1, v2, v3);
  385. r.FocusNext ();
  386. Assert.False (v1.HasFocus);
  387. Assert.False (v2.HasFocus);
  388. Assert.False (v3.HasFocus);
  389. r.FocusNext ();
  390. Assert.False (v1.HasFocus);
  391. Assert.False (v2.HasFocus);
  392. Assert.False (v3.HasFocus);
  393. r.FocusNext ();
  394. Assert.False (v1.HasFocus);
  395. Assert.False (v2.HasFocus);
  396. Assert.False (v3.HasFocus);
  397. }
  398. [Fact]
  399. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  400. {
  401. var r = new View ();
  402. var v1 = new View ();
  403. var v2 = new View ();
  404. var v3 = new View ();
  405. r.Add (v1, v2, v3);
  406. r.FocusNext ();
  407. Assert.False (v1.HasFocus);
  408. Assert.False (v2.HasFocus);
  409. Assert.False (v3.HasFocus);
  410. v1.CanFocus = true;
  411. r.FocusNext ();
  412. Assert.True (v1.HasFocus);
  413. Assert.False (v2.HasFocus);
  414. Assert.False (v3.HasFocus);
  415. v2.CanFocus = true;
  416. r.FocusNext ();
  417. Assert.False (v1.HasFocus);
  418. Assert.True (v2.HasFocus);
  419. Assert.False (v3.HasFocus);
  420. v3.CanFocus = true;
  421. r.FocusNext ();
  422. Assert.False (v1.HasFocus);
  423. Assert.False (v2.HasFocus);
  424. Assert.True (v3.HasFocus);
  425. }
  426. [Fact]
  427. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  428. {
  429. var r = new View ();
  430. var v1 = new View () { CanFocus = true, TabStop = false };
  431. var v2 = new View () { CanFocus = true, TabStop = false };
  432. var v3 = new View () { CanFocus = true, TabStop = false };
  433. r.Add (v1, v2, v3);
  434. r.FocusNext ();
  435. Assert.False (v1.HasFocus);
  436. Assert.False (v2.HasFocus);
  437. Assert.False (v3.HasFocus);
  438. v1.TabStop = true;
  439. r.FocusNext ();
  440. Assert.True (v1.HasFocus);
  441. Assert.False (v2.HasFocus);
  442. Assert.False (v3.HasFocus);
  443. v2.TabStop = true;
  444. r.FocusNext ();
  445. Assert.False (v1.HasFocus);
  446. Assert.True (v2.HasFocus);
  447. Assert.False (v3.HasFocus);
  448. v3.TabStop = true;
  449. r.FocusNext ();
  450. Assert.False (v1.HasFocus);
  451. Assert.False (v2.HasFocus);
  452. Assert.True (v3.HasFocus);
  453. }
  454. [Fact]
  455. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  456. {
  457. var r = new View ();
  458. var v1 = new View ("1");
  459. var v2 = new View ("2");
  460. var v3 = new View ("3");
  461. r.Add (v1, v2, v3);
  462. v2.CanFocus = true;
  463. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  464. Assert.Equal (0, v2.TabIndex);
  465. Assert.True (v2.TabStop);
  466. v1.CanFocus = true;
  467. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  468. Assert.Equal (1, v1.TabIndex);
  469. Assert.True (v1.TabStop);
  470. v1.TabIndex = 2;
  471. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  472. Assert.Equal (1, v1.TabIndex);
  473. v3.CanFocus = true;
  474. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  475. Assert.Equal (1, v1.TabIndex);
  476. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  477. Assert.Equal (2, v3.TabIndex);
  478. Assert.True (v3.TabStop);
  479. v2.CanFocus = false;
  480. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  481. Assert.Equal (1, v1.TabIndex);
  482. Assert.True (v1.TabStop);
  483. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  484. Assert.Equal (-1, v2.TabIndex);
  485. Assert.False (v2.TabStop);
  486. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  487. Assert.Equal (2, v3.TabIndex);
  488. Assert.True (v3.TabStop);
  489. }
  490. [Fact]
  491. public void Initialized_Event_Comparing_With_Added_Event ()
  492. {
  493. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  494. var t = new Toplevel () { Id = "0", };
  495. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  496. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  497. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  498. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  499. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  500. w.Added += (e) => {
  501. Assert.Equal (e.Frame.Width, w.Frame.Width);
  502. Assert.Equal (e.Frame.Height, w.Frame.Height);
  503. };
  504. v1.Added += (e) => {
  505. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  506. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  507. };
  508. v2.Added += (e) => {
  509. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  510. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  511. };
  512. sv1.Added += (e) => {
  513. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  514. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  515. };
  516. t.Initialized += (s, e) => {
  517. tc++;
  518. Assert.Equal (1, tc);
  519. Assert.Equal (1, wc);
  520. Assert.Equal (1, v1c);
  521. Assert.Equal (1, v2c);
  522. Assert.Equal (1, sv1c);
  523. Assert.True (t.CanFocus);
  524. Assert.True (w.CanFocus);
  525. Assert.False (v1.CanFocus);
  526. Assert.False (v2.CanFocus);
  527. Assert.False (sv1.CanFocus);
  528. Application.Refresh ();
  529. };
  530. w.Initialized += (s, e) => {
  531. wc++;
  532. Assert.Equal (t.Frame.Width, w.Frame.Width);
  533. Assert.Equal (t.Frame.Height, w.Frame.Height);
  534. };
  535. v1.Initialized += (s, e) => {
  536. v1c++;
  537. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  538. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  539. };
  540. v2.Initialized += (s, e) => {
  541. v2c++;
  542. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  543. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  544. };
  545. sv1.Initialized += (s, e) => {
  546. sv1c++;
  547. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  548. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  549. Assert.False (sv1.CanFocus);
  550. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  551. Assert.False (sv1.CanFocus);
  552. };
  553. v1.Add (sv1);
  554. w.Add (v1, v2);
  555. t.Add (w);
  556. Application.Iteration = () => {
  557. Application.Refresh ();
  558. t.Running = false;
  559. };
  560. Application.Run (t);
  561. Application.Shutdown ();
  562. Assert.Equal (1, tc);
  563. Assert.Equal (1, wc);
  564. Assert.Equal (1, v1c);
  565. Assert.Equal (1, v2c);
  566. Assert.Equal (1, sv1c);
  567. Assert.True (t.CanFocus);
  568. Assert.True (w.CanFocus);
  569. Assert.False (v1.CanFocus);
  570. Assert.False (v2.CanFocus);
  571. Assert.False (sv1.CanFocus);
  572. v1.CanFocus = true;
  573. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  574. }
  575. [Fact]
  576. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  577. {
  578. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  579. var t = new Toplevel () { Id = "0", };
  580. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  581. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  582. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  583. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  584. t.Initialized += (s, e) => {
  585. tc++;
  586. Assert.Equal (1, tc);
  587. Assert.Equal (1, wc);
  588. Assert.Equal (1, v1c);
  589. Assert.Equal (1, v2c);
  590. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  591. Assert.True (t.CanFocus);
  592. Assert.True (w.CanFocus);
  593. Assert.False (v1.CanFocus);
  594. Assert.False (v2.CanFocus);
  595. Application.Refresh ();
  596. };
  597. w.Initialized += (s, e) => {
  598. wc++;
  599. Assert.Equal (t.Frame.Width, w.Frame.Width);
  600. Assert.Equal (t.Frame.Height, w.Frame.Height);
  601. };
  602. v1.Initialized += (s, e) => {
  603. v1c++;
  604. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  605. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  606. };
  607. v2.Initialized += (s, e) => {
  608. v2c++;
  609. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  610. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  611. };
  612. w.Add (v1, v2);
  613. t.Add (w);
  614. Application.Iteration = () => {
  615. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  616. sv1.Initialized += (s, e) => {
  617. sv1c++;
  618. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  619. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  620. Assert.False (sv1.CanFocus);
  621. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  622. Assert.False (sv1.CanFocus);
  623. };
  624. v1.Add (sv1);
  625. Application.Refresh ();
  626. t.Running = false;
  627. };
  628. Application.Run (t);
  629. Application.Shutdown ();
  630. Assert.Equal (1, tc);
  631. Assert.Equal (1, wc);
  632. Assert.Equal (1, v1c);
  633. Assert.Equal (1, v2c);
  634. Assert.Equal (1, sv1c);
  635. Assert.True (t.CanFocus);
  636. Assert.True (w.CanFocus);
  637. Assert.False (v1.CanFocus);
  638. Assert.False (v2.CanFocus);
  639. }
  640. [Fact]
  641. [AutoInitShutdown]
  642. public void CanFocus_Faced_With_Container ()
  643. {
  644. var t = new Toplevel ();
  645. var w = new Window ();
  646. var f = new FrameView ();
  647. var v = new View () { CanFocus = true };
  648. f.Add (v);
  649. w.Add (f);
  650. t.Add (w);
  651. Assert.True (t.CanFocus);
  652. Assert.True (w.CanFocus);
  653. Assert.True (f.CanFocus);
  654. Assert.True (v.CanFocus);
  655. f.CanFocus = false;
  656. Assert.False (f.CanFocus);
  657. Assert.True (v.CanFocus);
  658. v.CanFocus = false;
  659. Assert.False (f.CanFocus);
  660. Assert.False (v.CanFocus);
  661. v.CanFocus = true;
  662. Assert.False (f.CanFocus);
  663. Assert.True (v.CanFocus);
  664. }
  665. [Fact]
  666. public void CanFocus_Faced_With_Container_Before_Run ()
  667. {
  668. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  669. var t = Application.Top;
  670. var w = new Window ("w");
  671. var f = new FrameView ("f");
  672. var v = new View ("v") { CanFocus = true };
  673. f.Add (v);
  674. w.Add (f);
  675. t.Add (w);
  676. Assert.True (t.CanFocus);
  677. Assert.True (w.CanFocus);
  678. Assert.True (f.CanFocus);
  679. Assert.True (v.CanFocus);
  680. f.CanFocus = false;
  681. Assert.False (f.CanFocus);
  682. Assert.True (v.CanFocus);
  683. v.CanFocus = false;
  684. Assert.False (f.CanFocus);
  685. Assert.False (v.CanFocus);
  686. v.CanFocus = true;
  687. Assert.False (f.CanFocus);
  688. Assert.True (v.CanFocus);
  689. Application.Iteration += () => Application.RequestStop ();
  690. Application.Run ();
  691. Application.Shutdown ();
  692. }
  693. [Fact]
  694. public void CanFocus_Faced_With_Container_After_Run ()
  695. {
  696. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  697. var t = Application.Top;
  698. var w = new Window ("w");
  699. var f = new FrameView ("f");
  700. var v = new View ("v") { CanFocus = true };
  701. f.Add (v);
  702. w.Add (f);
  703. t.Add (w);
  704. t.Ready += () => {
  705. Assert.True (t.CanFocus);
  706. Assert.True (w.CanFocus);
  707. Assert.True (f.CanFocus);
  708. Assert.True (v.CanFocus);
  709. f.CanFocus = false;
  710. Assert.False (f.CanFocus);
  711. Assert.False (v.CanFocus);
  712. v.CanFocus = false;
  713. Assert.False (f.CanFocus);
  714. Assert.False (v.CanFocus);
  715. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  716. Assert.False (f.CanFocus);
  717. Assert.False (v.CanFocus);
  718. f.CanFocus = true;
  719. Assert.True (f.CanFocus);
  720. Assert.True (v.CanFocus);
  721. };
  722. Application.Iteration += () => Application.RequestStop ();
  723. Application.Run ();
  724. Application.Shutdown ();
  725. }
  726. [Fact]
  727. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  728. {
  729. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  730. var t = Application.Top;
  731. var w = new Window ("w");
  732. var f = new FrameView ("f");
  733. var v1 = new View ("v1") { CanFocus = true };
  734. var v2 = new View ("v2") { CanFocus = true };
  735. f.Add (v1, v2);
  736. w.Add (f);
  737. t.Add (w);
  738. t.Ready += () => {
  739. Assert.True (t.CanFocus);
  740. Assert.True (w.CanFocus);
  741. Assert.True (f.CanFocus);
  742. Assert.True (v1.CanFocus);
  743. Assert.True (v2.CanFocus);
  744. w.CanFocus = false;
  745. Assert.False (w.CanFocus);
  746. Assert.False (f.CanFocus);
  747. Assert.False (v1.CanFocus);
  748. Assert.False (v2.CanFocus);
  749. };
  750. Application.Iteration += () => Application.RequestStop ();
  751. Application.Run ();
  752. Application.Shutdown ();
  753. }
  754. [Fact]
  755. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  756. {
  757. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  758. var t = Application.Top;
  759. var w = new Window ("w");
  760. var f = new FrameView ("f");
  761. var v1 = new View ("v1");
  762. var v2 = new View ("v2") { CanFocus = true };
  763. f.Add (v1, v2);
  764. w.Add (f);
  765. t.Add (w);
  766. t.Ready += () => {
  767. Assert.True (t.CanFocus);
  768. Assert.True (w.CanFocus);
  769. Assert.True (f.CanFocus);
  770. Assert.False (v1.CanFocus);
  771. Assert.True (v2.CanFocus);
  772. w.CanFocus = false;
  773. Assert.False (w.CanFocus);
  774. Assert.False (f.CanFocus);
  775. Assert.False (v1.CanFocus);
  776. Assert.False (v2.CanFocus);
  777. w.CanFocus = true;
  778. Assert.True (w.CanFocus);
  779. Assert.True (f.CanFocus);
  780. Assert.False (v1.CanFocus);
  781. Assert.True (v2.CanFocus);
  782. };
  783. Application.Iteration += () => Application.RequestStop ();
  784. Application.Run ();
  785. Application.Shutdown ();
  786. }
  787. [Fact]
  788. public void Navigation_With_Null_Focused_View ()
  789. {
  790. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  791. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  792. Application.Top.Ready += () => {
  793. Assert.Null (Application.Top.Focused);
  794. };
  795. // Keyboard navigation with tab
  796. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  797. Application.Iteration += () => Application.RequestStop ();
  798. Application.Run ();
  799. Application.Shutdown ();
  800. }
  801. [Fact]
  802. public void Multi_Thread_Toplevels ()
  803. {
  804. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  805. var t = Application.Top;
  806. var w = new Window ();
  807. t.Add (w);
  808. int count = 0, count1 = 0, count2 = 0;
  809. bool log = false, log1 = false, log2 = false;
  810. bool fromTopStillKnowFirstIsRunning = false;
  811. bool fromTopStillKnowSecondIsRunning = false;
  812. bool fromFirstStillKnowSecondIsRunning = false;
  813. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  814. count++;
  815. if (count1 == 5) {
  816. log1 = true;
  817. }
  818. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  819. fromTopStillKnowFirstIsRunning = true;
  820. }
  821. if (count1 == 7 && count2 == 7 && count == 8) {
  822. fromTopStillKnowSecondIsRunning = true;
  823. }
  824. if (count == 30) {
  825. Assert.Equal (30, count);
  826. Assert.Equal (20, count1);
  827. Assert.Equal (10, count2);
  828. Assert.True (log);
  829. Assert.True (log1);
  830. Assert.True (log2);
  831. Assert.True (fromTopStillKnowFirstIsRunning);
  832. Assert.True (fromTopStillKnowSecondIsRunning);
  833. Assert.True (fromFirstStillKnowSecondIsRunning);
  834. Application.RequestStop ();
  835. return false;
  836. }
  837. return true;
  838. });
  839. t.Ready += FirstDialogToplevel;
  840. void FirstDialogToplevel ()
  841. {
  842. var od = new OpenDialog ();
  843. od.Ready += SecoundDialogToplevel;
  844. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  845. count1++;
  846. if (count2 == 5) {
  847. log2 = true;
  848. }
  849. if (count2 == 4 && count1 == 5 && count == 5) {
  850. fromFirstStillKnowSecondIsRunning = true;
  851. }
  852. if (count1 == 20) {
  853. Assert.Equal (20, count1);
  854. Application.RequestStop ();
  855. return false;
  856. }
  857. return true;
  858. });
  859. Application.Run (od);
  860. }
  861. void SecoundDialogToplevel ()
  862. {
  863. var d = new Dialog ();
  864. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  865. count2++;
  866. if (count < 30) {
  867. log = true;
  868. }
  869. if (count2 == 10) {
  870. Assert.Equal (10, count2);
  871. Application.RequestStop ();
  872. return false;
  873. }
  874. return true;
  875. });
  876. Application.Run (d);
  877. }
  878. Application.Run ();
  879. Application.Shutdown ();
  880. }
  881. [Fact]
  882. public void View_With_No_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  883. {
  884. // Object Initializer
  885. var view = new View () {
  886. X = 1,
  887. Y = 2,
  888. Width = 3,
  889. Height = 4
  890. };
  891. Assert.Equal (1, view.X);
  892. Assert.Equal (2, view.Y);
  893. Assert.Equal (3, view.Width);
  894. Assert.Equal (4, view.Height);
  895. Assert.False (view.Frame.IsEmpty);
  896. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  897. Assert.False (view.Bounds.IsEmpty);
  898. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  899. view.LayoutSubviews ();
  900. Assert.Equal (1, view.X);
  901. Assert.Equal (2, view.Y);
  902. Assert.Equal (3, view.Width);
  903. Assert.Equal (4, view.Height);
  904. Assert.False (view.Frame.IsEmpty);
  905. Assert.False (view.Bounds.IsEmpty);
  906. // Default Constructor
  907. view = new View ();
  908. Assert.Equal (0, view.X);
  909. Assert.Equal (0, view.Y);
  910. Assert.Equal (0, view.Width);
  911. Assert.Equal (0, view.Height);
  912. Assert.True (view.Frame.IsEmpty);
  913. Assert.True (view.Bounds.IsEmpty);
  914. // Constructor
  915. view = new View (1, 2, "");
  916. Assert.NotNull (view.X);
  917. Assert.NotNull (view.Y);
  918. Assert.NotNull (view.Width);
  919. Assert.NotNull (view.Height);
  920. Assert.False (view.Frame.IsEmpty);
  921. Assert.True (view.Bounds.IsEmpty);
  922. // Default Constructor and post assignment equivalent to Object Initializer
  923. view = new View ();
  924. view.X = 1;
  925. view.Y = 2;
  926. view.Width = 3;
  927. view.Height = 4;
  928. Assert.Equal (1, view.X);
  929. Assert.Equal (2, view.Y);
  930. Assert.Equal (3, view.Width);
  931. Assert.Equal (4, view.Height);
  932. Assert.False (view.Frame.IsEmpty);
  933. Assert.Equal (new Rect (1, 2, 3, 4), view.Frame);
  934. Assert.False (view.Bounds.IsEmpty);
  935. Assert.Equal (new Rect (0, 0, 3, 4), view.Bounds);
  936. }
  937. [Fact]
  938. public void FocusNearestView_Ensure_Focus_Ordered ()
  939. {
  940. var top = new Toplevel ();
  941. var win = new Window ();
  942. var winSubview = new View ("WindowSubview") {
  943. CanFocus = true
  944. };
  945. win.Add (winSubview);
  946. top.Add (win);
  947. var frm = new FrameView ();
  948. var frmSubview = new View ("FrameSubview") {
  949. CanFocus = true
  950. };
  951. frm.Add (frmSubview);
  952. top.Add (frm);
  953. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  954. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  955. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  956. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  957. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  958. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  959. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  960. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  961. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  962. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  963. }
  964. [Fact]
  965. public void KeyPress_Handled_To_True_Prevents_Changes ()
  966. {
  967. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  968. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  969. var top = Application.Top;
  970. var text = new TextField ("");
  971. text.KeyPress += (e) => {
  972. e.Handled = true;
  973. Assert.True (e.Handled);
  974. Assert.Equal (Key.N, e.KeyEvent.Key);
  975. };
  976. top.Add (text);
  977. Application.Iteration += () => {
  978. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  979. Assert.Equal ("", text.Text);
  980. Application.RequestStop ();
  981. };
  982. Application.Run ();
  983. // Shutdown must be called to safely clean up Application if Init has been called
  984. Application.Shutdown ();
  985. }
  986. [Fact]
  987. public void SetWidth_CanSetWidth ()
  988. {
  989. var top = new View () {
  990. X = 0,
  991. Y = 0,
  992. Width = 80,
  993. };
  994. var v = new View () {
  995. Width = Dim.Fill ()
  996. };
  997. top.Add (v);
  998. Assert.False (v.SetWidth (70, out int rWidth));
  999. Assert.Equal (70, rWidth);
  1000. v.Width = Dim.Fill (1);
  1001. Assert.False (v.SetWidth (70, out rWidth));
  1002. Assert.Equal (69, rWidth);
  1003. v.Width = null;
  1004. Assert.True (v.SetWidth (70, out rWidth));
  1005. Assert.Equal (70, rWidth);
  1006. v.IsInitialized = true;
  1007. v.Width = Dim.Fill (1);
  1008. Assert.Throws<ArgumentException> (() => v.Width = 75);
  1009. v.LayoutStyle = LayoutStyle.Absolute;
  1010. v.Width = 75;
  1011. Assert.True (v.SetWidth (60, out rWidth));
  1012. Assert.Equal (60, rWidth);
  1013. }
  1014. [Fact]
  1015. public void SetHeight_CanSetHeight ()
  1016. {
  1017. var top = new View () {
  1018. X = 0,
  1019. Y = 0,
  1020. Height = 20
  1021. };
  1022. var v = new View () {
  1023. Height = Dim.Fill ()
  1024. };
  1025. top.Add (v);
  1026. Assert.False (v.SetHeight (10, out int rHeight));
  1027. Assert.Equal (10, rHeight);
  1028. v.Height = Dim.Fill (1);
  1029. Assert.False (v.SetHeight (10, out rHeight));
  1030. Assert.Equal (9, rHeight);
  1031. v.Height = null;
  1032. Assert.True (v.SetHeight (10, out rHeight));
  1033. Assert.Equal (10, rHeight);
  1034. v.IsInitialized = true;
  1035. v.Height = Dim.Fill (1);
  1036. Assert.Throws<ArgumentException> (() => v.Height = 15);
  1037. v.LayoutStyle = LayoutStyle.Absolute;
  1038. v.Height = 15;
  1039. Assert.True (v.SetHeight (5, out rHeight));
  1040. Assert.Equal (5, rHeight);
  1041. }
  1042. [Fact]
  1043. public void GetCurrentWidth_CanSetWidth ()
  1044. {
  1045. var top = new View () {
  1046. X = 0,
  1047. Y = 0,
  1048. Width = 80,
  1049. };
  1050. var v = new View () {
  1051. Width = Dim.Fill ()
  1052. };
  1053. top.Add (v);
  1054. Assert.False (v.GetCurrentWidth (out int cWidth));
  1055. Assert.Equal (80, cWidth);
  1056. v.Width = Dim.Fill (1);
  1057. Assert.False (v.GetCurrentWidth (out cWidth));
  1058. Assert.Equal (79, cWidth);
  1059. }
  1060. [Fact]
  1061. public void GetCurrentHeight_CanSetHeight ()
  1062. {
  1063. var top = new View () {
  1064. X = 0,
  1065. Y = 0,
  1066. Height = 20
  1067. };
  1068. var v = new View () {
  1069. Height = Dim.Fill ()
  1070. };
  1071. top.Add (v);
  1072. Assert.False (v.GetCurrentHeight (out int cHeight));
  1073. Assert.Equal (20, cHeight);
  1074. v.Height = Dim.Fill (1);
  1075. Assert.False (v.GetCurrentHeight (out cHeight));
  1076. Assert.Equal (19, cHeight);
  1077. }
  1078. [Fact]
  1079. public void AutoSize_False_ResizeView_Is_Always_False ()
  1080. {
  1081. var label = new Label () { AutoSize = false };
  1082. label.Text = "New text";
  1083. Assert.False (label.AutoSize);
  1084. Assert.Equal ("{X=0,Y=0,Width=0,Height=0}", label.Bounds.ToString ());
  1085. }
  1086. [Fact]
  1087. public void AutoSize_True_ResizeView_With_Dim_Absolute ()
  1088. {
  1089. var label = new Label ();
  1090. label.Text = "New text";
  1091. Assert.True (label.AutoSize);
  1092. Assert.Equal ("{X=0,Y=0,Width=8,Height=1}", label.Bounds.ToString ());
  1093. }
  1094. [Fact]
  1095. public void AutoSize_True_ResizeView_With_Dim_Fill ()
  1096. {
  1097. var win = new Window (new Rect (0, 0, 30, 80), "");
  1098. var label = new Label () { Width = Dim.Fill (), Height = Dim.Fill () };
  1099. win.Add (label);
  1100. label.Text = "New text\nNew line";
  1101. win.LayoutSubviews ();
  1102. Assert.True (label.AutoSize);
  1103. Assert.Equal ("{X=0,Y=0,Width=28,Height=78}", label.Bounds.ToString ());
  1104. }
  1105. [Fact]
  1106. public void AutoSize_True_SetWidthHeight_With_Dim_Fill_And_Dim_Absolute ()
  1107. {
  1108. var win = new Window (new Rect (0, 0, 30, 80), "");
  1109. var label = new Label () { Width = Dim.Fill () };
  1110. win.Add (label);
  1111. label.Text = "New text\nNew line";
  1112. win.LayoutSubviews ();
  1113. Assert.True (label.AutoSize);
  1114. Assert.Equal ("{X=0,Y=0,Width=28,Height=2}", label.Bounds.ToString ());
  1115. }
  1116. [Fact, AutoInitShutdown]
  1117. public void AutoSize_True_Setting_With_Height_Sets_AutoSize_False_Horizontal ()
  1118. {
  1119. var label = new Label ("Hello") { Width = 10, Height = 2 };
  1120. var viewX = new View ("X") { X = Pos.Right (label) };
  1121. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  1122. Application.Top.Add (label, viewX, viewY);
  1123. Application.Begin (Application.Top);
  1124. Assert.False (label.AutoSize);
  1125. Assert.Equal (new Rect (0, 0, 10, 2), label.Frame);
  1126. var expected = @"
  1127. Hello X
  1128. Y
  1129. ";
  1130. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1131. Assert.Equal (new Rect (0, 0, 11, 3), pos);
  1132. }
  1133. [Fact, AutoInitShutdown]
  1134. public void AutoSize_True_Setting_With_Height_Sets_AutoSize_False_Vertical ()
  1135. {
  1136. var label = new Label ("Hello") { Width = 2, Height = 10, TextDirection = TextDirection.TopBottom_LeftRight };
  1137. var viewX = new View ("X") { X = Pos.Right (label) };
  1138. var viewY = new View ("Y") { Y = Pos.Bottom (label) };
  1139. Application.Top.Add (label, viewX, viewY);
  1140. Application.Begin (Application.Top);
  1141. Assert.False (label.AutoSize);
  1142. Assert.Equal (new Rect (0, 0, 2, 10), label.Frame);
  1143. var expected = @"
  1144. H X
  1145. e
  1146. l
  1147. l
  1148. o
  1149. Y
  1150. ";
  1151. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1152. Assert.Equal (new Rect (0, 0, 3, 11), pos);
  1153. }
  1154. [Theory]
  1155. [InlineData (1)]
  1156. [InlineData (2)]
  1157. [InlineData (3)]
  1158. public void LabelChangeText_RendersCorrectly_Constructors (int choice)
  1159. {
  1160. var driver = new FakeDriver ();
  1161. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  1162. try {
  1163. // Create a label with a short text
  1164. Label lbl;
  1165. var text = "test";
  1166. if (choice == 1) {
  1167. // An object initializer should call the default constructor.
  1168. lbl = new Label { Text = text };
  1169. } else if (choice == 2) {
  1170. // Calling the default constructor followed by the object initializer.
  1171. lbl = new Label () { Text = text };
  1172. } else {
  1173. // Calling the Text constructor.
  1174. lbl = new Label (text);
  1175. }
  1176. lbl.ColorScheme = new ColorScheme ();
  1177. lbl.Redraw (lbl.Bounds);
  1178. // should have the initial text
  1179. Assert.Equal ('t', driver.Contents [0, 0, 0]);
  1180. Assert.Equal ('e', driver.Contents [0, 1, 0]);
  1181. Assert.Equal ('s', driver.Contents [0, 2, 0]);
  1182. Assert.Equal ('t', driver.Contents [0, 3, 0]);
  1183. Assert.Equal (' ', driver.Contents [0, 4, 0]);
  1184. } finally {
  1185. Application.Shutdown ();
  1186. }
  1187. }
  1188. [Fact]
  1189. [AutoInitShutdown]
  1190. public void Internal_Tests ()
  1191. {
  1192. Assert.Equal (new [] { View.Direction.Forward, View.Direction.Backward },
  1193. Enum.GetValues (typeof (View.Direction)));
  1194. var rect = new Rect (1, 1, 10, 1);
  1195. var view = new View (rect);
  1196. var top = Application.Top;
  1197. top.Add (view);
  1198. Assert.Equal (View.Direction.Forward, view.FocusDirection);
  1199. view.FocusDirection = View.Direction.Backward;
  1200. Assert.Equal (View.Direction.Backward, view.FocusDirection);
  1201. Assert.Empty (view.InternalSubviews);
  1202. Assert.Equal (new Rect (new Point (0, 0), rect.Size), view.NeedDisplay);
  1203. Assert.True (view.LayoutNeeded);
  1204. Assert.False (view.ChildNeedsDisplay);
  1205. Assert.False (view.addingView);
  1206. view.addingView = true;
  1207. Assert.True (view.addingView);
  1208. view.ViewToScreen (0, 0, out int rcol, out int rrow);
  1209. Assert.Equal (1, rcol);
  1210. Assert.Equal (1, rrow);
  1211. Assert.Equal (rect, view.ViewToScreen (view.Bounds));
  1212. Assert.Equal (top.Bounds, view.ScreenClip (top.Bounds));
  1213. view.Width = Dim.Fill ();
  1214. view.Height = Dim.Fill ();
  1215. Assert.Equal (10, view.Bounds.Width);
  1216. Assert.Equal (1, view.Bounds.Height);
  1217. view.SetRelativeLayout (top.Bounds);
  1218. Assert.Equal (79, view.Bounds.Width);
  1219. Assert.Equal (24, view.Bounds.Height);
  1220. bool layoutStarted = false;
  1221. view.LayoutStarted += (_) => { layoutStarted = true; };
  1222. view.OnLayoutStarted (null);
  1223. Assert.True (layoutStarted);
  1224. view.LayoutComplete += (_) => { layoutStarted = false; };
  1225. view.OnLayoutComplete (null);
  1226. Assert.False (layoutStarted);
  1227. view.X = Pos.Center () - 41;
  1228. view.Y = Pos.Center () - 13;
  1229. view.SetRelativeLayout (top.Bounds);
  1230. view.ViewToScreen (0, 0, out rcol, out rrow);
  1231. Assert.Equal (-1, rcol);
  1232. Assert.Equal (-1, rrow);
  1233. }
  1234. [Fact]
  1235. [AutoInitShutdown]
  1236. public void Enabled_False_Sets_HasFocus_To_False ()
  1237. {
  1238. var wasClicked = false;
  1239. var view = new Button ("Click Me");
  1240. view.Clicked += () => wasClicked = !wasClicked;
  1241. Application.Top.Add (view);
  1242. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1243. Assert.True (wasClicked);
  1244. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1245. Assert.False (wasClicked);
  1246. Assert.True (view.Enabled);
  1247. Assert.True (view.CanFocus);
  1248. Assert.True (view.HasFocus);
  1249. view.Enabled = false;
  1250. view.ProcessKey (new KeyEvent (Key.Enter, null));
  1251. Assert.False (wasClicked);
  1252. view.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1253. Assert.False (wasClicked);
  1254. Assert.False (view.Enabled);
  1255. Assert.True (view.CanFocus);
  1256. Assert.False (view.HasFocus);
  1257. view.SetFocus ();
  1258. Assert.False (view.HasFocus);
  1259. }
  1260. [Fact]
  1261. [AutoInitShutdown]
  1262. public void Enabled_Sets_Also_Sets_Subviews ()
  1263. {
  1264. var wasClicked = false;
  1265. var button = new Button ("Click Me");
  1266. button.Clicked += () => wasClicked = !wasClicked;
  1267. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1268. win.Add (button);
  1269. Application.Top.Add (win);
  1270. var iterations = 0;
  1271. Application.Iteration += () => {
  1272. iterations++;
  1273. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1274. Assert.True (wasClicked);
  1275. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1276. Assert.False (wasClicked);
  1277. Assert.True (button.Enabled);
  1278. Assert.True (button.CanFocus);
  1279. Assert.True (button.HasFocus);
  1280. Assert.True (win.Enabled);
  1281. Assert.True (win.CanFocus);
  1282. Assert.True (win.HasFocus);
  1283. win.Enabled = false;
  1284. button.ProcessKey (new KeyEvent (Key.Enter, null));
  1285. Assert.False (wasClicked);
  1286. button.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Clicked });
  1287. Assert.False (wasClicked);
  1288. Assert.False (button.Enabled);
  1289. Assert.True (button.CanFocus);
  1290. Assert.False (button.HasFocus);
  1291. Assert.False (win.Enabled);
  1292. Assert.True (win.CanFocus);
  1293. Assert.False (win.HasFocus);
  1294. button.SetFocus ();
  1295. Assert.False (button.HasFocus);
  1296. Assert.False (win.HasFocus);
  1297. win.SetFocus ();
  1298. Assert.False (button.HasFocus);
  1299. Assert.False (win.HasFocus);
  1300. win.Enabled = true;
  1301. win.FocusFirst ();
  1302. Assert.True (button.HasFocus);
  1303. Assert.True (win.HasFocus);
  1304. Application.RequestStop ();
  1305. };
  1306. Application.Run ();
  1307. Assert.Equal (1, iterations);
  1308. }
  1309. [Fact]
  1310. [AutoInitShutdown]
  1311. public void Visible_Sets_Also_Sets_Subviews ()
  1312. {
  1313. var button = new Button ("Click Me");
  1314. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1315. win.Add (button);
  1316. var top = Application.Top;
  1317. top.Add (win);
  1318. var iterations = 0;
  1319. Application.Iteration += () => {
  1320. iterations++;
  1321. Assert.True (button.Visible);
  1322. Assert.True (button.CanFocus);
  1323. Assert.True (button.HasFocus);
  1324. Assert.True (win.Visible);
  1325. Assert.True (win.CanFocus);
  1326. Assert.True (win.HasFocus);
  1327. Assert.True (RunesCount () > 0);
  1328. win.Visible = false;
  1329. Assert.True (button.Visible);
  1330. Assert.True (button.CanFocus);
  1331. Assert.False (button.HasFocus);
  1332. Assert.False (win.Visible);
  1333. Assert.True (win.CanFocus);
  1334. Assert.False (win.HasFocus);
  1335. button.SetFocus ();
  1336. Assert.False (button.HasFocus);
  1337. Assert.False (win.HasFocus);
  1338. win.SetFocus ();
  1339. Assert.False (button.HasFocus);
  1340. Assert.False (win.HasFocus);
  1341. top.Redraw (top.Bounds);
  1342. Assert.True (RunesCount () == 0);
  1343. win.Visible = true;
  1344. win.FocusFirst ();
  1345. Assert.True (button.HasFocus);
  1346. Assert.True (win.HasFocus);
  1347. top.Redraw (top.Bounds);
  1348. Assert.True (RunesCount () > 0);
  1349. Application.RequestStop ();
  1350. };
  1351. Application.Run ();
  1352. Assert.Equal (1, iterations);
  1353. int RunesCount ()
  1354. {
  1355. var contents = ((FakeDriver)Application.Driver).Contents;
  1356. var runesCount = 0;
  1357. for (int i = 0; i < Application.Driver.Rows; i++) {
  1358. for (int j = 0; j < Application.Driver.Cols; j++) {
  1359. if (contents [i, j, 0] != ' ') {
  1360. runesCount++;
  1361. }
  1362. }
  1363. }
  1364. return runesCount;
  1365. }
  1366. }
  1367. [Fact]
  1368. [AutoInitShutdown]
  1369. public void GetTopSuperView_Test ()
  1370. {
  1371. var v1 = new View ();
  1372. var fv1 = new FrameView ();
  1373. fv1.Add (v1);
  1374. var tf1 = new TextField ();
  1375. var w1 = new Window ();
  1376. w1.Add (fv1, tf1);
  1377. var top1 = new Toplevel ();
  1378. top1.Add (w1);
  1379. var v2 = new View ();
  1380. var fv2 = new FrameView ();
  1381. fv2.Add (v2);
  1382. var tf2 = new TextField ();
  1383. var w2 = new Window ();
  1384. w2.Add (fv2, tf2);
  1385. var top2 = new Toplevel ();
  1386. top2.Add (w2);
  1387. Assert.Equal (top1, v1.GetTopSuperView ());
  1388. Assert.Equal (top2, v2.GetTopSuperView ());
  1389. }
  1390. [Fact]
  1391. [AutoInitShutdown]
  1392. public void Excess_Text_Is_Erased_When_The_Width_Is_Reduced ()
  1393. {
  1394. var lbl = new Label ("123");
  1395. Application.Top.Add (lbl);
  1396. Application.Begin (Application.Top);
  1397. Assert.Equal ("123 ", GetContents ());
  1398. lbl.Text = "12";
  1399. if (!lbl.SuperView.NeedDisplay.IsEmpty) {
  1400. lbl.SuperView.Redraw (lbl.SuperView.NeedDisplay);
  1401. }
  1402. Assert.Equal ("12 ", GetContents ());
  1403. string GetContents ()
  1404. {
  1405. var text = "";
  1406. for (int i = 0; i < 4; i++) {
  1407. text += (char)Application.Driver.Contents [0, i, 0];
  1408. }
  1409. return text;
  1410. }
  1411. }
  1412. [Fact]
  1413. [AutoInitShutdown]
  1414. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  1415. {
  1416. var view = new View () { CanFocus = true };
  1417. var win = new Window () { Width = Dim.Fill (), Height = Dim.Fill () };
  1418. win.Add (view);
  1419. Application.Top.Add (win);
  1420. Application.Begin (Application.Top);
  1421. Assert.True (view.CanFocus);
  1422. Assert.True (view.HasFocus);
  1423. view.CanFocus = false;
  1424. Assert.False (view.CanFocus);
  1425. Assert.False (view.HasFocus);
  1426. Assert.Null (Application.Current.Focused);
  1427. Assert.Null (Application.Current.MostFocused);
  1428. }
  1429. [Fact]
  1430. [AutoInitShutdown]
  1431. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  1432. {
  1433. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1434. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1435. win1.Add (view1);
  1436. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1437. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1438. win2.Add (view2);
  1439. Application.Top.Add (win1, win2);
  1440. Application.Begin (Application.Top);
  1441. Assert.True (view1.CanFocus);
  1442. Assert.True (view1.HasFocus);
  1443. Assert.True (view2.CanFocus);
  1444. Assert.True (view2.HasFocus);
  1445. view1.CanFocus = false;
  1446. Assert.False (view1.CanFocus);
  1447. Assert.False (view1.HasFocus);
  1448. Assert.Equal (win2, Application.Current.Focused);
  1449. Assert.Equal (view2, Application.Current.MostFocused);
  1450. }
  1451. [Fact]
  1452. [AutoInitShutdown]
  1453. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  1454. {
  1455. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1456. var view12 = new View () { Y = 5, Width = 10, Height = 1, CanFocus = true };
  1457. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1458. win1.Add (view1, view12);
  1459. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1460. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1461. win2.Add (view2);
  1462. Application.Top.Add (win1, win2);
  1463. Application.Begin (Application.Top);
  1464. Assert.True (view1.CanFocus);
  1465. Assert.True (view1.HasFocus);
  1466. Assert.True (view2.CanFocus);
  1467. Assert.True (view2.HasFocus);
  1468. view1.CanFocus = false;
  1469. Assert.False (view1.CanFocus);
  1470. Assert.False (view1.HasFocus);
  1471. Assert.Equal (win1, Application.Current.Focused);
  1472. Assert.Equal (view12, Application.Current.MostFocused);
  1473. }
  1474. [Fact]
  1475. [AutoInitShutdown]
  1476. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  1477. {
  1478. var view1 = new View () { Width = 10, Height = 1, CanFocus = true };
  1479. var win1 = new Window () { Width = Dim.Percent (50), Height = Dim.Fill () };
  1480. win1.Add (view1);
  1481. var view2 = new View () { Width = 20, Height = 2, CanFocus = true };
  1482. var win2 = new Window () { X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  1483. win2.Add (view2);
  1484. Application.Top.Add (win1, win2);
  1485. Application.Begin (Application.Top);
  1486. Assert.True (view1.CanFocus);
  1487. Assert.True (view1.HasFocus);
  1488. Assert.True (view2.CanFocus);
  1489. Assert.True (view2.HasFocus);
  1490. win1.CanFocus = false;
  1491. Assert.False (view1.CanFocus);
  1492. Assert.False (view1.HasFocus);
  1493. Assert.False (win1.CanFocus);
  1494. Assert.False (win1.HasFocus);
  1495. Assert.Equal (win2, Application.Current.Focused);
  1496. Assert.Equal (view2, Application.Current.MostFocused);
  1497. }
  1498. [Fact]
  1499. [AutoInitShutdown]
  1500. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  1501. {
  1502. var sbQuiting = false;
  1503. var tfQuiting = false;
  1504. var topQuiting = false;
  1505. var sb = new StatusBar (new StatusItem [] {
  1506. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1507. });
  1508. var tf = new TextField ();
  1509. tf.KeyPress += Tf_KeyPress;
  1510. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1511. {
  1512. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1513. obj.Handled = tfQuiting = true;
  1514. }
  1515. }
  1516. var win = new Window ();
  1517. win.Add (sb, tf);
  1518. var top = Application.Top;
  1519. top.KeyPress += Top_KeyPress;
  1520. void Top_KeyPress (View.KeyEventEventArgs obj)
  1521. {
  1522. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1523. obj.Handled = topQuiting = true;
  1524. }
  1525. }
  1526. top.Add (win);
  1527. Application.Begin (top);
  1528. Assert.False (sbQuiting);
  1529. Assert.False (tfQuiting);
  1530. Assert.False (topQuiting);
  1531. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1532. Assert.False (sbQuiting);
  1533. Assert.True (tfQuiting);
  1534. Assert.False (topQuiting);
  1535. tf.KeyPress -= Tf_KeyPress;
  1536. tfQuiting = false;
  1537. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1538. Application.MainLoop.MainIteration ();
  1539. Assert.True (sbQuiting);
  1540. Assert.False (tfQuiting);
  1541. Assert.False (topQuiting);
  1542. sb.RemoveItem (0);
  1543. sbQuiting = false;
  1544. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1545. Application.MainLoop.MainIteration ();
  1546. Assert.False (sbQuiting);
  1547. Assert.False (tfQuiting);
  1548. Assert.True (topQuiting);
  1549. }
  1550. [Fact]
  1551. [AutoInitShutdown]
  1552. public void ProcessHotKey_Will_Invoke_ProcessKey_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  1553. {
  1554. var sbQuiting = false;
  1555. var tfQuiting = false;
  1556. var sb = new StatusBar (new StatusItem [] {
  1557. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => sbQuiting = true )
  1558. });
  1559. var tf = new TextField ();
  1560. tf.KeyPress += Tf_KeyPress;
  1561. void Tf_KeyPress (View.KeyEventEventArgs obj)
  1562. {
  1563. if (obj.KeyEvent.Key == (Key.Q | Key.CtrlMask)) {
  1564. obj.Handled = tfQuiting = true;
  1565. }
  1566. }
  1567. var win = new Window ();
  1568. win.Add (sb, tf);
  1569. var top = Application.Top;
  1570. top.Add (win);
  1571. Application.Begin (top);
  1572. Assert.False (sbQuiting);
  1573. Assert.False (tfQuiting);
  1574. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1575. Assert.False (sbQuiting);
  1576. Assert.True (tfQuiting);
  1577. tf.KeyPress -= Tf_KeyPress;
  1578. tfQuiting = false;
  1579. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  1580. Application.MainLoop.MainIteration ();
  1581. Assert.True (sbQuiting);
  1582. Assert.False (tfQuiting);
  1583. }
  1584. [Fact]
  1585. [AutoInitShutdown]
  1586. public void WindowDispose_CanFocusProblem ()
  1587. {
  1588. // Arrange
  1589. Application.Init ();
  1590. using var top = Toplevel.Create ();
  1591. using var view = new View (
  1592. x: 0,
  1593. y: 1,
  1594. text: nameof (WindowDispose_CanFocusProblem));
  1595. using var window = new Window ();
  1596. top.Add (window);
  1597. window.Add (view);
  1598. // Act
  1599. Application.Begin (top);
  1600. Application.Shutdown ();
  1601. // Assert does Not throw NullReferenceException
  1602. top.SetFocus ();
  1603. }
  1604. [Fact, AutoInitShutdown]
  1605. public void DrawFrame_With_Positive_Positions ()
  1606. {
  1607. var view = new View (new Rect (0, 0, 8, 4));
  1608. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1609. Assert.Equal (Point.Empty, new Point (view.Frame.X, view.Frame.Y));
  1610. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1611. Application.Top.Add (view);
  1612. Application.Begin (Application.Top);
  1613. var expected = @"
  1614. ┌──────┐
  1615. │ │
  1616. │ │
  1617. └──────┘
  1618. ";
  1619. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1620. Assert.Equal (new Rect (0, 0, 8, 4), pos);
  1621. }
  1622. [Fact, AutoInitShutdown]
  1623. public void DrawFrame_With_Negative_Positions ()
  1624. {
  1625. var view = new View (new Rect (-1, 0, 8, 4));
  1626. view.DrawContent += (_) => view.DrawFrame (view.Bounds, 0, true);
  1627. Assert.Equal (new Point (-1, 0), new Point (view.Frame.X, view.Frame.Y));
  1628. Assert.Equal (new Size (8, 4), new Size (view.Frame.Width, view.Frame.Height));
  1629. Application.Top.Add (view);
  1630. Application.Begin (Application.Top);
  1631. var expected = @"
  1632. ──────┐
  1633. ──────┘
  1634. ";
  1635. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1636. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1637. view.Frame = new Rect (-1, -1, 8, 4);
  1638. Application.Refresh ();
  1639. expected = @"
  1640. ──────┘
  1641. ";
  1642. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1643. Assert.Equal (new Rect (6, 0, 7, 3), pos);
  1644. view.Frame = new Rect (0, 0, 8, 4);
  1645. ((FakeDriver)Application.Driver).SetBufferSize (7, 4);
  1646. expected = @"
  1647. ┌──────
  1648. └──────
  1649. ";
  1650. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1651. Assert.Equal (new Rect (0, 0, 7, 4), pos);
  1652. view.Frame = new Rect (0, 0, 8, 4);
  1653. ((FakeDriver)Application.Driver).SetBufferSize (7, 3);
  1654. }
  1655. [Fact, AutoInitShutdown]
  1656. public void DrawTextFormatter_Respects_The_Clip_Bounds ()
  1657. {
  1658. var view = new View (new Rect (0, 0, 20, 20));
  1659. view.Add (new Label ("0123456789abcdefghij"));
  1660. view.Add (new Label (0, 1, "1\n2\n3\n4\n5\n6\n7\n8\n9\n0"));
  1661. view.Add (new Button (1, 1, "Press me!"));
  1662. var scrollView = new ScrollView (new Rect (1, 1, 15, 10)) {
  1663. ContentSize = new Size (40, 40),
  1664. ShowHorizontalScrollIndicator = true,
  1665. ShowVerticalScrollIndicator = true
  1666. };
  1667. scrollView.Add (view);
  1668. var win = new Window (new Rect (1, 1, 20, 14), "Test");
  1669. win.Add (scrollView);
  1670. Application.Top.Add (win);
  1671. Application.Begin (Application.Top);
  1672. var expected = @"
  1673. ┌ Test ────────────┐
  1674. │ │
  1675. │ 0123456789abcd▲ │
  1676. │ 1[ Press me! ]┬ │
  1677. │ 2 │ │
  1678. │ 3 ┴ │
  1679. │ 4 ░ │
  1680. │ 5 ░ │
  1681. │ 6 ░ │
  1682. │ 7 ░ │
  1683. │ 8 ▼ │
  1684. │ ◄├───┤░░░░░░░► │
  1685. │ │
  1686. └──────────────────┘
  1687. ";
  1688. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1689. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1690. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1691. Application.Top.Redraw (Application.Top.Bounds);
  1692. expected = @"
  1693. ┌ Test ────────────┐
  1694. │ │
  1695. │ 123456789abcde▲ │
  1696. │ [ Press me! ] ┬ │
  1697. │ │ │
  1698. │ ┴ │
  1699. │ ░ │
  1700. │ ░ │
  1701. │ ░ │
  1702. │ ░ │
  1703. │ ▼ │
  1704. │ ◄├───┤░░░░░░░► │
  1705. │ │
  1706. └──────────────────┘
  1707. ";
  1708. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1709. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1710. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1711. Application.Top.Redraw (Application.Top.Bounds);
  1712. expected = @"
  1713. ┌ Test ────────────┐
  1714. │ │
  1715. │ 23456789abcdef▲ │
  1716. │ Press me! ] ┬ │
  1717. │ │ │
  1718. │ ┴ │
  1719. │ ░ │
  1720. │ ░ │
  1721. │ ░ │
  1722. │ ░ │
  1723. │ ▼ │
  1724. │ ◄├────┤░░░░░░► │
  1725. │ │
  1726. └──────────────────┘
  1727. ";
  1728. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1729. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1730. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1731. Application.Top.Redraw (Application.Top.Bounds);
  1732. expected = @"
  1733. ┌ Test ────────────┐
  1734. │ │
  1735. │ 3456789abcdefg▲ │
  1736. │ Press me! ] ┬ │
  1737. │ │ │
  1738. │ ┴ │
  1739. │ ░ │
  1740. │ ░ │
  1741. │ ░ │
  1742. │ ░ │
  1743. │ ▼ │
  1744. │ ◄├────┤░░░░░░► │
  1745. │ │
  1746. └──────────────────┘
  1747. ";
  1748. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1749. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1750. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1751. Application.Top.Redraw (Application.Top.Bounds);
  1752. expected = @"
  1753. ┌ Test ────────────┐
  1754. │ │
  1755. │ 456789abcdefgh▲ │
  1756. │ ress me! ] ┬ │
  1757. │ │ │
  1758. │ ┴ │
  1759. │ ░ │
  1760. │ ░ │
  1761. │ ░ │
  1762. │ ░ │
  1763. │ ▼ │
  1764. │ ◄░├───┤░░░░░░► │
  1765. │ │
  1766. └──────────────────┘
  1767. ";
  1768. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1769. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1770. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1771. Application.Top.Redraw (Application.Top.Bounds);
  1772. expected = @"
  1773. ┌ Test ────────────┐
  1774. │ │
  1775. │ 56789abcdefghi▲ │
  1776. │ ess me! ] ┬ │
  1777. │ │ │
  1778. │ ┴ │
  1779. │ ░ │
  1780. │ ░ │
  1781. │ ░ │
  1782. │ ░ │
  1783. │ ▼ │
  1784. │ ◄░├────┤░░░░░► │
  1785. │ │
  1786. └──────────────────┘
  1787. ";
  1788. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1789. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1790. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1791. Application.Top.Redraw (Application.Top.Bounds);
  1792. expected = @"
  1793. ┌ Test ────────────┐
  1794. │ │
  1795. │ 6789abcdefghij▲ │
  1796. │ ss me! ] ┬ │
  1797. │ │ │
  1798. │ ┴ │
  1799. │ ░ │
  1800. │ ░ │
  1801. │ ░ │
  1802. │ ░ │
  1803. │ ▼ │
  1804. │ ◄░├────┤░░░░░► │
  1805. │ │
  1806. └──────────────────┘
  1807. ";
  1808. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1809. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1810. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  1811. Application.Top.Redraw (Application.Top.Bounds);
  1812. expected = @"
  1813. ┌ Test ────────────┐
  1814. │ │
  1815. │ 789abcdefghij ▲ │
  1816. │ s me! ] ┬ │
  1817. │ │ │
  1818. │ ┴ │
  1819. │ ░ │
  1820. │ ░ │
  1821. │ ░ │
  1822. │ ░ │
  1823. │ ▼ │
  1824. │ ◄░░├───┤░░░░░► │
  1825. │ │
  1826. └──────────────────┘
  1827. ";
  1828. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1829. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1830. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Home, new KeyModifiers ())));
  1831. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1832. Application.Top.Redraw (Application.Top.Bounds);
  1833. expected = @"
  1834. ┌ Test ────────────┐
  1835. │ │
  1836. │ 1[ Press me! ]▲ │
  1837. │ 2 ┬ │
  1838. │ 3 │ │
  1839. │ 4 ┴ │
  1840. │ 5 ░ │
  1841. │ 6 ░ │
  1842. │ 7 ░ │
  1843. │ 8 ░ │
  1844. │ 9 ▼ │
  1845. │ ◄├───┤░░░░░░░► │
  1846. │ │
  1847. └──────────────────┘
  1848. ";
  1849. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1850. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1851. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1852. Application.Top.Redraw (Application.Top.Bounds);
  1853. expected = @"
  1854. ┌ Test ────────────┐
  1855. │ │
  1856. │ 2 ▲ │
  1857. │ 3 ┬ │
  1858. │ 4 │ │
  1859. │ 5 ┴ │
  1860. │ 6 ░ │
  1861. │ 7 ░ │
  1862. │ 8 ░ │
  1863. │ 9 ░ │
  1864. │ 0 ▼ │
  1865. │ ◄├───┤░░░░░░░► │
  1866. │ │
  1867. └──────────────────┘
  1868. ";
  1869. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1870. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1871. Assert.True (scrollView.ProcessKey (new KeyEvent (Key.CursorDown, new KeyModifiers ())));
  1872. Application.Top.Redraw (Application.Top.Bounds);
  1873. expected = @"
  1874. ┌ Test ────────────┐
  1875. │ │
  1876. │ 3 ▲ │
  1877. │ 4 ┬ │
  1878. │ 5 │ │
  1879. │ 6 ┴ │
  1880. │ 7 ░ │
  1881. │ 8 ░ │
  1882. │ 9 ░ │
  1883. │ 0 ░ │
  1884. │ ▼ │
  1885. │ ◄├───┤░░░░░░░► │
  1886. │ │
  1887. └──────────────────┘
  1888. ";
  1889. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1890. Assert.Equal (new Rect (1, 1, 21, 14), pos);
  1891. }
  1892. [Fact, AutoInitShutdown]
  1893. public void Clear_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1894. {
  1895. var view = new View () {
  1896. Width = Dim.Fill (),
  1897. Height = Dim.Fill ()
  1898. };
  1899. view.LayoutComplete += e => {
  1900. view.DrawFrame (view.Bounds);
  1901. var savedClip = Application.Driver.Clip;
  1902. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1903. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1904. Application.Driver.Move (1, row + 1);
  1905. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1906. Application.Driver.AddStr ($"{col}");
  1907. }
  1908. }
  1909. Application.Driver.Clip = savedClip;
  1910. };
  1911. Application.Top.Add (view);
  1912. Application.Begin (Application.Top);
  1913. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1914. var expected = @"
  1915. ┌──────────────────┐
  1916. │012345678910111213│
  1917. │012345678910111213│
  1918. │012345678910111213│
  1919. │012345678910111213│
  1920. │012345678910111213│
  1921. │012345678910111213│
  1922. │012345678910111213│
  1923. │012345678910111213│
  1924. └──────────────────┘
  1925. ";
  1926. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1927. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1928. view.Clear ();
  1929. expected = @"
  1930. ";
  1931. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1932. Assert.Equal (Rect.Empty, pos);
  1933. }
  1934. [Fact, AutoInitShutdown]
  1935. public void Clear_Bounds_Can_Use_Driver_AddRune_Or_AddStr_Methods ()
  1936. {
  1937. var view = new View () {
  1938. Width = Dim.Fill (),
  1939. Height = Dim.Fill ()
  1940. };
  1941. view.LayoutComplete += e => {
  1942. view.DrawFrame (view.Bounds);
  1943. var savedClip = Application.Driver.Clip;
  1944. Application.Driver.Clip = new Rect (1, 1, view.Bounds.Width - 2, view.Bounds.Height - 2);
  1945. for (int row = 0; row < view.Bounds.Height - 2; row++) {
  1946. Application.Driver.Move (1, row + 1);
  1947. for (int col = 0; col < view.Bounds.Width - 2; col++) {
  1948. Application.Driver.AddStr ($"{col}");
  1949. }
  1950. }
  1951. Application.Driver.Clip = savedClip;
  1952. };
  1953. Application.Top.Add (view);
  1954. Application.Begin (Application.Top);
  1955. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  1956. var expected = @"
  1957. ┌──────────────────┐
  1958. │012345678910111213│
  1959. │012345678910111213│
  1960. │012345678910111213│
  1961. │012345678910111213│
  1962. │012345678910111213│
  1963. │012345678910111213│
  1964. │012345678910111213│
  1965. │012345678910111213│
  1966. └──────────────────┘
  1967. ";
  1968. var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1969. Assert.Equal (new Rect (0, 0, 20, 10), pos);
  1970. view.Clear (view.Bounds);
  1971. expected = @"
  1972. ";
  1973. pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output);
  1974. Assert.Equal (Rect.Empty, pos);
  1975. }
  1976. }
  1977. }