NavigationTests.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewTests;
  3. public class NavigationTests (ITestOutputHelper _output) : TestsAllViews
  4. {
  5. [Theory]
  6. [MemberData (nameof (AllViewTypes))]
  7. public void AllViews_AtLeastOneNavKey_Leaves (Type viewType)
  8. {
  9. View view = CreateInstanceIfNotGeneric (viewType);
  10. if (view == null)
  11. {
  12. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  13. return;
  14. }
  15. if (!view.CanFocus)
  16. {
  17. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  18. return;
  19. }
  20. Application.Init (new FakeDriver ());
  21. Toplevel top = new ();
  22. View otherView = new ()
  23. {
  24. Id = "otherView",
  25. CanFocus = true,
  26. TabStop = view.TabStop
  27. };
  28. top.Add (view, otherView);
  29. Application.Begin (top);
  30. // Start with the focus on our test view
  31. view.SetFocus ();
  32. Key [] navKeys = { Key.Tab, Key.Tab.WithShift, Key.CursorUp, Key.CursorDown, Key.CursorLeft, Key.CursorRight };
  33. if (view.TabStop == TabBehavior.TabGroup)
  34. {
  35. navKeys = new [] { Key.F6, Key.F6.WithShift };
  36. }
  37. var left = false;
  38. foreach (Key key in navKeys)
  39. {
  40. switch (view.TabStop)
  41. {
  42. case TabBehavior.TabStop:
  43. case TabBehavior.NoStop:
  44. case TabBehavior.TabGroup:
  45. Application.OnKeyDown (key);
  46. break;
  47. default:
  48. Application.OnKeyDown (Key.Tab);
  49. break;
  50. }
  51. if (!view.HasFocus)
  52. {
  53. left = true;
  54. _output.WriteLine ($"{view.GetType ().Name} - {key} Left.");
  55. view.SetFocus ();
  56. }
  57. else
  58. {
  59. _output.WriteLine ($"{view.GetType ().Name} - {key} did not Leave.");
  60. }
  61. }
  62. top.Dispose ();
  63. Application.Shutdown ();
  64. Assert.True (left);
  65. }
  66. [Theory]
  67. [MemberData (nameof (AllViewTypes))]
  68. public void AllViews_Enter_Leave_Events (Type viewType)
  69. {
  70. View view = CreateInstanceIfNotGeneric (viewType);
  71. if (view == null)
  72. {
  73. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  74. return;
  75. }
  76. if (!view.CanFocus)
  77. {
  78. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  79. return;
  80. }
  81. if (view is Toplevel && ((Toplevel)view).Modal)
  82. {
  83. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  84. return;
  85. }
  86. Application.Init (new FakeDriver ());
  87. Toplevel top = new ()
  88. {
  89. Height = 10,
  90. Width = 10
  91. };
  92. View otherView = new ()
  93. {
  94. Id = "otherView",
  95. X = 0, Y = 0,
  96. Height = 1,
  97. Width = 1,
  98. CanFocus = true,
  99. TabStop = view.TabStop
  100. };
  101. view.X = Pos.Right (otherView);
  102. view.Y = 0;
  103. view.Width = 10;
  104. view.Height = 1;
  105. var nEnter = 0;
  106. var nLeave = 0;
  107. view.Enter += (s, e) => nEnter++;
  108. view.Leave += (s, e) => nLeave++;
  109. top.Add (view, otherView);
  110. Assert.False (view.HasFocus);
  111. Assert.False (otherView.HasFocus);
  112. Application.Begin (top);
  113. Assert.True (Application.Current!.HasFocus);
  114. Assert.True (top.HasFocus);
  115. // Start with the focus on our test view
  116. Assert.True (view.HasFocus);
  117. Assert.Equal (1, nEnter);
  118. Assert.Equal (0, nLeave);
  119. // Use keyboard to navigate to next view (otherView).
  120. if (view is TextView)
  121. {
  122. Application.OnKeyDown (Key.F6);
  123. }
  124. else
  125. {
  126. var tries = 0;
  127. while (view.HasFocus)
  128. {
  129. if (++tries > 10)
  130. {
  131. Assert.Fail ($"{view} is not leaving.");
  132. }
  133. switch (view.TabStop)
  134. {
  135. case TabBehavior.NoStop:
  136. Application.OnKeyDown (Key.Tab);
  137. break;
  138. case TabBehavior.TabStop:
  139. Application.OnKeyDown (Key.Tab);
  140. break;
  141. case TabBehavior.TabGroup:
  142. Application.OnKeyDown (Key.F6);
  143. break;
  144. case null:
  145. Application.OnKeyDown (Key.Tab);
  146. break;
  147. default:
  148. throw new ArgumentOutOfRangeException ();
  149. }
  150. }
  151. }
  152. Assert.Equal (1, nEnter);
  153. Assert.Equal (1, nLeave);
  154. Assert.False (view.HasFocus);
  155. Assert.True (otherView.HasFocus);
  156. // Now navigate back to our test view
  157. switch (view.TabStop)
  158. {
  159. case TabBehavior.NoStop:
  160. view.SetFocus ();
  161. break;
  162. case TabBehavior.TabStop:
  163. Application.OnKeyDown (Key.Tab);
  164. break;
  165. case TabBehavior.TabGroup:
  166. Application.OnKeyDown (Key.F6);
  167. break;
  168. case null:
  169. Application.OnKeyDown (Key.Tab);
  170. break;
  171. default:
  172. throw new ArgumentOutOfRangeException ();
  173. }
  174. Assert.Equal (2, nEnter);
  175. Assert.Equal (1, nLeave);
  176. Assert.True (view.HasFocus);
  177. Assert.False (otherView.HasFocus);
  178. // Cache state because Shutdown has side effects.
  179. // Also ensures other tests can continue running if there's a fail
  180. bool otherViewHasFocus = otherView.HasFocus;
  181. bool viewHasFocus = view.HasFocus;
  182. int enterCount = nEnter;
  183. int leaveCount = nLeave;
  184. top.Dispose ();
  185. Application.Shutdown ();
  186. Assert.False (otherViewHasFocus);
  187. Assert.True (viewHasFocus);
  188. Assert.Equal (2, enterCount);
  189. Assert.Equal (1, leaveCount);
  190. }
  191. [Theory]
  192. [MemberData (nameof (AllViewTypes))]
  193. public void AllViews_Enter_Leave_Events_Visible_False (Type viewType)
  194. {
  195. View view = CreateInstanceIfNotGeneric (viewType);
  196. if (view == null)
  197. {
  198. _output.WriteLine ($"Ignoring {viewType} - It's a Generic");
  199. return;
  200. }
  201. if (!view.CanFocus)
  202. {
  203. _output.WriteLine ($"Ignoring {viewType} - It can't focus.");
  204. return;
  205. }
  206. if (view is Toplevel && ((Toplevel)view).Modal)
  207. {
  208. _output.WriteLine ($"Ignoring {viewType} - It's a Modal Toplevel");
  209. return;
  210. }
  211. Application.Init (new FakeDriver ());
  212. Toplevel top = new ()
  213. {
  214. Height = 10,
  215. Width = 10
  216. };
  217. View otherView = new ()
  218. {
  219. X = 0, Y = 0,
  220. Height = 1,
  221. Width = 1,
  222. CanFocus = true
  223. };
  224. view.Visible = false;
  225. view.X = Pos.Right (otherView);
  226. view.Y = 0;
  227. view.Width = 10;
  228. view.Height = 1;
  229. var nEnter = 0;
  230. var nLeave = 0;
  231. view.Enter += (s, e) => nEnter++;
  232. view.Leave += (s, e) => nLeave++;
  233. top.Add (view, otherView);
  234. Application.Begin (top);
  235. // Start with the focus on our test view
  236. view.SetFocus ();
  237. Assert.Equal (0, nEnter);
  238. Assert.Equal (0, nLeave);
  239. // Use keyboard to navigate to next view (otherView).
  240. if (view is TextView)
  241. {
  242. Application.OnKeyDown (Key.F6);
  243. }
  244. else if (view is DatePicker)
  245. {
  246. for (var i = 0; i < 4; i++)
  247. {
  248. Application.OnKeyDown (Key.F6);
  249. }
  250. }
  251. else
  252. {
  253. Application.OnKeyDown (Key.Tab);
  254. }
  255. Assert.Equal (0, nEnter);
  256. Assert.Equal (0, nLeave);
  257. top.NewKeyDownEvent (Key.Tab);
  258. Assert.Equal (0, nEnter);
  259. Assert.Equal (0, nLeave);
  260. top.Dispose ();
  261. Application.Shutdown ();
  262. }
  263. [Fact]
  264. public void BringSubviewForward_Subviews_vs_TabIndexes ()
  265. {
  266. var r = new View ();
  267. var v1 = new View { CanFocus = true };
  268. var v2 = new View { CanFocus = true };
  269. var v3 = new View { CanFocus = true };
  270. r.Add (v1, v2, v3);
  271. r.BringSubviewForward (v1);
  272. Assert.True (r.Subviews.IndexOf (v1) == 1);
  273. Assert.True (r.Subviews.IndexOf (v2) == 0);
  274. Assert.True (r.Subviews.IndexOf (v3) == 2);
  275. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  276. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  277. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  278. r.Dispose ();
  279. }
  280. [Fact]
  281. public void BringSubviewToFront_Subviews_vs_TabIndexes ()
  282. {
  283. var r = new View ();
  284. var v1 = new View { CanFocus = true };
  285. var v2 = new View { CanFocus = true };
  286. var v3 = new View { CanFocus = true };
  287. r.Add (v1, v2, v3);
  288. r.BringSubviewToFront (v1);
  289. Assert.True (r.Subviews.IndexOf (v1) == 2);
  290. Assert.True (r.Subviews.IndexOf (v2) == 0);
  291. Assert.True (r.Subviews.IndexOf (v3) == 1);
  292. Assert.True (r.TabIndexes.IndexOf (v1) == 0);
  293. Assert.True (r.TabIndexes.IndexOf (v2) == 1);
  294. Assert.True (r.TabIndexes.IndexOf (v3) == 2);
  295. r.Dispose ();
  296. }
  297. [Fact]
  298. [AutoInitShutdown]
  299. public void Enabled_Sets_Also_Sets_Subviews ()
  300. {
  301. var wasClicked = false;
  302. var button = new Button { Text = "Click Me" };
  303. button.IsDefault = true;
  304. button.Accept += (s, e) => wasClicked = !wasClicked;
  305. var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
  306. win.Add (button);
  307. var top = new Toplevel ();
  308. top.Add (win);
  309. var iterations = 0;
  310. Application.Iteration += (s, a) =>
  311. {
  312. iterations++;
  313. win.NewKeyDownEvent (Key.Enter);
  314. Assert.True (wasClicked);
  315. button.NewMouseEvent (new () { Flags = MouseFlags.Button1Clicked });
  316. Assert.False (wasClicked);
  317. Assert.True (button.Enabled);
  318. Assert.True (button.CanFocus);
  319. Assert.True (button.HasFocus);
  320. Assert.True (win.Enabled);
  321. Assert.True (win.CanFocus);
  322. Assert.True (win.HasFocus);
  323. win.Enabled = false;
  324. button.NewKeyDownEvent (Key.Enter);
  325. Assert.False (wasClicked);
  326. button.NewMouseEvent (new () { Flags = MouseFlags.Button1Clicked });
  327. Assert.False (wasClicked);
  328. Assert.False (button.Enabled);
  329. Assert.True (button.CanFocus);
  330. Assert.False (button.HasFocus);
  331. Assert.False (win.Enabled);
  332. Assert.True (win.CanFocus);
  333. Assert.False (win.HasFocus);
  334. button.SetFocus ();
  335. Assert.False (button.HasFocus);
  336. Assert.False (win.HasFocus);
  337. win.SetFocus ();
  338. Assert.False (button.HasFocus);
  339. Assert.False (win.HasFocus);
  340. win.Enabled = true;
  341. win.FocusDeepest (null, NavigationDirection.Forward);
  342. Assert.True (button.HasFocus);
  343. Assert.True (win.HasFocus);
  344. Application.RequestStop ();
  345. };
  346. Application.Run (top);
  347. Assert.Equal (1, iterations);
  348. top.Dispose ();
  349. }
  350. // View.Focused & View.MostFocused tests
  351. // View.Focused - No subviews
  352. [Fact]
  353. [Trait ("BUGBUG", "Fix in Issue #3444")]
  354. public void Focused_NoSubviews ()
  355. {
  356. var view = new View ();
  357. Assert.Null (view.GetFocused ());
  358. view.CanFocus = true;
  359. view.SetFocus ();
  360. Assert.True (view.HasFocus);
  361. Assert.Null (view.GetFocused ()); // BUGBUG: Should be view
  362. }
  363. [Fact]
  364. [AutoInitShutdown]
  365. public void FocusNearestView_Ensure_Focus_Ordered ()
  366. {
  367. var top = new Toplevel ();
  368. var win = new Window ();
  369. var winSubview = new View { CanFocus = true, Text = "WindowSubview" };
  370. win.Add (winSubview);
  371. top.Add (win);
  372. var frm = new FrameView ();
  373. var frmSubview = new View { CanFocus = true, Text = "FrameSubview" };
  374. frm.Add (frmSubview);
  375. top.Add (frm);
  376. Application.Begin (top);
  377. Assert.Equal ("WindowSubview", top.GetMostFocused ().Text);
  378. Application.OnKeyDown (Key.Tab);
  379. Assert.Equal ("WindowSubview", top.GetMostFocused ().Text);
  380. Application.OnKeyDown (Key.F6);
  381. Assert.Equal ("FrameSubview", top.GetMostFocused ().Text);
  382. Application.OnKeyDown (Key.Tab);
  383. Assert.Equal ("FrameSubview", top.GetMostFocused ().Text);
  384. Application.OnKeyDown (Key.F6);
  385. Assert.Equal ("WindowSubview", top.GetMostFocused ().Text);
  386. Application.OnKeyDown (Key.F6.WithShift);
  387. Assert.Equal ("FrameSubview", top.GetMostFocused ().Text);
  388. Application.OnKeyDown (Key.F6.WithShift);
  389. Assert.Equal ("WindowSubview", top.GetMostFocused ().Text);
  390. top.Dispose ();
  391. }
  392. [Fact]
  393. [AutoInitShutdown]
  394. public void FocusNext_Does_Not_Throws_If_A_View_Was_Removed_From_The_Collection ()
  395. {
  396. Toplevel top1 = new ();
  397. var view1 = new View { Id = "view1", Width = 10, Height = 5, CanFocus = true };
  398. var top2 = new Toplevel { Id = "top2", Y = 1, Width = 10, Height = 5 };
  399. var view2 = new View
  400. {
  401. Id = "view2",
  402. Y = 1,
  403. Width = 10,
  404. Height = 5,
  405. CanFocus = true
  406. };
  407. View view3 = null;
  408. var removed = false;
  409. view2.Enter += (s, e) =>
  410. {
  411. if (!removed)
  412. {
  413. removed = true;
  414. view3 = new () { Id = "view3", Y = 1, Width = 10, Height = 5 };
  415. Application.Current.Add (view3);
  416. Application.Current.BringSubviewToFront (view3);
  417. Assert.False (view3.HasFocus);
  418. }
  419. };
  420. view2.Leave += (s, e) =>
  421. {
  422. Application.Current.Remove (view3);
  423. view3.Dispose ();
  424. view3 = null;
  425. };
  426. top2.Add (view2);
  427. top1.Add (view1, top2);
  428. Application.Begin (top1);
  429. Assert.True (top1.HasFocus);
  430. Assert.True (view1.HasFocus);
  431. Assert.False (view2.HasFocus);
  432. Assert.False (removed);
  433. Assert.Null (view3);
  434. Assert.True (Application.OnKeyDown (Key.F6));
  435. Assert.True (top1.HasFocus);
  436. Assert.False (view1.HasFocus);
  437. Assert.True (view2.HasFocus);
  438. Assert.True (removed);
  439. Assert.NotNull (view3);
  440. Exception exception = Record.Exception (() => Application.OnKeyDown (Key.F6));
  441. Assert.Null (exception);
  442. Assert.True (removed);
  443. //Assert.Null (view3);
  444. top1.Dispose ();
  445. }
  446. [Fact]
  447. public void GetMostFocused_NoSubviews_Returns_Null ()
  448. {
  449. var view = new View ();
  450. Assert.Null (view.GetFocused ());
  451. view.CanFocus = true;
  452. Assert.False (view.HasFocus);
  453. view.SetFocus ();
  454. Assert.True (view.HasFocus);
  455. Assert.Null (view.GetMostFocused ());
  456. }
  457. [Fact]
  458. public void GetMostFocused_Returns_Most ()
  459. {
  460. var view = new View ()
  461. {
  462. Id = "view",
  463. CanFocus = true
  464. };
  465. var subview = new View ()
  466. {
  467. Id = "subview",
  468. CanFocus = true
  469. };
  470. view.Add (subview);
  471. view.SetFocus ();
  472. Assert.True (view.HasFocus);
  473. Assert.True (subview.HasFocus);
  474. Assert.Equal (subview, view.GetMostFocused ());
  475. var subview2 = new View ()
  476. {
  477. Id = "subview2",
  478. CanFocus = true
  479. };
  480. view.Add (subview2);
  481. Assert.Equal (subview2, view.GetMostFocused ());
  482. }
  483. // [Fact]
  484. // [AutoInitShutdown]
  485. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_With_Top_KeyPress_Event ()
  486. // {
  487. // var sbQuiting = false;
  488. // var tfQuiting = false;
  489. // var topQuiting = false;
  490. // var sb = new StatusBar (
  491. // new Shortcut []
  492. // {
  493. // new (
  494. // KeyCode.CtrlMask | KeyCode.Q,
  495. // "Quit",
  496. // () => sbQuiting = true
  497. // )
  498. // }
  499. // );
  500. // var tf = new TextField ();
  501. // tf.KeyDown += Tf_KeyPressed;
  502. // void Tf_KeyPressed (object sender, Key obj)
  503. // {
  504. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  505. // {
  506. // obj.Handled = tfQuiting = true;
  507. // }
  508. // }
  509. // var win = new Window ();
  510. // win.Add (sb, tf);
  511. // Toplevel top = new ();
  512. // top.KeyDown += Top_KeyPress;
  513. // void Top_KeyPress (object sender, Key obj)
  514. // {
  515. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  516. // {
  517. // obj.Handled = topQuiting = true;
  518. // }
  519. // }
  520. // top.Add (win);
  521. // Application.Begin (top);
  522. // Assert.False (sbQuiting);
  523. // Assert.False (tfQuiting);
  524. // Assert.False (topQuiting);
  525. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  526. // Assert.False (sbQuiting);
  527. // Assert.True (tfQuiting);
  528. // Assert.False (topQuiting);
  529. //#if BROKE_WITH_2927
  530. // tf.KeyPressed -= Tf_KeyPress;
  531. // tfQuiting = false;
  532. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  533. // Application.MainLoop.RunIteration ();
  534. // Assert.True (sbQuiting);
  535. // Assert.False (tfQuiting);
  536. // Assert.False (topQuiting);
  537. // sb.RemoveItem (0);
  538. // sbQuiting = false;
  539. // Application.Driver?.SendKeys ('q', ConsoleKey.Q, false, false, true);
  540. // Application.MainLoop.RunIteration ();
  541. // Assert.False (sbQuiting);
  542. // Assert.False (tfQuiting);
  543. //// This test is now invalid because `win` is focused, so it will receive the keypress
  544. // Assert.True (topQuiting);
  545. //#endif
  546. // top.Dispose ();
  547. // }
  548. // [Fact]
  549. // [AutoInitShutdown]
  550. // public void HotKey_Will_Invoke_KeyPressed_Only_For_The_MostFocused_Without_Top_KeyPress_Event ()
  551. // {
  552. // var sbQuiting = false;
  553. // var tfQuiting = false;
  554. // var sb = new StatusBar (
  555. // new Shortcut []
  556. // {
  557. // new (
  558. // KeyCode.CtrlMask | KeyCode.Q,
  559. // "~^Q~ Quit",
  560. // () => sbQuiting = true
  561. // )
  562. // }
  563. // );
  564. // var tf = new TextField ();
  565. // tf.KeyDown += Tf_KeyPressed;
  566. // void Tf_KeyPressed (object sender, Key obj)
  567. // {
  568. // if (obj.KeyCode == (KeyCode.Q | KeyCode.CtrlMask))
  569. // {
  570. // obj.Handled = tfQuiting = true;
  571. // }
  572. // }
  573. // var win = new Window ();
  574. // win.Add (sb, tf);
  575. // Toplevel top = new ();
  576. // top.Add (win);
  577. // Application.Begin (top);
  578. // Assert.False (sbQuiting);
  579. // Assert.False (tfQuiting);
  580. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  581. // Assert.False (sbQuiting);
  582. // Assert.True (tfQuiting);
  583. // tf.KeyDown -= Tf_KeyPressed;
  584. // tfQuiting = false;
  585. // Application.Driver?.SendKeys ('Q', ConsoleKey.Q, false, false, true);
  586. // Application.MainLoop.RunIteration ();
  587. //#if BROKE_WITH_2927
  588. // Assert.True (sbQuiting);
  589. // Assert.False (tfQuiting);
  590. //#endif
  591. // top.Dispose ();
  592. // }
  593. [Fact]
  594. [SetupFakeDriver]
  595. public void Navigation_With_Null_Focused_View ()
  596. {
  597. // Non-regression test for #882 (NullReferenceException during keyboard navigation when Focused is null)
  598. Application.Init (new FakeDriver ());
  599. var top = new Toplevel ();
  600. top.Ready += (s, e) => { Assert.Null (top.GetFocused ()); };
  601. // Keyboard navigation with tab
  602. FakeConsole.MockKeyPresses.Push (new ('\t', ConsoleKey.Tab, false, false, false));
  603. Application.Iteration += (s, a) => Application.RequestStop ();
  604. Application.Run (top);
  605. top.Dispose ();
  606. Application.Shutdown ();
  607. }
  608. #if V2_NEW_FOCUS_IMPL // bogus test - Depends on auto setting of CanFocus
  609. [Fact]
  610. [AutoInitShutdown]
  611. public void Remove_Does_Not_Change_Focus ()
  612. {
  613. var top = new Toplevel ();
  614. Assert.True (top.CanFocus);
  615. Assert.False (top.HasFocus);
  616. var container = new View { Width = 10, Height = 10 };
  617. var leave = false;
  618. container.Leave += (s, e) => leave = true;
  619. Assert.False (container.CanFocus);
  620. var child = new View { Width = Dim.Fill (), Height = Dim.Fill (), CanFocus = true };
  621. container.Add (child);
  622. Assert.True (container.CanFocus);
  623. Assert.False (container.HasFocus);
  624. Assert.True (child.CanFocus);
  625. Assert.False (child.HasFocus);
  626. top.Add (container);
  627. Application.Begin (top);
  628. Assert.True (top.CanFocus);
  629. Assert.True (top.HasFocus);
  630. Assert.True (container.CanFocus);
  631. Assert.True (container.HasFocus);
  632. Assert.True (child.CanFocus);
  633. Assert.True (child.HasFocus);
  634. container.Remove (child);
  635. child.Dispose ();
  636. child = null;
  637. Assert.True (top.HasFocus);
  638. Assert.True (container.CanFocus);
  639. Assert.True (container.HasFocus);
  640. Assert.Null (child);
  641. Assert.False (leave);
  642. top.Dispose ();
  643. }
  644. #endif
  645. [Fact]
  646. [AutoInitShutdown]
  647. public void ScreenToView_ViewToScreen_FindDeepestView_Full_Top ()
  648. {
  649. Toplevel top = new ();
  650. top.BorderStyle = LineStyle.Single;
  651. var view = new View
  652. {
  653. X = 3,
  654. Y = 2,
  655. Width = 10,
  656. Height = 1,
  657. Text = "0123456789"
  658. };
  659. top.Add (view);
  660. Application.Begin (top);
  661. Assert.Equal (Application.Current, top);
  662. Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  663. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  664. Assert.Equal (new (0, 0, 80, 25), top.Frame);
  665. ((FakeDriver)Application.Driver!).SetBufferSize (20, 10);
  666. Assert.Equal (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  667. Assert.Equal (new (0, 0, 20, 10), top.Frame);
  668. _ = TestHelpers.AssertDriverContentsWithFrameAre (
  669. @"
  670. ┌──────────────────┐
  671. │ │
  672. │ │
  673. │ 0123456789 │
  674. │ │
  675. │ │
  676. │ │
  677. │ │
  678. │ │
  679. └──────────────────┘",
  680. _output
  681. );
  682. // top
  683. Assert.Equal (Point.Empty, top.ScreenToFrame (new (0, 0)));
  684. Point screen = top.Margin.ViewportToScreen (new Point (0, 0));
  685. Assert.Equal (0, screen.X);
  686. Assert.Equal (0, screen.Y);
  687. screen = top.Border.ViewportToScreen (new Point (0, 0));
  688. Assert.Equal (0, screen.X);
  689. Assert.Equal (0, screen.Y);
  690. screen = top.Padding.ViewportToScreen (new Point (0, 0));
  691. Assert.Equal (1, screen.X);
  692. Assert.Equal (1, screen.Y);
  693. screen = top.ViewportToScreen (new Point (0, 0));
  694. Assert.Equal (1, screen.X);
  695. Assert.Equal (1, screen.Y);
  696. screen = top.ViewportToScreen (new Point (-1, -1));
  697. Assert.Equal (0, screen.X);
  698. Assert.Equal (0, screen.Y);
  699. var found = View.FindDeepestView (top, new (0, 0));
  700. Assert.Equal (top.Border, found);
  701. Assert.Equal (0, found.Frame.X);
  702. Assert.Equal (0, found.Frame.Y);
  703. Assert.Equal (new (3, 2), top.ScreenToFrame (new (3, 2)));
  704. screen = top.ViewportToScreen (new Point (3, 2));
  705. Assert.Equal (4, screen.X);
  706. Assert.Equal (3, screen.Y);
  707. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  708. Assert.Equal (view, found);
  709. //Assert.Equal (0, found.FrameToScreen ().X);
  710. //Assert.Equal (0, found.FrameToScreen ().Y);
  711. found = View.FindDeepestView (top, new (3, 2));
  712. Assert.Equal (top, found);
  713. //Assert.Equal (3, found.FrameToScreen ().X);
  714. //Assert.Equal (2, found.FrameToScreen ().Y);
  715. Assert.Equal (new (13, 2), top.ScreenToFrame (new (13, 2)));
  716. screen = top.ViewportToScreen (new Point (12, 2));
  717. Assert.Equal (13, screen.X);
  718. Assert.Equal (3, screen.Y);
  719. found = View.FindDeepestView (top, new (screen.X, screen.Y));
  720. Assert.Equal (view, found);
  721. //Assert.Equal (9, found.FrameToScreen ().X);
  722. //Assert.Equal (0, found.FrameToScreen ().Y);
  723. screen = top.ViewportToScreen (new Point (13, 2));
  724. Assert.Equal (14, screen.X);
  725. Assert.Equal (3, screen.Y);
  726. found = View.FindDeepestView (top, new (13, 2));
  727. Assert.Equal (top, found);
  728. //Assert.Equal (13, found.FrameToScreen ().X);
  729. //Assert.Equal (2, found.FrameToScreen ().Y);
  730. Assert.Equal (new (14, 3), top.ScreenToFrame (new (14, 3)));
  731. screen = top.ViewportToScreen (new Point (14, 3));
  732. Assert.Equal (15, screen.X);
  733. Assert.Equal (4, screen.Y);
  734. found = View.FindDeepestView (top, new (14, 3));
  735. Assert.Equal (top, found);
  736. //Assert.Equal (14, found.FrameToScreen ().X);
  737. //Assert.Equal (3, found.FrameToScreen ().Y);
  738. // view
  739. Assert.Equal (new (-4, -3), view.ScreenToFrame (new (0, 0)));
  740. screen = view.Margin.ViewportToScreen (new Point (-3, -2));
  741. Assert.Equal (1, screen.X);
  742. Assert.Equal (1, screen.Y);
  743. screen = view.Border.ViewportToScreen (new Point (-3, -2));
  744. Assert.Equal (1, screen.X);
  745. Assert.Equal (1, screen.Y);
  746. screen = view.Padding.ViewportToScreen (new Point (-3, -2));
  747. Assert.Equal (1, screen.X);
  748. Assert.Equal (1, screen.Y);
  749. screen = view.ViewportToScreen (new Point (-3, -2));
  750. Assert.Equal (1, screen.X);
  751. Assert.Equal (1, screen.Y);
  752. screen = view.ViewportToScreen (new Point (-4, -3));
  753. Assert.Equal (0, screen.X);
  754. Assert.Equal (0, screen.Y);
  755. found = View.FindDeepestView (top, new (0, 0));
  756. Assert.Equal (top.Border, found);
  757. Assert.Equal (new (-1, -1), view.ScreenToFrame (new (3, 2)));
  758. screen = view.ViewportToScreen (new Point (0, 0));
  759. Assert.Equal (4, screen.X);
  760. Assert.Equal (3, screen.Y);
  761. found = View.FindDeepestView (top, new (4, 3));
  762. Assert.Equal (view, found);
  763. Assert.Equal (new (9, -1), view.ScreenToFrame (new (13, 2)));
  764. screen = view.ViewportToScreen (new Point (10, 0));
  765. Assert.Equal (14, screen.X);
  766. Assert.Equal (3, screen.Y);
  767. found = View.FindDeepestView (top, new (14, 3));
  768. Assert.Equal (top, found);
  769. Assert.Equal (new (10, 0), view.ScreenToFrame (new (14, 3)));
  770. screen = view.ViewportToScreen (new Point (11, 1));
  771. Assert.Equal (15, screen.X);
  772. Assert.Equal (4, screen.Y);
  773. found = View.FindDeepestView (top, new (15, 4));
  774. Assert.Equal (top, found);
  775. top.Dispose ();
  776. }
  777. [Fact]
  778. [AutoInitShutdown]
  779. public void ScreenToView_ViewToScreen_FindDeepestView_Smaller_Top ()
  780. {
  781. var top = new Toplevel
  782. {
  783. X = 3,
  784. Y = 2,
  785. Width = 20,
  786. Height = 10,
  787. BorderStyle = LineStyle.Single
  788. };
  789. var view = new View
  790. {
  791. X = 3,
  792. Y = 2,
  793. Width = 10,
  794. Height = 1,
  795. Text = "0123456789"
  796. };
  797. top.Add (view);
  798. Application.Begin (top);
  799. Assert.Equal (Application.Current, top);
  800. Assert.Equal (new (0, 0, 80, 25), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  801. Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  802. Assert.Equal (new (3, 2, 20, 10), top.Frame);
  803. ((FakeDriver)Application.Driver!).SetBufferSize (30, 20);
  804. Assert.Equal (new (0, 0, 30, 20), new Rectangle (0, 0, View.Driver.Cols, View.Driver.Rows));
  805. Assert.NotEqual (new (0, 0, View.Driver.Cols, View.Driver.Rows), top.Frame);
  806. Assert.Equal (new (3, 2, 20, 10), top.Frame);
  807. Rectangle frame = TestHelpers.AssertDriverContentsWithFrameAre (
  808. @"
  809. ┌──────────────────┐
  810. │ │
  811. │ │
  812. │ 0123456789 │
  813. │ │
  814. │ │
  815. │ │
  816. │ │
  817. │ │
  818. └──────────────────┘",
  819. _output
  820. );
  821. // mean the output started at col 3 and line 2
  822. // which result with a width of 23 and a height of 10 on the output
  823. Assert.Equal (new (3, 2, 23, 10), frame);
  824. // top
  825. Assert.Equal (new (-3, -2), top.ScreenToFrame (new (0, 0)));
  826. Point screen = top.Margin.ViewportToScreen (new Point (-3, -2));
  827. Assert.Equal (0, screen.X);
  828. Assert.Equal (0, screen.Y);
  829. screen = top.Border.ViewportToScreen (new Point (-3, -2));
  830. Assert.Equal (0, screen.X);
  831. Assert.Equal (0, screen.Y);
  832. screen = top.Padding.ViewportToScreen (new Point (-3, -2));
  833. Assert.Equal (1, screen.X);
  834. Assert.Equal (1, screen.Y);
  835. screen = top.ViewportToScreen (new Point (-3, -2));
  836. Assert.Equal (1, screen.X);
  837. Assert.Equal (1, screen.Y);
  838. screen = top.ViewportToScreen (new Point (-4, -3));
  839. Assert.Equal (0, screen.X);
  840. Assert.Equal (0, screen.Y);
  841. var found = View.FindDeepestView (top, new (-4, -3));
  842. Assert.Null (found);
  843. Assert.Equal (Point.Empty, top.ScreenToFrame (new (3, 2)));
  844. screen = top.ViewportToScreen (new Point (0, 0));
  845. Assert.Equal (4, screen.X);
  846. Assert.Equal (3, screen.Y);
  847. Assert.Equal (top.Border, View.FindDeepestView (top, new (3, 2)));
  848. //Assert.Equal (0, found.FrameToScreen ().X);
  849. //Assert.Equal (0, found.FrameToScreen ().Y);
  850. Assert.Equal (new (10, 0), top.ScreenToFrame (new (13, 2)));
  851. screen = top.ViewportToScreen (new Point (10, 0));
  852. Assert.Equal (14, screen.X);
  853. Assert.Equal (3, screen.Y);
  854. Assert.Equal (top.Border, View.FindDeepestView (top, new (13, 2)));
  855. //Assert.Equal (10, found.FrameToScreen ().X);
  856. //Assert.Equal (0, found.FrameToScreen ().Y);
  857. Assert.Equal (new (11, 1), top.ScreenToFrame (new (14, 3)));
  858. screen = top.ViewportToScreen (new Point (11, 1));
  859. Assert.Equal (15, screen.X);
  860. Assert.Equal (4, screen.Y);
  861. Assert.Equal (top, View.FindDeepestView (top, new (14, 3)));
  862. // view
  863. Assert.Equal (new (-7, -5), view.ScreenToFrame (new (0, 0)));
  864. screen = view.Margin.ViewportToScreen (new Point (-6, -4));
  865. Assert.Equal (1, screen.X);
  866. Assert.Equal (1, screen.Y);
  867. screen = view.Border.ViewportToScreen (new Point (-6, -4));
  868. Assert.Equal (1, screen.X);
  869. Assert.Equal (1, screen.Y);
  870. screen = view.Padding.ViewportToScreen (new Point (-6, -4));
  871. Assert.Equal (1, screen.X);
  872. Assert.Equal (1, screen.Y);
  873. screen = view.ViewportToScreen (new Point (-6, -4));
  874. Assert.Equal (1, screen.X);
  875. Assert.Equal (1, screen.Y);
  876. Assert.Null (View.FindDeepestView (top, new (1, 1)));
  877. Assert.Equal (new (-4, -3), view.ScreenToFrame (new (3, 2)));
  878. screen = view.ViewportToScreen (new Point (-3, -2));
  879. Assert.Equal (4, screen.X);
  880. Assert.Equal (3, screen.Y);
  881. Assert.Equal (top, View.FindDeepestView (top, new (4, 3)));
  882. Assert.Equal (new (-1, -1), view.ScreenToFrame (new (6, 4)));
  883. screen = view.ViewportToScreen (new Point (0, 0));
  884. Assert.Equal (7, screen.X);
  885. Assert.Equal (5, screen.Y);
  886. Assert.Equal (view, View.FindDeepestView (top, new (7, 5)));
  887. Assert.Equal (new (6, -1), view.ScreenToFrame (new (13, 4)));
  888. screen = view.ViewportToScreen (new Point (7, 0));
  889. Assert.Equal (14, screen.X);
  890. Assert.Equal (5, screen.Y);
  891. Assert.Equal (view, View.FindDeepestView (top, new (14, 5)));
  892. Assert.Equal (new (7, -2), view.ScreenToFrame (new (14, 3)));
  893. screen = view.ViewportToScreen (new Point (8, -1));
  894. Assert.Equal (15, screen.X);
  895. Assert.Equal (4, screen.Y);
  896. Assert.Equal (top, View.FindDeepestView (top, new (15, 4)));
  897. Assert.Equal (new (16, -2), view.ScreenToFrame (new (23, 3)));
  898. screen = view.ViewportToScreen (new Point (17, -1));
  899. Assert.Equal (24, screen.X);
  900. Assert.Equal (4, screen.Y);
  901. Assert.Null (View.FindDeepestView (top, new (24, 4)));
  902. top.Dispose ();
  903. }
  904. }