OverlappedTests.cs 38 KB

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