ApplicationTests.cs 26 KB

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