ApplicationTests.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. // Alias Console to MockConsole so we don't accidentally use Console
  8. using Console = Terminal.Gui.FakeConsole;
  9. namespace Terminal.Gui.Core {
  10. public class ApplicationTests {
  11. public ApplicationTests ()
  12. {
  13. #if DEBUG_IDISPOSABLE
  14. Responder.Instances.Clear ();
  15. Application.RunState.Instances.Clear ();
  16. #endif
  17. }
  18. void Pre_Init_State ()
  19. {
  20. Assert.Null (Application.Driver);
  21. Assert.Null (Application.Top);
  22. Assert.Null (Application.Current);
  23. Assert.Throws<ArgumentNullException> (() => Application.HeightAsBuffer == true);
  24. Assert.Null (Application.MainLoop);
  25. Assert.Null (Application.Iteration);
  26. Assert.Null (Application.RootMouseEvent);
  27. Assert.Null (Application.Resized);
  28. }
  29. void Post_Init_State ()
  30. {
  31. Assert.NotNull (Application.Driver);
  32. Assert.NotNull (Application.Top);
  33. Assert.NotNull (Application.Current);
  34. Assert.False (Application.HeightAsBuffer);
  35. Assert.NotNull (Application.MainLoop);
  36. Assert.Null (Application.Iteration);
  37. Assert.Null (Application.RootMouseEvent);
  38. Assert.Null (Application.Resized);
  39. }
  40. void Init ()
  41. {
  42. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  43. Assert.NotNull (Application.Driver);
  44. Assert.NotNull (Application.MainLoop);
  45. Assert.NotNull (SynchronizationContext.Current);
  46. }
  47. void Shutdown ()
  48. {
  49. Application.Shutdown ();
  50. }
  51. [Fact]
  52. public void Init_Shutdown_Cleans_Up ()
  53. {
  54. // Verify initial state is per spec
  55. Pre_Init_State ();
  56. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  57. // Verify post-Init state is correct
  58. Post_Init_State ();
  59. // MockDriver is always 80x25
  60. Assert.Equal (80, Application.Driver.Cols);
  61. Assert.Equal (25, Application.Driver.Rows);
  62. Application.Shutdown ();
  63. // Verify state is back to initial
  64. Pre_Init_State ();
  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. }
  72. [Fact]
  73. public void Init_Shutdown_Toplevel_Not_Disposed ()
  74. {
  75. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  76. Application.Shutdown ();
  77. Assert.Single (Responder.Instances);
  78. Assert.True (Responder.Instances [0].WasDisposed);
  79. }
  80. [Fact]
  81. public void Init_Unbalanced_Throwss ()
  82. {
  83. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  84. Toplevel topLevel = null;
  85. Assert.Throws<InvalidOperationException> (() => Application.Init (() => topLevel = new TestToplevel (), new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))));
  86. Shutdown ();
  87. Assert.Null (Application.Top);
  88. Assert.Null (Application.MainLoop);
  89. Assert.Null (Application.Driver);
  90. // Now try the other way
  91. topLevel = null;
  92. Application.Init (() => topLevel = new TestToplevel (), new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  93. Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true))));
  94. Shutdown ();
  95. Assert.Null (Application.Top);
  96. Assert.Null (Application.MainLoop);
  97. Assert.Null (Application.Driver);
  98. }
  99. class TestToplevel : Toplevel {
  100. public TestToplevel ()
  101. {
  102. IsMdiContainer = false;
  103. }
  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. Application.RunState runstate = null;
  115. Action<Application.RunState> NewRunStateFn = (rs) => {
  116. Assert.NotNull (rs);
  117. runstate = rs;
  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.Init (() => topLevel = new TestToplevel (), new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  150. Application.RunState runstate = null;
  151. Action<Application.RunState> NewRunStateFn = (rs) => {
  152. Assert.NotNull (rs);
  153. runstate = rs;
  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 (), new FakeMainLoop (() => FakeConsole.ReadKey (true))));
  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> (errorHandler: null);
  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_NoInit_Throws ()
  227. {
  228. Application.Iteration = () => {
  229. Application.RequestStop ();
  230. };
  231. // Init has NOT been called and we're passing no driver to Run<TestToplevel>. This is an error.
  232. Assert.Throws<ArgumentException> (() => Application.Run<TestToplevel> (errorHandler: null, driver: null, mainLoopDriver: null));
  233. Shutdown ();
  234. Assert.Null (Application.Top);
  235. Assert.Null (Application.MainLoop);
  236. Assert.Null (Application.Driver);
  237. }
  238. [Fact]
  239. public void Run_T_NoInit_WithDriver_DoesNotThrow ()
  240. {
  241. Application.Iteration = () => {
  242. Application.RequestStop ();
  243. };
  244. // Init has NOT been called and we're passing a valid driver to Run<TestTopLevel>. This is ok.
  245. Application.Run<TestToplevel> (errorHandler: null, new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  246. Shutdown ();
  247. Assert.Null (Application.Top);
  248. Assert.Null (Application.MainLoop);
  249. Assert.Null (Application.Driver);
  250. }
  251. [Fact]
  252. public void Run_RequestStop_Stops ()
  253. {
  254. // Setup Mock driver
  255. Init ();
  256. var top = new Toplevel ();
  257. var rs = Application.Begin (top);
  258. Assert.NotNull (rs);
  259. Assert.Equal (top, Application.Current);
  260. Application.Iteration = () => {
  261. Application.RequestStop ();
  262. };
  263. Application.Run (top);
  264. Application.Shutdown ();
  265. Assert.Null (Application.Current);
  266. Assert.Null (Application.Top);
  267. Assert.Null (Application.MainLoop);
  268. Assert.Null (Application.Driver);
  269. }
  270. [Fact]
  271. public void Run_RunningFalse_Stops ()
  272. {
  273. // Setup Mock driver
  274. Init ();
  275. var top = new Toplevel ();
  276. var rs = Application.Begin (top);
  277. Assert.NotNull (rs);
  278. Assert.Equal (top, Application.Current);
  279. Application.Iteration = () => {
  280. top.Running = false;
  281. };
  282. Application.Run (top);
  283. Application.Shutdown ();
  284. Assert.Null (Application.Current);
  285. Assert.Null (Application.Top);
  286. Assert.Null (Application.MainLoop);
  287. Assert.Null (Application.Driver);
  288. }
  289. [Fact]
  290. public void Run_Loaded_Ready_Unlodaded_Events ()
  291. {
  292. Init ();
  293. var top = Application.Top;
  294. var count = 0;
  295. top.Loaded += () => count++;
  296. top.Ready += () => count++;
  297. top.Unloaded += () => count++;
  298. Application.Iteration = () => Application.RequestStop ();
  299. Application.Run ();
  300. Application.Shutdown ();
  301. Assert.Equal (3, count);
  302. }
  303. #endregion
  304. #region ShutdownTests
  305. [Fact]
  306. public void Shutdown_Allows_Async ()
  307. {
  308. static async Task TaskWithAsyncContinuation ()
  309. {
  310. await Task.Yield ();
  311. await Task.Yield ();
  312. }
  313. Init ();
  314. Application.Shutdown ();
  315. var task = TaskWithAsyncContinuation ();
  316. Thread.Sleep (20);
  317. Assert.True (task.IsCompletedSuccessfully);
  318. }
  319. [Fact]
  320. public void Shutdown_Resets_SyncContext ()
  321. {
  322. Init ();
  323. Application.Shutdown ();
  324. Assert.Null (SynchronizationContext.Current);
  325. }
  326. #endregion
  327. [Fact]
  328. [AutoInitShutdown]
  329. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  330. {
  331. var t1 = new Toplevel ();
  332. var t2 = new Toplevel ();
  333. var t3 = new Toplevel ();
  334. var d = new Dialog ();
  335. var t4 = new Toplevel ();
  336. // t1, t2, t3, d, t4
  337. var iterations = 5;
  338. t1.Ready += () => {
  339. Assert.Equal (t1, Application.Top);
  340. Application.Run (t2);
  341. };
  342. t2.Ready += () => {
  343. Assert.Equal (t2, Application.Top);
  344. Application.Run (t3);
  345. };
  346. t3.Ready += () => {
  347. Assert.Equal (t3, Application.Top);
  348. Application.Run (d);
  349. };
  350. d.Ready += () => {
  351. Assert.Equal (t3, Application.Top);
  352. Application.Run (t4);
  353. };
  354. t4.Ready += () => {
  355. Assert.Equal (t4, Application.Top);
  356. t4.RequestStop ();
  357. d.RequestStop ();
  358. t3.RequestStop ();
  359. t2.RequestStop ();
  360. };
  361. // Now this will close the MdiContainer when all MdiChildes was closed
  362. t2.Closed += (_) => {
  363. t1.RequestStop ();
  364. };
  365. Application.Iteration += () => {
  366. if (iterations == 5) {
  367. // The Current still is t4 because Current.Running is false.
  368. Assert.Equal (t4, Application.Current);
  369. Assert.False (Application.Current.Running);
  370. Assert.Equal (t4, Application.Top);
  371. } else if (iterations == 4) {
  372. // The Current is d and Current.Running is false.
  373. Assert.Equal (d, Application.Current);
  374. Assert.False (Application.Current.Running);
  375. Assert.Equal (t4, Application.Top);
  376. } else if (iterations == 3) {
  377. // The Current is t3 and Current.Running is false.
  378. Assert.Equal (t3, Application.Current);
  379. Assert.False (Application.Current.Running);
  380. Assert.Equal (t3, Application.Top);
  381. } else if (iterations == 2) {
  382. // The Current is t2 and Current.Running is false.
  383. Assert.Equal (t2, Application.Current);
  384. Assert.False (Application.Current.Running);
  385. Assert.Equal (t2, Application.Top);
  386. } else {
  387. // The Current is t1.
  388. Assert.Equal (t1, Application.Current);
  389. Assert.False (Application.Current.Running);
  390. Assert.Equal (t1, Application.Top);
  391. }
  392. iterations--;
  393. };
  394. Application.Run (t1);
  395. Assert.Equal (t1, Application.Top);
  396. }
  397. [Fact]
  398. [AutoInitShutdown]
  399. public void Internal_Properties_Correct ()
  400. {
  401. Assert.True (Application._initialized);
  402. Assert.NotNull (Application.Top);
  403. var rs = Application.Begin (Application.Top);
  404. Assert.Equal (Application.Top, rs.Toplevel);
  405. Assert.Null (Application.MouseGrabView); // public
  406. Assert.Null (Application.WantContinuousButtonPressedView); // public
  407. Assert.False (Application.DebugDrawBounds);
  408. Assert.False (Application.ShowChild (Application.Top));
  409. }
  410. #region KeyboardTests
  411. [Fact]
  412. public void KeyUp_Event ()
  413. {
  414. // Setup Mock driver
  415. Init ();
  416. // Setup some fake keypresses (This)
  417. var input = "Tests";
  418. // Put a control-q in at the end
  419. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  420. foreach (var c in input.Reverse ()) {
  421. if (char.IsLetter (c)) {
  422. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  423. } else {
  424. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  425. }
  426. }
  427. int stackSize = Console.MockKeyPresses.Count;
  428. int iterations = 0;
  429. Application.Iteration = () => {
  430. iterations++;
  431. // Stop if we run out of control...
  432. if (iterations > 10) {
  433. Application.RequestStop ();
  434. }
  435. };
  436. int keyUps = 0;
  437. var output = string.Empty;
  438. Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
  439. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  440. output += (char)args.KeyEvent.KeyValue;
  441. }
  442. keyUps++;
  443. };
  444. Application.Run (Application.Top);
  445. // Input string should match output
  446. Assert.Equal (input, output);
  447. // # of key up events should match stack size
  448. //Assert.Equal (stackSize, keyUps);
  449. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  450. // it must be literal (Linux only).
  451. Assert.Equal (6, keyUps);
  452. // # of key up events should match # of iterations
  453. Assert.Equal (stackSize, iterations);
  454. Application.Shutdown ();
  455. Assert.Null (Application.Current);
  456. Assert.Null (Application.Top);
  457. Assert.Null (Application.MainLoop);
  458. Assert.Null (Application.Driver);
  459. }
  460. [Fact]
  461. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  462. {
  463. Init ();
  464. var top = Application.Top;
  465. var w1 = new Window ();
  466. var v1 = new TextField ();
  467. var v2 = new TextView ();
  468. w1.Add (v1, v2);
  469. var w2 = new Window ();
  470. var v3 = new CheckBox ();
  471. var v4 = new Button ();
  472. w2.Add (v3, v4);
  473. top.Add (w1, w2);
  474. Application.Iteration += () => {
  475. Assert.True (v1.HasFocus);
  476. // Using default keys.
  477. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  478. new KeyModifiers () { Ctrl = true }));
  479. Assert.True (v2.HasFocus);
  480. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  481. new KeyModifiers () { Ctrl = true }));
  482. Assert.True (v3.HasFocus);
  483. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  484. new KeyModifiers () { Ctrl = true }));
  485. Assert.True (v4.HasFocus);
  486. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  487. new KeyModifiers () { Ctrl = true }));
  488. Assert.True (v1.HasFocus);
  489. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  490. new KeyModifiers () { Shift = true, Ctrl = true }));
  491. Assert.True (v4.HasFocus);
  492. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  493. new KeyModifiers () { Shift = true, Ctrl = true }));
  494. Assert.True (v3.HasFocus);
  495. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  496. new KeyModifiers () { Shift = true, Ctrl = true }));
  497. Assert.True (v2.HasFocus);
  498. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  499. new KeyModifiers () { Shift = true, Ctrl = true }));
  500. Assert.True (v1.HasFocus);
  501. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  502. new KeyModifiers () { Ctrl = true }));
  503. Assert.True (v2.HasFocus);
  504. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  505. new KeyModifiers () { Ctrl = true }));
  506. Assert.True (v3.HasFocus);
  507. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  508. new KeyModifiers () { Ctrl = true }));
  509. Assert.True (v4.HasFocus);
  510. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  511. new KeyModifiers () { Ctrl = true }));
  512. Assert.True (v1.HasFocus);
  513. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  514. new KeyModifiers () { Ctrl = true }));
  515. Assert.True (v4.HasFocus);
  516. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  517. new KeyModifiers () { Ctrl = true }));
  518. Assert.True (v3.HasFocus);
  519. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  520. new KeyModifiers () { Ctrl = true }));
  521. Assert.True (v2.HasFocus);
  522. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  523. new KeyModifiers () { Ctrl = true }));
  524. Assert.True (v1.HasFocus);
  525. // Using another's alternate keys.
  526. Application.AlternateForwardKey = Key.F7;
  527. Application.AlternateBackwardKey = Key.F6;
  528. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  529. Assert.True (v2.HasFocus);
  530. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  531. Assert.True (v3.HasFocus);
  532. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  533. Assert.True (v4.HasFocus);
  534. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  535. Assert.True (v1.HasFocus);
  536. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  537. Assert.True (v4.HasFocus);
  538. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  539. Assert.True (v3.HasFocus);
  540. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  541. Assert.True (v2.HasFocus);
  542. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  543. Assert.True (v1.HasFocus);
  544. Application.RequestStop ();
  545. };
  546. Application.Run (top);
  547. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  548. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  549. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  550. Application.QuitKey = Key.Q | Key.CtrlMask;
  551. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  552. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  553. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  554. // Shutdown must be called to safely clean up Application if Init has been called
  555. Application.Shutdown ();
  556. }
  557. [Fact]
  558. [AutoInitShutdown]
  559. public void QuitKey_Getter_Setter ()
  560. {
  561. var top = Application.Top;
  562. var isQuiting = false;
  563. top.Closing += (e) => {
  564. isQuiting = true;
  565. e.Cancel = true;
  566. };
  567. Application.Begin (top);
  568. top.Running = true;
  569. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  570. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  571. Assert.True (isQuiting);
  572. isQuiting = false;
  573. Application.QuitKey = Key.C | Key.CtrlMask;
  574. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  575. Assert.False (isQuiting);
  576. Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
  577. Assert.True (isQuiting);
  578. // Reset the QuitKey to avoid throws errors on another tests
  579. Application.QuitKey = Key.Q | Key.CtrlMask;
  580. }
  581. [Fact]
  582. [AutoInitShutdown]
  583. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard_And_Mouse ()
  584. {
  585. var top = Application.Top;
  586. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  587. var tf = new TextField () { Width = 10 };
  588. win.Add (tf);
  589. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  590. var tf2 = new TextField () { Width = 10 };
  591. win2.Add (tf2);
  592. top.Add (win, win2);
  593. Application.Begin (top);
  594. Assert.True (win.CanFocus);
  595. Assert.True (win.HasFocus);
  596. Assert.True (win2.CanFocus);
  597. Assert.False (win2.HasFocus);
  598. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  599. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  600. Assert.True (win.CanFocus);
  601. Assert.False (win.HasFocus);
  602. Assert.True (win2.CanFocus);
  603. Assert.True (win2.HasFocus);
  604. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  605. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  606. Assert.True (win.CanFocus);
  607. Assert.True (win.HasFocus);
  608. Assert.True (win2.CanFocus);
  609. Assert.False (win2.HasFocus);
  610. Assert.Equal ("win", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  611. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  612. Assert.True (win.CanFocus);
  613. Assert.False (win.HasFocus);
  614. Assert.True (win2.CanFocus);
  615. Assert.True (win2.HasFocus);
  616. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  617. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  618. Assert.Null (Toplevel.dragPosition);
  619. }
  620. [Fact]
  621. [AutoInitShutdown]
  622. public void EnsuresTopOnFront_CanFocus_False_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. win.CanFocus = false;
  639. Assert.False (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 (win2.CanFocus);
  646. Assert.False (win.HasFocus);
  647. Assert.True (win2.CanFocus);
  648. Assert.True (win2.HasFocus);
  649. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  650. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  651. Assert.False (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. win.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  657. Assert.False (win.CanFocus);
  658. Assert.False (win.HasFocus);
  659. Assert.True (win2.CanFocus);
  660. Assert.True (win2.HasFocus);
  661. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  662. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  663. Assert.Null (Toplevel.dragPosition);
  664. }
  665. #endregion
  666. [Fact, AutoInitShutdown]
  667. public void GetSupportedCultures_Method ()
  668. {
  669. var cultures = Application.GetSupportedCultures ();
  670. Assert.Equal (cultures.Count, Application.SupportedCultures.Count);
  671. }
  672. #region mousegrabtests
  673. [Fact, AutoInitShutdown]
  674. public void MouseGrabView_WithNullMouseEventView ()
  675. {
  676. var tf = new TextField () { Width = 10 };
  677. var sv = new ScrollView () {
  678. Width = Dim.Fill (),
  679. Height = Dim.Fill (),
  680. ContentSize = new Size (100, 100)
  681. };
  682. sv.Add (tf);
  683. Application.Top.Add (sv);
  684. var iterations = -1;
  685. Application.Iteration = () => {
  686. iterations++;
  687. if (iterations == 0) {
  688. Assert.True (tf.HasFocus);
  689. Assert.Null (Application.MouseGrabView);
  690. ReflectionTools.InvokePrivate (
  691. typeof (Application),
  692. "ProcessMouseEvent",
  693. new MouseEvent () {
  694. X = 5,
  695. Y = 5,
  696. Flags = MouseFlags.ReportMousePosition
  697. });
  698. Assert.Equal (sv, Application.MouseGrabView);
  699. MessageBox.Query ("Title", "Test", "Ok");
  700. Assert.Null (Application.MouseGrabView);
  701. } else if (iterations == 1) {
  702. Assert.Equal (sv, Application.MouseGrabView);
  703. ReflectionTools.InvokePrivate (
  704. typeof (Application),
  705. "ProcessMouseEvent",
  706. new MouseEvent () {
  707. X = 5,
  708. Y = 5,
  709. Flags = MouseFlags.ReportMousePosition
  710. });
  711. Assert.Null (Application.MouseGrabView);
  712. ReflectionTools.InvokePrivate (
  713. typeof (Application),
  714. "ProcessMouseEvent",
  715. new MouseEvent () {
  716. X = 40,
  717. Y = 12,
  718. Flags = MouseFlags.ReportMousePosition
  719. });
  720. Assert.Null (Application.MouseGrabView);
  721. ReflectionTools.InvokePrivate (
  722. typeof (Application),
  723. "ProcessMouseEvent",
  724. new MouseEvent () {
  725. X = 0,
  726. Y = 0,
  727. Flags = MouseFlags.Button1Pressed
  728. });
  729. Assert.Null (Application.MouseGrabView);
  730. Application.RequestStop ();
  731. } else if (iterations == 2) {
  732. Assert.Null (Application.MouseGrabView);
  733. Application.RequestStop ();
  734. }
  735. };
  736. Application.Run ();
  737. }
  738. [Fact, AutoInitShutdown]
  739. public void MouseGrabView_GrabbedMouse_UnGrabbedMouse ()
  740. {
  741. View grabView = null;
  742. var count = 0;
  743. var view1 = new View ();
  744. var view2 = new View ();
  745. Application.GrabbedMouse += Application_GrabbedMouse;
  746. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  747. Application.GrabMouse (view1);
  748. Assert.Equal (0, count);
  749. Assert.Equal (grabView, view1);
  750. Assert.Equal (view1, Application.MouseGrabView);
  751. Application.UngrabMouse ();
  752. Assert.Equal (1, count);
  753. Assert.Equal (grabView, view1);
  754. Assert.Null (Application.MouseGrabView);
  755. Application.GrabbedMouse += Application_GrabbedMouse;
  756. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  757. Application.GrabMouse (view2);
  758. Assert.Equal (1, count);
  759. Assert.Equal (grabView, view2);
  760. Assert.Equal (view2, Application.MouseGrabView);
  761. Application.UngrabMouse ();
  762. Assert.Equal (2, count);
  763. Assert.Equal (grabView, view2);
  764. Assert.Null (Application.MouseGrabView);
  765. void Application_GrabbedMouse (View obj)
  766. {
  767. if (count == 0) {
  768. Assert.Equal (view1, obj);
  769. grabView = view1;
  770. } else {
  771. Assert.Equal (view2, obj);
  772. grabView = view2;
  773. }
  774. Application.GrabbedMouse -= Application_GrabbedMouse;
  775. }
  776. void Application_UnGrabbedMouse (View obj)
  777. {
  778. if (count == 0) {
  779. Assert.Equal (view1, obj);
  780. Assert.Equal (grabView, obj);
  781. } else {
  782. Assert.Equal (view2, obj);
  783. Assert.Equal (grabView, obj);
  784. }
  785. count++;
  786. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  787. }
  788. }
  789. #endregion
  790. }
  791. }