ApplicationTests.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. }
  969. }