ApplicationTests.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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.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.NotNull (Application.MainLoop);
  35. Assert.Null (Application.Iteration);
  36. Assert.Null (Application.RootMouseEvent);
  37. Assert.Null (Application.Resized);
  38. }
  39. void Init ()
  40. {
  41. Application.Init (new FakeDriver ());
  42. Assert.NotNull (Application.Driver);
  43. Assert.NotNull (Application.MainLoop);
  44. Assert.NotNull (SynchronizationContext.Current);
  45. }
  46. void Shutdown ()
  47. {
  48. Application.Shutdown ();
  49. }
  50. [Fact]
  51. public void Init_Shutdown_Cleans_Up ()
  52. {
  53. // Verify initial state is per spec
  54. Pre_Init_State ();
  55. Application.Init (new FakeDriver ());
  56. // Verify post-Init state is correct
  57. Post_Init_State ();
  58. // MockDriver is always 80x25
  59. Assert.Equal (80, Application.Driver.Cols);
  60. Assert.Equal (25, Application.Driver.Rows);
  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_Shutdown_Toplevel_Not_Disposed ()
  75. {
  76. Application.Init (new FakeDriver ());
  77. Application.Shutdown ();
  78. #if DEBUG_IDISPOSABLE
  79. Assert.Empty (Responder.Instances);
  80. #endif
  81. }
  82. [Fact]
  83. public void Init_Unbalanced_Throwss ()
  84. {
  85. Application.Init (new FakeDriver ());
  86. Toplevel topLevel = null;
  87. Assert.Throws<InvalidOperationException> (() => Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ()));
  88. Shutdown ();
  89. Assert.Null (Application.Top);
  90. Assert.Null (Application.MainLoop);
  91. Assert.Null (Application.Driver);
  92. // Now try the other way
  93. topLevel = null;
  94. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  95. Assert.Throws<InvalidOperationException> (() => Application.Init (new FakeDriver ()));
  96. Shutdown ();
  97. Assert.Null (Application.Top);
  98. Assert.Null (Application.MainLoop);
  99. Assert.Null (Application.Driver);
  100. }
  101. class TestToplevel : Toplevel {
  102. public TestToplevel ()
  103. {
  104. IsMdiContainer = false;
  105. }
  106. }
  107. [Fact]
  108. public void Init_Begin_End_Cleans_Up ()
  109. {
  110. Init ();
  111. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  112. // if we don't stop
  113. Application.Iteration = () => {
  114. Application.RequestStop ();
  115. };
  116. Application.RunState runstate = null;
  117. Action<Application.RunState> NewRunStateFn = (rs) => {
  118. Assert.NotNull (rs);
  119. runstate = rs;
  120. };
  121. Application.NotifyNewRunState += NewRunStateFn;
  122. Toplevel topLevel = new Toplevel ();
  123. var rs = Application.Begin (topLevel);
  124. Assert.NotNull (rs);
  125. Assert.NotNull (runstate);
  126. Assert.Equal (rs, runstate);
  127. Assert.Equal (topLevel, Application.Top);
  128. Assert.Equal (topLevel, Application.Current);
  129. Application.NotifyNewRunState -= NewRunStateFn;
  130. Application.End (runstate);
  131. Assert.Null (Application.Current);
  132. Assert.Null (Application.Top);
  133. Assert.NotNull (Application.MainLoop);
  134. Assert.NotNull (Application.Driver);
  135. Shutdown ();
  136. Assert.Null (Application.MainLoop);
  137. Assert.Null (Application.Driver);
  138. }
  139. [Fact]
  140. public void InitWithTopLevelFactory_Begin_End_Cleans_Up ()
  141. {
  142. // Begin will cause Run() to be called, which will call Begin(). Thus will block the tests
  143. // if we don't stop
  144. Application.Iteration = () => {
  145. Application.RequestStop ();
  146. };
  147. // NOTE: Run<T>, when called after Init has been called behaves differently than
  148. // when called if Init has not been called.
  149. Toplevel topLevel = null;
  150. Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
  151. Application.RunState runstate = null;
  152. Action<Application.RunState> NewRunStateFn = (rs) => {
  153. Assert.NotNull (rs);
  154. runstate = rs;
  155. };
  156. Application.NotifyNewRunState += NewRunStateFn;
  157. var rs = Application.Begin (topLevel);
  158. Assert.NotNull (rs);
  159. Assert.NotNull (runstate);
  160. Assert.Equal (rs, runstate);
  161. Assert.Equal (topLevel, Application.Top);
  162. Assert.Equal (topLevel, Application.Current);
  163. Application.NotifyNewRunState -= NewRunStateFn;
  164. Application.End (runstate);
  165. Assert.Null (Application.Current);
  166. Assert.Null (Application.Top);
  167. Assert.NotNull (Application.MainLoop);
  168. Assert.NotNull (Application.Driver);
  169. Shutdown ();
  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 += () => count++;
  328. top.Ready += () => count++;
  329. top.Unloaded += () => 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]
  361. [AutoInitShutdown]
  362. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  363. {
  364. var t1 = new Toplevel ();
  365. var t2 = new Toplevel ();
  366. var t3 = new Toplevel ();
  367. var d = new Dialog ();
  368. var t4 = new Toplevel ();
  369. // t1, t2, t3, d, t4
  370. var iterations = 5;
  371. t1.Ready += () => {
  372. Assert.Equal (t1, Application.Top);
  373. Application.Run (t2);
  374. };
  375. t2.Ready += () => {
  376. Assert.Equal (t2, Application.Top);
  377. Application.Run (t3);
  378. };
  379. t3.Ready += () => {
  380. Assert.Equal (t3, Application.Top);
  381. Application.Run (d);
  382. };
  383. d.Ready += () => {
  384. Assert.Equal (t3, Application.Top);
  385. Application.Run (t4);
  386. };
  387. t4.Ready += () => {
  388. Assert.Equal (t4, Application.Top);
  389. t4.RequestStop ();
  390. d.RequestStop ();
  391. t3.RequestStop ();
  392. t2.RequestStop ();
  393. };
  394. // Now this will close the MdiContainer when all MdiChildes was closed
  395. t2.Closed += (_) => {
  396. t1.RequestStop ();
  397. };
  398. Application.Iteration += () => {
  399. if (iterations == 5) {
  400. // The Current still is t4 because Current.Running is false.
  401. Assert.Equal (t4, Application.Current);
  402. Assert.False (Application.Current.Running);
  403. Assert.Equal (t4, Application.Top);
  404. } else if (iterations == 4) {
  405. // The Current is d and Current.Running is false.
  406. Assert.Equal (d, Application.Current);
  407. Assert.False (Application.Current.Running);
  408. Assert.Equal (t4, Application.Top);
  409. } else if (iterations == 3) {
  410. // The Current is t3 and Current.Running is false.
  411. Assert.Equal (t3, Application.Current);
  412. Assert.False (Application.Current.Running);
  413. Assert.Equal (t3, Application.Top);
  414. } else if (iterations == 2) {
  415. // The Current is t2 and Current.Running is false.
  416. Assert.Equal (t2, Application.Current);
  417. Assert.False (Application.Current.Running);
  418. Assert.Equal (t2, Application.Top);
  419. } else {
  420. // The Current is t1.
  421. Assert.Equal (t1, Application.Current);
  422. Assert.False (Application.Current.Running);
  423. Assert.Equal (t1, Application.Top);
  424. }
  425. iterations--;
  426. };
  427. Application.Run (t1);
  428. Assert.Null (Application.Top);
  429. #if DEBUG_IDISPOSABLE
  430. Assert.True (t1.WasDisposed);
  431. #endif
  432. }
  433. [Fact]
  434. [AutoInitShutdown]
  435. public void Internal_Properties_Correct ()
  436. {
  437. Assert.True (Application._initialized);
  438. Assert.NotNull (Application.Top);
  439. var rs = Application.Begin (Application.Top);
  440. Assert.Equal (Application.Top, rs.Toplevel);
  441. Assert.Null (Application.MouseGrabView); // public
  442. Assert.Null (Application.WantContinuousButtonPressedView); // public
  443. Assert.False (Application.DebugDrawBounds);
  444. Assert.False (Application.ShowChild (Application.Top));
  445. }
  446. #region KeyboardTests
  447. [Fact]
  448. public void KeyUp_Event ()
  449. {
  450. // Setup Mock driver
  451. Init ();
  452. // Setup some fake keypresses (This)
  453. var input = "Tests";
  454. // Put a control-q in at the end
  455. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  456. foreach (var c in input.Reverse ()) {
  457. if (char.IsLetter (c)) {
  458. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  459. } else {
  460. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  461. }
  462. }
  463. int stackSize = Console.MockKeyPresses.Count;
  464. int iterations = 0;
  465. Application.Iteration = () => {
  466. iterations++;
  467. // Stop if we run out of control...
  468. if (iterations > 10) {
  469. Application.RequestStop ();
  470. }
  471. };
  472. int keyUps = 0;
  473. var output = string.Empty;
  474. Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
  475. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  476. output += (char)args.KeyEvent.KeyValue;
  477. }
  478. keyUps++;
  479. };
  480. Application.Run (Application.Top);
  481. // Input string should match output
  482. Assert.Equal (input, output);
  483. // # of key up events should match stack size
  484. //Assert.Equal (stackSize, keyUps);
  485. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  486. // it must be literal (Linux only).
  487. Assert.Equal (6, keyUps);
  488. // # of key up events should match # of iterations
  489. Assert.Equal (stackSize, iterations);
  490. Application.Shutdown ();
  491. Assert.Null (Application.Current);
  492. Assert.Null (Application.Top);
  493. Assert.Null (Application.MainLoop);
  494. Assert.Null (Application.Driver);
  495. }
  496. [Fact]
  497. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  498. {
  499. Init ();
  500. var top = Application.Top;
  501. var w1 = new Window ();
  502. var v1 = new TextField ();
  503. var v2 = new TextView ();
  504. w1.Add (v1, v2);
  505. var w2 = new Window ();
  506. var v3 = new CheckBox ();
  507. var v4 = new Button ();
  508. w2.Add (v3, v4);
  509. top.Add (w1, w2);
  510. Application.Iteration += () => {
  511. Assert.True (v1.HasFocus);
  512. // Using default keys.
  513. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  514. new KeyModifiers () { Ctrl = true }));
  515. Assert.True (v2.HasFocus);
  516. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  517. new KeyModifiers () { Ctrl = true }));
  518. Assert.True (v3.HasFocus);
  519. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  520. new KeyModifiers () { Ctrl = true }));
  521. Assert.True (v4.HasFocus);
  522. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  523. new KeyModifiers () { Ctrl = true }));
  524. Assert.True (v1.HasFocus);
  525. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  526. new KeyModifiers () { Shift = true, Ctrl = true }));
  527. Assert.True (v4.HasFocus);
  528. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  529. new KeyModifiers () { Shift = true, Ctrl = true }));
  530. Assert.True (v3.HasFocus);
  531. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  532. new KeyModifiers () { Shift = true, Ctrl = true }));
  533. Assert.True (v2.HasFocus);
  534. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  535. new KeyModifiers () { Shift = true, Ctrl = true }));
  536. Assert.True (v1.HasFocus);
  537. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  538. new KeyModifiers () { Ctrl = true }));
  539. Assert.True (v2.HasFocus);
  540. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  541. new KeyModifiers () { Ctrl = true }));
  542. Assert.True (v3.HasFocus);
  543. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  544. new KeyModifiers () { Ctrl = true }));
  545. Assert.True (v4.HasFocus);
  546. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  547. new KeyModifiers () { Ctrl = true }));
  548. Assert.True (v1.HasFocus);
  549. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  550. new KeyModifiers () { Ctrl = true }));
  551. Assert.True (v4.HasFocus);
  552. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  553. new KeyModifiers () { Ctrl = true }));
  554. Assert.True (v3.HasFocus);
  555. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  556. new KeyModifiers () { Ctrl = true }));
  557. Assert.True (v2.HasFocus);
  558. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  559. new KeyModifiers () { Ctrl = true }));
  560. Assert.True (v1.HasFocus);
  561. // Using another's alternate keys.
  562. Application.AlternateForwardKey = Key.F7;
  563. Application.AlternateBackwardKey = Key.F6;
  564. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  565. Assert.True (v2.HasFocus);
  566. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  567. Assert.True (v3.HasFocus);
  568. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  569. Assert.True (v4.HasFocus);
  570. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  571. Assert.True (v1.HasFocus);
  572. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  573. Assert.True (v4.HasFocus);
  574. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  575. Assert.True (v3.HasFocus);
  576. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  577. Assert.True (v2.HasFocus);
  578. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  579. Assert.True (v1.HasFocus);
  580. Application.RequestStop ();
  581. };
  582. Application.Run (top);
  583. // Replacing the defaults keys to avoid errors on others unit tests that are using it.
  584. Application.AlternateForwardKey = Key.PageDown | Key.CtrlMask;
  585. Application.AlternateBackwardKey = Key.PageUp | Key.CtrlMask;
  586. Application.QuitKey = Key.Q | Key.CtrlMask;
  587. Assert.Equal (Key.PageDown | Key.CtrlMask, Application.AlternateForwardKey);
  588. Assert.Equal (Key.PageUp | Key.CtrlMask, Application.AlternateBackwardKey);
  589. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  590. // Shutdown must be called to safely clean up Application if Init has been called
  591. Application.Shutdown ();
  592. }
  593. [Fact]
  594. [AutoInitShutdown]
  595. public void QuitKey_Getter_Setter ()
  596. {
  597. var top = Application.Top;
  598. var isQuiting = false;
  599. top.Closing += (e) => {
  600. isQuiting = true;
  601. e.Cancel = true;
  602. };
  603. Application.Begin (top);
  604. top.Running = true;
  605. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  606. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  607. Assert.True (isQuiting);
  608. isQuiting = false;
  609. Application.QuitKey = Key.C | Key.CtrlMask;
  610. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  611. Assert.False (isQuiting);
  612. Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
  613. Assert.True (isQuiting);
  614. // Reset the QuitKey to avoid throws errors on another tests
  615. Application.QuitKey = Key.Q | Key.CtrlMask;
  616. }
  617. [Fact]
  618. [AutoInitShutdown]
  619. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard_And_Mouse ()
  620. {
  621. var top = Application.Top;
  622. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  623. var tf = new TextField () { Width = 10 };
  624. win.Add (tf);
  625. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  626. var tf2 = new TextField () { Width = 10 };
  627. win2.Add (tf2);
  628. top.Add (win, win2);
  629. Application.Begin (top);
  630. Assert.True (win.CanFocus);
  631. Assert.True (win.HasFocus);
  632. Assert.True (win2.CanFocus);
  633. Assert.False (win2.HasFocus);
  634. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  635. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  636. Assert.True (win.CanFocus);
  637. Assert.False (win.HasFocus);
  638. Assert.True (win2.CanFocus);
  639. Assert.True (win2.HasFocus);
  640. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  641. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  642. Assert.True (win.CanFocus);
  643. Assert.True (win.HasFocus);
  644. Assert.True (win2.CanFocus);
  645. Assert.False (win2.HasFocus);
  646. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  647. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  648. Assert.True (win.CanFocus);
  649. Assert.False (win.HasFocus);
  650. Assert.True (win2.CanFocus);
  651. Assert.True (win2.HasFocus);
  652. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  653. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  654. Assert.Null (Toplevel.dragPosition);
  655. }
  656. [Fact]
  657. [AutoInitShutdown]
  658. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard_And_Mouse ()
  659. {
  660. var top = Application.Top;
  661. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  662. var tf = new TextField () { Width = 10 };
  663. win.Add (tf);
  664. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  665. var tf2 = new TextField () { Width = 10 };
  666. win2.Add (tf2);
  667. top.Add (win, win2);
  668. Application.Begin (top);
  669. Assert.True (win.CanFocus);
  670. Assert.True (win.HasFocus);
  671. Assert.True (win2.CanFocus);
  672. Assert.False (win2.HasFocus);
  673. Assert.Equal ("win", ((Window)top.Subviews [^1]).Title);
  674. win.CanFocus = false;
  675. Assert.False (win.CanFocus);
  676. Assert.False (win.HasFocus);
  677. Assert.True (win2.CanFocus);
  678. Assert.True (win2.HasFocus);
  679. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  680. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  681. Assert.True (win2.CanFocus);
  682. Assert.False (win.HasFocus);
  683. Assert.True (win2.CanFocus);
  684. Assert.True (win2.HasFocus);
  685. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  686. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  687. Assert.False (win.CanFocus);
  688. Assert.False (win.HasFocus);
  689. Assert.True (win2.CanFocus);
  690. Assert.True (win2.HasFocus);
  691. Assert.Equal ("win2", ((Window)top.Subviews [^1]).Title);
  692. win.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  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 [^1]).Title);
  698. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  699. Assert.Null (Toplevel.dragPosition);
  700. }
  701. #endregion
  702. [Fact, AutoInitShutdown]
  703. public void GetSupportedCultures_Method ()
  704. {
  705. var cultures = Application.GetSupportedCultures ();
  706. Assert.Equal (cultures.Count, Application.SupportedCultures.Count);
  707. }
  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. Assert.Equal (sv, Application.MouseGrabView);
  739. ReflectionTools.InvokePrivate (
  740. typeof (Application),
  741. "ProcessMouseEvent",
  742. new MouseEvent () {
  743. X = 5,
  744. Y = 5,
  745. Flags = MouseFlags.ReportMousePosition
  746. });
  747. Assert.Equal (sv, Application.MouseGrabView);
  748. ReflectionTools.InvokePrivate (
  749. typeof (Application),
  750. "ProcessMouseEvent",
  751. new MouseEvent () {
  752. X = 40,
  753. Y = 12,
  754. Flags = MouseFlags.ReportMousePosition
  755. });
  756. Assert.Null (Application.MouseGrabView);
  757. ReflectionTools.InvokePrivate (
  758. typeof (Application),
  759. "ProcessMouseEvent",
  760. new MouseEvent () {
  761. X = 0,
  762. Y = 0,
  763. Flags = MouseFlags.Button1Pressed
  764. });
  765. Assert.Null (Application.MouseGrabView);
  766. Application.RequestStop ();
  767. } else if (iterations == 2) {
  768. Assert.Null (Application.MouseGrabView);
  769. Application.RequestStop ();
  770. }
  771. };
  772. Application.Run ();
  773. }
  774. [Fact, AutoInitShutdown]
  775. public void MouseGrabView_GrabbedMouse_UnGrabbedMouse_GrabbingMouse_UnGrabbingMouse ()
  776. {
  777. View grabView = null;
  778. var count = 0;
  779. var wasGrabbingMouse = false;
  780. var wasUnGrabbingMouse = false;
  781. var view1 = new View ();
  782. var view2 = new View ();
  783. Application.GrabbedMouse += Application_GrabbedMouse;
  784. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  785. Application.GrabbingMouse += Application_GrabbingMouse;
  786. Application.UnGrabbingMouse += Application_UnGrabbingMouse;
  787. Application.GrabMouse (view1);
  788. Assert.Equal (0, count);
  789. Assert.Equal (grabView, view1);
  790. Assert.Equal (view1, Application.MouseGrabView);
  791. Application.UngrabMouse ();
  792. Assert.Equal (1, count);
  793. Assert.Equal (grabView, view1);
  794. Assert.Null (Application.MouseGrabView);
  795. Application.GrabbedMouse += Application_GrabbedMouse;
  796. Application.UnGrabbedMouse += Application_UnGrabbedMouse;
  797. Application.GrabbingMouse += Application_GrabbingMouse;
  798. Application.UnGrabbingMouse += Application_UnGrabbingMouse;
  799. Application.GrabMouse (view2);
  800. Assert.Equal (1, count);
  801. Assert.Equal (grabView, view2);
  802. Assert.Equal (view2, Application.MouseGrabView);
  803. Assert.True (wasGrabbingMouse);
  804. Application.UngrabMouse ();
  805. Assert.Equal (2, count);
  806. Assert.Equal (grabView, view2);
  807. Assert.Null (Application.MouseGrabView);
  808. Assert.True (wasUnGrabbingMouse);
  809. void Application_GrabbedMouse (View obj)
  810. {
  811. if (count == 0) {
  812. Assert.Equal (view1, obj);
  813. grabView = view1;
  814. } else {
  815. Assert.Equal (view2, obj);
  816. grabView = view2;
  817. }
  818. Application.GrabbedMouse -= Application_GrabbedMouse;
  819. }
  820. void Application_UnGrabbedMouse (View obj)
  821. {
  822. if (count == 0) {
  823. Assert.Equal (view1, obj);
  824. Assert.Equal (grabView, obj);
  825. } else {
  826. Assert.Equal (view2, obj);
  827. Assert.Equal (grabView, obj);
  828. }
  829. count++;
  830. Application.UnGrabbedMouse -= Application_UnGrabbedMouse;
  831. }
  832. bool Application_GrabbingMouse (View obj)
  833. {
  834. if (count == 0) {
  835. Assert.Equal (view1, obj);
  836. grabView = view1;
  837. } else {
  838. Assert.Equal (view2, obj);
  839. grabView = view2;
  840. }
  841. wasGrabbingMouse = true;
  842. Application.GrabbingMouse -= Application_GrabbingMouse;
  843. return false;
  844. }
  845. bool Application_UnGrabbingMouse (View obj)
  846. {
  847. if (count == 0) {
  848. Assert.Equal (view1, obj);
  849. Assert.Equal (grabView, obj);
  850. } else {
  851. Assert.Equal (view2, obj);
  852. Assert.Equal (grabView, obj);
  853. }
  854. wasUnGrabbingMouse = true;
  855. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  856. return false;
  857. }
  858. }
  859. [Fact, AutoInitShutdown]
  860. public void GrabbingMouse_UnGrabbingMouse_Does_Not_Throws_If_Null ()
  861. {
  862. // This is needed to unsubscribe all the toplevel static events.
  863. Application.Top.Dispose ();
  864. var view = new View ();
  865. var exception = Record.Exception (() => Application.GrabMouse (view));
  866. Assert.Null (exception);
  867. Assert.Equal (view, Application.MouseGrabView);
  868. exception = Record.Exception (() => Application.UngrabMouse ());
  869. Assert.Null (exception);
  870. }
  871. #endregion
  872. }
  873. }