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