OverlappedTests.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class OverlappedTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public OverlappedTests (ITestOutputHelper output)
  7. {
  8. _output = output;
  9. #if DEBUG_IDISPOSABLE
  10. Responder.Instances.Clear ();
  11. RunState.Instances.Clear ();
  12. #endif
  13. }
  14. [Fact]
  15. [AutoInitShutdown]
  16. public void AllChildClosed_Event_Test ()
  17. {
  18. var overlapped = new Overlapped ();
  19. var c1 = new Toplevel ();
  20. var c2 = new Window ();
  21. var c3 = new Window ();
  22. // OverlappedChild = c1, c2, c3
  23. var iterations = 3;
  24. overlapped.Ready += (s, e) =>
  25. {
  26. Assert.Empty (Application.OverlappedChildren);
  27. Application.Run (c1);
  28. };
  29. c1.Ready += (s, e) =>
  30. {
  31. Assert.Single (Application.OverlappedChildren);
  32. Application.Run (c2);
  33. };
  34. c2.Ready += (s, e) =>
  35. {
  36. Assert.Equal (2, Application.OverlappedChildren.Count);
  37. Application.Run (c3);
  38. };
  39. c3.Ready += (s, e) =>
  40. {
  41. Assert.Equal (3, Application.OverlappedChildren.Count);
  42. c3.RequestStop ();
  43. c2.RequestStop ();
  44. c1.RequestStop ();
  45. };
  46. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  47. overlapped.AllChildClosed += (s, e) => { overlapped.RequestStop (); };
  48. Application.Iteration += (s, a) =>
  49. {
  50. if (iterations == 3)
  51. {
  52. // The Current still is c3 because Current.Running is false.
  53. Assert.True (Application.Current == c3);
  54. Assert.False (Application.Current.Running);
  55. // But the Children order were reorder by Running = false
  56. Assert.True (Application.OverlappedChildren [0] == c3);
  57. Assert.True (Application.OverlappedChildren [1] == c2);
  58. Assert.True (Application.OverlappedChildren [^1] == c1);
  59. }
  60. else if (iterations == 2)
  61. {
  62. // The Current is c2 and Current.Running is false.
  63. Assert.True (Application.Current == c2);
  64. Assert.False (Application.Current.Running);
  65. Assert.True (Application.OverlappedChildren [0] == c2);
  66. Assert.True (Application.OverlappedChildren [^1] == c1);
  67. }
  68. else if (iterations == 1)
  69. {
  70. // The Current is c1 and Current.Running is false.
  71. Assert.True (Application.Current == c1);
  72. Assert.False (Application.Current.Running);
  73. Assert.True (Application.OverlappedChildren [^1] == c1);
  74. }
  75. else
  76. {
  77. // The Current is overlapped.
  78. Assert.True (Application.Current == overlapped);
  79. Assert.False (Application.Current.Running);
  80. Assert.Empty (Application.OverlappedChildren);
  81. }
  82. iterations--;
  83. };
  84. Application.Run (overlapped);
  85. Assert.Null (Application.OverlappedChildren);
  86. Assert.Null (Application.OverlappedTop);
  87. Assert.NotNull (Application.Top);
  88. }
  89. [Fact]
  90. [AutoInitShutdown]
  91. public void Application_RequestStop_With_Params_On_A_Not_OverlappedContainer_Always_Use_Application_Current ()
  92. {
  93. var top1 = new Toplevel ();
  94. var top2 = new Toplevel ();
  95. var top3 = new Window ();
  96. var top4 = new Window ();
  97. var d = new Dialog ();
  98. // top1, top2, top3, d1 = 4
  99. var iterations = 4;
  100. top1.Ready += (s, e) =>
  101. {
  102. Assert.Null (Application.OverlappedChildren);
  103. Application.Run (top2);
  104. };
  105. top2.Ready += (s, e) =>
  106. {
  107. Assert.Null (Application.OverlappedChildren);
  108. Application.Run (top3);
  109. };
  110. top3.Ready += (s, e) =>
  111. {
  112. Assert.Null (Application.OverlappedChildren);
  113. Application.Run (top4);
  114. };
  115. top4.Ready += (s, e) =>
  116. {
  117. Assert.Null (Application.OverlappedChildren);
  118. Application.Run (d);
  119. };
  120. d.Ready += (s, e) =>
  121. {
  122. Assert.Null (Application.OverlappedChildren);
  123. // This will close the d because on a not OverlappedContainer the Application.Current it always used.
  124. Application.RequestStop (top1);
  125. Assert.True (Application.Current == d);
  126. };
  127. d.Closed += (s, e) => Application.RequestStop (top1);
  128. Application.Iteration += (s, a) =>
  129. {
  130. Assert.Null (Application.OverlappedChildren);
  131. if (iterations == 4)
  132. {
  133. Assert.True (Application.Current == d);
  134. }
  135. else if (iterations == 3)
  136. {
  137. Assert.True (Application.Current == top4);
  138. }
  139. else if (iterations == 2)
  140. {
  141. Assert.True (Application.Current == top3);
  142. }
  143. else if (iterations == 1)
  144. {
  145. Assert.True (Application.Current == top2);
  146. }
  147. else
  148. {
  149. Assert.True (Application.Current == top1);
  150. }
  151. Application.RequestStop (top1);
  152. iterations--;
  153. };
  154. Application.Run (top1);
  155. Assert.Null (Application.OverlappedChildren);
  156. }
  157. [Fact]
  158. [TestRespondersDisposed]
  159. public void Dispose_Toplevel_IsOverlappedContainer_False_With_Begin_End ()
  160. {
  161. Application.Init (new FakeDriver ());
  162. var top = new Toplevel ();
  163. RunState rs = Application.Begin (top);
  164. Application.End (rs);
  165. Application.Shutdown ();
  166. #if DEBUG_IDISPOSABLE
  167. Assert.Empty (Responder.Instances);
  168. #endif
  169. }
  170. [Fact]
  171. [TestRespondersDisposed]
  172. public void Dispose_Toplevel_IsOverlappedContainer_True_With_Begin ()
  173. {
  174. Application.Init (new FakeDriver ());
  175. var overlapped = new Toplevel { IsOverlappedContainer = true };
  176. RunState rs = Application.Begin (overlapped);
  177. Application.End (rs);
  178. Application.Shutdown ();
  179. }
  180. [Fact]
  181. [AutoInitShutdown]
  182. public void IsOverlappedChild_Testing ()
  183. {
  184. var overlapped = new Overlapped ();
  185. var c1 = new Toplevel ();
  186. var c2 = new Window ();
  187. var c3 = new Window ();
  188. var d = new Dialog ();
  189. Application.Iteration += (s, a) =>
  190. {
  191. Assert.False (overlapped.IsOverlapped);
  192. Assert.True (c1.IsOverlapped);
  193. Assert.True (c2.IsOverlapped);
  194. Assert.True (c3.IsOverlapped);
  195. Assert.False (d.IsOverlapped);
  196. overlapped.RequestStop ();
  197. };
  198. Application.Run (overlapped);
  199. }
  200. [Fact]
  201. [AutoInitShutdown]
  202. public void
  203. Modal_Toplevel_Can_Open_Another_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  204. {
  205. var overlapped = new Overlapped ();
  206. var c1 = new Toplevel ();
  207. var c2 = new Window ();
  208. var c3 = new Window ();
  209. var d1 = new Dialog ();
  210. var d2 = new Dialog ();
  211. // OverlappedChild = c1, c2, c3 = 3
  212. // d1, d2 = 2
  213. var iterations = 5;
  214. overlapped.Ready += (s, e) =>
  215. {
  216. Assert.Empty (Application.OverlappedChildren);
  217. Application.Run (c1);
  218. };
  219. c1.Ready += (s, e) =>
  220. {
  221. Assert.Single (Application.OverlappedChildren);
  222. Application.Run (c2);
  223. };
  224. c2.Ready += (s, e) =>
  225. {
  226. Assert.Equal (2, Application.OverlappedChildren.Count);
  227. Application.Run (c3);
  228. };
  229. c3.Ready += (s, e) =>
  230. {
  231. Assert.Equal (3, Application.OverlappedChildren.Count);
  232. Application.Run (d1);
  233. };
  234. d1.Ready += (s, e) =>
  235. {
  236. Assert.Equal (3, Application.OverlappedChildren.Count);
  237. Application.Run (d2);
  238. };
  239. d2.Ready += (s, e) =>
  240. {
  241. Assert.Equal (3, Application.OverlappedChildren.Count);
  242. Assert.True (Application.Current == d2);
  243. Assert.True (Application.Current.Running);
  244. // Trying to close the Dialog1
  245. d1.RequestStop ();
  246. };
  247. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  248. d1.Closed += (s, e) =>
  249. {
  250. Assert.True (Application.Current == d1);
  251. Assert.False (Application.Current.Running);
  252. overlapped.RequestStop ();
  253. };
  254. Application.Iteration += (s, a) =>
  255. {
  256. if (iterations == 5)
  257. {
  258. // The Dialog2 still is the current top and we can't request stop to OverlappedContainer
  259. // because Dialog2 and Dialog1 must be closed first.
  260. // Dialog2 will be closed in this iteration.
  261. Assert.True (Application.Current == d2);
  262. Assert.False (Application.Current.Running);
  263. Assert.False (d1.Running);
  264. }
  265. else if (iterations == 4)
  266. {
  267. // Dialog1 will be closed in this iteration.
  268. Assert.True (Application.Current == d1);
  269. Assert.False (Application.Current.Running);
  270. }
  271. else
  272. {
  273. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  274. for (var i = 0; i < iterations; i++)
  275. {
  276. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  277. }
  278. }
  279. iterations--;
  280. };
  281. Application.Run (overlapped);
  282. Assert.Null (Application.OverlappedChildren);
  283. Assert.Null (Application.OverlappedTop);
  284. Assert.NotNull (Application.Top);
  285. }
  286. [Fact]
  287. [AutoInitShutdown]
  288. public void
  289. Modal_Toplevel_Can_Open_Another_Not_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  290. {
  291. var overlapped = new Overlapped ();
  292. var c1 = new Toplevel ();
  293. var c2 = new Window ();
  294. var c3 = new Window ();
  295. var d1 = new Dialog ();
  296. var c4 = new Toplevel ();
  297. // OverlappedChild = c1, c2, c3, c4 = 4
  298. // d1 = 1
  299. var iterations = 5;
  300. overlapped.Ready += (s, e) =>
  301. {
  302. Assert.Empty (Application.OverlappedChildren);
  303. Application.Run (c1);
  304. };
  305. c1.Ready += (s, e) =>
  306. {
  307. Assert.Single (Application.OverlappedChildren);
  308. Application.Run (c2);
  309. };
  310. c2.Ready += (s, e) =>
  311. {
  312. Assert.Equal (2, Application.OverlappedChildren.Count);
  313. Application.Run (c3);
  314. };
  315. c3.Ready += (s, e) =>
  316. {
  317. Assert.Equal (3, Application.OverlappedChildren.Count);
  318. Application.Run (d1);
  319. };
  320. d1.Ready += (s, e) =>
  321. {
  322. Assert.Equal (3, Application.OverlappedChildren.Count);
  323. Application.Run (c4);
  324. };
  325. c4.Ready += (s, e) =>
  326. {
  327. Assert.Equal (4, Application.OverlappedChildren.Count);
  328. // Trying to close the Dialog1
  329. d1.RequestStop ();
  330. };
  331. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  332. d1.Closed += (s, e) => { overlapped.RequestStop (); };
  333. Application.Iteration += (s, a) =>
  334. {
  335. if (iterations == 5)
  336. {
  337. // The Dialog2 still is the current top and we can't request stop to OverlappedContainer
  338. // because Dialog2 and Dialog1 must be closed first.
  339. // Using request stop here will call the Dialog again without need
  340. Assert.True (Application.Current == d1);
  341. Assert.False (Application.Current.Running);
  342. Assert.True (c4.Running);
  343. }
  344. else
  345. {
  346. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  347. for (var i = 0; i < iterations; i++)
  348. {
  349. Assert.Equal (
  350. (iterations - i + (iterations == 4 && i == 0 ? 2 : 1)).ToString (),
  351. Application.OverlappedChildren [i].Id
  352. );
  353. }
  354. }
  355. iterations--;
  356. };
  357. Application.Run (overlapped);
  358. Assert.Null (Application.OverlappedChildren);
  359. Assert.Null (Application.OverlappedTop);
  360. Assert.NotNull (Application.Top);
  361. }
  362. [Fact]
  363. [AutoInitShutdown]
  364. public void MoveCurrent_Returns_False_If_The_Current_And_Top_Parameter_Are_Both_With_Running_Set_To_False ()
  365. {
  366. var overlapped = new Overlapped ();
  367. var c1 = new Toplevel ();
  368. var c2 = new Window ();
  369. var c3 = new Window ();
  370. // OverlappedChild = c1, c2, c3
  371. var iterations = 3;
  372. overlapped.Ready += (s, e) =>
  373. {
  374. Assert.Empty (Application.OverlappedChildren);
  375. Application.Run (c1);
  376. };
  377. c1.Ready += (s, e) =>
  378. {
  379. Assert.Single (Application.OverlappedChildren);
  380. Application.Run (c2);
  381. };
  382. c2.Ready += (s, e) =>
  383. {
  384. Assert.Equal (2, Application.OverlappedChildren.Count);
  385. Application.Run (c3);
  386. };
  387. c3.Ready += (s, e) =>
  388. {
  389. Assert.Equal (3, Application.OverlappedChildren.Count);
  390. c3.RequestStop ();
  391. c1.RequestStop ();
  392. };
  393. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  394. c1.Closed += (s, e) => { overlapped.RequestStop (); };
  395. Application.Iteration += (s, a) =>
  396. {
  397. if (iterations == 3)
  398. {
  399. // The Current still is c3 because Current.Running is false.
  400. Assert.True (Application.Current == c3);
  401. Assert.False (Application.Current.Running);
  402. // But the Children order were reorder by Running = false
  403. Assert.True (Application.OverlappedChildren [0] == c3);
  404. Assert.True (Application.OverlappedChildren [1] == c1);
  405. Assert.True (Application.OverlappedChildren [^1] == c2);
  406. }
  407. else if (iterations == 2)
  408. {
  409. // The Current is c1 and Current.Running is false.
  410. Assert.True (Application.Current == c1);
  411. Assert.False (Application.Current.Running);
  412. Assert.True (Application.OverlappedChildren [0] == c1);
  413. Assert.True (Application.OverlappedChildren [^1] == c2);
  414. }
  415. else if (iterations == 1)
  416. {
  417. // The Current is c2 and Current.Running is false.
  418. Assert.True (Application.Current == c2);
  419. Assert.False (Application.Current.Running);
  420. Assert.True (Application.OverlappedChildren [^1] == c2);
  421. }
  422. else
  423. {
  424. // The Current is overlapped.
  425. Assert.True (Application.Current == overlapped);
  426. Assert.Empty (Application.OverlappedChildren);
  427. }
  428. iterations--;
  429. };
  430. Application.Run (overlapped);
  431. Assert.Null (Application.OverlappedChildren);
  432. Assert.Null (Application.OverlappedTop);
  433. Assert.NotNull (Application.Top);
  434. }
  435. [Fact]
  436. public void MoveToOverlappedChild_Throw_NullReferenceException_Passing_Null_Parameter ()
  437. {
  438. Assert.Throws<NullReferenceException> (delegate { Application.MoveToOverlappedChild (null); });
  439. }
  440. [Fact]
  441. [AutoInitShutdown]
  442. public void OverlappedContainer_Open_And_Close_Modal_And_Open_Not_Modal_Toplevels_Randomly ()
  443. {
  444. var overlapped = new Overlapped ();
  445. var logger = new Toplevel ();
  446. var iterations = 1; // The logger
  447. var running = true;
  448. var stageCompleted = true;
  449. var allStageClosed = false;
  450. var overlappedRequestStop = false;
  451. overlapped.Ready += (s, e) =>
  452. {
  453. Assert.Empty (Application.OverlappedChildren);
  454. Application.Run (logger);
  455. };
  456. logger.Ready += (s, e) => Assert.Single (Application.OverlappedChildren);
  457. Application.Iteration += (s, a) =>
  458. {
  459. if (stageCompleted && running)
  460. {
  461. stageCompleted = false;
  462. var stage = new Window { Modal = true };
  463. stage.Ready += (s, e) =>
  464. {
  465. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  466. stage.RequestStop ();
  467. };
  468. stage.Closed += (_, _) =>
  469. {
  470. if (iterations == 11)
  471. {
  472. allStageClosed = true;
  473. }
  474. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  475. if (running)
  476. {
  477. stageCompleted = true;
  478. var rpt = new Window ();
  479. rpt.Ready += (s, e) =>
  480. {
  481. iterations++;
  482. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  483. };
  484. Application.Run (rpt);
  485. }
  486. };
  487. Application.Run (stage);
  488. }
  489. else if (iterations == 11 && running)
  490. {
  491. running = false;
  492. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  493. }
  494. else if (!overlappedRequestStop && running && !allStageClosed)
  495. {
  496. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  497. }
  498. else if (!overlappedRequestStop && !running && allStageClosed)
  499. {
  500. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  501. overlappedRequestStop = true;
  502. overlapped.RequestStop ();
  503. }
  504. else
  505. {
  506. Assert.Empty (Application.OverlappedChildren);
  507. }
  508. };
  509. Application.Run (overlapped);
  510. Assert.Null (Application.OverlappedChildren);
  511. Assert.Null (Application.OverlappedTop);
  512. Assert.NotNull (Application.Top);
  513. }
  514. [Fact]
  515. [AutoInitShutdown]
  516. public void OverlappedContainer_Throws_If_More_Than_One ()
  517. {
  518. var overlapped = new Overlapped ();
  519. var overlapped2 = new Overlapped ();
  520. overlapped.Ready += (s, e) =>
  521. {
  522. Assert.Throws<InvalidOperationException> (() => Application.Run (overlapped2));
  523. overlapped.RequestStop ();
  524. };
  525. Application.Run (overlapped);
  526. }
  527. [Fact]
  528. [AutoInitShutdown]
  529. public void OverlappedContainer_With_Application_RequestStop_OverlappedTop_With_Params ()
  530. {
  531. var overlapped = new Overlapped ();
  532. var c1 = new Toplevel ();
  533. var c2 = new Window ();
  534. var c3 = new Window ();
  535. var d = new Dialog ();
  536. // OverlappedChild = c1, c2, c3
  537. // d1 = 1
  538. var iterations = 4;
  539. overlapped.Ready += (s, e) =>
  540. {
  541. Assert.Empty (Application.OverlappedChildren);
  542. Application.Run (c1);
  543. };
  544. c1.Ready += (s, e) =>
  545. {
  546. Assert.Single (Application.OverlappedChildren);
  547. Application.Run (c2);
  548. };
  549. c2.Ready += (s, e) =>
  550. {
  551. Assert.Equal (2, Application.OverlappedChildren.Count);
  552. Application.Run (c3);
  553. };
  554. c3.Ready += (s, e) =>
  555. {
  556. Assert.Equal (3, Application.OverlappedChildren.Count);
  557. Application.Run (d);
  558. };
  559. // Also easy because the Overlapped Container handles all at once
  560. d.Ready += (s, e) =>
  561. {
  562. Assert.Equal (3, Application.OverlappedChildren.Count);
  563. // This will not close the OverlappedContainer because d is a modal Toplevel
  564. Application.RequestStop (overlapped);
  565. };
  566. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  567. d.Closed += (s, e) => Application.RequestStop (overlapped);
  568. Application.Iteration += (s, a) =>
  569. {
  570. if (iterations == 4)
  571. {
  572. // The Dialog was not closed before and will be closed now.
  573. Assert.True (Application.Current == d);
  574. Assert.False (d.Running);
  575. }
  576. else
  577. {
  578. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  579. for (var i = 0; i < iterations; i++)
  580. {
  581. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  582. }
  583. }
  584. iterations--;
  585. };
  586. Application.Run (overlapped);
  587. Assert.Null (Application.OverlappedChildren);
  588. Assert.Null (Application.OverlappedTop);
  589. Assert.NotNull (Application.Top);
  590. }
  591. [Fact]
  592. [AutoInitShutdown]
  593. public void OverlappedContainer_With_Application_RequestStop_OverlappedTop_Without_Params ()
  594. {
  595. var overlapped = new Overlapped ();
  596. var c1 = new Toplevel ();
  597. var c2 = new Window ();
  598. var c3 = new Window ();
  599. var d = new Dialog ();
  600. // OverlappedChild = c1, c2, c3 = 3
  601. // d1 = 1
  602. var iterations = 4;
  603. overlapped.Ready += (s, e) =>
  604. {
  605. Assert.Empty (Application.OverlappedChildren);
  606. Application.Run (c1);
  607. };
  608. c1.Ready += (s, e) =>
  609. {
  610. Assert.Single (Application.OverlappedChildren);
  611. Application.Run (c2);
  612. };
  613. c2.Ready += (s, e) =>
  614. {
  615. Assert.Equal (2, Application.OverlappedChildren.Count);
  616. Application.Run (c3);
  617. };
  618. c3.Ready += (s, e) =>
  619. {
  620. Assert.Equal (3, Application.OverlappedChildren.Count);
  621. Application.Run (d);
  622. };
  623. //More harder because it's sequential.
  624. d.Ready += (s, e) =>
  625. {
  626. Assert.Equal (3, Application.OverlappedChildren.Count);
  627. // Close the Dialog
  628. Application.RequestStop ();
  629. };
  630. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  631. d.Closed += (s, e) => Application.RequestStop (overlapped);
  632. Application.Iteration += (s, a) =>
  633. {
  634. if (iterations == 4)
  635. {
  636. // The Dialog still is the current top and we can't request stop to OverlappedContainer
  637. // because we are not using parameter calls.
  638. Assert.True (Application.Current == d);
  639. Assert.False (d.Running);
  640. }
  641. else
  642. {
  643. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  644. for (var i = 0; i < iterations; i++)
  645. {
  646. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  647. }
  648. }
  649. iterations--;
  650. };
  651. Application.Run (overlapped);
  652. Assert.Null (Application.OverlappedChildren);
  653. Assert.Null (Application.OverlappedTop);
  654. Assert.NotNull (Application.Top);
  655. }
  656. [Fact]
  657. [AutoInitShutdown]
  658. public void OverlappedContainer_With_Toplevel_RequestStop_Balanced ()
  659. {
  660. var overlapped = new Overlapped ();
  661. var c1 = new Toplevel ();
  662. var c2 = new Window ();
  663. var c3 = new Window ();
  664. var d = new Dialog ();
  665. // OverlappedChild = c1, c2, c3
  666. // d1 = 1
  667. var iterations = 4;
  668. overlapped.Ready += (s, e) =>
  669. {
  670. Assert.Empty (Application.OverlappedChildren);
  671. Application.Run (c1);
  672. };
  673. c1.Ready += (s, e) =>
  674. {
  675. Assert.Single (Application.OverlappedChildren);
  676. Application.Run (c2);
  677. };
  678. c2.Ready += (s, e) =>
  679. {
  680. Assert.Equal (2, Application.OverlappedChildren.Count);
  681. Application.Run (c3);
  682. };
  683. c3.Ready += (s, e) =>
  684. {
  685. Assert.Equal (3, Application.OverlappedChildren.Count);
  686. Application.Run (d);
  687. };
  688. // More easy because the Overlapped Container handles all at once
  689. d.Ready += (s, e) =>
  690. {
  691. Assert.Equal (3, Application.OverlappedChildren.Count);
  692. // This will not close the OverlappedContainer because d is a modal Toplevel and will be closed.
  693. overlapped.RequestStop ();
  694. };
  695. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  696. d.Closed += (s, e) => { overlapped.RequestStop (); };
  697. Application.Iteration += (s, a) =>
  698. {
  699. if (iterations == 4)
  700. {
  701. // The Dialog was not closed before and will be closed now.
  702. Assert.True (Application.Current == d);
  703. Assert.False (d.Running);
  704. }
  705. else
  706. {
  707. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  708. for (var i = 0; i < iterations; i++)
  709. {
  710. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  711. }
  712. }
  713. iterations--;
  714. };
  715. Application.Run (overlapped);
  716. Assert.Null (Application.OverlappedChildren);
  717. Assert.Null (Application.OverlappedTop);
  718. Assert.NotNull (Application.Top);
  719. }
  720. [Fact]
  721. [AutoInitShutdown]
  722. public void Visible_False_Does_Not_Clear ()
  723. {
  724. var overlapped = new Overlapped ();
  725. var win1 = new Window { Width = 5, Height = 5, Visible = false };
  726. var win2 = new Window { X = 1, Y = 1, Width = 5, Height = 5 };
  727. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  728. RunState rs = Application.Begin (overlapped);
  729. Application.Begin (win1);
  730. Application.Begin (win2);
  731. Assert.Equal (win2, Application.Current);
  732. var firstIteration = false;
  733. Application.RunIteration (ref rs, ref firstIteration);
  734. TestHelpers.AssertDriverContentsWithFrameAre (
  735. @"
  736. ┌───┐
  737. │ │
  738. │ │
  739. │ │
  740. └───┘",
  741. _output
  742. );
  743. Attribute [] attributes =
  744. {
  745. // 0
  746. Colors.ColorSchemes ["TopLevel"].Normal,
  747. // 1
  748. Colors.ColorSchemes ["Base"].Normal
  749. };
  750. TestHelpers.AssertDriverAttributesAre (
  751. @"
  752. 0000000000
  753. 0111110000
  754. 0111110000
  755. 0111110000
  756. 0111110000
  757. 0111110000
  758. 0000000000
  759. 0000000000
  760. 0000000000
  761. 0000000000",
  762. null,
  763. attributes
  764. );
  765. Application.OnMouseEvent (
  766. new MouseEventEventArgs (
  767. new MouseEvent { X = 1, Y = 1, Flags = MouseFlags.Button1Pressed }
  768. )
  769. );
  770. Assert.Equal (win2.Border, Application.MouseGrabView);
  771. Application.OnMouseEvent (
  772. new MouseEventEventArgs (
  773. new MouseEvent
  774. {
  775. X = 2,
  776. Y = 2,
  777. Flags = MouseFlags.Button1Pressed
  778. | MouseFlags.ReportMousePosition
  779. }
  780. )
  781. );
  782. // Need to fool MainLoop into thinking it's running
  783. Application.MainLoop.Running = true;
  784. Application.RunIteration (ref rs, ref firstIteration);
  785. TestHelpers.AssertDriverContentsWithFrameAre (
  786. @"
  787. ┌───┐
  788. │ │
  789. │ │
  790. │ │
  791. └───┘",
  792. _output
  793. );
  794. TestHelpers.AssertDriverAttributesAre (
  795. @"
  796. 0000000000
  797. 0000000000
  798. 0011111000
  799. 0011111000
  800. 0011111000
  801. 0011111000
  802. 0011111000
  803. 0000000000
  804. 0000000000
  805. 0000000000",
  806. null,
  807. attributes
  808. );
  809. Application.Shutdown ();
  810. }
  811. private class Overlapped : Toplevel
  812. {
  813. public Overlapped () { IsOverlappedContainer = true; }
  814. }
  815. }