ViewTests.cs 26 KB

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