NavigationTests.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. using Xunit.Abstractions;
  2. // Alias Console to MockConsole so we don't accidentally use Console
  3. using Console = Terminal.Gui.FakeConsole;
  4. namespace Terminal.Gui.ViewTests;
  5. public class NavigationTests
  6. {
  7. private readonly ITestOutputHelper _output;
  8. public NavigationTests (ITestOutputHelper output) { _output = output; }
  9. [Fact]
  10. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  11. {
  12. var r = new View ();
  13. var v1 = new View { CanFocus = true };
  14. var v2 = new View { CanFocus = true };
  15. var v3 = new View { CanFocus = true };
  16. r.Add (v1, v2, v3);
  17. r.BringSubviewForward (v1);
  18. Assert.True (r.Subviews.IndexOf (v1) == 1);
  19. Assert.True (r.Subviews.IndexOf (v2) == 0);
  20. Assert.True (r.Subviews.IndexOf (v3) == 2);
  21. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  22. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  23. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  24. r.Dispose ();
  25. }
  26. [Fact]
  27. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  28. {
  29. var r = new View ();
  30. var v1 = new View { CanFocus = true };
  31. var v2 = new View { CanFocus = true };
  32. var v3 = new View { CanFocus = true };
  33. r.Add (v1, v2, v3);
  34. r.BringSubviewToFront (v1);
  35. Assert.True (r.Subviews.IndexOf (v1) == 2);
  36. Assert.True (r.Subviews.IndexOf (v2) == 0);
  37. Assert.True (r.Subviews.IndexOf (v3) == 1);
  38. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  39. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  40. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  41. r.Dispose ();
  42. }
  43. [Fact]
  44. public void CanFocus_Container_ToFalse_Turns_All_Subviews_ToFalse_Too ()
  45. {
  46. Application.Init (new FakeDriver ());
  47. Toplevel t = Application.Top;
  48. var w = new Window ();
  49. var f = new FrameView ();
  50. var v1 = new View { CanFocus = true };
  51. var v2 = new View { CanFocus = true };
  52. f.Add (v1, v2);
  53. w.Add (f);
  54. t.Add (w);
  55. t.Ready += (s, e) =>
  56. {
  57. Assert.True (t.CanFocus);
  58. Assert.True (w.CanFocus);
  59. Assert.True (f.CanFocus);
  60. Assert.True (v1.CanFocus);
  61. Assert.True (v2.CanFocus);
  62. w.CanFocus = false;
  63. Assert.False (w.CanFocus);
  64. Assert.False (f.CanFocus);
  65. Assert.False (v1.CanFocus);
  66. Assert.False (v2.CanFocus);
  67. };
  68. Application.Iteration += (s, a) => Application.RequestStop ();
  69. Application.Run ();
  70. Application.Shutdown ();
  71. }
  72. [Fact]
  73. public void CanFocus_Container_Toggling_All_Subviews_To_Old_Value_When_Is_True ()
  74. {
  75. Application.Init (new FakeDriver ());
  76. Toplevel t = Application.Top;
  77. var w = new Window ();
  78. var f = new FrameView ();
  79. var v1 = new View ();
  80. var v2 = new View { CanFocus = true };
  81. f.Add (v1, v2);
  82. w.Add (f);
  83. t.Add (w);
  84. t.Ready += (s, e) =>
  85. {
  86. Assert.True (t.CanFocus);
  87. Assert.True (w.CanFocus);
  88. Assert.True (f.CanFocus);
  89. Assert.False (v1.CanFocus);
  90. Assert.True (v2.CanFocus);
  91. w.CanFocus = false;
  92. Assert.False (w.CanFocus);
  93. Assert.False (f.CanFocus);
  94. Assert.False (v1.CanFocus);
  95. Assert.False (v2.CanFocus);
  96. w.CanFocus = true;
  97. Assert.True (w.CanFocus);
  98. Assert.True (f.CanFocus);
  99. Assert.False (v1.CanFocus);
  100. Assert.True (v2.CanFocus);
  101. };
  102. Application.Iteration += (s, a) => Application.RequestStop ();
  103. Application.Run ();
  104. Application.Shutdown ();
  105. }
  106. [Fact]
  107. [AutoInitShutdown]
  108. public void CanFocus_Faced_With_Container ()
  109. {
  110. var t = new Toplevel ();
  111. var w = new Window ();
  112. var f = new FrameView ();
  113. var v = new View { CanFocus = true };
  114. f.Add (v);
  115. w.Add (f);
  116. t.Add (w);
  117. Assert.True (t.CanFocus);
  118. Assert.True (w.CanFocus);
  119. Assert.True (f.CanFocus);
  120. Assert.True (v.CanFocus);
  121. f.CanFocus = false;
  122. Assert.False (f.CanFocus);
  123. Assert.True (v.CanFocus);
  124. v.CanFocus = false;
  125. Assert.False (f.CanFocus);
  126. Assert.False (v.CanFocus);
  127. v.CanFocus = true;
  128. Assert.False (f.CanFocus);
  129. Assert.True (v.CanFocus);
  130. t.Dispose ();
  131. }
  132. [Fact]
  133. public void CanFocus_Faced_With_Container_After_Run ()
  134. {
  135. Application.Init (new FakeDriver ());
  136. Toplevel t = Application.Top;
  137. var w = new Window ();
  138. var f = new FrameView ();
  139. var v = new View { CanFocus = true };
  140. f.Add (v);
  141. w.Add (f);
  142. t.Add (w);
  143. t.Ready += (s, e) =>
  144. {
  145. Assert.True (t.CanFocus);
  146. Assert.True (w.CanFocus);
  147. Assert.True (f.CanFocus);
  148. Assert.True (v.CanFocus);
  149. f.CanFocus = false;
  150. Assert.False (f.CanFocus);
  151. Assert.False (v.CanFocus);
  152. v.CanFocus = false;
  153. Assert.False (f.CanFocus);
  154. Assert.False (v.CanFocus);
  155. Assert.Throws<InvalidOperationException> (() => v.CanFocus = true);
  156. Assert.False (f.CanFocus);
  157. Assert.False (v.CanFocus);
  158. f.CanFocus = true;
  159. Assert.True (f.CanFocus);
  160. Assert.True (v.CanFocus);
  161. };
  162. Application.Iteration += (s, a) => Application.RequestStop ();
  163. Application.Run ();
  164. Application.Shutdown ();
  165. }
  166. [Fact]
  167. public void CanFocus_Faced_With_Container_Before_Run ()
  168. {
  169. Application.Init (new FakeDriver ());
  170. Toplevel t = Application.Top;
  171. var w = new Window ();
  172. var f = new FrameView ();
  173. var v = new View { CanFocus = true };
  174. f.Add (v);
  175. w.Add (f);
  176. t.Add (w);
  177. Assert.True (t.CanFocus);
  178. Assert.True (w.CanFocus);
  179. Assert.True (f.CanFocus);
  180. Assert.True (v.CanFocus);
  181. f.CanFocus = false;
  182. Assert.False (f.CanFocus);
  183. Assert.True (v.CanFocus);
  184. v.CanFocus = false;
  185. Assert.False (f.CanFocus);
  186. Assert.False (v.CanFocus);
  187. v.CanFocus = true;
  188. Assert.False (f.CanFocus);
  189. Assert.True (v.CanFocus);
  190. Application.Iteration += (s, a) => Application.RequestStop ();
  191. Application.Run ();
  192. Application.Shutdown ();
  193. }
  194. [Fact]
  195. public void CanFocus_Set_Changes_TabIndex_And_TabStop ()
  196. {
  197. var r = new View ();
  198. var v1 = new View { Text = "1" };
  199. var v2 = new View { Text = "2" };
  200. var v3 = new View { Text = "3" };
  201. r.Add (v1, v2, v3);
  202. v2.CanFocus = true;
  203. Assert.Equal (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  204. Assert.Equal (0, v2.TabIndex);
  205. Assert.True (v2.TabStop);
  206. v1.CanFocus = true;
  207. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  208. Assert.Equal (1, v1.TabIndex);
  209. Assert.True (v1.TabStop);
  210. v1.TabIndex = 2;
  211. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  212. Assert.Equal (1, v1.TabIndex);
  213. v3.CanFocus = true;
  214. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  215. Assert.Equal (1, v1.TabIndex);
  216. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  217. Assert.Equal (2, v3.TabIndex);
  218. Assert.True (v3.TabStop);
  219. v2.CanFocus = false;
  220. Assert.Equal (r.TabIndexes.IndexOf (v1), v1.TabIndex);
  221. Assert.Equal (1, v1.TabIndex);
  222. Assert.True (v1.TabStop);
  223. Assert.NotEqual (r.TabIndexes.IndexOf (v2), v2.TabIndex);
  224. Assert.Equal (-1, v2.TabIndex);
  225. Assert.False (v2.TabStop);
  226. Assert.Equal (r.TabIndexes.IndexOf (v3), v3.TabIndex);
  227. Assert.Equal (2, v3.TabIndex);
  228. Assert.True (v3.TabStop);
  229. r.Dispose ();
  230. }
  231. [Fact]
  232. [AutoInitShutdown]
  233. public void CanFocus_Sets_To_False_Does_Not_Sets_HasFocus_To_True ()
  234. {
  235. var view = new View { CanFocus = true };
  236. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  237. win.Add (view);
  238. Application.Top.Add (win);
  239. Application.Begin (Application.Top);
  240. Assert.True (view.CanFocus);
  241. Assert.True (view.HasFocus);
  242. view.CanFocus = false;
  243. Assert.False (view.CanFocus);
  244. Assert.False (view.HasFocus);
  245. Assert.Null (Application.Current.Focused);
  246. Assert.Null (Application.Current.MostFocused);
  247. }
  248. [Fact]
  249. [AutoInitShutdown]
  250. public void CanFocus_Sets_To_False_On_Single_View_Focus_View_On_Another_Toplevel ()
  251. {
  252. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  253. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  254. win1.Add (view1);
  255. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  256. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  257. win2.Add (view2);
  258. Application.Top.Add (win1, win2);
  259. Application.Begin (Application.Top);
  260. Assert.True (view1.CanFocus);
  261. Assert.True (view1.HasFocus);
  262. Assert.True (view2.CanFocus);
  263. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  264. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
  265. Assert.True (view1.CanFocus);
  266. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  267. Assert.True (view2.CanFocus);
  268. Assert.True (view2.HasFocus);
  269. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab));
  270. Assert.True (view1.CanFocus);
  271. Assert.True (view1.HasFocus);
  272. Assert.True (view2.CanFocus);
  273. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  274. view1.CanFocus = false;
  275. Assert.False (view1.CanFocus);
  276. Assert.False (view1.HasFocus);
  277. Assert.True (view2.CanFocus);
  278. Assert.True (view2.HasFocus);
  279. Assert.Equal (win2, Application.Current.Focused);
  280. Assert.Equal (view2, Application.Current.MostFocused);
  281. }
  282. [Fact]
  283. [AutoInitShutdown]
  284. public void CanFocus_Sets_To_False_On_Toplevel_Focus_View_On_Another_Toplevel ()
  285. {
  286. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  287. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  288. win1.Add (view1);
  289. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  290. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  291. win2.Add (view2);
  292. Application.Top.Add (win1, win2);
  293. Application.Begin (Application.Top);
  294. Assert.True (view1.CanFocus);
  295. Assert.True (view1.HasFocus);
  296. Assert.True (view2.CanFocus);
  297. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  298. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  299. Assert.True (view1.CanFocus);
  300. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  301. Assert.True (view2.CanFocus);
  302. Assert.True (view2.HasFocus);
  303. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  304. Assert.True (view1.CanFocus);
  305. Assert.True (view1.HasFocus);
  306. Assert.True (view2.CanFocus);
  307. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  308. win1.CanFocus = false;
  309. Assert.False (view1.CanFocus);
  310. Assert.False (view1.HasFocus);
  311. Assert.False (win1.CanFocus);
  312. Assert.False (win1.HasFocus);
  313. Assert.True (view2.CanFocus);
  314. Assert.True (view2.HasFocus);
  315. Assert.Equal (win2, Application.Current.Focused);
  316. Assert.Equal (view2, Application.Current.MostFocused);
  317. }
  318. [Fact]
  319. [AutoInitShutdown]
  320. public void CanFocus_Sets_To_False_With_Two_Views_Focus_Another_View_On_The_Same_Toplevel ()
  321. {
  322. var view1 = new View { Id = "view1", Width = 10, Height = 1, CanFocus = true };
  323. var view12 = new View
  324. {
  325. Id = "view12",
  326. Y = 5,
  327. Width = 10,
  328. Height = 1,
  329. CanFocus = true
  330. };
  331. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  332. win1.Add (view1, view12);
  333. var view2 = new View { Id = "view2", Width = 20, Height = 2, CanFocus = true };
  334. var win2 = new Window { Id = "win2", X = Pos.Right (win1), Width = Dim.Fill (), Height = Dim.Fill () };
  335. win2.Add (view2);
  336. Application.Top.Add (win1, win2);
  337. Application.Begin (Application.Top);
  338. Assert.True (view1.CanFocus);
  339. Assert.True (view1.HasFocus);
  340. Assert.True (view2.CanFocus);
  341. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  342. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  343. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  344. Assert.True (view1.CanFocus);
  345. Assert.False (view1.HasFocus); // Only one of the most focused toplevels view can have focus
  346. Assert.True (view2.CanFocus);
  347. Assert.True (view2.HasFocus);
  348. Assert.True (Application.Top.NewKeyDownEvent (Key.Tab.WithCtrl));
  349. Assert.True (view1.CanFocus);
  350. Assert.True (view1.HasFocus);
  351. Assert.True (view2.CanFocus);
  352. Assert.False (view2.HasFocus); // Only one of the most focused toplevels view can have focus
  353. view1.CanFocus = false;
  354. Assert.False (view1.CanFocus);
  355. Assert.False (view1.HasFocus);
  356. Assert.True (view2.CanFocus);
  357. Assert.False (view2.HasFocus);
  358. Assert.Equal (win1, Application.Current.Focused);
  359. Assert.Equal (view12, Application.Current.MostFocused);
  360. }
  361. [Fact]
  362. [AutoInitShutdown]
  363. public void Enabled_False_Sets_HasFocus_To_False ()
  364. {
  365. var wasClicked = false;
  366. var view = new Button { Text = "Click Me" };
  367. view.Accept += (s, e) => wasClicked = !wasClicked;
  368. Application.Top.Add (view);
  369. view.NewKeyDownEvent (Key.Space);
  370. Assert.True (wasClicked);
  371. view.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  372. Assert.False (wasClicked);
  373. Assert.True (view.Enabled);
  374. Assert.True (view.CanFocus);
  375. Assert.True (view.HasFocus);
  376. view.Enabled = false;
  377. view.NewKeyDownEvent (Key.Space);
  378. Assert.False (wasClicked);
  379. view.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  380. Assert.False (wasClicked);
  381. Assert.False (view.Enabled);
  382. Assert.True (view.CanFocus);
  383. Assert.False (view.HasFocus);
  384. view.SetFocus ();
  385. Assert.False (view.HasFocus);
  386. }
  387. [Fact]
  388. [AutoInitShutdown]
  389. public void Enabled_Sets_Also_Sets_Subviews ()
  390. {
  391. var wasClicked = false;
  392. var button = new Button { Text = "Click Me" };
  393. button.IsDefault = true;
  394. button.Accept += (s, e) => wasClicked = !wasClicked;
  395. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  396. win.Add (button);
  397. Application.Top.Add (win);
  398. var iterations = 0;
  399. Application.Iteration += (s, a) =>
  400. {
  401. iterations++;
  402. win.NewKeyDownEvent (Key.Enter);
  403. Assert.True (wasClicked);
  404. button.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  405. Assert.False (wasClicked);
  406. Assert.True (button.Enabled);
  407. Assert.True (button.CanFocus);
  408. Assert.True (button.HasFocus);
  409. Assert.True (win.Enabled);
  410. Assert.True (win.CanFocus);
  411. Assert.True (win.HasFocus);
  412. win.Enabled = false;
  413. button.NewKeyDownEvent (Key.Enter);
  414. Assert.False (wasClicked);
  415. button.OnMouseEvent (new MouseEvent { Flags = MouseFlags.Button1Clicked });
  416. Assert.False (wasClicked);
  417. Assert.False (button.Enabled);
  418. Assert.True (button.CanFocus);
  419. Assert.False (button.HasFocus);
  420. Assert.False (win.Enabled);
  421. Assert.True (win.CanFocus);
  422. Assert.False (win.HasFocus);
  423. button.SetFocus ();
  424. Assert.False (button.HasFocus);
  425. Assert.False (win.HasFocus);
  426. win.SetFocus ();
  427. Assert.False (button.HasFocus);
  428. Assert.False (win.HasFocus);
  429. win.Enabled = true;
  430. win.FocusFirst ();
  431. Assert.True (button.HasFocus);
  432. Assert.True (win.HasFocus);
  433. Application.RequestStop ();
  434. };
  435. Application.Run ();
  436. Assert.Equal (1, iterations);
  437. }
  438. [Fact]
  439. public void FocusNearestView_Ensure_Focus_Ordered ()
  440. {
  441. var top = new Toplevel ();
  442. var win = new Window ();
  443. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  444. win.Add (winSubview);
  445. top.Add (win);
  446. var frm = new FrameView ();
  447. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  448. frm.Add (frmSubview);
  449. top.Add (frm);
  450. top.NewKeyDownEvent (Key.Tab);
  451. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  452. top.NewKeyDownEvent (Key.Tab);
  453. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  454. top.NewKeyDownEvent (Key.Tab);
  455. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  456. top.NewKeyDownEvent (Key.Tab.WithShift);
  457. Assert.Equal ("FrameSubview", top.MostFocused.Text);
  458. top.NewKeyDownEvent (Key.Tab.WithShift);
  459. Assert.Equal ("WindowSubview", top.MostFocused.Text);
  460. top.Dispose ();
  461. }
  462. [Fact]
  463. [AutoInitShutdown]
  464. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  465. {
  466. Toplevel top1 = Application.Top;
  467. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  468. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  469. var view2 = new View
  470. {
  471. Id = "view2",
  472. Y = 1,
  473. Width = 10,
  474. Height = 5,
  475. CanFocus = true
  476. };
  477. View view3 = null;
  478. var removed = false;
  479. view2.Enter += (s, e) =>
  480. {
  481. if (!removed)
  482. {
  483. removed = true;
  484. view3 = new View { Id = "view3", Y = 1, Width = 10, Height = 5 };
  485. Application.Current.Add (view3);
  486. Application.Current.BringSubviewToFront (view3);
  487. Assert.False (view3.HasFocus);
  488. }
  489. };
  490. view2.Leave += (s, e) =>
  491. {
  492. Application.Current.Remove (view3);
  493. view3.Dispose ();
  494. view3 = null;
  495. };
  496. top2.Add (view2);
  497. top1.Add (view1, top2);
  498. Application.Begin (top1);
  499. Assert.True (top1.HasFocus);
  500. Assert.True (view1.HasFocus);
  501. Assert.False (view2.HasFocus);
  502. Assert.False (removed);
  503. Assert.Null (view3);
  504. Assert.True (top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  505. Assert.True (top1.HasFocus);
  506. Assert.False (view1.HasFocus);
  507. Assert.True (view2.HasFocus);
  508. Assert.True (removed);
  509. Assert.NotNull (view3);
  510. Exception exception =
  511. Record.Exception (() => top1.NewKeyDownEvent (Key.Tab.WithCtrl));
  512. Assert.Null (exception);
  513. Assert.True (removed);
  514. Assert.Null (view3);
  515. }
  516. [Fact]
  517. [AutoInitShutdown]
  518. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  519. {
  520. var sbQuiting = false;
  521. var tfQuiting = false;
  522. var topQuiting = false;
  523. var sb = new StatusBar (
  524. new StatusItem []
  525. {
  526. new (
  527. KeyCode.CtrlMask | KeyCode.Q,
  528. "~^Q~ Quit",
  529. () => sbQuiting = true
  530. )
  531. }
  532. );
  533. var tf = new TextField ();
  534. tf.KeyDown += Tf_KeyPressed;
  535. void Tf_KeyPressed (object sender, Key obj)
  536. {
  537. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  538. {
  539. obj.Handled = tfQuiting = true;
  540. }
  541. }
  542. var win = new Window ();
  543. win.Add (sb, tf);
  544. Toplevel top = Application.Top;
  545. top.KeyDown += Top_KeyPress;
  546. void Top_KeyPress (object sender, Key obj)
  547. {
  548. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  549. {
  550. obj.Handled = topQuiting = true;
  551. }
  552. }
  553. top.Add (win);
  554. Application.Begin (top);
  555. Assert.False (sbQuiting);
  556. Assert.False (tfQuiting);
  557. Assert.False (topQuiting);
  558. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  559. Assert.False (sbQuiting);
  560. Assert.True (tfQuiting);
  561. Assert.False (topQuiting);
  562. #if BROKE_WITH_2927
  563. tf.KeyPressed -= Tf_KeyPress;
  564. tfQuiting = false;
  565. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  566. Application.MainLoop.RunIteration ();
  567. Assert.True (sbQuiting);
  568. Assert.False (tfQuiting);
  569. Assert.False (topQuiting);
  570. sb.RemoveItem (0);
  571. sbQuiting = false;
  572. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  573. Application.MainLoop.RunIteration ();
  574. Assert.False (sbQuiting);
  575. Assert.False (tfQuiting);
  576. // This test is now invalid because `win` is focused, so it will receive the keypress
  577. Assert.True (topQuiting);
  578. #endif
  579. }
  580. [Fact]
  581. [AutoInitShutdown]
  582. public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  583. {
  584. var sbQuiting = false;
  585. var tfQuiting = false;
  586. var sb = new StatusBar (
  587. new StatusItem []
  588. {
  589. new (
  590. KeyCode.CtrlMask | KeyCode.Q,
  591. "~^Q~ Quit",
  592. () => sbQuiting = true
  593. )
  594. }
  595. );
  596. var tf = new TextField ();
  597. tf.KeyDown += Tf_KeyPressed;
  598. void Tf_KeyPressed (object sender, Key obj)
  599. {
  600. if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  601. {
  602. obj.Handled = tfQuiting = true;
  603. }
  604. }
  605. var win = new Window ();
  606. win.Add (sb, tf);
  607. Toplevel top = Application.Top;
  608. top.Add (win);
  609. Application.Begin (top);
  610. Assert.False (sbQuiting);
  611. Assert.False (tfQuiting);
  612. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  613. Assert.False (sbQuiting);
  614. Assert.True (tfQuiting);
  615. tf.KeyDown -= Tf_KeyPressed;
  616. tfQuiting = false;
  617. Application.Driver.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  618. Application.MainLoop.RunIteration ();
  619. #if BROKE_WITH_2927
  620. Assert.True (sbQuiting);
  621. Assert.False (tfQuiting);
  622. #endif
  623. }
  624. [Fact]
  625. public void Navigation_With_Null_Focused_View ()
  626. {
  627. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  628. Application.Init (new FakeDriver ());
  629. Application.Top.Ready += (s, e) => { Assert.Null (Application.Top.Focused); };
  630. // Keyboard navigation with tab
  631. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('\t', ConsoleKey.Tab, false, false, false));
  632. Application.Iteration += (s, a) => Application.RequestStop ();
  633. Application.Run ();
  634. Application.Shutdown ();
  635. }
  636. [Fact]
  637. [AutoInitShutdown]
  638. public void Remove_Does_Not_Change_Focus ()
  639. {
  640. Assert.True (Application.Top.CanFocus);
  641. Assert.False (Application.Top.HasFocus);
  642. var container = new View { Width = 10, Height = 10 };
  643. var leave = false;
  644. container.Leave += (s, e) => leave = true;
  645. Assert.False (container.CanFocus);
  646. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  647. container.Add (child);
  648. Assert.True (container.CanFocus);
  649. Assert.False (container.HasFocus);
  650. Assert.True (child.CanFocus);
  651. Assert.False (child.HasFocus);
  652. Application.Top.Add (container);
  653. Application.Begin (Application.Top);
  654. Assert.True (Application.Top.CanFocus);
  655. Assert.True (Application.Top.HasFocus);
  656. Assert.True (container.CanFocus);
  657. Assert.True (container.HasFocus);
  658. Assert.True (child.CanFocus);
  659. Assert.True (child.HasFocus);
  660. container.Remove (child);
  661. child.Dispose ();
  662. child = null;
  663. Assert.True (Application.Top.HasFocus);
  664. Assert.True (container.CanFocus);
  665. Assert.True (container.HasFocus);
  666. Assert.Null (child);
  667. Assert.False (leave);
  668. }
  669. [Fact]
  670. [AutoInitShutdown]
  671. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  672. {
  673. Toplevel top = Application.Current;
  674. top.BorderStyle = LineStyle.Single;
  675. var view = new View
  676. {
  677. X = 3,
  678. Y = 2,
  679. Width = 10,
  680. Height = 1,
  681. Text = "0123456789"
  682. };
  683. top.Add (view);
  684. Application.Begin (top);
  685. Assert.Equal (Application.Current, top);
  686. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  687. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  688. Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
  689. ((FakeDriver)Application.Driver).SetBufferSize (20, 10);
  690. Assert.Equal (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  691. Assert.Equal (new Rectangle (0, 0, 20, 10), top.Frame);
  692. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  693. @"
  694. ┌──────────────────┐
  695. │ │
  696. │ │
  697. │ 0123456789 │
  698. │ │
  699. │ │
  700. │ │
  701. │ │
  702. │ │
  703. └──────────────────┘",
  704. _output
  705. );
  706. // top
  707. Assert.Equal (Point.Empty, top.ScreenToFrame (0, 0));
  708. top.Margin.BoundsToScreen (0, 0, out int col, out int row);
  709. Assert.Equal (0, col);
  710. Assert.Equal (0, row);
  711. top.Border.BoundsToScreen (0, 0, out col, out row);
  712. Assert.Equal (0, col);
  713. Assert.Equal (0, row);
  714. top.Padding.BoundsToScreen (0, 0, out col, out row);
  715. Assert.Equal (0, col);
  716. Assert.Equal (0, row);
  717. top.BoundsToScreen (0, 0, out col, out row);
  718. Assert.Equal (1, col);
  719. Assert.Equal (1, row);
  720. top.BoundsToScreen (-1, -1, out col, out row);
  721. Assert.Equal (0, col);
  722. Assert.Equal (0, row);
  723. var found = View.FindDeepestView (top, 0, 0);
  724. Assert.Equal (top.Border, found);
  725. Assert.Equal (0, found.Frame.X);
  726. Assert.Equal (0, found.Frame.Y);
  727. Assert.Equal (new Point (3, 2), top.ScreenToFrame (3, 2));
  728. top.BoundsToScreen (3, 2, out col, out row);
  729. Assert.Equal (4, col);
  730. Assert.Equal (3, row);
  731. found = View.FindDeepestView (top, col, row);
  732. Assert.Equal (view, found);
  733. //Assert.Equal (0, found.FrameToScreen ().X);
  734. //Assert.Equal (0, found.FrameToScreen ().Y);
  735. found = View.FindDeepestView (top, 3, 2);
  736. Assert.Equal (top, found);
  737. //Assert.Equal (3, found.FrameToScreen ().X);
  738. //Assert.Equal (2, found.FrameToScreen ().Y);
  739. Assert.Equal (new Point (13, 2), top.ScreenToFrame (13, 2));
  740. top.BoundsToScreen (12, 2, out col, out row);
  741. Assert.Equal (13, col);
  742. Assert.Equal (3, row);
  743. found = View.FindDeepestView (top, col, row);
  744. Assert.Equal (view, found);
  745. //Assert.Equal (9, found.FrameToScreen ().X);
  746. //Assert.Equal (0, found.FrameToScreen ().Y);
  747. top.BoundsToScreen (13, 2, out col, out row);
  748. Assert.Equal (14, col);
  749. Assert.Equal (3, row);
  750. found = View.FindDeepestView (top, 13, 2);
  751. Assert.Equal (top, found);
  752. //Assert.Equal (13, found.FrameToScreen ().X);
  753. //Assert.Equal (2, found.FrameToScreen ().Y);
  754. Assert.Equal (new Point (14, 3), top.ScreenToFrame (14, 3));
  755. top.BoundsToScreen (14, 3, out col, out row);
  756. Assert.Equal (15, col);
  757. Assert.Equal (4, row);
  758. found = View.FindDeepestView (top, 14, 3);
  759. Assert.Equal (top, found);
  760. //Assert.Equal (14, found.FrameToScreen ().X);
  761. //Assert.Equal (3, found.FrameToScreen ().Y);
  762. // view
  763. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (0, 0));
  764. view.Margin.BoundsToScreen (-3, -2, out col, out row);
  765. Assert.Equal (1, col);
  766. Assert.Equal (1, row);
  767. view.Border.BoundsToScreen (-3, -2, out col, out row);
  768. Assert.Equal (1, col);
  769. Assert.Equal (1, row);
  770. view.Padding.BoundsToScreen (-3, -2, out col, out row);
  771. Assert.Equal (1, col);
  772. Assert.Equal (1, row);
  773. view.BoundsToScreen (-3, -2, out col, out row);
  774. Assert.Equal (1, col);
  775. Assert.Equal (1, row);
  776. view.BoundsToScreen (-4, -3, out col, out row);
  777. Assert.Equal (0, col);
  778. Assert.Equal (0, row);
  779. found = View.FindDeepestView (top, 0, 0);
  780. Assert.Equal (top.Border, found);
  781. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (3, 2));
  782. view.BoundsToScreen (0, 0, out col, out row);
  783. Assert.Equal (4, col);
  784. Assert.Equal (3, row);
  785. found = View.FindDeepestView (top, 4, 3);
  786. Assert.Equal (view, found);
  787. Assert.Equal (new Point (9, -1), view.ScreenToFrame (13, 2));
  788. view.BoundsToScreen (10, 0, out col, out row);
  789. Assert.Equal (14, col);
  790. Assert.Equal (3, row);
  791. found = View.FindDeepestView (top, 14, 3);
  792. Assert.Equal (top, found);
  793. Assert.Equal (new Point (10, 0), view.ScreenToFrame (14, 3));
  794. view.BoundsToScreen (11, 1, out col, out row);
  795. Assert.Equal (15, col);
  796. Assert.Equal (4, row);
  797. found = View.FindDeepestView (top, 15, 4);
  798. Assert.Equal (top, found);
  799. }
  800. [Fact]
  801. [AutoInitShutdown]
  802. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  803. {
  804. var top = new Toplevel
  805. {
  806. X = 3,
  807. Y = 2,
  808. Width = 20,
  809. Height = 10,
  810. BorderStyle = LineStyle.Single
  811. };
  812. var view = new View
  813. {
  814. X = 3,
  815. Y = 2,
  816. Width = 10,
  817. Height = 1,
  818. Text = "0123456789"
  819. };
  820. top.Add (view);
  821. Application.Begin (top);
  822. Assert.Equal (Application.Current, top);
  823. Assert.Equal (new Rectangle (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  824. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  825. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  826. ((FakeDriver)Application.Driver).SetBufferSize (30, 20);
  827. Assert.Equal (new Rectangle (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  828. Assert.NotEqual (new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  829. Assert.Equal (new Rectangle (3, 2, 20, 10), top.Frame);
  830. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  831. @"
  832. ┌──────────────────┐
  833. │ │
  834. │ │
  835. │ 0123456789 │
  836. │ │
  837. │ │
  838. │ │
  839. │ │
  840. │ │
  841. └──────────────────┘",
  842. _output
  843. );
  844. // mean the output started at col 3 and line 2
  845. // which result with a width of 23 and a height of 10 on the output
  846. Assert.Equal (new Rectangle (3, 2, 23, 10), frame);
  847. // top
  848. Assert.Equal (new Point (-3, -2), top.ScreenToFrame (0, 0));
  849. top.Margin.BoundsToScreen (-3, -2, out int col, out int row);
  850. Assert.Equal (0, col);
  851. Assert.Equal (0, row);
  852. top.Border.BoundsToScreen (-3, -2, out col, out row);
  853. Assert.Equal (0, col);
  854. Assert.Equal (0, row);
  855. top.Padding.BoundsToScreen (-3, -2, out col, out row);
  856. Assert.Equal (0, col);
  857. Assert.Equal (0, row);
  858. top.BoundsToScreen (-3, -2, out col, out row);
  859. Assert.Equal (1, col);
  860. Assert.Equal (1, row);
  861. top.BoundsToScreen (-4, -3, out col, out row);
  862. Assert.Equal (0, col);
  863. Assert.Equal (0, row);
  864. var found = View.FindDeepestView (top, -4, -3);
  865. Assert.Null (found);
  866. Assert.Equal (Point.Empty, top.ScreenToFrame (3, 2));
  867. top.BoundsToScreen (0, 0, out col, out row);
  868. Assert.Equal (4, col);
  869. Assert.Equal (3, row);
  870. Assert.Equal (top.Border, View.FindDeepestView (top, 3, 2));
  871. Assert.Equal (new Point (10, 0), top.ScreenToFrame (13, 2));
  872. top.BoundsToScreen (10, 0, out col, out row);
  873. Assert.Equal (14, col);
  874. Assert.Equal (3, row);
  875. Assert.Equal (top.Border, View.FindDeepestView (top, 13, 2));
  876. Assert.Equal (new Point (11, 1), top.ScreenToFrame (14, 3));
  877. top.BoundsToScreen (11, 1, out col, out row);
  878. Assert.Equal (15, col);
  879. Assert.Equal (4, row);
  880. Assert.Equal (top, View.FindDeepestView (top, 14, 3));
  881. // view
  882. Assert.Equal (new Point (-7, -5), view.ScreenToFrame (0, 0));
  883. view.Margin.BoundsToScreen (-6, -4, out col, out row);
  884. Assert.Equal (1, col);
  885. Assert.Equal (1, row);
  886. view.Border.BoundsToScreen (-6, -4, out col, out row);
  887. Assert.Equal (1, col);
  888. Assert.Equal (1, row);
  889. view.Padding.BoundsToScreen (-6, -4, out col, out row);
  890. Assert.Equal (1, col);
  891. Assert.Equal (1, row);
  892. view.BoundsToScreen (-6, -4, out col, out row);
  893. Assert.Equal (1, col);
  894. Assert.Equal (1, row);
  895. Assert.Null (View.FindDeepestView (top, 1, 1));
  896. Assert.Equal (new Point (-4, -3), view.ScreenToFrame (3, 2));
  897. view.BoundsToScreen (-3, -2, out col, out row);
  898. Assert.Equal (4, col);
  899. Assert.Equal (3, row);
  900. Assert.Equal (top, View.FindDeepestView (top, 4, 3));
  901. Assert.Equal (new Point (-1, -1), view.ScreenToFrame (6, 4));
  902. view.BoundsToScreen (0, 0, out col, out row);
  903. Assert.Equal (7, col);
  904. Assert.Equal (5, row);
  905. Assert.Equal (view, View.FindDeepestView (top, 7, 5));
  906. Assert.Equal (new Point (6, -1), view.ScreenToFrame (13, 4));
  907. view.BoundsToScreen (7, 0, out col, out row);
  908. Assert.Equal (14, col);
  909. Assert.Equal (5, row);
  910. Assert.Equal (view, View.FindDeepestView (top, 14, 5));
  911. Assert.Equal (new Point (7, -2), view.ScreenToFrame (14, 3));
  912. view.BoundsToScreen (8, -1, out col, out row);
  913. Assert.Equal (15, col);
  914. Assert.Equal (4, row);
  915. Assert.Equal (top, View.FindDeepestView (top, 15, 4));
  916. Assert.Equal (new Point (16, -2), view.ScreenToFrame (23, 3));
  917. view.BoundsToScreen (17, -1, out col, out row);
  918. Assert.Equal (24, col);
  919. Assert.Equal (4, row);
  920. Assert.Null (View.FindDeepestView (top, 24, 4));
  921. }
  922. [Fact]
  923. public void SendSubviewBackwards_Subviews_vs_TabIndexes ()
  924. {
  925. var r = new View ();
  926. var v1 = new View { CanFocus = true };
  927. var v2 = new View { CanFocus = true };
  928. var v3 = new View { CanFocus = true };
  929. r.Add (v1, v2, v3);
  930. r.SendSubviewBackwards (v3);
  931. Assert.True (r.Subviews.IndexOf (v1) == 0);
  932. Assert.True (r.Subviews.IndexOf (v2) == 2);
  933. Assert.True (r.Subviews.IndexOf (v3) == 1);
  934. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  935. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  936. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  937. r.Dispose ();
  938. }
  939. [Fact]
  940. public void SendSubviewToBack_Subviews_vs_TabIndexes ()
  941. {
  942. var r = new View ();
  943. var v1 = new View { CanFocus = true };
  944. var v2 = new View { CanFocus = true };
  945. var v3 = new View { CanFocus = true };
  946. r.Add (v1, v2, v3);
  947. r.SendSubviewToBack (v3);
  948. Assert.True (r.Subviews.IndexOf (v1) == 1);
  949. Assert.True (r.Subviews.IndexOf (v2) == 2);
  950. Assert.True (r.Subviews.IndexOf (v3) == 0);
  951. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  952. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  953. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  954. r.Dispose ();
  955. }
  956. [Fact]
  957. [AutoInitShutdown]
  958. public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
  959. {
  960. Assert.True (Application.Top.CanFocus);
  961. Assert.False (Application.Top.HasFocus);
  962. Exception exception = Record.Exception (Application.Top.SetFocus);
  963. Assert.Null (exception);
  964. Assert.True (Application.Top.CanFocus);
  965. Assert.True (Application.Top.HasFocus);
  966. }
  967. [Fact]
  968. [AutoInitShutdown]
  969. public void SetHasFocus_Do_Not_Throws_If_OnLeave_Remove_Focused_Changing_To_Null ()
  970. {
  971. var view1Leave = false;
  972. var subView1Leave = false;
  973. var subView1subView1Leave = false;
  974. Toplevel top = Application.Top;
  975. var view1 = new View { CanFocus = true };
  976. var subView1 = new View { CanFocus = true };
  977. var subView1subView1 = new View { CanFocus = true };
  978. view1.Leave += (s, e) => { view1Leave = true; };
  979. subView1.Leave += (s, e) =>
  980. {
  981. subView1.Remove (subView1subView1);
  982. subView1Leave = true;
  983. };
  984. view1.Add (subView1);
  985. subView1subView1.Leave += (s, e) =>
  986. {
  987. // This is never invoked
  988. subView1subView1Leave = true;
  989. };
  990. subView1.Add (subView1subView1);
  991. var view2 = new View { CanFocus = true };
  992. top.Add (view1, view2);
  993. RunState rs = Application.Begin (top);
  994. view2.SetFocus ();
  995. Assert.True (view1Leave);
  996. Assert.True (subView1Leave);
  997. Assert.False (subView1subView1Leave);
  998. Application.End (rs);
  999. subView1subView1.Dispose ();
  1000. }
  1001. [Fact]
  1002. public void Subviews_TabIndexes_AreEqual ()
  1003. {
  1004. var r = new View ();
  1005. var v1 = new View { CanFocus = true };
  1006. var v2 = new View { CanFocus = true };
  1007. var v3 = new View { CanFocus = true };
  1008. r.Add (v1, v2, v3);
  1009. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1010. Assert.True (r.Subviews.IndexOf (v2) == 1);
  1011. Assert.True (r.Subviews.IndexOf (v3) == 2);
  1012. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1013. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  1014. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  1015. Assert.Equal (r.Subviews.IndexOf (v1), r.TabIndexes.IndexOf (v1));
  1016. Assert.Equal (r.Subviews.IndexOf (v2), r.TabIndexes.IndexOf (v2));
  1017. Assert.Equal (r.Subviews.IndexOf (v3), r.TabIndexes.IndexOf (v3));
  1018. r.Dispose ();
  1019. }
  1020. [Fact]
  1021. public void TabIndex_Set_CanFocus_False ()
  1022. {
  1023. var r = new View ();
  1024. var v1 = new View { CanFocus = true };
  1025. var v2 = new View { CanFocus = true };
  1026. var v3 = new View { CanFocus = true };
  1027. r.Add (v1, v2, v3);
  1028. v1.CanFocus = false;
  1029. v1.TabIndex = 0;
  1030. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1031. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1032. Assert.Equal (-1, v1.TabIndex);
  1033. r.Dispose ();
  1034. }
  1035. [Fact]
  1036. public void TabIndex_Set_CanFocus_False_To_True ()
  1037. {
  1038. var r = new View ();
  1039. var v1 = new View ();
  1040. var v2 = new View { CanFocus = true };
  1041. var v3 = new View { CanFocus = true };
  1042. r.Add (v1, v2, v3);
  1043. v1.CanFocus = true;
  1044. v1.TabIndex = 1;
  1045. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1046. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1047. r.Dispose ();
  1048. }
  1049. [Fact]
  1050. public void TabIndex_Set_CanFocus_HigherValues ()
  1051. {
  1052. var r = new View ();
  1053. var v1 = new View { CanFocus = true };
  1054. var v2 = new View { CanFocus = true };
  1055. var v3 = new View { CanFocus = true };
  1056. r.Add (v1, v2, v3);
  1057. v1.TabIndex = 3;
  1058. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1059. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1060. r.Dispose ();
  1061. }
  1062. [Fact]
  1063. public void TabIndex_Set_CanFocus_LowerValues ()
  1064. {
  1065. var r = new View ();
  1066. var v1 = new View { CanFocus = true };
  1067. var v2 = new View { CanFocus = true };
  1068. var v3 = new View { CanFocus = true };
  1069. r.Add (v1, v2, v3);
  1070. v1.TabIndex = -1;
  1071. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1072. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  1073. r.Dispose ();
  1074. }
  1075. [Fact]
  1076. public void TabIndex_Set_CanFocus_ValidValues ()
  1077. {
  1078. var r = new View ();
  1079. var v1 = new View { CanFocus = true };
  1080. var v2 = new View { CanFocus = true };
  1081. var v3 = new View { CanFocus = true };
  1082. r.Add (v1, v2, v3);
  1083. v1.TabIndex = 1;
  1084. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1085. Assert.True (r.TabIndexes.IndexOf (v1) == 1);
  1086. v1.TabIndex = 2;
  1087. Assert.True (r.Subviews.IndexOf (v1) == 0);
  1088. Assert.True (r.TabIndexes.IndexOf (v1) == 2);
  1089. r.Dispose ();
  1090. }
  1091. [Fact]
  1092. public void TabStop_All_False_And_All_True_And_Changing_TabStop_Later ()
  1093. {
  1094. var r = new View ();
  1095. var v1 = new View { CanFocus = true, TabStop = false };
  1096. var v2 = new View { CanFocus = true, TabStop = false };
  1097. var v3 = new View { CanFocus = true, TabStop = false };
  1098. r.Add (v1, v2, v3);
  1099. r.FocusNext ();
  1100. Assert.False (v1.HasFocus);
  1101. Assert.False (v2.HasFocus);
  1102. Assert.False (v3.HasFocus);
  1103. v1.TabStop = true;
  1104. r.FocusNext ();
  1105. Assert.True (v1.HasFocus);
  1106. Assert.False (v2.HasFocus);
  1107. Assert.False (v3.HasFocus);
  1108. v2.TabStop = true;
  1109. r.FocusNext ();
  1110. Assert.False (v1.HasFocus);
  1111. Assert.True (v2.HasFocus);
  1112. Assert.False (v3.HasFocus);
  1113. v3.TabStop = true;
  1114. r.FocusNext ();
  1115. Assert.False (v1.HasFocus);
  1116. Assert.False (v2.HasFocus);
  1117. Assert.True (v3.HasFocus);
  1118. r.Dispose ();
  1119. }
  1120. [Fact]
  1121. public void TabStop_All_True_And_Changing_CanFocus_Later ()
  1122. {
  1123. var r = new View ();
  1124. var v1 = new View ();
  1125. var v2 = new View ();
  1126. var v3 = new View ();
  1127. r.Add (v1, v2, v3);
  1128. r.FocusNext ();
  1129. Assert.False (v1.HasFocus);
  1130. Assert.False (v2.HasFocus);
  1131. Assert.False (v3.HasFocus);
  1132. v1.CanFocus = true;
  1133. r.FocusNext ();
  1134. Assert.True (v1.HasFocus);
  1135. Assert.False (v2.HasFocus);
  1136. Assert.False (v3.HasFocus);
  1137. v2.CanFocus = true;
  1138. r.FocusNext ();
  1139. Assert.False (v1.HasFocus);
  1140. Assert.True (v2.HasFocus);
  1141. Assert.False (v3.HasFocus);
  1142. v3.CanFocus = true;
  1143. r.FocusNext ();
  1144. Assert.False (v1.HasFocus);
  1145. Assert.False (v2.HasFocus);
  1146. Assert.True (v3.HasFocus);
  1147. r.Dispose ();
  1148. }
  1149. [Fact]
  1150. public void TabStop_And_CanFocus_Are_All_True ()
  1151. {
  1152. var r = new View ();
  1153. var v1 = new View { CanFocus = true };
  1154. var v2 = new View { CanFocus = true };
  1155. var v3 = new View { CanFocus = true };
  1156. r.Add (v1, v2, v3);
  1157. r.FocusNext ();
  1158. Assert.True (v1.HasFocus);
  1159. Assert.False (v2.HasFocus);
  1160. Assert.False (v3.HasFocus);
  1161. r.FocusNext ();
  1162. Assert.False (v1.HasFocus);
  1163. Assert.True (v2.HasFocus);
  1164. Assert.False (v3.HasFocus);
  1165. r.FocusNext ();
  1166. Assert.False (v1.HasFocus);
  1167. Assert.False (v2.HasFocus);
  1168. Assert.True (v3.HasFocus);
  1169. r.Dispose ();
  1170. }
  1171. [Fact]
  1172. public void TabStop_And_CanFocus_Mixed_And_BothFalse ()
  1173. {
  1174. var r = new View ();
  1175. var v1 = new View { CanFocus = true, TabStop = false };
  1176. var v2 = new View { CanFocus = false, TabStop = true };
  1177. var v3 = new View { CanFocus = false, TabStop = false };
  1178. r.Add (v1, v2, v3);
  1179. r.FocusNext ();
  1180. Assert.False (v1.HasFocus);
  1181. Assert.False (v2.HasFocus);
  1182. Assert.False (v3.HasFocus);
  1183. r.FocusNext ();
  1184. Assert.False (v1.HasFocus);
  1185. Assert.False (v2.HasFocus);
  1186. Assert.False (v3.HasFocus);
  1187. r.FocusNext ();
  1188. Assert.False (v1.HasFocus);
  1189. Assert.False (v2.HasFocus);
  1190. Assert.False (v3.HasFocus);
  1191. r.Dispose ();
  1192. }
  1193. [Fact]
  1194. public void TabStop_Are_All_False_And_CanFocus_Are_All_True ()
  1195. {
  1196. var r = new View ();
  1197. var v1 = new View { CanFocus = true, TabStop = false };
  1198. var v2 = new View { CanFocus = true, TabStop = false };
  1199. var v3 = new View { CanFocus = true, TabStop = false };
  1200. r.Add (v1, v2, v3);
  1201. r.FocusNext ();
  1202. Assert.False (v1.HasFocus);
  1203. Assert.False (v2.HasFocus);
  1204. Assert.False (v3.HasFocus);
  1205. r.FocusNext ();
  1206. Assert.False (v1.HasFocus);
  1207. Assert.False (v2.HasFocus);
  1208. Assert.False (v3.HasFocus);
  1209. r.FocusNext ();
  1210. Assert.False (v1.HasFocus);
  1211. Assert.False (v2.HasFocus);
  1212. Assert.False (v3.HasFocus);
  1213. r.Dispose ();
  1214. }
  1215. [Fact]
  1216. public void TabStop_Are_All_True_And_CanFocus_Are_All_False ()
  1217. {
  1218. var r = new View ();
  1219. var v1 = new View ();
  1220. var v2 = new View ();
  1221. var v3 = new View ();
  1222. r.Add (v1, v2, v3);
  1223. r.FocusNext ();
  1224. Assert.False (v1.HasFocus);
  1225. Assert.False (v2.HasFocus);
  1226. Assert.False (v3.HasFocus);
  1227. r.FocusNext ();
  1228. Assert.False (v1.HasFocus);
  1229. Assert.False (v2.HasFocus);
  1230. Assert.False (v3.HasFocus);
  1231. r.FocusNext ();
  1232. Assert.False (v1.HasFocus);
  1233. Assert.False (v2.HasFocus);
  1234. Assert.False (v3.HasFocus);
  1235. r.Dispose ();
  1236. }
  1237. [Fact]
  1238. [AutoInitShutdown]
  1239. public void WindowDispose_CanFocusProblem ()
  1240. {
  1241. // Arrange
  1242. Application.Init ();
  1243. using var top = new Toplevel ();
  1244. using var view = new View { X = 0, Y = 1, Text = nameof (WindowDispose_CanFocusProblem) };
  1245. using var window = new Window ();
  1246. top.Add (window);
  1247. window.Add (view);
  1248. // Act
  1249. RunState rs = Application.Begin (top);
  1250. Application.End (rs);
  1251. Application.Shutdown ();
  1252. // Assert does Not throw NullReferenceException
  1253. top.SetFocus ();
  1254. }
  1255. }