ApplicationTests.cs 28 KB

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