ApplicationTests.cs 28 KB

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