ApplicationTests.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. // removed below as HeightAsBuffer now works without a driver loaded
  25. //Assert.Throws<ArgumentNullException> (() => Application.HeightAsBuffer == true);
  26. Assert.Null (Application.MainLoop);
  27. Assert.Null (Application.Iteration);
  28. Assert.Null (Application.RootMouseEvent);
  29. Assert.Null (Application.Resized);
  30. }
  31. void Post_Init_State ()
  32. {
  33. Assert.NotNull (Application.Driver);
  34. Assert.NotNull (Application.Top);
  35. Assert.NotNull (Application.Current);
  36. Assert.False (Application.HeightAsBuffer);
  37. Assert.NotNull (Application.MainLoop);
  38. Assert.Null (Application.Iteration);
  39. Assert.Null (Application.RootMouseEvent);
  40. Assert.Null (Application.Resized);
  41. }
  42. void Init ()
  43. {
  44. Application.Init (new FakeDriver ());
  45. Assert.NotNull (Application.Driver);
  46. Assert.NotNull (Application.MainLoop);
  47. Assert.NotNull (SynchronizationContext.Current);
  48. }
  49. void Shutdown ()
  50. {
  51. Application.Shutdown ();
  52. }
  53. [Fact]
  54. public void Init_Shutdown_Cleans_Up ()
  55. {
  56. // Verify initial state is per spec
  57. Pre_Init_State ();
  58. Application.Init (new FakeDriver ());
  59. // Verify post-Init state is correct
  60. Post_Init_State ();
  61. // MockDriver is always 80x25
  62. Assert.Equal (80, Application.Driver.Cols);
  63. Assert.Equal (25, Application.Driver.Rows);
  64. Application.Shutdown ();
  65. // Verify state is back to initial
  66. Pre_Init_State ();
  67. #if DEBUG_IDISPOSABLE
  68. // Validate there are no outstanding Responder-based instances
  69. // after a scenario was selected to run. This proves the main UI Catalog
  70. // 'app' closed cleanly.
  71. foreach (var inst in Responder.Instances) {
  72. Assert.True (inst.WasDisposed);
  73. }
  74. #endif
  75. }
  76. [Fact]
  77. public void Init_Shutdown_Toplevel_Not_Disposed ()
  78. {
  79. Application.Init (new FakeDriver ());
  80. Application.Shutdown ();
  81. #if DEBUG_IDISPOSABLE
  82. Assert.Single (Responder.Instances);
  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. Action<Application.RunState> NewRunStateFn = (rs) => {
  122. Assert.NotNull (rs);
  123. runstate = rs;
  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. Action<Application.RunState> NewRunStateFn = (rs) => {
  158. Assert.NotNull (rs);
  159. runstate = rs;
  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 += () => count++;
  334. top.Ready += () => count++;
  335. top.Unloaded += () => 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]
  367. [AutoInitShutdown]
  368. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  369. {
  370. var t1 = new Toplevel ();
  371. var t2 = new Toplevel ();
  372. var t3 = new Toplevel ();
  373. var d = new Dialog ();
  374. var t4 = new Toplevel ();
  375. // t1, t2, t3, d, t4
  376. var iterations = 5;
  377. t1.Ready += () => {
  378. Assert.Equal (t1, Application.Top);
  379. Application.Run (t2);
  380. };
  381. t2.Ready += () => {
  382. Assert.Equal (t2, Application.Top);
  383. Application.Run (t3);
  384. };
  385. t3.Ready += () => {
  386. Assert.Equal (t3, Application.Top);
  387. Application.Run (d);
  388. };
  389. d.Ready += () => {
  390. Assert.Equal (t3, Application.Top);
  391. Application.Run (t4);
  392. };
  393. t4.Ready += () => {
  394. Assert.Equal (t4, Application.Top);
  395. t4.RequestStop ();
  396. d.RequestStop ();
  397. t3.RequestStop ();
  398. t2.RequestStop ();
  399. };
  400. // Now this will close the MdiContainer when all MdiChildes was closed
  401. t2.Closed += (_) => {
  402. t1.RequestStop ();
  403. };
  404. Application.Iteration += () => {
  405. if (iterations == 5) {
  406. // The Current still is t4 because Current.Running is false.
  407. Assert.Equal (t4, Application.Current);
  408. Assert.False (Application.Current.Running);
  409. Assert.Equal (t4, Application.Top);
  410. } else if (iterations == 4) {
  411. // The Current is d and Current.Running is false.
  412. Assert.Equal (d, Application.Current);
  413. Assert.False (Application.Current.Running);
  414. Assert.Equal (t4, Application.Top);
  415. } else if (iterations == 3) {
  416. // The Current is t3 and Current.Running is false.
  417. Assert.Equal (t3, Application.Current);
  418. Assert.False (Application.Current.Running);
  419. Assert.Equal (t3, Application.Top);
  420. } else if (iterations == 2) {
  421. // The Current is t2 and Current.Running is false.
  422. Assert.Equal (t2, Application.Current);
  423. Assert.False (Application.Current.Running);
  424. Assert.Equal (t2, Application.Top);
  425. } else {
  426. // The Current is t1.
  427. Assert.Equal (t1, Application.Current);
  428. Assert.False (Application.Current.Running);
  429. Assert.Equal (t1, Application.Top);
  430. }
  431. iterations--;
  432. };
  433. Application.Run (t1);
  434. Assert.Equal (t1, Application.Top);
  435. }
  436. [Fact]
  437. [AutoInitShutdown]
  438. public void Internal_Properties_Correct ()
  439. {
  440. Assert.True (Application._initialized);
  441. Assert.NotNull (Application.Top);
  442. var rs = Application.Begin (Application.Top);
  443. Assert.Equal (Application.Top, rs.Toplevel);
  444. Assert.Null (Application.MouseGrabView); // public
  445. Assert.Null (Application.WantContinuousButtonPressedView); // public
  446. Assert.False (Application.DebugDrawBounds);
  447. Assert.False (Application.ShowChild (Application.Top));
  448. }
  449. #region KeyboardTests
  450. [Fact]
  451. public void KeyUp_Event ()
  452. {
  453. // Setup Mock driver
  454. Init ();
  455. // Setup some fake keypresses (This)
  456. var input = "Tests";
  457. // Put a control-q in at the end
  458. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  459. foreach (var c in input.Reverse ()) {
  460. if (char.IsLetter (c)) {
  461. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  462. } else {
  463. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  464. }
  465. }
  466. int stackSize = Console.MockKeyPresses.Count;
  467. int iterations = 0;
  468. Application.Iteration = () => {
  469. iterations++;
  470. // Stop if we run out of control...
  471. if (iterations > 10) {
  472. Application.RequestStop ();
  473. }
  474. };
  475. int keyUps = 0;
  476. var output = string.Empty;
  477. Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
  478. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  479. output += (char)args.KeyEvent.KeyValue;
  480. }
  481. keyUps++;
  482. };
  483. Application.Run (Application.Top);
  484. // Input string should match output
  485. Assert.Equal (input, output);
  486. // # of key up events should match stack size
  487. //Assert.Equal (stackSize, keyUps);
  488. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  489. // it must be literal (Linux only).
  490. Assert.Equal (6, keyUps);
  491. // # of key up events should match # of iterations
  492. Assert.Equal (stackSize, iterations);
  493. Application.Shutdown ();
  494. Assert.Null (Application.Current);
  495. Assert.Null (Application.Top);
  496. Assert.Null (Application.MainLoop);
  497. Assert.Null (Application.Driver);
  498. }
  499. [Fact]
  500. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  501. {
  502. Init ();
  503. var top = Application.Top;
  504. var w1 = new Window ();
  505. var v1 = new TextField ();
  506. var v2 = new TextView ();
  507. w1.Add (v1, v2);
  508. var w2 = new Window ();
  509. var v3 = new CheckBox ();
  510. var v4 = new Button ();
  511. w2.Add (v3, v4);
  512. top.Add (w1, w2);
  513. Application.Iteration += () => {
  514. Assert.True (v1.HasFocus);
  515. // Using default keys.
  516. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  517. new KeyModifiers () { Ctrl = true }));
  518. Assert.True (v2.HasFocus);
  519. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  520. new KeyModifiers () { Ctrl = true }));
  521. Assert.True (v3.HasFocus);
  522. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  523. new KeyModifiers () { Ctrl = true }));
  524. Assert.True (v4.HasFocus);
  525. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  526. new KeyModifiers () { Ctrl = true }));
  527. Assert.True (v1.HasFocus);
  528. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  529. new KeyModifiers () { Shift = true, Ctrl = true }));
  530. Assert.True (v4.HasFocus);
  531. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  532. new KeyModifiers () { Shift = true, Ctrl = true }));
  533. Assert.True (v3.HasFocus);
  534. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  535. new KeyModifiers () { Shift = true, Ctrl = true }));
  536. Assert.True (v2.HasFocus);
  537. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  538. new KeyModifiers () { Shift = true, Ctrl = true }));
  539. Assert.True (v1.HasFocus);
  540. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  541. new KeyModifiers () { Ctrl = true }));
  542. Assert.True (v2.HasFocus);
  543. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  544. new KeyModifiers () { Ctrl = true }));
  545. Assert.True (v3.HasFocus);
  546. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  547. new KeyModifiers () { Ctrl = true }));
  548. Assert.True (v4.HasFocus);
  549. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  550. new KeyModifiers () { Ctrl = true }));
  551. Assert.True (v1.HasFocus);
  552. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  553. new KeyModifiers () { Ctrl = true }));
  554. Assert.True (v4.HasFocus);
  555. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  556. new KeyModifiers () { Ctrl = true }));
  557. Assert.True (v3.HasFocus);
  558. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  559. new KeyModifiers () { Ctrl = true }));
  560. Assert.True (v2.HasFocus);
  561. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  562. new KeyModifiers () { Ctrl = true }));
  563. Assert.True (v1.HasFocus);
  564. // Using another's alternate keys.
  565. Application.AlternateForwardKey = Key.F7;
  566. Application.AlternateBackwardKey = Key.F6;
  567. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  568. Assert.True (v2.HasFocus);
  569. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  570. Assert.True (v3.HasFocus);
  571. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  572. Assert.True (v4.HasFocus);
  573. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  574. Assert.True (v1.HasFocus);
  575. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  576. Assert.True (v4.HasFocus);
  577. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  578. Assert.True (v3.HasFocus);
  579. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  580. Assert.True (v2.HasFocus);
  581. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  582. Assert.True (v1.HasFocus);
  583. Application.RequestStop ();
  584. };
  585. Application.Run (top);
  586. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  587. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  588. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  589. Application.QuitKey = Key.Q | Key.CtrlMask;
  590. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  591. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  592. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  593. // Shutdown must be called to safely clean up Application if Init has been called
  594. Application.Shutdown ();
  595. }
  596. [Fact]
  597. [AutoInitShutdown]
  598. public void QuitKey_Getter_Setter ()
  599. {
  600. var top = Application.Top;
  601. var isQuiting = false;
  602. top.Closing += (e) => {
  603. isQuiting = true;
  604. e.Cancel = true;
  605. };
  606. Application.Begin (top);
  607. top.Running = true;
  608. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  609. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  610. Assert.True (isQuiting);
  611. isQuiting = false;
  612. Application.QuitKey = Key.C | Key.CtrlMask;
  613. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  614. Assert.False (isQuiting);
  615. Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
  616. Assert.True (isQuiting);
  617. // Reset the QuitKey to avoid throws errors on another tests
  618. Application.QuitKey = Key.Q | Key.CtrlMask;
  619. }
  620. [Fact]
  621. [AutoInitShutdown]
  622. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard_And_Mouse ()
  623. {
  624. var top = Application.Top;
  625. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  626. var tf = new TextField () { Width = 10 };
  627. win.Add (tf);
  628. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  629. var tf2 = new TextField () { Width = 10 };
  630. win2.Add (tf2);
  631. top.Add (win, win2);
  632. Application.Begin (top);
  633. Assert.True (win.CanFocus);
  634. Assert.True (win.HasFocus);
  635. Assert.True (win2.CanFocus);
  636. Assert.False (win2.HasFocus);
  637. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  638. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  639. Assert.True (win.CanFocus);
  640. Assert.False (win.HasFocus);
  641. Assert.True (win2.CanFocus);
  642. Assert.True (win2.HasFocus);
  643. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  644. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  645. Assert.True (win.CanFocus);
  646. Assert.True (win.HasFocus);
  647. Assert.True (win2.CanFocus);
  648. Assert.False (win2.HasFocus);
  649. Assert.Equal ("win", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  650. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  651. Assert.True (win.CanFocus);
  652. Assert.False (win.HasFocus);
  653. Assert.True (win2.CanFocus);
  654. Assert.True (win2.HasFocus);
  655. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  656. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  657. Assert.Null (Toplevel.dragPosition);
  658. }
  659. [Fact]
  660. [AutoInitShutdown]
  661. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard_And_Mouse ()
  662. {
  663. var top = Application.Top;
  664. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  665. var tf = new TextField () { Width = 10 };
  666. win.Add (tf);
  667. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  668. var tf2 = new TextField () { Width = 10 };
  669. win2.Add (tf2);
  670. top.Add (win, win2);
  671. Application.Begin (top);
  672. Assert.True (win.CanFocus);
  673. Assert.True (win.HasFocus);
  674. Assert.True (win2.CanFocus);
  675. Assert.False (win2.HasFocus);
  676. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  677. win.CanFocus = false;
  678. Assert.False (win.CanFocus);
  679. Assert.False (win.HasFocus);
  680. Assert.True (win2.CanFocus);
  681. Assert.True (win2.HasFocus);
  682. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  683. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  684. Assert.True (win2.CanFocus);
  685. Assert.False (win.HasFocus);
  686. Assert.True (win2.CanFocus);
  687. Assert.True (win2.HasFocus);
  688. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  689. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  690. Assert.False (win.CanFocus);
  691. Assert.False (win.HasFocus);
  692. Assert.True (win2.CanFocus);
  693. Assert.True (win2.HasFocus);
  694. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  695. win.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  696. Assert.False (win.CanFocus);
  697. Assert.False (win.HasFocus);
  698. Assert.True (win2.CanFocus);
  699. Assert.True (win2.HasFocus);
  700. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  701. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  702. Assert.Null (Toplevel.dragPosition);
  703. }
  704. #endregion
  705. [Fact, AutoInitShutdown]
  706. public void GetSupportedCultures_Method ()
  707. {
  708. var cultures = Application.GetSupportedCultures ();
  709. Assert.Equal (cultures.Count, Application.SupportedCultures.Count);
  710. }
  711. #region mousegrabtests
  712. [Fact, AutoInitShutdown]
  713. public void MouseGrabView_WithNullMouseEventView ()
  714. {
  715. var tf = new TextField () { Width = 10 };
  716. var sv = new ScrollView () {
  717. Width = Dim.Fill (),
  718. Height = Dim.Fill (),
  719. ContentSize = new Size (100, 100)
  720. };
  721. sv.Add (tf);
  722. Application.Top.Add (sv);
  723. var iterations = -1;
  724. Application.Iteration = () => {
  725. iterations++;
  726. if (iterations == 0) {
  727. Assert.True (tf.HasFocus);
  728. Assert.Null (Application.MouseGrabView);
  729. ReflectionTools.InvokePrivate (
  730. typeof (Application),
  731. "ProcessMouseEvent",
  732. new MouseEvent () {
  733. X = 5,
  734. Y = 5,
  735. Flags = MouseFlags.ReportMousePosition
  736. });
  737. Assert.Equal (sv, Application.MouseGrabView);
  738. MessageBox.Query ("Title", "Test", "Ok");
  739. Assert.Null (Application.MouseGrabView);
  740. } else if (iterations == 1) {
  741. Assert.Equal (sv, Application.MouseGrabView);
  742. ReflectionTools.InvokePrivate (
  743. typeof (Application),
  744. "ProcessMouseEvent",
  745. new MouseEvent () {
  746. X = 5,
  747. Y = 5,
  748. Flags = MouseFlags.ReportMousePosition
  749. });
  750. Assert.Equal (sv, Application.MouseGrabView);
  751. ReflectionTools.InvokePrivate (
  752. typeof (Application),
  753. "ProcessMouseEvent",
  754. new MouseEvent () {
  755. X = 40,
  756. Y = 12,
  757. Flags = MouseFlags.ReportMousePosition
  758. });
  759. Assert.Null (Application.MouseGrabView);
  760. ReflectionTools.InvokePrivate (
  761. typeof (Application),
  762. "ProcessMouseEvent",
  763. new MouseEvent () {
  764. X = 0,
  765. Y = 0,
  766. Flags = MouseFlags.Button1Pressed
  767. });
  768. Assert.Null (Application.MouseGrabView);
  769. Application.RequestStop ();
  770. } else if (iterations == 2) {
  771. Assert.Null (Application.MouseGrabView);
  772. Application.RequestStop ();
  773. }
  774. };
  775. Application.Run ();
  776. }
  777. [Fact, AutoInitShutdown]
  778. public void MouseGrabView_GrabbedMouse_UnGrabbedMouse ()
  779. {
  780. View grabView = null;
  781. var count = 0;
  782. var view1 = new View ();
  783. var view2 = new View ();
  784. Application.GrabbedMouse += Application_GrabbedMouse;
  785. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  786. Application.GrabMouse (view1);
  787. Assert.Equal (0, count);
  788. Assert.Equal (grabView, view1);
  789. Assert.Equal (view1, Application.MouseGrabView);
  790. Application.UngrabMouse ();
  791. Assert.Equal (1, count);
  792. Assert.Equal (grabView, view1);
  793. Assert.Null (Application.MouseGrabView);
  794. Application.GrabbedMouse += Application_GrabbedMouse;
  795. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  796. Application.GrabMouse (view2);
  797. Assert.Equal (1, count);
  798. Assert.Equal (grabView, view2);
  799. Assert.Equal (view2, Application.MouseGrabView);
  800. Application.UngrabMouse ();
  801. Assert.Equal (2, count);
  802. Assert.Equal (grabView, view2);
  803. Assert.Null (Application.MouseGrabView);
  804. void Application_GrabbedMouse (View obj)
  805. {
  806. if (count == 0) {
  807. Assert.Equal (view1, obj);
  808. grabView = view1;
  809. } else {
  810. Assert.Equal (view2, obj);
  811. grabView = view2;
  812. }
  813. Application.GrabbedMouse -= Application_GrabbedMouse;
  814. }
  815. void Application_UnGrabbedMouse (View obj)
  816. {
  817. if (count == 0) {
  818. Assert.Equal (view1, obj);
  819. Assert.Equal (grabView, obj);
  820. } else {
  821. Assert.Equal (view2, obj);
  822. Assert.Equal (grabView, obj);
  823. }
  824. count++;
  825. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  826. }
  827. }
  828. #endregion
  829. }
  830. }