ApplicationTests.cs 26 KB

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