ApplicationTests.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.Core {
  9. public class ApplicationTests {
  10. public ApplicationTests ()
  11. {
  12. #if DEBUG_IDISPOSABLE
  13. Responder.Instances.Clear ();
  14. #endif
  15. }
  16. [Fact]
  17. public void Init_Shutdown_Cleans_Up ()
  18. {
  19. // Verify initial state is per spec
  20. Pre_Init_State ();
  21. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  22. // Verify post-Init state is correct
  23. Post_Init_State ();
  24. // MockDriver is always 80x25
  25. Assert.Equal (80, Application.Driver.Cols);
  26. Assert.Equal (25, Application.Driver.Rows);
  27. Application.Shutdown ();
  28. // Verify state is back to initial
  29. Pre_Init_State ();
  30. }
  31. void Pre_Init_State ()
  32. {
  33. Assert.Null (Application.Driver);
  34. Assert.Null (Application.Top);
  35. Assert.Null (Application.Current);
  36. Assert.Throws<ArgumentNullException> (() => Application.HeightAsBuffer == true);
  37. Assert.False (Application.AlwaysSetPosition);
  38. Assert.Null (Application.MainLoop);
  39. Assert.Null (Application.Iteration);
  40. Assert.Null (Application.RootMouseEvent);
  41. Assert.Null (Application.Resized);
  42. }
  43. void Post_Init_State ()
  44. {
  45. Assert.NotNull (Application.Driver);
  46. Assert.NotNull (Application.Top);
  47. Assert.NotNull (Application.Current);
  48. Assert.False (Application.HeightAsBuffer);
  49. Assert.False (Application.AlwaysSetPosition);
  50. Assert.NotNull (Application.MainLoop);
  51. Assert.Null (Application.Iteration);
  52. Assert.Null (Application.RootMouseEvent);
  53. Assert.Null (Application.Resized);
  54. }
  55. [Fact]
  56. public void RunState_Dispose_Cleans_Up ()
  57. {
  58. var rs = new Application.RunState (null);
  59. Assert.NotNull (rs);
  60. // Should not throw because Toplevel was null
  61. rs.Dispose ();
  62. var top = new Toplevel ();
  63. rs = new Application.RunState (top);
  64. Assert.NotNull (rs);
  65. // Should throw because there's no stack
  66. Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
  67. }
  68. void Init ()
  69. {
  70. Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  71. Assert.NotNull (Application.Driver);
  72. Assert.NotNull (Application.MainLoop);
  73. }
  74. void Shutdown ()
  75. {
  76. Application.Shutdown ();
  77. }
  78. [Fact]
  79. public void Begin_End_Cleana_Up ()
  80. {
  81. // Setup Mock driver
  82. Init ();
  83. // Test null Toplevel
  84. Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
  85. var top = new Toplevel ();
  86. var rs = Application.Begin (top);
  87. Assert.NotNull (rs);
  88. Assert.Equal (top, Application.Current);
  89. Application.End (rs);
  90. Assert.Null (Application.Current);
  91. Assert.NotNull (Application.Top);
  92. Assert.NotNull (Application.MainLoop);
  93. Assert.NotNull (Application.Driver);
  94. Shutdown ();
  95. Assert.Null (Application.Top);
  96. Assert.Null (Application.MainLoop);
  97. Assert.Null (Application.Driver);
  98. }
  99. [Fact]
  100. public void RequestStop_Stops ()
  101. {
  102. // Setup Mock driver
  103. Init ();
  104. var top = new Toplevel ();
  105. var rs = Application.Begin (top);
  106. Assert.NotNull (rs);
  107. Assert.Equal (top, Application.Current);
  108. Application.Iteration = () => {
  109. Application.RequestStop ();
  110. };
  111. Application.Run (top);
  112. Application.Shutdown ();
  113. Assert.Null (Application.Current);
  114. Assert.Null (Application.Top);
  115. Assert.Null (Application.MainLoop);
  116. Assert.Null (Application.Driver);
  117. }
  118. [Fact]
  119. public void RunningFalse_Stops ()
  120. {
  121. // Setup Mock driver
  122. Init ();
  123. var top = new Toplevel ();
  124. var rs = Application.Begin (top);
  125. Assert.NotNull (rs);
  126. Assert.Equal (top, Application.Current);
  127. Application.Iteration = () => {
  128. top.Running = false;
  129. };
  130. Application.Run (top);
  131. Application.Shutdown ();
  132. Assert.Null (Application.Current);
  133. Assert.Null (Application.Top);
  134. Assert.Null (Application.MainLoop);
  135. Assert.Null (Application.Driver);
  136. }
  137. [Fact]
  138. public void KeyUp_Event ()
  139. {
  140. // Setup Mock driver
  141. Init ();
  142. // Setup some fake keypresses (This)
  143. var input = "Tests";
  144. // Put a control-q in at the end
  145. Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
  146. foreach (var c in input.Reverse ()) {
  147. if (char.IsLetter (c)) {
  148. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
  149. } else {
  150. Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
  151. }
  152. }
  153. int stackSize = Console.MockKeyPresses.Count;
  154. int iterations = 0;
  155. Application.Iteration = () => {
  156. iterations++;
  157. // Stop if we run out of control...
  158. if (iterations > 10) {
  159. Application.RequestStop ();
  160. }
  161. };
  162. int keyUps = 0;
  163. var output = string.Empty;
  164. Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
  165. if (args.KeyEvent.Key != (Key.CtrlMask | Key.Q)) {
  166. output += (char)args.KeyEvent.KeyValue;
  167. }
  168. keyUps++;
  169. };
  170. Application.Run (Application.Top);
  171. // Input string should match output
  172. Assert.Equal (input, output);
  173. // # of key up events should match stack size
  174. //Assert.Equal (stackSize, keyUps);
  175. // We can't use numbers variables on the left side of an Assert.Equal/NotEqual,
  176. // it must be literal (Linux only).
  177. Assert.Equal (6, keyUps);
  178. // # of key up events should match # of iterations
  179. Assert.Equal (stackSize, iterations);
  180. Application.Shutdown ();
  181. Assert.Null (Application.Current);
  182. Assert.Null (Application.Top);
  183. Assert.Null (Application.MainLoop);
  184. Assert.Null (Application.Driver);
  185. }
  186. [Fact]
  187. public void Loaded_Ready_Unlodaded_Events ()
  188. {
  189. Init ();
  190. var top = Application.Top;
  191. var count = 0;
  192. top.Loaded += () => count++;
  193. top.Ready += () => count++;
  194. top.Unloaded += () => count++;
  195. Application.Iteration = () => Application.RequestStop ();
  196. Application.Run ();
  197. Application.Shutdown ();
  198. Assert.Equal (3, count);
  199. }
  200. [Fact]
  201. public void Shutdown_Allows_Async ()
  202. {
  203. static async Task TaskWithAsyncContinuation ()
  204. {
  205. await Task.Yield ();
  206. await Task.Yield ();
  207. }
  208. Init ();
  209. Application.Shutdown ();
  210. var task = TaskWithAsyncContinuation ();
  211. Thread.Sleep (20);
  212. Assert.True (task.IsCompletedSuccessfully);
  213. }
  214. [Fact]
  215. public void Shutdown_Resets_SyncContext ()
  216. {
  217. Init ();
  218. Application.Shutdown ();
  219. Assert.Null (SynchronizationContext.Current);
  220. }
  221. [Fact]
  222. public void AlternateForwardKey_AlternateBackwardKey_Tests ()
  223. {
  224. Init ();
  225. var top = Application.Top;
  226. var w1 = new Window ();
  227. var v1 = new TextField ();
  228. var v2 = new TextView ();
  229. w1.Add (v1, v2);
  230. var w2 = new Window ();
  231. var v3 = new CheckBox ();
  232. var v4 = new Button ();
  233. w2.Add (v3, v4);
  234. top.Add (w1, w2);
  235. Application.Iteration += () => {
  236. Assert.True (v1.HasFocus);
  237. // Using default keys.
  238. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  239. new KeyModifiers () { Ctrl = true }));
  240. Assert.True (v2.HasFocus);
  241. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  242. new KeyModifiers () { Ctrl = true }));
  243. Assert.True (v3.HasFocus);
  244. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  245. new KeyModifiers () { Ctrl = true }));
  246. Assert.True (v4.HasFocus);
  247. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab,
  248. new KeyModifiers () { Ctrl = true }));
  249. Assert.True (v1.HasFocus);
  250. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  251. new KeyModifiers () { Shift = true, Ctrl = true }));
  252. Assert.True (v4.HasFocus);
  253. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  254. new KeyModifiers () { Shift = true, Ctrl = true }));
  255. Assert.True (v3.HasFocus);
  256. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  257. new KeyModifiers () { Shift = true, Ctrl = true }));
  258. Assert.True (v2.HasFocus);
  259. top.ProcessKey (new KeyEvent (Key.ShiftMask | Key.CtrlMask | Key.Tab,
  260. new KeyModifiers () { Shift = true, Ctrl = true }));
  261. Assert.True (v1.HasFocus);
  262. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  263. new KeyModifiers () { Ctrl = true }));
  264. Assert.True (v2.HasFocus);
  265. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  266. new KeyModifiers () { Ctrl = true }));
  267. Assert.True (v3.HasFocus);
  268. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  269. new KeyModifiers () { Ctrl = true }));
  270. Assert.True (v4.HasFocus);
  271. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageDown,
  272. new KeyModifiers () { Ctrl = true }));
  273. Assert.True (v1.HasFocus);
  274. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  275. new KeyModifiers () { Ctrl = true }));
  276. Assert.True (v4.HasFocus);
  277. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  278. new KeyModifiers () { Ctrl = true }));
  279. Assert.True (v3.HasFocus);
  280. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  281. new KeyModifiers () { Ctrl = true }));
  282. Assert.True (v2.HasFocus);
  283. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.PageUp,
  284. new KeyModifiers () { Ctrl = true }));
  285. Assert.True (v1.HasFocus);
  286. // Using another's alternate keys.
  287. Application.AlternateForwardKey = Key.F7;
  288. Application.AlternateBackwardKey = Key.F6;
  289. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  290. Assert.True (v2.HasFocus);
  291. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  292. Assert.True (v3.HasFocus);
  293. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  294. Assert.True (v4.HasFocus);
  295. top.ProcessKey (new KeyEvent (Key.F7, new KeyModifiers ()));
  296. Assert.True (v1.HasFocus);
  297. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  298. Assert.True (v4.HasFocus);
  299. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  300. Assert.True (v3.HasFocus);
  301. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  302. Assert.True (v2.HasFocus);
  303. top.ProcessKey (new KeyEvent (Key.F6, new KeyModifiers ()));
  304. Assert.True (v1.HasFocus);
  305. Application.RequestStop ();
  306. };
  307. Application.Run (top);
  308. // Shutdown must be called to safely clean up Application if Init has been called
  309. Application.Shutdown ();
  310. }
  311. [Fact]
  312. public void Application_RequestStop_With_Params_On_A_Not_MdiContainer_Always_Use_The_Application_Current ()
  313. {
  314. Init ();
  315. var top1 = new Toplevel ();
  316. var top2 = new Toplevel ();
  317. var top3 = new Window ();
  318. var top4 = new Window ();
  319. var d = new Dialog ();
  320. // top1, top2, top3, d1 = 4
  321. var iterations = 4;
  322. top1.Ready += () => {
  323. Assert.Null (Application.MdiChildes);
  324. Application.Run (top2);
  325. };
  326. top2.Ready += () => {
  327. Assert.Null (Application.MdiChildes);
  328. Application.Run (top3);
  329. };
  330. top3.Ready += () => {
  331. Assert.Null (Application.MdiChildes);
  332. Application.Run (top4);
  333. };
  334. top4.Ready += () => {
  335. Assert.Null (Application.MdiChildes);
  336. Application.Run (d);
  337. };
  338. d.Ready += () => {
  339. Assert.Null (Application.MdiChildes);
  340. // This will close the d because on a not MdiContainer the Application.Current it always used.
  341. Application.RequestStop (top1);
  342. Assert.True (Application.Current == d);
  343. };
  344. d.Closed += (e) => Application.RequestStop (top1);
  345. Application.Iteration += () => {
  346. Assert.Null (Application.MdiChildes);
  347. if (iterations == 4) {
  348. Assert.True (Application.Current == d);
  349. } else if (iterations == 3) {
  350. Assert.True (Application.Current == top4);
  351. } else if (iterations == 2) {
  352. Assert.True (Application.Current == top3);
  353. } else if (iterations == 1) {
  354. Assert.True (Application.Current == top2);
  355. } else {
  356. Assert.True (Application.Current == top1);
  357. }
  358. Application.RequestStop (top1);
  359. iterations--;
  360. };
  361. Application.Run (top1);
  362. Assert.Null (Application.MdiChildes);
  363. Application.Shutdown ();
  364. }
  365. class Mdi : Toplevel {
  366. public Mdi ()
  367. {
  368. IsMdiContainer = true;
  369. }
  370. }
  371. [Fact]
  372. public void MdiContainer_With_Toplevel_RequestStop_Balanced ()
  373. {
  374. Init ();
  375. var mdi = new Mdi ();
  376. var c1 = new Toplevel ();
  377. var c2 = new Window ();
  378. var c3 = new Window ();
  379. var d = new Dialog ();
  380. // MdiChild = c1, c2, c3
  381. // d1 = 1
  382. var iterations = 4;
  383. mdi.Ready += () => {
  384. Assert.Empty (Application.MdiChildes);
  385. Application.Run (c1);
  386. };
  387. c1.Ready += () => {
  388. Assert.Single (Application.MdiChildes);
  389. Application.Run (c2);
  390. };
  391. c2.Ready += () => {
  392. Assert.Equal (2, Application.MdiChildes.Count);
  393. Application.Run (c3);
  394. };
  395. c3.Ready += () => {
  396. Assert.Equal (3, Application.MdiChildes.Count);
  397. Application.Run (d);
  398. };
  399. // More easy because the Mdi Container handles all at once
  400. d.Ready += () => {
  401. Assert.Equal (3, Application.MdiChildes.Count);
  402. // This will not close the MdiContainer because d is a modal toplevel and will be closed.
  403. mdi.RequestStop ();
  404. };
  405. // Now this will close the MdiContainer propagating through the MdiChildes.
  406. d.Closed += (e) => {
  407. mdi.RequestStop ();
  408. };
  409. Application.Iteration += () => {
  410. if (iterations == 4) {
  411. // The Dialog was not closed before and will be closed now.
  412. Assert.True (Application.Current == d);
  413. Assert.False (d.Running);
  414. } else {
  415. Assert.Equal (iterations, Application.MdiChildes.Count);
  416. for (int i = 0; i < iterations; i++) {
  417. Assert.Equal ((iterations - i + 1).ToString (), Application.MdiChildes [i].Id);
  418. }
  419. }
  420. iterations--;
  421. };
  422. Application.Run (mdi);
  423. Assert.Empty (Application.MdiChildes);
  424. Application.Shutdown ();
  425. }
  426. [Fact]
  427. public void MdiContainer_With_Application_RequestStop_MdiTop_With_Params ()
  428. {
  429. Init ();
  430. var mdi = new Mdi ();
  431. var c1 = new Toplevel ();
  432. var c2 = new Window ();
  433. var c3 = new Window ();
  434. var d = new Dialog ();
  435. // MdiChild = c1, c2, c3
  436. // d1 = 1
  437. var iterations = 4;
  438. mdi.Ready += () => {
  439. Assert.Empty (Application.MdiChildes);
  440. Application.Run (c1);
  441. };
  442. c1.Ready += () => {
  443. Assert.Single (Application.MdiChildes);
  444. Application.Run (c2);
  445. };
  446. c2.Ready += () => {
  447. Assert.Equal (2, Application.MdiChildes.Count);
  448. Application.Run (c3);
  449. };
  450. c3.Ready += () => {
  451. Assert.Equal (3, Application.MdiChildes.Count);
  452. Application.Run (d);
  453. };
  454. // Also easy because the Mdi Container handles all at once
  455. d.Ready += () => {
  456. Assert.Equal (3, Application.MdiChildes.Count);
  457. // This will not close the MdiContainer because d is a modal toplevel
  458. Application.RequestStop (mdi);
  459. };
  460. // Now this will close the MdiContainer propagating through the MdiChildes.
  461. d.Closed += (e) => Application.RequestStop (mdi);
  462. Application.Iteration += () => {
  463. if (iterations == 4) {
  464. // The Dialog was not closed before and will be closed now.
  465. Assert.True (Application.Current == d);
  466. Assert.False (d.Running);
  467. } else {
  468. Assert.Equal (iterations, Application.MdiChildes.Count);
  469. for (int i = 0; i < iterations; i++) {
  470. Assert.Equal ((iterations - i + 1).ToString (), Application.MdiChildes [i].Id);
  471. }
  472. }
  473. iterations--;
  474. };
  475. Application.Run (mdi);
  476. Assert.Empty (Application.MdiChildes);
  477. Application.Shutdown ();
  478. }
  479. [Fact]
  480. public void MdiContainer_With_Application_RequestStop_MdiTop_Without_Params ()
  481. {
  482. Init ();
  483. var mdi = new Mdi ();
  484. var c1 = new Toplevel ();
  485. var c2 = new Window ();
  486. var c3 = new Window ();
  487. var d = new Dialog ();
  488. // MdiChild = c1, c2, c3 = 3
  489. // d1 = 1
  490. var iterations = 4;
  491. mdi.Ready += () => {
  492. Assert.Empty (Application.MdiChildes);
  493. Application.Run (c1);
  494. };
  495. c1.Ready += () => {
  496. Assert.Single (Application.MdiChildes);
  497. Application.Run (c2);
  498. };
  499. c2.Ready += () => {
  500. Assert.Equal (2, Application.MdiChildes.Count);
  501. Application.Run (c3);
  502. };
  503. c3.Ready += () => {
  504. Assert.Equal (3, Application.MdiChildes.Count);
  505. Application.Run (d);
  506. };
  507. //More harder because it's sequential.
  508. d.Ready += () => {
  509. Assert.Equal (3, Application.MdiChildes.Count);
  510. // Close the Dialog
  511. Application.RequestStop ();
  512. };
  513. // Now this will close the MdiContainer propagating through the MdiChildes.
  514. d.Closed += (e) => Application.RequestStop (mdi);
  515. Application.Iteration += () => {
  516. if (iterations == 4) {
  517. // The Dialog still is the current top and we can't request stop to MdiContainer
  518. // because we are not using parameter calls.
  519. Assert.True (Application.Current == d);
  520. Assert.False (d.Running);
  521. } else {
  522. Assert.Equal (iterations, Application.MdiChildes.Count);
  523. for (int i = 0; i < iterations; i++) {
  524. Assert.Equal ((iterations - i + 1).ToString (), Application.MdiChildes [i].Id);
  525. }
  526. }
  527. iterations--;
  528. };
  529. Application.Run (mdi);
  530. Assert.Empty (Application.MdiChildes);
  531. Application.Shutdown ();
  532. }
  533. [Fact]
  534. public void IsMdiChild_Testing ()
  535. {
  536. Init ();
  537. var mdi = new Mdi ();
  538. var c1 = new Toplevel ();
  539. var c2 = new Window ();
  540. var c3 = new Window ();
  541. var d = new Dialog ();
  542. Application.Iteration += () => {
  543. Assert.False (mdi.IsMdiChild);
  544. Assert.True (c1.IsMdiChild);
  545. Assert.True (c2.IsMdiChild);
  546. Assert.True (c3.IsMdiChild);
  547. Assert.False (d.IsMdiChild);
  548. mdi.RequestStop ();
  549. };
  550. Application.Run (mdi);
  551. Application.Shutdown ();
  552. }
  553. [Fact]
  554. public void Modal_Toplevel_Can_Open_Another_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  555. {
  556. Init ();
  557. var mdi = new Mdi ();
  558. var c1 = new Toplevel ();
  559. var c2 = new Window ();
  560. var c3 = new Window ();
  561. var d1 = new Dialog ();
  562. var d2 = new Dialog ();
  563. // MdiChild = c1, c2, c3 = 3
  564. // d1, d2 = 2
  565. var iterations = 5;
  566. mdi.Ready += () => {
  567. Assert.Empty (Application.MdiChildes);
  568. Application.Run (c1);
  569. };
  570. c1.Ready += () => {
  571. Assert.Single (Application.MdiChildes);
  572. Application.Run (c2);
  573. };
  574. c2.Ready += () => {
  575. Assert.Equal (2, Application.MdiChildes.Count);
  576. Application.Run (c3);
  577. };
  578. c3.Ready += () => {
  579. Assert.Equal (3, Application.MdiChildes.Count);
  580. Application.Run (d1);
  581. };
  582. d1.Ready += () => {
  583. Assert.Equal (3, Application.MdiChildes.Count);
  584. Application.Run (d2);
  585. };
  586. d2.Ready += () => {
  587. Assert.Equal (3, Application.MdiChildes.Count);
  588. Assert.True (Application.Current == d2);
  589. Assert.True (Application.Current.Running);
  590. // Trying to close the Dialog1
  591. d1.RequestStop ();
  592. };
  593. // Now this will close the MdiContainer propagating through the MdiChildes.
  594. d1.Closed += (e) => {
  595. Assert.True (Application.Current == d1);
  596. Assert.False (Application.Current.Running);
  597. mdi.RequestStop ();
  598. };
  599. Application.Iteration += () => {
  600. if (iterations == 5) {
  601. // The Dialog2 still is the current top and we can't request stop to MdiContainer
  602. // because Dialog2 and Dialog1 must be closed first.
  603. // Dialog2 will be closed in this iteration.
  604. Assert.True (Application.Current == d2);
  605. Assert.False (Application.Current.Running);
  606. Assert.False (d1.Running);
  607. } else if (iterations == 4) {
  608. // Dialog1 will be closed in this iteration.
  609. Assert.True (Application.Current == d1);
  610. Assert.False (Application.Current.Running);
  611. } else {
  612. Assert.Equal (iterations, Application.MdiChildes.Count);
  613. for (int i = 0; i < iterations; i++) {
  614. Assert.Equal ((iterations - i + 1).ToString (), Application.MdiChildes [i].Id);
  615. }
  616. }
  617. iterations--;
  618. };
  619. Application.Run (mdi);
  620. Assert.Empty (Application.MdiChildes);
  621. Application.Shutdown ();
  622. }
  623. [Fact]
  624. public void Modal_Toplevel_Can_Open_Another_Not_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  625. {
  626. Init ();
  627. var mdi = new Mdi ();
  628. var c1 = new Toplevel ();
  629. var c2 = new Window ();
  630. var c3 = new Window ();
  631. var d1 = new Dialog ();
  632. var c4 = new Toplevel ();
  633. // MdiChild = c1, c2, c3, c4 = 4
  634. // d1 = 1
  635. var iterations = 5;
  636. mdi.Ready += () => {
  637. Assert.Empty (Application.MdiChildes);
  638. Application.Run (c1);
  639. };
  640. c1.Ready += () => {
  641. Assert.Single (Application.MdiChildes);
  642. Application.Run (c2);
  643. };
  644. c2.Ready += () => {
  645. Assert.Equal (2, Application.MdiChildes.Count);
  646. Application.Run (c3);
  647. };
  648. c3.Ready += () => {
  649. Assert.Equal (3, Application.MdiChildes.Count);
  650. Application.Run (d1);
  651. };
  652. d1.Ready += () => {
  653. Assert.Equal (3, Application.MdiChildes.Count);
  654. Application.Run (c4);
  655. };
  656. c4.Ready += () => {
  657. Assert.Equal (4, Application.MdiChildes.Count);
  658. // Trying to close the Dialog1
  659. d1.RequestStop ();
  660. };
  661. // Now this will close the MdiContainer propagating through the MdiChildes.
  662. d1.Closed += (e) => {
  663. mdi.RequestStop ();
  664. };
  665. Application.Iteration += () => {
  666. if (iterations == 5) {
  667. // The Dialog2 still is the current top and we can't request stop to MdiContainer
  668. // because Dialog2 and Dialog1 must be closed first.
  669. // Using request stop here will call the Dialog again without need
  670. Assert.True (Application.Current == d1);
  671. Assert.False (Application.Current.Running);
  672. Assert.True (c4.Running);
  673. } else {
  674. Assert.Equal (iterations, Application.MdiChildes.Count);
  675. for (int i = 0; i < iterations; i++) {
  676. Assert.Equal ((iterations - i + (iterations == 4 && i == 0 ? 2 : 1)).ToString (),
  677. Application.MdiChildes [i].Id);
  678. }
  679. }
  680. iterations--;
  681. };
  682. Application.Run (mdi);
  683. Assert.Empty (Application.MdiChildes);
  684. Application.Shutdown ();
  685. }
  686. [Fact]
  687. public void MoveCurrent_Returns_False_If_The_Current_And_Top_Parameter_Are_Both_With_Running_Set_To_False ()
  688. {
  689. Init ();
  690. var mdi = new Mdi ();
  691. var c1 = new Toplevel ();
  692. var c2 = new Window ();
  693. var c3 = new Window ();
  694. // MdiChild = c1, c2, c3
  695. var iterations = 3;
  696. mdi.Ready += () => {
  697. Assert.Empty (Application.MdiChildes);
  698. Application.Run (c1);
  699. };
  700. c1.Ready += () => {
  701. Assert.Single (Application.MdiChildes);
  702. Application.Run (c2);
  703. };
  704. c2.Ready += () => {
  705. Assert.Equal (2, Application.MdiChildes.Count);
  706. Application.Run (c3);
  707. };
  708. c3.Ready += () => {
  709. Assert.Equal (3, Application.MdiChildes.Count);
  710. c3.RequestStop ();
  711. c1.RequestStop ();
  712. };
  713. // Now this will close the MdiContainer propagating through the MdiChildes.
  714. c1.Closed += (e) => {
  715. mdi.RequestStop ();
  716. };
  717. Application.Iteration += () => {
  718. if (iterations == 3) {
  719. // The Current still is c3 because Current.Running is false.
  720. Assert.True (Application.Current == c3);
  721. Assert.False (Application.Current.Running);
  722. // But the childes order were reorder by Running = false
  723. Assert.True (Application.MdiChildes [0] == c3);
  724. Assert.True (Application.MdiChildes [1] == c1);
  725. Assert.True (Application.MdiChildes [^1] == c2);
  726. } else if (iterations == 2) {
  727. // The Current is c1 and Current.Running is false.
  728. Assert.True (Application.Current == c1);
  729. Assert.False (Application.Current.Running);
  730. Assert.True (Application.MdiChildes [0] == c1);
  731. Assert.True (Application.MdiChildes [^1] == c2);
  732. } else if (iterations == 1) {
  733. // The Current is c2 and Current.Running is false.
  734. Assert.True (Application.Current == c2);
  735. Assert.False (Application.Current.Running);
  736. Assert.True (Application.MdiChildes [^1] == c2);
  737. } else {
  738. // The Current is mdi.
  739. Assert.True (Application.Current == mdi);
  740. Assert.Empty (Application.MdiChildes);
  741. }
  742. iterations--;
  743. };
  744. Application.Run (mdi);
  745. Assert.Empty (Application.MdiChildes);
  746. Application.Shutdown ();
  747. }
  748. [Fact]
  749. public void MdiContainer_Throws_If_More_Than_One ()
  750. {
  751. Init ();
  752. var mdi = new Mdi ();
  753. var mdi2 = new Mdi ();
  754. mdi.Ready += () => {
  755. Assert.Throws<InvalidOperationException> (() => Application.Run (mdi2));
  756. mdi.RequestStop ();
  757. };
  758. Application.Run (mdi);
  759. Application.Shutdown ();
  760. }
  761. [Fact]
  762. public void MdiContainer_Open_And_Close_Modal_And_Open_Not_Modal_Toplevels_Randomly ()
  763. {
  764. Init ();
  765. var mdi = new Mdi ();
  766. var logger = new Toplevel ();
  767. var iterations = 1; // The logger
  768. var running = true;
  769. var stageCompleted = true;
  770. var allStageClosed = false;
  771. var mdiRequestStop = false;
  772. mdi.Ready += () => {
  773. Assert.Empty (Application.MdiChildes);
  774. Application.Run (logger);
  775. };
  776. logger.Ready += () => Assert.Single (Application.MdiChildes);
  777. Application.Iteration += () => {
  778. if (stageCompleted && running) {
  779. stageCompleted = false;
  780. var stage = new Window () { Modal = true };
  781. stage.Ready += () => {
  782. Assert.Equal (iterations, Application.MdiChildes.Count);
  783. stage.RequestStop ();
  784. };
  785. stage.Closed += (_) => {
  786. if (iterations == 11) {
  787. allStageClosed = true;
  788. }
  789. Assert.Equal (iterations, Application.MdiChildes.Count);
  790. if (running) {
  791. stageCompleted = true;
  792. var rpt = new Window ();
  793. rpt.Ready += () => {
  794. iterations++;
  795. Assert.Equal (iterations, Application.MdiChildes.Count);
  796. };
  797. Application.Run (rpt);
  798. }
  799. };
  800. Application.Run (stage);
  801. } else if (iterations == 11 && running) {
  802. running = false;
  803. Assert.Equal (iterations, Application.MdiChildes.Count);
  804. } else if (!mdiRequestStop && running && !allStageClosed) {
  805. Assert.Equal (iterations, Application.MdiChildes.Count);
  806. } else if (!mdiRequestStop && !running && allStageClosed) {
  807. Assert.Equal (iterations, Application.MdiChildes.Count);
  808. mdiRequestStop = true;
  809. mdi.RequestStop ();
  810. } else {
  811. Assert.Empty (Application.MdiChildes);
  812. }
  813. };
  814. Application.Run (mdi);
  815. Assert.Empty (Application.MdiChildes);
  816. Application.Shutdown ();
  817. }
  818. [Fact]
  819. public void AllChildClosed_Event_Test ()
  820. {
  821. Init ();
  822. var mdi = new Mdi ();
  823. var c1 = new Toplevel ();
  824. var c2 = new Window ();
  825. var c3 = new Window ();
  826. // MdiChild = c1, c2, c3
  827. var iterations = 3;
  828. mdi.Ready += () => {
  829. Assert.Empty (Application.MdiChildes);
  830. Application.Run (c1);
  831. };
  832. c1.Ready += () => {
  833. Assert.Single (Application.MdiChildes);
  834. Application.Run (c2);
  835. };
  836. c2.Ready += () => {
  837. Assert.Equal (2, Application.MdiChildes.Count);
  838. Application.Run (c3);
  839. };
  840. c3.Ready += () => {
  841. Assert.Equal (3, Application.MdiChildes.Count);
  842. c3.RequestStop ();
  843. c2.RequestStop ();
  844. c1.RequestStop ();
  845. };
  846. // Now this will close the MdiContainer when all MdiChildes was closed
  847. mdi.AllChildClosed += () => {
  848. mdi.RequestStop ();
  849. };
  850. Application.Iteration += () => {
  851. if (iterations == 3) {
  852. // The Current still is c3 because Current.Running is false.
  853. Assert.True (Application.Current == c3);
  854. Assert.False (Application.Current.Running);
  855. // But the childes order were reorder by Running = false
  856. Assert.True (Application.MdiChildes [0] == c3);
  857. Assert.True (Application.MdiChildes [1] == c2);
  858. Assert.True (Application.MdiChildes [^1] == c1);
  859. } else if (iterations == 2) {
  860. // The Current is c2 and Current.Running is false.
  861. Assert.True (Application.Current == c2);
  862. Assert.False (Application.Current.Running);
  863. Assert.True (Application.MdiChildes [0] == c2);
  864. Assert.True (Application.MdiChildes [^1] == c1);
  865. } else if (iterations == 1) {
  866. // The Current is c1 and Current.Running is false.
  867. Assert.True (Application.Current == c1);
  868. Assert.False (Application.Current.Running);
  869. Assert.True (Application.MdiChildes [^1] == c1);
  870. } else {
  871. // The Current is mdi.
  872. Assert.True (Application.Current == mdi);
  873. Assert.False (Application.Current.Running);
  874. Assert.Empty (Application.MdiChildes);
  875. }
  876. iterations--;
  877. };
  878. Application.Run (mdi);
  879. Assert.Empty (Application.MdiChildes);
  880. Application.Shutdown ();
  881. }
  882. [Fact]
  883. public void SetCurrentAsTop_Run_A_Not_Modal_Toplevel_Make_It_The_Current_Application_Top ()
  884. {
  885. Init ();
  886. var t1 = new Toplevel ();
  887. var t2 = new Toplevel ();
  888. var t3 = new Toplevel ();
  889. var d = new Dialog ();
  890. var t4 = new Toplevel ();
  891. // t1, t2, t3, d, t4
  892. var iterations = 5;
  893. t1.Ready += () => {
  894. Assert.Equal (t1, Application.Top);
  895. Application.Run (t2);
  896. };
  897. t2.Ready += () => {
  898. Assert.Equal (t2, Application.Top);
  899. Application.Run (t3);
  900. };
  901. t3.Ready += () => {
  902. Assert.Equal (t3, Application.Top);
  903. Application.Run (d);
  904. };
  905. d.Ready += () => {
  906. Assert.Equal (t3, Application.Top);
  907. Application.Run (t4);
  908. };
  909. t4.Ready += () => {
  910. Assert.Equal (t4, Application.Top);
  911. t4.RequestStop ();
  912. d.RequestStop ();
  913. t3.RequestStop ();
  914. t2.RequestStop ();
  915. };
  916. // Now this will close the MdiContainer when all MdiChildes was closed
  917. t2.Closed += (_) => {
  918. t1.RequestStop ();
  919. };
  920. Application.Iteration += () => {
  921. if (iterations == 5) {
  922. // The Current still is t4 because Current.Running is false.
  923. Assert.Equal (t4, Application.Current);
  924. Assert.False (Application.Current.Running);
  925. Assert.Equal (t4, Application.Top);
  926. } else if (iterations == 4) {
  927. // The Current is d and Current.Running is false.
  928. Assert.Equal (d, Application.Current);
  929. Assert.False (Application.Current.Running);
  930. Assert.Equal (t4, Application.Top);
  931. } else if (iterations == 3) {
  932. // The Current is t3 and Current.Running is false.
  933. Assert.Equal (t3, Application.Current);
  934. Assert.False (Application.Current.Running);
  935. Assert.Equal (t3, Application.Top);
  936. } else if (iterations == 2) {
  937. // The Current is t2 and Current.Running is false.
  938. Assert.Equal (t2, Application.Current);
  939. Assert.False (Application.Current.Running);
  940. Assert.Equal (t2, Application.Top);
  941. } else {
  942. // The Current is t1.
  943. Assert.Equal (t1, Application.Current);
  944. Assert.False (Application.Current.Running);
  945. Assert.Equal (t1, Application.Top);
  946. }
  947. iterations--;
  948. };
  949. Application.Run (t1);
  950. Assert.Equal (t1, Application.Top);
  951. Application.Shutdown ();
  952. Assert.Null (Application.Top);
  953. }
  954. [Fact]
  955. [AutoInitShutdown]
  956. public void Internal_Tests ()
  957. {
  958. Assert.True (Application._initialized);
  959. Assert.NotNull (Application.Top);
  960. var rs = Application.Begin (Application.Top);
  961. Assert.Equal (Application.Top, rs.Toplevel);
  962. Assert.Null (Application.mouseGrabView);
  963. Assert.Null (Application.wantContinuousButtonPressedView);
  964. Assert.False (Application.DebugDrawBounds);
  965. Assert.False (Application.ShowChild (Application.Top));
  966. Application.End (Application.Top);
  967. }
  968. [Fact]
  969. [AutoInitShutdown]
  970. public void QuitKey_Getter_Setter ()
  971. {
  972. var top = Application.Top;
  973. var isQuiting = false;
  974. top.Closing += (e) => {
  975. isQuiting = true;
  976. e.Cancel = true;
  977. };
  978. Application.Begin (top);
  979. top.Running = true;
  980. Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
  981. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  982. Assert.True (isQuiting);
  983. isQuiting = false;
  984. Application.QuitKey = Key.C | Key.CtrlMask;
  985. Application.Driver.SendKeys ('q', ConsoleKey.Q, false, false, true);
  986. Assert.False (isQuiting);
  987. Application.Driver.SendKeys ('c', ConsoleKey.C, false, false, true);
  988. Assert.True (isQuiting);
  989. // Reset the QuitKey to avoid throws errors on another tests
  990. Application.QuitKey = Key.Q | Key.CtrlMask;
  991. }
  992. [Fact]
  993. [AutoInitShutdown]
  994. public void EnsuresTopOnFront_CanFocus_True_By_Keyboard_And_Mouse ()
  995. {
  996. var top = Application.Top;
  997. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  998. var tf = new TextField () { Width = 10 };
  999. win.Add (tf);
  1000. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  1001. var tf2 = new TextField () { Width = 10 };
  1002. win2.Add (tf2);
  1003. top.Add (win, win2);
  1004. Application.Begin (top);
  1005. Assert.True (win.CanFocus);
  1006. Assert.True (win.HasFocus);
  1007. Assert.True (win2.CanFocus);
  1008. Assert.False (win2.HasFocus);
  1009. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1010. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  1011. Assert.True (win.CanFocus);
  1012. Assert.False (win.HasFocus);
  1013. Assert.True (win2.CanFocus);
  1014. Assert.True (win2.HasFocus);
  1015. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1016. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  1017. Assert.True (win.CanFocus);
  1018. Assert.True (win.HasFocus);
  1019. Assert.True (win2.CanFocus);
  1020. Assert.False (win2.HasFocus);
  1021. Assert.Equal ("win", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1022. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  1023. Assert.True (win.CanFocus);
  1024. Assert.False (win.HasFocus);
  1025. Assert.True (win2.CanFocus);
  1026. Assert.True (win2.HasFocus);
  1027. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1028. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  1029. Assert.Null (Toplevel.dragPosition);
  1030. }
  1031. [Fact]
  1032. [AutoInitShutdown]
  1033. public void EnsuresTopOnFront_CanFocus_False_By_Keyboard_And_Mouse ()
  1034. {
  1035. var top = Application.Top;
  1036. var win = new Window ("win") { X = 0, Y = 0, Width = 20, Height = 10 };
  1037. var tf = new TextField () { Width = 10 };
  1038. win.Add (tf);
  1039. var win2 = new Window ("win2") { X = 22, Y = 0, Width = 20, Height = 10 };
  1040. var tf2 = new TextField () { Width = 10 };
  1041. win2.Add (tf2);
  1042. top.Add (win, win2);
  1043. Application.Begin (top);
  1044. win.CanFocus = false;
  1045. Assert.False (win.CanFocus);
  1046. Assert.False (win.HasFocus);
  1047. Assert.True (win2.CanFocus);
  1048. Assert.True (win2.HasFocus);
  1049. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1050. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  1051. Assert.True (win2.CanFocus);
  1052. Assert.False (win.HasFocus);
  1053. Assert.True (win2.CanFocus);
  1054. Assert.True (win2.HasFocus);
  1055. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1056. top.ProcessKey (new KeyEvent (Key.CtrlMask | Key.Tab, new KeyModifiers ()));
  1057. Assert.False (win.CanFocus);
  1058. Assert.False (win.HasFocus);
  1059. Assert.True (win2.CanFocus);
  1060. Assert.True (win2.HasFocus);
  1061. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1062. win.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Pressed });
  1063. Assert.False (win.CanFocus);
  1064. Assert.False (win.HasFocus);
  1065. Assert.True (win2.CanFocus);
  1066. Assert.True (win2.HasFocus);
  1067. Assert.Equal ("win2", ((Window)top.Subviews [top.Subviews.Count - 1]).Title);
  1068. win2.MouseEvent (new MouseEvent () { Flags = MouseFlags.Button1Released });
  1069. Assert.Null (Toplevel.dragPosition);
  1070. }
  1071. }
  1072. }