ApplicationTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 async void Shutdown_Allows_Async ()
  332. {
  333. bool isCompletedSuccessfully = false;
  334. async Task TaskWithAsyncContinuation ()
  335. {
  336. await Task.Yield ();
  337. await Task.Yield ();
  338. isCompletedSuccessfully = true;
  339. }
  340. Init ();
  341. Application.Shutdown ();
  342. Assert.False (isCompletedSuccessfully);
  343. await TaskWithAsyncContinuation ();
  344. Thread.Sleep (100);
  345. Assert.True (isCompletedSuccessfully);
  346. }
  347. [Fact]
  348. public void Shutdown_Resets_SyncContext ()
  349. {
  350. Init ();
  351. Application.Shutdown ();
  352. Assert.Null (SynchronizationContext.Current);
  353. }
  354. #endregion
  355. [Fact, AutoInitShutdown]
  356. public void Begin_Sets_Application_Top_To_Console_Size ()
  357. {
  358. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  359. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  360. Application.Begin (Application.Top);
  361. Assert.Equal (new Rect (0, 0, 80, 25), Application.Top.Frame);
  362. ((FakeDriver)Application.Driver).SetBufferSize (5, 5);
  363. Assert.Equal (new Rect (0, 0, 5, 5), Application.Top.Frame);
  364. }
  365. [Fact]
  366. [AutoInitShutdown]
  367. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  368. {
  369. var t1 = new Toplevel ();
  370. var t2 = new Toplevel ();
  371. var t3 = new Toplevel ();
  372. var d = new Dialog ();
  373. var t4 = new Toplevel ();
  374. // t1, t2, t3, d, t4
  375. var iterations = 5;
  376. t1.Ready += (s, e) => {
  377. Assert.Equal (t1, Application.Top);
  378. Application.Run (t2);
  379. };
  380. t2.Ready += (s, e) => {
  381. Assert.Equal (t2, Application.Top);
  382. Application.Run (t3);
  383. };
  384. t3.Ready += (s, e) => {
  385. Assert.Equal (t3, Application.Top);
  386. Application.Run (d);
  387. };
  388. d.Ready += (s, e) => {
  389. Assert.Equal (t3, Application.Top);
  390. Application.Run (t4);
  391. };
  392. t4.Ready += (s, e) => {
  393. Assert.Equal (t4, Application.Top);
  394. t4.RequestStop ();
  395. d.RequestStop ();
  396. t3.RequestStop ();
  397. t2.RequestStop ();
  398. };
  399. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  400. t2.Closed += (s, _) => {
  401. t1.RequestStop ();
  402. };
  403. Application.Iteration += (s, a) => {
  404. if (iterations == 5) {
  405. // The Current still is t4 because Current.Running is false.
  406. Assert.Equal (t4, Application.Current);
  407. Assert.False (Application.Current.Running);
  408. Assert.Equal (t4, Application.Top);
  409. } else if (iterations == 4) {
  410. // The Current is d and Current.Running is false.
  411. Assert.Equal (d, Application.Current);
  412. Assert.False (Application.Current.Running);
  413. Assert.Equal (t4, Application.Top);
  414. } else if (iterations == 3) {
  415. // The Current is t3 and Current.Running is false.
  416. Assert.Equal (t3, Application.Current);
  417. Assert.False (Application.Current.Running);
  418. Assert.Equal (t3, Application.Top);
  419. } else if (iterations == 2) {
  420. // The Current is t2 and Current.Running is false.
  421. Assert.Equal (t2, Application.Current);
  422. Assert.False (Application.Current.Running);
  423. Assert.Equal (t2, Application.Top);
  424. } else {
  425. // The Current is t1.
  426. Assert.Equal (t1, Application.Current);
  427. Assert.False (Application.Current.Running);
  428. Assert.Equal (t1, Application.Top);
  429. }
  430. iterations--;
  431. };
  432. Application.Run (t1);
  433. Assert.Equal (t1, Application.Top);
  434. }
  435. [Fact]
  436. [AutoInitShutdown]
  437. public void Internal_Properties_Correct ()
  438. {
  439. Assert.True (Application._initialized);
  440. Assert.NotNull (Application.Top);
  441. var rs = Application.Begin (Application.Top);
  442. Assert.Equal (Application.Top, rs.Toplevel);
  443. Assert.Null (Application.MouseGrabView); // public
  444. Assert.Null (Application.WantContinuousButtonPressedView); // public
  445. Assert.False (Application.MoveToOverlappedChild (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. FakeConsole.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. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  460. } else {
  461. FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  462. }
  463. }
  464. int stackSize = FakeConsole.MockKeyPresses.Count;
  465. int iterations = 0;
  466. Application.Iteration += (s, a) => {
  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 += (object sender, 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 += (s, a) => {
  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 += (s, 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 () { Title = "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 () { Title = "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 () { Title = "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 () { Title = "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. // Invoke Tests
  704. // TODO: Test with threading scenarios
  705. [Fact]
  706. public void Invoke_Adds_Idle ()
  707. {
  708. Application.Init (new FakeDriver ());
  709. var top = new Toplevel ();
  710. var rs = Application.Begin (top);
  711. bool firstIteration = false;
  712. var actionCalled = 0;
  713. Application.Invoke (() => { actionCalled++; });
  714. Application.RunIteration (ref rs, ref firstIteration);
  715. Assert.Equal (1, actionCalled);
  716. Application.Shutdown ();
  717. }
  718. #region mousegrabtests
  719. [Fact, AutoInitShutdown]
  720. public void MouseGrabView_WithNullMouseEventView ()
  721. {
  722. var tf = new TextField () { Width = 10 };
  723. var sv = new ScrollView () {
  724. Width = Dim.Fill (),
  725. Height = Dim.Fill (),
  726. ContentSize = new Size (100, 100)
  727. };
  728. sv.Add (tf);
  729. Application.Top.Add (sv);
  730. var iterations = -1;
  731. Application.Iteration += (s, a) => {
  732. iterations++;
  733. if (iterations == 0) {
  734. Assert.True (tf.HasFocus);
  735. Assert.Null (Application.MouseGrabView);
  736. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  737. X = 5,
  738. Y = 5,
  739. Flags = MouseFlags.ReportMousePosition
  740. }));
  741. Assert.Equal (sv, Application.MouseGrabView);
  742. MessageBox.Query ("Title", "Test", "Ok");
  743. Assert.Null (Application.MouseGrabView);
  744. } else if (iterations == 1) {
  745. // Application.MouseGrabView is null because
  746. // another toplevel (Dialog) was opened
  747. Assert.Null (Application.MouseGrabView);
  748. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  749. X = 5,
  750. Y = 5,
  751. Flags = MouseFlags.ReportMousePosition
  752. }));
  753. Assert.Null (Application.MouseGrabView);
  754. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  755. X = 40,
  756. Y = 12,
  757. Flags = MouseFlags.ReportMousePosition
  758. }));
  759. Assert.Null (Application.MouseGrabView);
  760. Application.OnMouseEvent (new MouseEventEventArgs (new MouseEvent () {
  761. X = 0,
  762. Y = 0,
  763. Flags = MouseFlags.Button1Pressed
  764. }));
  765. Assert.Null (Application.MouseGrabView);
  766. Application.RequestStop ();
  767. } else if (iterations == 2) {
  768. Assert.Null (Application.MouseGrabView);
  769. Application.RequestStop ();
  770. }
  771. };
  772. Application.Run ();
  773. }
  774. [Fact, AutoInitShutdown]
  775. public void MouseGrabView_GrabbedMouse_UnGrabbedMouse ()
  776. {
  777. View grabView = null;
  778. var count = 0;
  779. var view1 = new View ();
  780. var view2 = new View ();
  781. Application.GrabbedMouse += Application_GrabbedMouse;
  782. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  783. Application.GrabMouse (view1);
  784. Assert.Equal (0, count);
  785. Assert.Equal (grabView, view1);
  786. Assert.Equal (view1, Application.MouseGrabView);
  787. Application.UngrabMouse ();
  788. Assert.Equal (1, count);
  789. Assert.Equal (grabView, view1);
  790. Assert.Null (Application.MouseGrabView);
  791. Application.GrabbedMouse += Application_GrabbedMouse;
  792. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  793. Application.GrabMouse (view2);
  794. Assert.Equal (1, count);
  795. Assert.Equal (grabView, view2);
  796. Assert.Equal (view2, Application.MouseGrabView);
  797. Application.UngrabMouse ();
  798. Assert.Equal (2, count);
  799. Assert.Equal (grabView, view2);
  800. Assert.Null (Application.MouseGrabView);
  801. void Application_GrabbedMouse (object sender, ViewEventArgs e)
  802. {
  803. if (count == 0) {
  804. Assert.Equal (view1, e.View);
  805. grabView = view1;
  806. } else {
  807. Assert.Equal (view2, e.View);
  808. grabView = view2;
  809. }
  810. Application.GrabbedMouse -= Application_GrabbedMouse;
  811. }
  812. void Application_UnGrabbedMouse (object sender, ViewEventArgs e)
  813. {
  814. if (count == 0) {
  815. Assert.Equal (view1, e.View);
  816. Assert.Equal (grabView, e.View);
  817. } else {
  818. Assert.Equal (view2, e.View);
  819. Assert.Equal (grabView, e.View);
  820. }
  821. count++;
  822. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  823. }
  824. }
  825. #endregion
  826. }