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