OverlappedTests.cs 38 KB

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