ApplicationTests.cs 28 KB

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