ApplicationTests.cs 27 KB

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