ApplicationTests.cs 28 KB

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