ApplicationTests.cs 28 KB

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