ApplicationTests.cs 28 KB

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