ViewTests.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.Views {
  11. public class ViewTests {
  12. [Fact]
  13. public void New_Initializes ()
  14. {
  15. // Parameterless
  16. var r = new View ();
  17. Assert.NotNull (r);
  18. Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
  19. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  20. Assert.False (r.CanFocus);
  21. Assert.False (r.HasFocus);
  22. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  23. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  24. Assert.Null (r.Focused);
  25. Assert.Null (r.ColorScheme);
  26. Assert.Equal (Dim.Sized (0), r.Width);
  27. Assert.Equal (Dim.Sized (0), r.Height);
  28. // FIXED: Pos needs equality implemented
  29. Assert.Equal (Pos.At (0), r.X);
  30. Assert.Equal (Pos.At (0), r.Y);
  31. Assert.False (r.IsCurrentTop);
  32. Assert.Empty (r.Id);
  33. Assert.Empty (r.Subviews);
  34. Assert.False (r.WantContinuousButtonPressed);
  35. Assert.False (r.WantMousePositionReports);
  36. Assert.Null (r.SuperView);
  37. Assert.Null (r.MostFocused);
  38. // Empty Rect
  39. r = new View (Rect.Empty);
  40. Assert.NotNull (r);
  41. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  42. Assert.Equal ("View()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
  43. Assert.False (r.CanFocus);
  44. Assert.False (r.HasFocus);
  45. Assert.Equal (new Rect (0, 0, 0, 0), r.Bounds);
  46. Assert.Equal (new Rect (0, 0, 0, 0), r.Frame);
  47. Assert.Null (r.Focused);
  48. Assert.Null (r.ColorScheme);
  49. Assert.NotNull (r.Width); // All view Dim are initialized now,
  50. Assert.NotNull (r.Height); // avoiding Dim errors.
  51. Assert.NotNull (r.X); // All view Pos are initialized now,
  52. Assert.NotNull (r.Y); // avoiding Pos errors.
  53. Assert.False (r.IsCurrentTop);
  54. Assert.Empty (r.Id);
  55. Assert.Empty (r.Subviews);
  56. Assert.False (r.WantContinuousButtonPressed);
  57. Assert.False (r.WantMousePositionReports);
  58. Assert.Null (r.SuperView);
  59. Assert.Null (r.MostFocused);
  60. // Rect with values
  61. r = new View (new Rect (1, 2, 3, 4));
  62. Assert.NotNull (r);
  63. Assert.Equal (LayoutStyle.Absolute, r.LayoutStyle);
  64. Assert.Equal ("View()({X=1,Y=2,Width=3,Height=4})", r.ToString ());
  65. Assert.False (r.CanFocus);
  66. Assert.False (r.HasFocus);
  67. Assert.Equal (new Rect (0, 0, 3, 4), r.Bounds);
  68. Assert.Equal (new Rect (1, 2, 3, 4), r.Frame);
  69. Assert.Null (r.Focused);
  70. Assert.Null (r.ColorScheme);
  71. Assert.NotNull (r.Width);
  72. Assert.NotNull (r.Height);
  73. Assert.NotNull (r.X);
  74. Assert.NotNull (r.Y);
  75. Assert.False (r.IsCurrentTop);
  76. Assert.Empty (r.Id);
  77. Assert.Empty (r.Subviews);
  78. Assert.False (r.WantContinuousButtonPressed);
  79. Assert.False (r.WantMousePositionReports);
  80. Assert.Null (r.SuperView);
  81. Assert.Null (r.MostFocused);
  82. }
  83. [Fact]
  84. public void New_Methods_Return_False ()
  85. {
  86. var r = new View ();
  87. Assert.False (r.ProcessKey (new KeyEvent () { Key = Key.Unknown }));
  88. Assert.False (r.ProcessHotKey (new KeyEvent () { Key = Key.Unknown }));
  89. Assert.False (r.ProcessColdKey (new KeyEvent () { Key = Key.Unknown }));
  90. Assert.False (r.OnKeyDown (new KeyEvent () { Key = Key.Unknown }));
  91. Assert.False (r.OnKeyUp (new KeyEvent () { Key = Key.Unknown }));
  92. Assert.False (r.MouseEvent (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  93. Assert.False (r.OnMouseEnter (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  94. Assert.False (r.OnMouseLeave (new MouseEvent () { Flags = MouseFlags.AllEvents }));
  95. Assert.False (r.OnEnter (new View ()));
  96. Assert.False (r.OnLeave (new View ()));
  97. // TODO: Add more
  98. }
  99. [Fact]
  100. public void TopologicalSort_Missing_Add ()
  101. {
  102. var root = new View ();
  103. var sub1 = new View ();
  104. root.Add (sub1);
  105. var sub2 = new View ();
  106. sub1.Width = Dim.Width (sub2);
  107. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  108. sub2.Width = Dim.Width (sub1);
  109. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  110. }
  111. [Fact]
  112. public void TopologicalSort_Recursive_Ref ()
  113. {
  114. var root = new View ();
  115. var sub1 = new View ();
  116. root.Add (sub1);
  117. var sub2 = new View ();
  118. root.Add (sub2);
  119. sub2.Width = Dim.Width (sub2);
  120. Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
  121. }
  122. [Fact]
  123. public void Added_Removed ()
  124. {
  125. var v = new View (new Rect (0, 0, 10, 24));
  126. var t = new View ();
  127. v.Added += (View e) => {
  128. Assert.True (v.SuperView == e);
  129. };
  130. v.Removed += (View e) => {
  131. Assert.True (v.SuperView == null);
  132. };
  133. t.Add (v);
  134. Assert.True (t.Subviews.Count == 1);
  135. t.Remove (v);
  136. Assert.True (t.Subviews.Count == 0);
  137. }
  138. [Fact]
  139. public void Subviews_TabIndexes_AreEqual ()
  140. {
  141. var r = new View ();
  142. var v1 = new View () { CanFocus = true };
  143. var v2 = new View () { CanFocus = true };
  144. var v3 = new View () { CanFocus = true };
  145. r.Add (v1, v2, v3);
  146. Assert.True (r.Subviews.IndexOf (v1) == 0);
  147. Assert.True (r.Subviews.IndexOf (v2) == 1);
  148. Assert.True (r.Subviews.IndexOf (v3) == 2);
  149. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  150. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  151. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  152. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  153. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  154. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  155. }
  156. [Fact]
  157. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  158. {
  159. var r = new View ();
  160. var v1 = new View () { CanFocus = true };
  161. var v2 = new View () { CanFocus = true };
  162. var v3 = new View () { CanFocus = true };
  163. r.Add (v1, v2, v3);
  164. r.BringSubviewToFront (v1);
  165. Assert.True (r.Subviews.IndexOf (v1) == 2);
  166. Assert.True (r.Subviews.IndexOf (v2) == 0);
  167. Assert.True (r.Subviews.IndexOf (v3) == 1);
  168. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  169. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  170. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  171. }
  172. [Fact]
  173. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  174. {
  175. var r = new View ();
  176. var v1 = new View () { CanFocus = true };
  177. var v2 = new View () { CanFocus = true };
  178. var v3 = new View () { CanFocus = true };
  179. r.Add (v1, v2, v3);
  180. r.BringSubviewForward (v1);
  181. Assert.True (r.Subviews.IndexOf (v1) == 1);
  182. Assert.True (r.Subviews.IndexOf (v2) == 0);
  183. Assert.True (r.Subviews.IndexOf (v3) == 2);
  184. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  185. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  186. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  187. }
  188. [Fact]
  189. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  190. {
  191. var r = new View ();
  192. var v1 = new View () { CanFocus = true };
  193. var v2 = new View () { CanFocus = true };
  194. var v3 = new View () { CanFocus = true };
  195. r.Add (v1, v2, v3);
  196. r.SendSubviewToBack (v3);
  197. Assert.True (r.Subviews.IndexOf (v1) == 1);
  198. Assert.True (r.Subviews.IndexOf (v2) == 2);
  199. Assert.True (r.Subviews.IndexOf (v3) == 0);
  200. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  201. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  202. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  203. }
  204. [Fact]
  205. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  206. {
  207. var r = new View ();
  208. var v1 = new View () { CanFocus = true };
  209. var v2 = new View () { CanFocus = true };
  210. var v3 = new View () { CanFocus = true };
  211. r.Add (v1, v2, v3);
  212. r.SendSubviewBackwards (v3);
  213. Assert.True (r.Subviews.IndexOf (v1) == 0);
  214. Assert.True (r.Subviews.IndexOf (v2) == 2);
  215. Assert.True (r.Subviews.IndexOf (v3) == 1);
  216. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  217. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  218. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  219. }
  220. [Fact]
  221. public void TabIndex_Set_CanFocus_ValidValues ()
  222. {
  223. var r = new View ();
  224. var v1 = new View () { CanFocus = true };
  225. var v2 = new View () { CanFocus = true };
  226. var v3 = new View () { CanFocus = true };
  227. r.Add (v1, v2, v3);
  228. v1.TabIndex = 1;
  229. Assert.True (r.Subviews.IndexOf (v1) == 0);
  230. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  231. v1.TabIndex = 2;
  232. Assert.True (r.Subviews.IndexOf (v1) == 0);
  233. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  234. }
  235. [Fact]
  236. public void TabIndex_Set_CanFocus_HigherValues ()
  237. {
  238. var r = new View ();
  239. var v1 = new View () { CanFocus = true };
  240. var v2 = new View () { CanFocus = true };
  241. var v3 = new View () { CanFocus = true };
  242. r.Add (v1, v2, v3);
  243. v1.TabIndex = 3;
  244. Assert.True (r.Subviews.IndexOf (v1) == 0);
  245. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  246. }
  247. [Fact]
  248. public void TabIndex_Set_CanFocus_LowerValues ()
  249. {
  250. var r = new View ();
  251. var v1 = new View () { CanFocus = true };
  252. var v2 = new View () { CanFocus = true };
  253. var v3 = new View () { CanFocus = true };
  254. r.Add (v1, v2, v3);
  255. v1.TabIndex = -1;
  256. Assert.True (r.Subviews.IndexOf (v1) == 0);
  257. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  258. }
  259. [Fact]
  260. public void TabIndex_Set_CanFocus_False ()
  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. v1.CanFocus = false;
  268. v1.TabIndex = 0;
  269. Assert.True (r.Subviews.IndexOf (v1) == 0);
  270. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  271. Assert.Equal (-1, v1.TabIndex);
  272. }
  273. [Fact]
  274. public void TabIndex_Set_CanFocus_False_To_True ()
  275. {
  276. var r = new View ();
  277. var v1 = new View ();
  278. var v2 = new View () { CanFocus = true };
  279. var v3 = new View () { CanFocus = true };
  280. r.Add (v1, v2, v3);
  281. v1.CanFocus = true;
  282. v1.TabIndex = 1;
  283. Assert.True (r.Subviews.IndexOf (v1) == 0);
  284. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  285. }
  286. [Fact]
  287. public void TabStop_And_CanFocus_Are_All_True ()
  288. {
  289. var r = new View ();
  290. var v1 = new View () { CanFocus = true };
  291. var v2 = new View () { CanFocus = true };
  292. var v3 = new View () { CanFocus = true };
  293. r.Add (v1, v2, v3);
  294. r.FocusNext ();
  295. Assert.True (v1.HasFocus);
  296. Assert.False (v2.HasFocus);
  297. Assert.False (v3.HasFocus);
  298. r.FocusNext ();
  299. Assert.False (v1.HasFocus);
  300. Assert.True (v2.HasFocus);
  301. Assert.False (v3.HasFocus);
  302. r.FocusNext ();
  303. Assert.False (v1.HasFocus);
  304. Assert.False (v2.HasFocus);
  305. Assert.True (v3.HasFocus);
  306. }
  307. [Fact]
  308. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  309. {
  310. var r = new View ();
  311. var v1 = new View ();
  312. var v2 = new View ();
  313. var v3 = new View ();
  314. r.Add (v1, v2, v3);
  315. r.FocusNext ();
  316. Assert.False (v1.HasFocus);
  317. Assert.False (v2.HasFocus);
  318. Assert.False (v3.HasFocus);
  319. r.FocusNext ();
  320. Assert.False (v1.HasFocus);
  321. Assert.False (v2.HasFocus);
  322. Assert.False (v3.HasFocus);
  323. r.FocusNext ();
  324. Assert.False (v1.HasFocus);
  325. Assert.False (v2.HasFocus);
  326. Assert.False (v3.HasFocus);
  327. }
  328. [Fact]
  329. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  330. {
  331. var r = new View ();
  332. var v1 = new View () { CanFocus = true, TabStop = false };
  333. var v2 = new View () { CanFocus = true, TabStop = false };
  334. var v3 = new View () { CanFocus = true, TabStop = false };
  335. r.Add (v1, v2, v3);
  336. r.FocusNext ();
  337. Assert.False (v1.HasFocus);
  338. Assert.False (v2.HasFocus);
  339. Assert.False (v3.HasFocus);
  340. r.FocusNext ();
  341. Assert.False (v1.HasFocus);
  342. Assert.False (v2.HasFocus);
  343. Assert.False (v3.HasFocus);
  344. r.FocusNext ();
  345. Assert.False (v1.HasFocus);
  346. Assert.False (v2.HasFocus);
  347. Assert.False (v3.HasFocus);
  348. }
  349. [Fact]
  350. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  351. {
  352. var r = new View ();
  353. var v1 = new View () { CanFocus = true, TabStop = false };
  354. var v2 = new View () { CanFocus = false, TabStop = true };
  355. var v3 = new View () { CanFocus = false, TabStop = false };
  356. r.Add (v1, v2, v3);
  357. r.FocusNext ();
  358. Assert.False (v1.HasFocus);
  359. Assert.False (v2.HasFocus);
  360. Assert.False (v3.HasFocus);
  361. r.FocusNext ();
  362. Assert.False (v1.HasFocus);
  363. Assert.False (v2.HasFocus);
  364. Assert.False (v3.HasFocus);
  365. r.FocusNext ();
  366. Assert.False (v1.HasFocus);
  367. Assert.False (v2.HasFocus);
  368. Assert.False (v3.HasFocus);
  369. }
  370. [Fact]
  371. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  372. {
  373. var r = new View ();
  374. var v1 = new View ();
  375. var v2 = new View ();
  376. var v3 = new View ();
  377. r.Add (v1, v2, v3);
  378. r.FocusNext ();
  379. Assert.False (v1.HasFocus);
  380. Assert.False (v2.HasFocus);
  381. Assert.False (v3.HasFocus);
  382. v1.CanFocus = true;
  383. r.FocusNext ();
  384. Assert.True (v1.HasFocus);
  385. Assert.False (v2.HasFocus);
  386. Assert.False (v3.HasFocus);
  387. v2.CanFocus = true;
  388. r.FocusNext ();
  389. Assert.False (v1.HasFocus);
  390. Assert.True (v2.HasFocus);
  391. Assert.False (v3.HasFocus);
  392. v3.CanFocus = true;
  393. r.FocusNext ();
  394. Assert.False (v1.HasFocus);
  395. Assert.False (v2.HasFocus);
  396. Assert.True (v3.HasFocus);
  397. }
  398. [Fact]
  399. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  400. {
  401. var r = new View ();
  402. var v1 = new View () { CanFocus = true, TabStop = false };
  403. var v2 = new View () { CanFocus = true, TabStop = false };
  404. var v3 = new View () { CanFocus = true, TabStop = false };
  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.TabStop = true;
  411. r.FocusNext ();
  412. Assert.True (v1.HasFocus);
  413. Assert.False (v2.HasFocus);
  414. Assert.False (v3.HasFocus);
  415. v2.TabStop = true;
  416. r.FocusNext ();
  417. Assert.False (v1.HasFocus);
  418. Assert.True (v2.HasFocus);
  419. Assert.False (v3.HasFocus);
  420. v3.TabStop = 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 CanFocus_Set_Changes_TabIndex_And_TabStop ()
  428. {
  429. var r = new View ();
  430. var v1 = new View ("1");
  431. var v2 = new View ("2");
  432. var v3 = new View ("3");
  433. r.Add (v1, v2, v3);
  434. v2.CanFocus = true;
  435. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  436. Assert.Equal (0, v2.TabIndex);
  437. Assert.True (v2.TabStop);
  438. v1.CanFocus = true;
  439. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  440. Assert.Equal (1, v1.TabIndex);
  441. Assert.True (v1.TabStop);
  442. v1.TabIndex = 2;
  443. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  444. Assert.Equal (1, v1.TabIndex);
  445. v3.CanFocus = true;
  446. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  447. Assert.Equal (1, v1.TabIndex);
  448. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  449. Assert.Equal (2, v3.TabIndex);
  450. Assert.True (v3.TabStop);
  451. v2.CanFocus = false;
  452. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  453. Assert.Equal (1, v1.TabIndex);
  454. Assert.True (v1.TabStop);
  455. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  456. Assert.Equal (-1, v2.TabIndex);
  457. Assert.False (v2.TabStop);
  458. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  459. Assert.Equal (2, v3.TabIndex);
  460. Assert.True (v3.TabStop);
  461. }
  462. [Fact]
  463. public void Initialized_Event_Comparing_With_Added_Event ()
  464. {
  465. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  466. var t = new Toplevel () { Id = "0", };
  467. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  468. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  469. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  470. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  471. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  472. w.Added += (e) => {
  473. Assert.Equal (e.Frame.Width, w.Frame.Width);
  474. Assert.Equal (e.Frame.Height, w.Frame.Height);
  475. };
  476. v1.Added += (e) => {
  477. Assert.Equal (e.Frame.Width, v1.Frame.Width);
  478. Assert.Equal (e.Frame.Height, v1.Frame.Height);
  479. };
  480. v2.Added += (e) => {
  481. Assert.Equal (e.Frame.Width, v2.Frame.Width);
  482. Assert.Equal (e.Frame.Height, v2.Frame.Height);
  483. };
  484. sv1.Added += (e) => {
  485. Assert.Equal (e.Frame.Width, sv1.Frame.Width);
  486. Assert.Equal (e.Frame.Height, sv1.Frame.Height);
  487. };
  488. t.Initialized += (s, e) => {
  489. tc++;
  490. Assert.Equal (1, tc);
  491. Assert.Equal (1, wc);
  492. Assert.Equal (1, v1c);
  493. Assert.Equal (1, v2c);
  494. Assert.Equal (1, sv1c);
  495. Assert.True (t.CanFocus);
  496. Assert.True (w.CanFocus);
  497. Assert.False (v1.CanFocus);
  498. Assert.False (v2.CanFocus);
  499. Assert.False (sv1.CanFocus);
  500. Application.Refresh ();
  501. };
  502. w.Initialized += (s, e) => {
  503. wc++;
  504. Assert.Equal (t.Frame.Width, w.Frame.Width);
  505. Assert.Equal (t.Frame.Height, w.Frame.Height);
  506. };
  507. v1.Initialized += (s, e) => {
  508. v1c++;
  509. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  510. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  511. };
  512. v2.Initialized += (s, e) => {
  513. v2c++;
  514. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  515. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  516. };
  517. sv1.Initialized += (s, e) => {
  518. sv1c++;
  519. Assert.Equal (t.Frame.Width, sv1.Frame.Width);
  520. Assert.Equal (t.Frame.Height, sv1.Frame.Height);
  521. Assert.False (sv1.CanFocus);
  522. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  523. Assert.False (sv1.CanFocus);
  524. };
  525. v1.Add (sv1);
  526. w.Add (v1, v2);
  527. t.Add (w);
  528. Application.Iteration = () => {
  529. Application.Refresh ();
  530. t.Running = false;
  531. };
  532. Application.Run (t);
  533. Application.Shutdown ();
  534. Assert.Equal (1, tc);
  535. Assert.Equal (1, wc);
  536. Assert.Equal (1, v1c);
  537. Assert.Equal (1, v2c);
  538. Assert.Equal (1, sv1c);
  539. Assert.True (t.CanFocus);
  540. Assert.True (w.CanFocus);
  541. Assert.False (v1.CanFocus);
  542. Assert.False (v2.CanFocus);
  543. Assert.False (sv1.CanFocus);
  544. v1.CanFocus = true;
  545. Assert.False (sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1.
  546. }
  547. [Fact]
  548. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  549. {
  550. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  551. var t = new Toplevel () { Id = "0", };
  552. var w = new Window () { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  553. var v1 = new View () { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  554. var v2 = new View () { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  555. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  556. t.Initialized += (s, e) => {
  557. tc++;
  558. Assert.Equal (1, tc);
  559. Assert.Equal (1, wc);
  560. Assert.Equal (1, v1c);
  561. Assert.Equal (1, v2c);
  562. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  563. Assert.True (t.CanFocus);
  564. Assert.True (w.CanFocus);
  565. Assert.False (v1.CanFocus);
  566. Assert.False (v2.CanFocus);
  567. Application.Refresh ();
  568. };
  569. w.Initialized += (s, e) => {
  570. wc++;
  571. Assert.Equal (t.Frame.Width, w.Frame.Width);
  572. Assert.Equal (t.Frame.Height, w.Frame.Height);
  573. };
  574. v1.Initialized += (s, e) => {
  575. v1c++;
  576. Assert.Equal (t.Frame.Width, v1.Frame.Width);
  577. Assert.Equal (t.Frame.Height, v1.Frame.Height);
  578. };
  579. v2.Initialized += (s, e) => {
  580. v2c++;
  581. Assert.Equal (t.Frame.Width, v2.Frame.Width);
  582. Assert.Equal (t.Frame.Height, v2.Frame.Height);
  583. };
  584. w.Add (v1, v2);
  585. t.Add (w);
  586. Application.Iteration = () => {
  587. var sv1 = new View () { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  588. sv1.Initialized += (s, e) => {
  589. sv1c++;
  590. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  591. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  592. Assert.False (sv1.CanFocus);
  593. Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  594. Assert.False (sv1.CanFocus);
  595. };
  596. v1.Add (sv1);
  597. Application.Refresh ();
  598. t.Running = false;
  599. };
  600. Application.Run (t);
  601. Application.Shutdown ();
  602. Assert.Equal (1, tc);
  603. Assert.Equal (1, wc);
  604. Assert.Equal (1, v1c);
  605. Assert.Equal (1, v2c);
  606. Assert.Equal (1, sv1c);
  607. Assert.True (t.CanFocus);
  608. Assert.True (w.CanFocus);
  609. Assert.False (v1.CanFocus);
  610. Assert.False (v2.CanFocus);
  611. }
  612. [Fact]
  613. public void CanFocus_Faced_With_Container ()
  614. {
  615. var t = new Toplevel ();
  616. var w = new Window ();
  617. var f = new FrameView ();
  618. var v = new View () { CanFocus = true };
  619. f.Add (v);
  620. w.Add (f);
  621. t.Add (w);
  622. Assert.True (t.CanFocus);
  623. Assert.True (w.CanFocus);
  624. Assert.True (f.CanFocus);
  625. Assert.True (v.CanFocus);
  626. f.CanFocus = false;
  627. Assert.False (f.CanFocus);
  628. Assert.True (v.CanFocus);
  629. v.CanFocus = false;
  630. Assert.False (f.CanFocus);
  631. Assert.False (v.CanFocus);
  632. v.CanFocus = true;
  633. Assert.False (f.CanFocus);
  634. Assert.True (v.CanFocus);
  635. }
  636. [Fact]
  637. public void CanFocus_Faced_With_Container_Before_Run ()
  638. {
  639. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  640. var t = Application.Top;
  641. var w = new Window ("w");
  642. var f = new FrameView ("f");
  643. var v = new View ("v") { CanFocus = true };
  644. f.Add (v);
  645. w.Add (f);
  646. t.Add (w);
  647. Assert.True (t.CanFocus);
  648. Assert.True (w.CanFocus);
  649. Assert.True (f.CanFocus);
  650. Assert.True (v.CanFocus);
  651. f.CanFocus = false;
  652. Assert.False (f.CanFocus);
  653. Assert.True (v.CanFocus);
  654. v.CanFocus = false;
  655. Assert.False (f.CanFocus);
  656. Assert.False (v.CanFocus);
  657. v.CanFocus = true;
  658. Assert.False (f.CanFocus);
  659. Assert.True (v.CanFocus);
  660. Application.Iteration += () => Application.RequestStop ();
  661. Application.Run ();
  662. Application.Shutdown ();
  663. }
  664. [Fact]
  665. public void CanFocus_Faced_With_Container_After_Run ()
  666. {
  667. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  668. var t = Application.Top;
  669. var w = new Window ("w");
  670. var f = new FrameView ("f");
  671. var v = new View ("v") { CanFocus = true };
  672. f.Add (v);
  673. w.Add (f);
  674. t.Add (w);
  675. t.Ready += () => {
  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.False (v.CanFocus);
  683. v.CanFocus = false;
  684. Assert.False (f.CanFocus);
  685. Assert.False (v.CanFocus);
  686. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  687. Assert.False (f.CanFocus);
  688. Assert.False (v.CanFocus);
  689. f.CanFocus = true;
  690. Assert.True (f.CanFocus);
  691. Assert.True (v.CanFocus);
  692. };
  693. Application.Iteration += () => Application.RequestStop ();
  694. Application.Run ();
  695. Application.Shutdown ();
  696. }
  697. [Fact]
  698. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  699. {
  700. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  701. var t = Application.Top;
  702. var w = new Window ("w");
  703. var f = new FrameView ("f");
  704. var v1 = new View ("v1") { CanFocus = true };
  705. var v2 = new View ("v2") { CanFocus = true };
  706. f.Add (v1, v2);
  707. w.Add (f);
  708. t.Add (w);
  709. t.Ready += () => {
  710. Assert.True (t.CanFocus);
  711. Assert.True (w.CanFocus);
  712. Assert.True (f.CanFocus);
  713. Assert.True (v1.CanFocus);
  714. Assert.True (v2.CanFocus);
  715. w.CanFocus = false;
  716. Assert.True (w.CanFocus);
  717. Assert.False (f.CanFocus);
  718. Assert.False (v1.CanFocus);
  719. Assert.False (v2.CanFocus);
  720. };
  721. Application.Iteration += () => Application.RequestStop ();
  722. Application.Run ();
  723. Application.Shutdown ();
  724. }
  725. [Fact]
  726. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  727. {
  728. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  729. var t = Application.Top;
  730. var w = new Window ("w");
  731. var f = new FrameView ("f");
  732. var v1 = new View ("v1");
  733. var v2 = new View ("v2") { CanFocus = true };
  734. f.Add (v1, v2);
  735. w.Add (f);
  736. t.Add (w);
  737. t.Ready += () => {
  738. Assert.True (t.CanFocus);
  739. Assert.True (w.CanFocus);
  740. Assert.True (f.CanFocus);
  741. Assert.False (v1.CanFocus);
  742. Assert.True (v2.CanFocus);
  743. w.CanFocus = false;
  744. Assert.True (w.CanFocus);
  745. Assert.False (f.CanFocus);
  746. Assert.False (v1.CanFocus);
  747. Assert.False (v2.CanFocus);
  748. w.CanFocus = true;
  749. Assert.True (w.CanFocus);
  750. Assert.True (f.CanFocus);
  751. Assert.False (v1.CanFocus);
  752. Assert.True (v2.CanFocus);
  753. };
  754. Application.Iteration += () => Application.RequestStop ();
  755. Application.Run ();
  756. Application.Shutdown ();
  757. }
  758. [Fact]
  759. public void Navigation_With_Null_Focused_View ()
  760. {
  761. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  762. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  763. Application.Top.Ready += () => {
  764. Assert.Null (Application.Top.Focused);
  765. };
  766. // Keyboard navigation with tab
  767. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  768. Application.Iteration += () => Application.RequestStop ();
  769. Application.Run ();
  770. Application.Shutdown ();
  771. }
  772. [Fact]
  773. public void Multi_Thread_Toplevels ()
  774. {
  775. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  776. var t = Application.Top;
  777. var w = new Window ();
  778. t.Add (w);
  779. int count = 0, count1 = 0, count2 = 0;
  780. bool log = false, log1 = false, log2 = false;
  781. bool fromTopStillKnowFirstIsRunning = false;
  782. bool fromTopStillKnowSecondIsRunning = false;
  783. bool fromFirstStillKnowSecondIsRunning = false;
  784. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  785. count++;
  786. if (count1 == 5) {
  787. log1 = true;
  788. }
  789. if (count1 == 14 && count2 == 10 && count == 15) { // count2 is already stopped
  790. fromTopStillKnowFirstIsRunning = true;
  791. }
  792. if (count1 == 7 && count2 == 7 && count == 8) {
  793. fromTopStillKnowSecondIsRunning = true;
  794. }
  795. if (count == 30) {
  796. Assert.Equal (30, count);
  797. Assert.Equal (20, count1);
  798. Assert.Equal (10, count2);
  799. Assert.True (log);
  800. Assert.True (log1);
  801. Assert.True (log2);
  802. Assert.True (fromTopStillKnowFirstIsRunning);
  803. Assert.True (fromTopStillKnowSecondIsRunning);
  804. Assert.True (fromFirstStillKnowSecondIsRunning);
  805. Application.RequestStop ();
  806. return false;
  807. }
  808. return true;
  809. });
  810. t.Ready += FirstDialogToplevel;
  811. void FirstDialogToplevel ()
  812. {
  813. var od = new OpenDialog ();
  814. od.Ready += SecoundDialogToplevel;
  815. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  816. count1++;
  817. if (count2 == 5) {
  818. log2 = true;
  819. }
  820. if (count2 == 4 && count1 == 5 && count == 5) {
  821. fromFirstStillKnowSecondIsRunning = true;
  822. }
  823. if (count1 == 20) {
  824. Assert.Equal (20, count1);
  825. Application.RequestStop ();
  826. return false;
  827. }
  828. return true;
  829. });
  830. Application.Run (od);
  831. }
  832. void SecoundDialogToplevel ()
  833. {
  834. var d = new Dialog ();
  835. Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => {
  836. count2++;
  837. if (count < 30) {
  838. log = true;
  839. }
  840. if (count2 == 10) {
  841. Assert.Equal (10, count2);
  842. Application.RequestStop ();
  843. return false;
  844. }
  845. return true;
  846. });
  847. Application.Run (d);
  848. }
  849. Application.Run ();
  850. Application.Shutdown ();
  851. }
  852. [Fact]
  853. public void View_Difference_Between_An_Object_Initializer_And_A_Constructor ()
  854. {
  855. // Object Initializer
  856. var view = new View () {
  857. X = 1,
  858. Y = 2,
  859. Width = 3,
  860. Height = 4
  861. };
  862. Assert.Equal (1, view.X);
  863. Assert.Equal (2, view.Y);
  864. Assert.Equal (3, view.Width);
  865. Assert.Equal (4, view.Height);
  866. Assert.True (view.Frame.IsEmpty);
  867. Assert.True (view.Bounds.IsEmpty);
  868. view.LayoutSubviews ();
  869. Assert.Equal (1, view.X);
  870. Assert.Equal (2, view.Y);
  871. Assert.Equal (3, view.Width);
  872. Assert.Equal (4, view.Height);
  873. Assert.False (view.Frame.IsEmpty);
  874. Assert.False (view.Bounds.IsEmpty);
  875. // Default Constructor
  876. view = new View ();
  877. Assert.Equal (0, view.X);
  878. Assert.Equal (0, view.Y);
  879. Assert.Equal (0, view.Width);
  880. Assert.Equal (0, view.Height);
  881. Assert.True (view.Frame.IsEmpty);
  882. Assert.True (view.Bounds.IsEmpty);
  883. // Constructor
  884. view = new View (1, 2, "");
  885. Assert.NotNull (view.X);
  886. Assert.NotNull (view.Y);
  887. Assert.NotNull (view.Width);
  888. Assert.NotNull (view.Height);
  889. Assert.False (view.Frame.IsEmpty);
  890. Assert.True (view.Bounds.IsEmpty);
  891. // Default Constructor and post assignment equivalent to Object Initializer
  892. view = new View ();
  893. view.X = 1;
  894. view.Y = 2;
  895. view.Width = 3;
  896. view.Height = 4;
  897. Assert.Equal (1, view.X);
  898. Assert.Equal (2, view.Y);
  899. Assert.Equal (3, view.Width);
  900. Assert.Equal (4, view.Height);
  901. Assert.True (view.Frame.IsEmpty);
  902. Assert.True (view.Bounds.IsEmpty);
  903. }
  904. [Fact]
  905. public void FocusNearestView_Ensure_Focus_Ordered ()
  906. {
  907. var top = new Toplevel ();
  908. var win = new Window ();
  909. var winSubview = new View ("WindowSubview") {
  910. CanFocus = true
  911. };
  912. win.Add (winSubview);
  913. top.Add (win);
  914. var frm = new FrameView ();
  915. var frmSubview = new View ("FrameSubview") {
  916. CanFocus = true
  917. };
  918. frm.Add (frmSubview);
  919. top.Add (frm);
  920. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  921. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  922. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  923. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  924. top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
  925. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  926. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  927. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  928. top.ProcessKey (new KeyEvent (Key.BackTab | Key.ShiftMask, new KeyModifiers ()));
  929. Assert.Equal ($"WindowSubview", top.MostFocused.Text);
  930. }
  931. [Fact]
  932. public void KeyPress_Handled_To_True_Prevents_Changes ()
  933. {
  934. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  935. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  936. var top = Application.Top;
  937. var text = new TextField ("");
  938. text.KeyPress += (e) => {
  939. e.Handled = true;
  940. Assert.True (e.Handled);
  941. Assert.Equal (Key.N, e.KeyEvent.Key);
  942. };
  943. top.Add (text);
  944. Application.Iteration += () => {
  945. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('N', ConsoleKey.N, false, false, false));
  946. Assert.Equal ("", text.Text);
  947. Application.RequestStop ();
  948. };
  949. Application.Run ();
  950. }
  951. }
  952. }