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