ApplicationTests.cs 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Xunit;
  8. // Alias Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui.ApplicationTests;
  11. public class ApplicationTests {
  12. public ApplicationTests ()
  13. {
  14. #if DEBUG_IDISPOSABLE
  15. Responder.Instances.Clear ();
  16. RunState.Instances.Clear ();
  17. #endif
  18. }
  19. void Pre_Init_State ()
  20. {
  21. Assert.Null (Application.Driver);
  22. Assert.Null (Application.Top);
  23. Assert.Null (Application.Current);
  24. Assert.Null (Application.MainLoop);
  25. }
  26. void Post_Init_State ()
  27. {
  28. Assert.NotNull (Application.Driver);
  29. Assert.NotNull (Application.Top);
  30. Assert.NotNull (Application.Current);
  31. Assert.NotNull (Application.MainLoop);
  32. // FakeDriver is always 80x25
  33. Assert.Equal (80, Application.Driver.Cols);
  34. Assert.Equal (25, Application.Driver.Rows);
  35. }
  36. void Init ()
  37. {
  38. Application.Init (new FakeDriver ());
  39. Assert.NotNull (Application.Driver);
  40. Assert.NotNull (Application.MainLoop);
  41. Assert.NotNull (SynchronizationContext.Current);
  42. }
  43. void Shutdown ()
  44. {
  45. Application.Shutdown ();
  46. }
  47. [Fact]
  48. public void Init_Shutdown_Cleans_Up ()
  49. {
  50. // Verify initial state is per spec
  51. //Pre_Init_State ();
  52. Application.Init (new FakeDriver ());
  53. // Verify post-Init state is correct
  54. //Post_Init_State ();
  55. Application.Shutdown ();
  56. // Verify state is back to initial
  57. //Pre_Init_State ();
  58. #if DEBUG_IDISPOSABLE
  59. // Validate there are no outstanding Responder-based instances
  60. // after a scenario was selected to run. This proves the main UI Catalog
  61. // 'app' closed cleanly.
  62. Assert.Empty (Responder.Instances);
  63. #endif
  64. }
  65. [Fact]
  66. public void Init_Unbalanced_Throws ()
  67. {
  68. Application.Init (new FakeDriver ());
  69. Toplevel topLevel = null;
  70. Assert.Throws<InvalidOperationException> (() => Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ()));
  71. Shutdown ();
  72. Assert.Null (Application.Top);
  73. Assert.Null (Application.MainLoop);
  74. Assert.Null (Application.Driver);
  75. // Now try the other way
  76. topLevel = null;
  77. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  78. Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
  79. Shutdown ();
  80. Assert.Null (Application.Top);
  81. Assert.Null (Application.MainLoop);
  82. Assert.Null (Application.Driver);
  83. }
  84. class TestToplevel : Toplevel {
  85. public TestToplevel ()
  86. {
  87. IsOverlappedContainer = false;
  88. }
  89. }
  90. [Fact]
  91. public void Init_Null_Driver_Should_Pick_A_Driver ()
  92. {
  93. Application.Init (null);
  94. Assert.NotNull (Application.Driver);
  95. Shutdown ();
  96. }
  97. [Fact]
  98. public void Init_Begin_End_Cleans_Up ()
  99. {
  100. Init ();
  101. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  102. // if we don't stop
  103. Application.Iteration += (s, a) => {
  104. Application.RequestStop ();
  105. };
  106. RunState runstate = null;
  107. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
  108. Assert.NotNull (e.State);
  109. runstate = e.State;
  110. };
  111. Application.NotifyNewRunState += NewRunStateFn;
  112. Toplevel topLevel = new Toplevel ();
  113. var rs = Application.Begin (topLevel);
  114. Assert.NotNull (rs);
  115. Assert.NotNull (runstate);
  116. Assert.Equal (rs, runstate);
  117. Assert.Equal (topLevel, Application.Top);
  118. Assert.Equal (topLevel, Application.Current);
  119. Application.NotifyNewRunState -= NewRunStateFn;
  120. Application.End (runstate);
  121. Assert.Null (Application.Current);
  122. Assert.NotNull (Application.Top);
  123. Assert.NotNull (Application.MainLoop);
  124. Assert.NotNull (Application.Driver);
  125. Shutdown ();
  126. Assert.Null (Application.Top);
  127. Assert.Null (Application.MainLoop);
  128. Assert.Null (Application.Driver);
  129. }
  130. [Fact]
  131. public void InitWithTopLevelFactory_Begin_End_Cleans_Up ()
  132. {
  133. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  134. // if we don't stop
  135. Application.Iteration += (s, a) => {
  136. Application.RequestStop ();
  137. };
  138. // NOTE: Run<T>, when called after Init has been called behaves differently than
  139. // when called if Init has not been called.
  140. Toplevel topLevel = null;
  141. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  142. RunState runstate = null;
  143. EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
  144. Assert.NotNull (e.State);
  145. runstate = e.State;
  146. };
  147. Application.NotifyNewRunState += NewRunStateFn;
  148. var rs = Application.Begin (topLevel);
  149. Assert.NotNull (rs);
  150. Assert.NotNull (runstate);
  151. Assert.Equal (rs, runstate);
  152. Assert.Equal (topLevel, Application.Top);
  153. Assert.Equal (topLevel, Application.Current);
  154. Application.NotifyNewRunState -= NewRunStateFn;
  155. Application.End (runstate);
  156. Assert.Null (Application.Current);
  157. Assert.NotNull (Application.Top);
  158. Assert.NotNull (Application.MainLoop);
  159. Assert.NotNull (Application.Driver);
  160. Shutdown ();
  161. Assert.Null (Application.Top);
  162. Assert.Null (Application.MainLoop);
  163. Assert.Null (Application.Driver);
  164. }
  165. [Fact]
  166. public void Begin_Null_Toplevel_Throws ()
  167. {
  168. // Setup Mock driver
  169. Init ();
  170. // Test null Toplevel
  171. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  172. Shutdown ();
  173. Assert.Null (Application.Top);
  174. Assert.Null (Application.MainLoop);
  175. Assert.Null (Application.Driver);
  176. }
  177. #region RunTests
  178. [Fact]
  179. public void Run_T_After_InitWithDriver_with_TopLevel_Throws ()
  180. {
  181. // Setup Mock driver
  182. Init ();
  183. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not dervied from TopLevel)
  184. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> (errorHandler: null));
  185. Shutdown ();
  186. Assert.Null (Application.Top);
  187. Assert.Null (Application.MainLoop);
  188. Assert.Null (Application.Driver);
  189. }
  190. [Fact]
  191. public void Run_T_After_InitWithDriver_with_TopLevel_and_Driver_Throws ()
  192. {
  193. // Setup Mock driver
  194. Init ();
  195. // Run<Toplevel> when already initialized with a Driver will throw (because Toplevel is not dervied from TopLevel)
  196. Assert.Throws<ArgumentException> (() => Application.Run<Toplevel> (errorHandler: null, new FakeDriver ()));
  197. Shutdown ();
  198. Assert.Null (Application.Top);
  199. Assert.Null (Application.MainLoop);
  200. Assert.Null (Application.Driver);
  201. }
  202. [Fact]
  203. public void Run_T_After_InitWithDriver_with_TestTopLevel_DoesNotThrow ()
  204. {
  205. // Setup Mock driver
  206. Init ();
  207. Application.Iteration += (s, a) => {
  208. Application.RequestStop ();
  209. };
  210. // Init has been called and we're passing no driver to Run<TestTopLevel>. This is ok.
  211. Application.Run<TestToplevel> ();
  212. Shutdown ();
  213. Assert.Null (Application.Top);
  214. Assert.Null (Application.MainLoop);
  215. Assert.Null (Application.Driver);
  216. }
  217. [Fact]
  218. public void Run_T_After_InitNullDriver_with_TestTopLevel_Throws ()
  219. {
  220. Application._forceFakeConsole = true;
  221. Application.Init (null);
  222. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  223. Application.Iteration += (s, a) => {
  224. Application.RequestStop ();
  225. };
  226. // Init has been called without selecting a driver and we're passing no driver to Run<TestTopLevel>. Bad
  227. Application.Run<TestToplevel> ();
  228. Shutdown ();
  229. Assert.Null (Application.Top);
  230. Assert.Null (Application.MainLoop);
  231. Assert.Null (Application.Driver);
  232. }
  233. [Fact]
  234. public void Run_T_Init_Driver_Cleared_with_TestTopLevel_Throws ()
  235. {
  236. Init ();
  237. Application.Driver = null;
  238. Application.Iteration += (s, a) => {
  239. Application.RequestStop ();
  240. };
  241. // Init has been called, but Driver has been set to null. Bad.
  242. Assert.Throws<InvalidOperationException> (() => Application.Run<TestToplevel> ());
  243. Shutdown ();
  244. Assert.Null (Application.Top);
  245. Assert.Null (Application.MainLoop);
  246. Assert.Null (Application.Driver);
  247. }
  248. [Fact]
  249. public void Run_T_NoInit_DoesNotThrow ()
  250. {
  251. Application._forceFakeConsole = true;
  252. Application.Iteration += (s, a) => {
  253. Application.RequestStop ();
  254. };
  255. Application.Run<TestToplevel> ();
  256. Assert.Equal (typeof (FakeDriver), Application.Driver.GetType ());
  257. Shutdown ();
  258. Assert.Null (Application.Top);
  259. Assert.Null (Application.MainLoop);
  260. Assert.Null (Application.Driver);
  261. }
  262. [Fact]
  263. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  264. {
  265. Application.Iteration += (s, a) => {
  266. Application.RequestStop ();
  267. };
  268. // Init has NOT been called and we're passing a valid driver to Run<TestTopLevel>. This is ok.
  269. Application.Run<TestToplevel> (errorHandler: null, new FakeDriver ());
  270. Shutdown ();
  271. Assert.Null (Application.Top);
  272. Assert.Null (Application.MainLoop);
  273. Assert.Null (Application.Driver);
  274. }
  275. [Fact]
  276. public void Run_RequestStop_Stops ()
  277. {
  278. // Setup Mock driver
  279. Init ();
  280. var top = new Toplevel ();
  281. var rs = Application.Begin (top);
  282. Assert.NotNull (rs);
  283. Assert.Equal (top, Application.Current);
  284. Application.Iteration += (s, a) => {
  285. Application.RequestStop ();
  286. };
  287. Application.Run (top);
  288. Application.Shutdown ();
  289. Assert.Null (Application.Current);
  290. Assert.Null (Application.Top);
  291. Assert.Null (Application.MainLoop);
  292. Assert.Null (Application.Driver);
  293. }
  294. [Fact]
  295. public void Run_RunningFalse_Stops ()
  296. {
  297. // Setup Mock driver
  298. Init ();
  299. var top = new Toplevel ();
  300. var rs = Application.Begin (top);
  301. Assert.NotNull (rs);
  302. Assert.Equal (top, Application.Current);
  303. Application.Iteration += (s, a) => {
  304. top.Running = false;
  305. };
  306. Application.Run (top);
  307. Application.Shutdown ();
  308. Assert.Null (Application.Current);
  309. Assert.Null (Application.Top);
  310. Assert.Null (Application.MainLoop);
  311. Assert.Null (Application.Driver);
  312. }
  313. [Fact]
  314. public void Run_Loaded_Ready_Unlodaded_Events ()
  315. {
  316. Init ();
  317. var top = Application.Top;
  318. var count = 0;
  319. top.Loaded += (s, e) => count++;
  320. top.Ready += (s, e) => count++;
  321. top.Unloaded += (s, e) => count++;
  322. Application.Iteration += (s, a) => Application.RequestStop ();
  323. Application.Run ();
  324. Application.Shutdown ();
  325. Assert.Equal (3, count);
  326. }
  327. // TODO: Add tests for Run that test errorHandler
  328. #endregion
  329. #region ShutdownTests
  330. [Fact]
  331. public void Shutdown_Allows_Async ()
  332. {
  333. static async Task TaskWithAsyncContinuation ()
  334. {
  335. await Task.Yield ();
  336. await Task.Yield ();
  337. }
  338. Init ();
  339. Application.Shutdown ();
  340. var task = TaskWithAsyncContinuation ();
  341. Thread.Sleep (20);
  342. Assert.True (task.IsCompletedSuccessfully);
  343. }
  344. [Fact]
  345. public void Shutdown_Resets_SyncContext ()
  346. {
  347. Init ();
  348. Application.Shutdown ();
  349. Assert.Null (SynchronizationContext.Current);
  350. }
  351. #endregion
  352. [Fact, AutoInitShutdown]
  353. public void Begin_Sets_Application_Top_To_Console_Size ()
  354. {
  355. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  356. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  357. Application.Begin (Application.Top);
  358. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  359. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  360. Assert.Equal (new Rect (0, 0, 5, 5), Application.Top.Frame);
  361. }
  362. [Fact]
  363. [AutoInitShutdown]
  364. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  365. {
  366. var t1 = new Toplevel ();
  367. var t2 = new Toplevel ();
  368. var t3 = new Toplevel ();
  369. var d = new Dialog ();
  370. var t4 = new Toplevel ();
  371. // t1, t2, t3, d, t4
  372. var iterations = 5;
  373. t1.Ready += (s, e) => {
  374. Assert.Equal (t1, Application.Top);
  375. Application.Run (t2);
  376. };
  377. t2.Ready += (s, e) => {
  378. Assert.Equal (t2, Application.Top);
  379. Application.Run (t3);
  380. };
  381. t3.Ready += (s, e) => {
  382. Assert.Equal (t3, Application.Top);
  383. Application.Run (d);
  384. };
  385. d.Ready += (s, e) => {
  386. Assert.Equal (t3, Application.Top);
  387. Application.Run (t4);
  388. };
  389. t4.Ready += (s, e) => {
  390. Assert.Equal (t4, Application.Top);
  391. t4.RequestStop ();
  392. d.RequestStop ();
  393. t3.RequestStop ();
  394. t2.RequestStop ();
  395. };
  396. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  397. t2.Closed += (s, _) => {
  398. t1.RequestStop ();
  399. };
  400. Application.Iteration += (s, a) => {
  401. if (iterations == 5) {
  402. // The Current still is t4 because Current.Running is false.
  403. Assert.Equal (t4, Application.Current);
  404. Assert.False (Application.Current.Running);
  405. Assert.Equal (t4, Application.Top);
  406. } else if (iterations == 4) {
  407. // The Current is d and Current.Running is false.
  408. Assert.Equal (d, Application.Current);
  409. Assert.False (Application.Current.Running);
  410. Assert.Equal (t4, Application.Top);
  411. } else if (iterations == 3) {
  412. // The Current is t3 and Current.Running is false.
  413. Assert.Equal (t3, Application.Current);
  414. Assert.False (Application.Current.Running);
  415. Assert.Equal (t3, Application.Top);
  416. } else if (iterations == 2) {
  417. // The Current is t2 and Current.Running is false.
  418. Assert.Equal (t2, Application.Current);
  419. Assert.False (Application.Current.Running);
  420. Assert.Equal (t2, Application.Top);
  421. } else {
  422. // The Current is t1.
  423. Assert.Equal (t1, Application.Current);
  424. Assert.False (Application.Current.Running);
  425. Assert.Equal (t1, Application.Top);
  426. }
  427. iterations--;
  428. };
  429. Application.Run (t1);
  430. Assert.Equal (t1, Application.Top);
  431. }
  432. [Fact]
  433. [AutoInitShutdown]
  434. public void Internal_Properties_Correct ()
  435. {
  436. Assert.True (Application._initialized);
  437. Assert.NotNull (Application.Top);
  438. var rs = Application.Begin (Application.Top);
  439. Assert.Equal (Application.Top, rs.Toplevel);
  440. Assert.Null (Application.MouseGrabView); // public
  441. Assert.Null (Application.WantContinuousButtonPressedView); // public
  442. Assert.False (Application.MoveToOverlappedChild (Application.Top));
  443. }
  444. #region KeyboardTests
  445. [Fact]
  446. public void KeyUp_Event ()
  447. {
  448. // Setup Mock driver
  449. Init ();
  450. // Setup some fake keypresses (This)
  451. var input = "Tests";
  452. // Put a control-q in at the end
  453. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  454. foreach (var c in input.Reverse ()) {
  455. if (char.IsLetter (c)) {
  456. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  457. } else {
  458. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  459. }
  460. }
  461. int stackSize = FakeConsole.MockKeyPresses.Count;
  462. int iterations = 0;
  463. Application.Iteration += (s, a) => {
  464. iterations++;
  465. // Stop if we run out of control...
  466. if (iterations > 10) {
  467. Application.RequestStop ();
  468. }
  469. };
  470. int keyUps = 0;
  471. var output = string.Empty;
  472. Application.Top.KeyUp += (object sender, KeyEventEventArgs args) => {
  473. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  474. output += (char)args.KeyEvent.KeyValue;
  475. }
  476. keyUps++;
  477. };
  478. Application.Run (Application.Top);
  479. // Input string should match output
  480. Assert.Equal (input, output);
  481. // # of key up events should match stack size
  482. //Assert.Equal (stackSize, keyUps);
  483. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  484. // it must be literal (Linux only).
  485. Assert.Equal (6, keyUps);
  486. // # of key up events should match # of iterations
  487. Assert.Equal (stackSize, iterations);
  488. Application.Shutdown ();
  489. Assert.Null (Application.Current);
  490. Assert.Null (Application.Top);
  491. Assert.Null (Application.MainLoop);
  492. Assert.Null (Application.Driver);
  493. }
  494. [Fact]
  495. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  496. {
  497. Init ();
  498. var top = Application.Top;
  499. var w1 = new Window ();
  500. var v1 = new TextField ();
  501. var v2 = new TextView ();
  502. w1.Add (v1, v2);
  503. var w2 = new Window ();
  504. var v3 = new CheckBox ();
  505. var v4 = new Button ();
  506. w2.Add (v3, v4);
  507. top.Add (w1, w2);
  508. Application.Iteration += (s, a) => {
  509. Assert.True (v1.HasFocus);
  510. // Using default keys.
  511. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  512. new KeyModifiers () { Ctrl = true }));
  513. Assert.True (v2.HasFocus);
  514. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  515. new KeyModifiers () { Ctrl = true }));
  516. Assert.True (v3.HasFocus);
  517. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  518. new KeyModifiers () { Ctrl = true }));
  519. Assert.True (v4.HasFocus);
  520. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  521. new KeyModifiers () { Ctrl = true }));
  522. Assert.True (v1.HasFocus);
  523. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  524. new KeyModifiers () { Shift = true, Ctrl = true }));
  525. Assert.True (v4.HasFocus);
  526. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  527. new KeyModifiers () { Shift = true, Ctrl = true }));
  528. Assert.True (v3.HasFocus);
  529. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  530. new KeyModifiers () { Shift = true, Ctrl = true }));
  531. Assert.True (v2.HasFocus);
  532. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  533. new KeyModifiers () { Shift = true, Ctrl = true }));
  534. Assert.True (v1.HasFocus);
  535. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  536. new KeyModifiers () { Ctrl = true }));
  537. Assert.True (v2.HasFocus);
  538. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  539. new KeyModifiers () { Ctrl = true }));
  540. Assert.True (v3.HasFocus);
  541. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  542. new KeyModifiers () { Ctrl = true }));
  543. Assert.True (v4.HasFocus);
  544. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  545. new KeyModifiers () { Ctrl = true }));
  546. Assert.True (v1.HasFocus);
  547. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  548. new KeyModifiers () { Ctrl = true }));
  549. Assert.True (v4.HasFocus);
  550. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  551. new KeyModifiers () { Ctrl = true }));
  552. Assert.True (v3.HasFocus);
  553. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  554. new KeyModifiers () { Ctrl = true }));
  555. Assert.True (v2.HasFocus);
  556. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  557. new KeyModifiers () { Ctrl = true }));
  558. Assert.True (v1.HasFocus);
  559. // Using another's alternate keys.
  560. Application.AlternateForwardKey = Key.F7;
  561. Application.AlternateBackwardKey = Key.F6;
  562. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  563. Assert.True (v2.HasFocus);
  564. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  565. Assert.True (v3.HasFocus);
  566. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  567. Assert.True (v4.HasFocus);
  568. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  569. Assert.True (v1.HasFocus);
  570. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  571. Assert.True (v4.HasFocus);
  572. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  573. Assert.True (v3.HasFocus);
  574. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  575. Assert.True (v2.HasFocus);
  576. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  577. Assert.True (v1.HasFocus);
  578. Application.RequestStop ();
  579. };
  580. Application.Run (top);
  581. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  582. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  583. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  584. Application.QuitKey = Key.Q | Key.CtrlMask;
  585. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  586. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  587. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  588. // Shutdown must be called to safely clean up Application if Init has been called
  589. Application.Shutdown ();
  590. }
  591. [Fact]
  592. [AutoInitShutdown]
  593. public void QuitKey_Getter_Setter ()
  594. {
  595. var top = Application.Top;
  596. var isQuiting = false;
  597. top.Closing += (s, e) => {
  598. isQuiting = true;
  599. e.Cancel = true;
  600. };
  601. Application.Begin (top);
  602. top.Running = true;
  603. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  604. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  605. Assert.True (isQuiting);
  606. isQuiting = false;
  607. Application.QuitKey = Key.C | Key.CtrlMask;
  608. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  609. Assert.False (isQuiting);
  610. Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
  611. Assert.True (isQuiting);
  612. // Reset the QuitKey to avoid throws errors on another tests
  613. Application.QuitKey = Key.Q | Key.CtrlMask;
  614. }
  615. [Fact]
  616. [AutoInitShutdown]
  617. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard_And_Mouse ()
  618. {
  619. var top = Application.Top;
  620. var win = new Window () { Title = "win", X = 0, Y = 0, Width = 20, Height = 10 };
  621. var tf = new TextField () { Width = 10 };
  622. win.Add (tf);
  623. var win2 = new Window () { Title = "win2", X = 22, Y = 0, Width = 20, Height = 10 };
  624. var tf2 = new TextField () { Width = 10 };
  625. win2.Add (tf2);
  626. top.Add (win, win2);
  627. Application.Begin (top);
  628. Assert.True (win.CanFocus);
  629. Assert.True (win.HasFocus);
  630. Assert.True (win2.CanFocus);
  631. Assert.False (win2.HasFocus);
  632. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  633. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  634. Assert.True (win.CanFocus);
  635. Assert.False (win.HasFocus);
  636. Assert.True (win2.CanFocus);
  637. Assert.True (win2.HasFocus);
  638. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  639. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  640. Assert.True (win.CanFocus);
  641. Assert.True (win.HasFocus);
  642. Assert.True (win2.CanFocus);
  643. Assert.False (win2.HasFocus);
  644. Assert.Equal ("win", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  645. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  646. Assert.True (win.CanFocus);
  647. Assert.False (win.HasFocus);
  648. Assert.True (win2.CanFocus);
  649. Assert.True (win2.HasFocus);
  650. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  651. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  652. Assert.Null (Toplevel._dragPosition);
  653. }
  654. [Fact]
  655. [AutoInitShutdown]
  656. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard_And_Mouse ()
  657. {
  658. var top = Application.Top;
  659. var win = new Window () { Title = "win", X = 0, Y = 0, Width = 20, Height = 10 };
  660. var tf = new TextField () { Width = 10 };
  661. win.Add (tf);
  662. var win2 = new Window () { Title = "win2", X = 22, Y = 0, Width = 20, Height = 10 };
  663. var tf2 = new TextField () { Width = 10 };
  664. win2.Add (tf2);
  665. top.Add (win, win2);
  666. Application.Begin (top);
  667. Assert.True (win.CanFocus);
  668. Assert.True (win.HasFocus);
  669. Assert.True (win2.CanFocus);
  670. Assert.False (win2.HasFocus);
  671. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  672. win.CanFocus = false;
  673. Assert.False (win.CanFocus);
  674. Assert.False (win.HasFocus);
  675. Assert.True (win2.CanFocus);
  676. Assert.True (win2.HasFocus);
  677. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  678. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  679. Assert.True (win2.CanFocus);
  680. Assert.False (win.HasFocus);
  681. Assert.True (win2.CanFocus);
  682. Assert.True (win2.HasFocus);
  683. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  684. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  685. Assert.False (win.CanFocus);
  686. Assert.False (win.HasFocus);
  687. Assert.True (win2.CanFocus);
  688. Assert.True (win2.HasFocus);
  689. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  690. win.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  691. Assert.False (win.CanFocus);
  692. Assert.False (win.HasFocus);
  693. Assert.True (win2.CanFocus);
  694. Assert.True (win2.HasFocus);
  695. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  696. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  697. Assert.Null (Toplevel._dragPosition);
  698. }
  699. #endregion
  700. // Invoke Tests
  701. // TODO: Test with threading scenarios
  702. [Fact]
  703. public void Invoke_Adds_Idle ()
  704. {
  705. Application.Init (new FakeDriver ());
  706. var top = new Toplevel ();
  707. var rs = Application.Begin (top);
  708. bool firstIteration = false;
  709. var actionCalled = 0;
  710. Application.Invoke (() => { actionCalled++; });
  711. Application.RunIteration (ref rs, ref firstIteration);
  712. Assert.Equal (1, actionCalled);
  713. Application.Shutdown ();
  714. }
  715. #region mousegrabtests
  716. [Fact, AutoInitShutdown]
  717. public void MouseGrabView_WithNullMouseEventView ()
  718. {
  719. var tf = new TextField () { Width = 10 };
  720. var sv = new ScrollView () {
  721. Width = Dim.Fill (),
  722. Height = Dim.Fill (),
  723. ContentSize = new Size (100, 100)
  724. };
  725. sv.Add (tf);
  726. Application.Top.Add (sv);
  727. var iterations = -1;
  728. Application.Iteration += (s, a) => {
  729. iterations++;
  730. if (iterations == 0) {
  731. Assert.True (tf.HasFocus);
  732. Assert.Null (Application.MouseGrabView);
  733. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  734. X = 5,
  735. Y = 5,
  736. Flags = MouseFlags.ReportMousePosition
  737. }));
  738. Assert.Equal (sv, Application.MouseGrabView);
  739. MessageBox.Query ("Title", "Test", "Ok");
  740. Assert.Null (Application.MouseGrabView);
  741. } else if (iterations == 1) {
  742. // Application.MouseGrabView is null because
  743. // another toplevel (Dialog) was opened
  744. Assert.Null (Application.MouseGrabView);
  745. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  746. X = 5,
  747. Y = 5,
  748. Flags = MouseFlags.ReportMousePosition
  749. }));
  750. Assert.Null (Application.MouseGrabView);
  751. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  752. X = 40,
  753. Y = 12,
  754. Flags = MouseFlags.ReportMousePosition
  755. }));
  756. Assert.Null (Application.MouseGrabView);
  757. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  758. X = 0,
  759. Y = 0,
  760. Flags = MouseFlags.Button1Pressed
  761. }));
  762. Assert.Null (Application.MouseGrabView);
  763. Application.RequestStop ();
  764. } else if (iterations == 2) {
  765. Assert.Null (Application.MouseGrabView);
  766. Application.RequestStop ();
  767. }
  768. };
  769. Application.Run ();
  770. }
  771. [Fact, AutoInitShutdown]
  772. public void MouseGrabView_GrabbedMouse_UnGrabbedMouse ()
  773. {
  774. View grabView = null;
  775. var count = 0;
  776. var view1 = new View ();
  777. var view2 = new View ();
  778. Application.GrabbedMouse += Application_GrabbedMouse;
  779. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  780. Application.GrabMouse (view1);
  781. Assert.Equal (0, count);
  782. Assert.Equal (grabView, view1);
  783. Assert.Equal (view1, Application.MouseGrabView);
  784. Application.UngrabMouse ();
  785. Assert.Equal (1, count);
  786. Assert.Equal (grabView, view1);
  787. Assert.Null (Application.MouseGrabView);
  788. Application.GrabbedMouse += Application_GrabbedMouse;
  789. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  790. Application.GrabMouse (view2);
  791. Assert.Equal (1, count);
  792. Assert.Equal (grabView, view2);
  793. Assert.Equal (view2, Application.MouseGrabView);
  794. Application.UngrabMouse ();
  795. Assert.Equal (2, count);
  796. Assert.Equal (grabView, view2);
  797. Assert.Null (Application.MouseGrabView);
  798. void Application_GrabbedMouse (object sender, ViewEventArgs e)
  799. {
  800. if (count == 0) {
  801. Assert.Equal (view1, e.View);
  802. grabView = view1;
  803. } else {
  804. Assert.Equal (view2, e.View);
  805. grabView = view2;
  806. }
  807. Application.GrabbedMouse -= Application_GrabbedMouse;
  808. }
  809. void Application_UnGrabbedMouse (object sender, ViewEventArgs e)
  810. {
  811. if (count == 0) {
  812. Assert.Equal (view1, e.View);
  813. Assert.Equal (grabView, e.View);
  814. } else {
  815. Assert.Equal (view2, e.View);
  816. Assert.Equal (grabView, e.View);
  817. }
  818. count++;
  819. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  820. }
  821. }
  822. #endregion
  823. }