OverlappedTests.cs 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. using System.Threading;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class OverlappedTests
  5. {
  6. private readonly ITestOutputHelper _output;
  7. public OverlappedTests (ITestOutputHelper output)
  8. {
  9. _output = output;
  10. #if DEBUG_IDISPOSABLE
  11. Responder.Instances.Clear ();
  12. RunState.Instances.Clear ();
  13. #endif
  14. }
  15. [Fact]
  16. [AutoInitShutdown]
  17. public void AllChildClosed_Event_Test ()
  18. {
  19. var overlapped = new Overlapped ();
  20. var c1 = new Toplevel ();
  21. var c2 = new Window ();
  22. var c3 = new Window ();
  23. // OverlappedChild = c1, c2, c3
  24. var iterations = 3;
  25. overlapped.Ready += (s, e) =>
  26. {
  27. Assert.Empty (Application.OverlappedChildren);
  28. Application.Run (c1);
  29. };
  30. c1.Ready += (s, e) =>
  31. {
  32. Assert.Single (Application.OverlappedChildren);
  33. Application.Run (c2);
  34. };
  35. c2.Ready += (s, e) =>
  36. {
  37. Assert.Equal (2, Application.OverlappedChildren.Count);
  38. Application.Run (c3);
  39. };
  40. c3.Ready += (s, e) =>
  41. {
  42. Assert.Equal (3, Application.OverlappedChildren.Count);
  43. c3.RequestStop ();
  44. c2.RequestStop ();
  45. c1.RequestStop ();
  46. };
  47. // Now this will close the OverlappedContainer when all OverlappedChildren was closed
  48. overlapped.AllChildClosed += (s, e) => { overlapped.RequestStop (); };
  49. Application.Iteration += (s, a) =>
  50. {
  51. if (iterations == 3)
  52. {
  53. // The Current still is c3 because Current.Running is false.
  54. Assert.True (Application.Current == c3);
  55. Assert.False (Application.Current.Running);
  56. // But the Children order were reorder by Running = false
  57. Assert.True (Application.OverlappedChildren [0] == c3);
  58. Assert.True (Application.OverlappedChildren [1] == c2);
  59. Assert.True (Application.OverlappedChildren [^1] == c1);
  60. }
  61. else if (iterations == 2)
  62. {
  63. // The Current is c2 and Current.Running is false.
  64. Assert.True (Application.Current == c2);
  65. Assert.False (Application.Current.Running);
  66. Assert.True (Application.OverlappedChildren [0] == c2);
  67. Assert.True (Application.OverlappedChildren [^1] == c1);
  68. }
  69. else if (iterations == 1)
  70. {
  71. // The Current is c1 and Current.Running is false.
  72. Assert.True (Application.Current == c1);
  73. Assert.False (Application.Current.Running);
  74. Assert.True (Application.OverlappedChildren [^1] == c1);
  75. }
  76. else
  77. {
  78. // The Current is overlapped.
  79. Assert.True (Application.Current == overlapped);
  80. Assert.False (Application.Current.Running);
  81. Assert.Empty (Application.OverlappedChildren);
  82. }
  83. iterations--;
  84. };
  85. Application.Run (overlapped);
  86. Assert.Empty (Application.OverlappedChildren);
  87. Assert.NotNull (Application.OverlappedTop);
  88. Assert.NotNull (Application.Top);
  89. overlapped.Dispose ();
  90. }
  91. [Fact]
  92. [AutoInitShutdown]
  93. public void Application_RequestStop_With_Params_On_A_Not_OverlappedContainer_Always_Use_Application_Current ()
  94. {
  95. var top1 = new Toplevel ();
  96. var top2 = new Toplevel ();
  97. var top3 = new Window ();
  98. var top4 = new Window ();
  99. var d = new Dialog ();
  100. // top1, top2, top3, d1 = 4
  101. var iterations = 4;
  102. top1.Ready += (s, e) =>
  103. {
  104. Assert.Null (Application.OverlappedChildren);
  105. Application.Run (top2);
  106. };
  107. top2.Ready += (s, e) =>
  108. {
  109. Assert.Null (Application.OverlappedChildren);
  110. Application.Run (top3);
  111. };
  112. top3.Ready += (s, e) =>
  113. {
  114. Assert.Null (Application.OverlappedChildren);
  115. Application.Run (top4);
  116. };
  117. top4.Ready += (s, e) =>
  118. {
  119. Assert.Null (Application.OverlappedChildren);
  120. Application.Run (d);
  121. };
  122. d.Ready += (s, e) =>
  123. {
  124. Assert.Null (Application.OverlappedChildren);
  125. // This will close the d because on a not OverlappedContainer the Application.Current it always used.
  126. Application.RequestStop (top1);
  127. Assert.True (Application.Current == d);
  128. };
  129. d.Closed += (s, e) => Application.RequestStop (top1);
  130. Application.Iteration += (s, a) =>
  131. {
  132. Assert.Null (Application.OverlappedChildren);
  133. if (iterations == 4)
  134. {
  135. Assert.True (Application.Current == d);
  136. }
  137. else if (iterations == 3)
  138. {
  139. Assert.True (Application.Current == top4);
  140. }
  141. else if (iterations == 2)
  142. {
  143. Assert.True (Application.Current == top3);
  144. }
  145. else if (iterations == 1)
  146. {
  147. Assert.True (Application.Current == top2);
  148. }
  149. else
  150. {
  151. Assert.True (Application.Current == top1);
  152. }
  153. Application.RequestStop (top1);
  154. iterations--;
  155. };
  156. Application.Run (top1);
  157. Assert.Null (Application.OverlappedChildren);
  158. top1.Dispose ();
  159. }
  160. [Fact]
  161. [TestRespondersDisposed]
  162. public void Dispose_Toplevel_IsOverlappedContainer_False_With_Begin_End ()
  163. {
  164. Application.Init (new FakeDriver ());
  165. var top = new Toplevel ();
  166. RunState rs = Application.Begin (top);
  167. Application.End (rs);
  168. top.Dispose ();
  169. Application.Shutdown ();
  170. #if DEBUG_IDISPOSABLE
  171. Assert.Empty (Responder.Instances);
  172. #endif
  173. }
  174. [Fact]
  175. [TestRespondersDisposed]
  176. public void Dispose_Toplevel_IsOverlappedContainer_True_With_Begin ()
  177. {
  178. Application.Init (new FakeDriver ());
  179. var overlapped = new Toplevel { IsOverlappedContainer = true };
  180. RunState rs = Application.Begin (overlapped);
  181. Application.End (rs);
  182. overlapped.Dispose ();
  183. Application.Shutdown ();
  184. }
  185. [Fact]
  186. [AutoInitShutdown]
  187. public void IsOverlappedChild_Testing ()
  188. {
  189. var overlapped = new Overlapped ();
  190. var c1 = new Toplevel ();
  191. var c2 = new Window ();
  192. var c3 = new Window ();
  193. var d = new Dialog ();
  194. Application.Iteration += (s, a) =>
  195. {
  196. Assert.False (overlapped.IsOverlapped);
  197. Assert.True (c1.IsOverlapped);
  198. Assert.True (c2.IsOverlapped);
  199. Assert.True (c3.IsOverlapped);
  200. Assert.False (d.IsOverlapped);
  201. overlapped.RequestStop ();
  202. };
  203. Application.Run (overlapped);
  204. overlapped.Dispose ();
  205. }
  206. [Fact]
  207. [AutoInitShutdown]
  208. public void
  209. Modal_Toplevel_Can_Open_Another_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  210. {
  211. var overlapped = new Overlapped ();
  212. var c1 = new Toplevel ();
  213. var c2 = new Window ();
  214. var c3 = new Window ();
  215. var d1 = new Dialog ();
  216. var d2 = new Dialog ();
  217. // OverlappedChild = c1, c2, c3 = 3
  218. // d1, d2 = 2
  219. var iterations = 5;
  220. overlapped.Ready += (s, e) =>
  221. {
  222. Assert.Empty (Application.OverlappedChildren);
  223. Application.Run (c1);
  224. };
  225. c1.Ready += (s, e) =>
  226. {
  227. Assert.Single (Application.OverlappedChildren);
  228. Application.Run (c2);
  229. };
  230. c2.Ready += (s, e) =>
  231. {
  232. Assert.Equal (2, Application.OverlappedChildren.Count);
  233. Application.Run (c3);
  234. };
  235. c3.Ready += (s, e) =>
  236. {
  237. Assert.Equal (3, Application.OverlappedChildren.Count);
  238. Application.Run (d1);
  239. };
  240. d1.Ready += (s, e) =>
  241. {
  242. Assert.Equal (3, Application.OverlappedChildren.Count);
  243. Application.Run (d2);
  244. };
  245. d2.Ready += (s, e) =>
  246. {
  247. Assert.Equal (3, Application.OverlappedChildren.Count);
  248. Assert.True (Application.Current == d2);
  249. Assert.True (Application.Current.Running);
  250. // Trying to close the Dialog1
  251. d1.RequestStop ();
  252. };
  253. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  254. d1.Closed += (s, e) =>
  255. {
  256. Assert.True (Application.Current == d1);
  257. Assert.False (Application.Current.Running);
  258. overlapped.RequestStop ();
  259. };
  260. Application.Iteration += (s, a) =>
  261. {
  262. if (iterations == 5)
  263. {
  264. // The Dialog2 still is the current top and we can't request stop to OverlappedContainer
  265. // because Dialog2 and Dialog1 must be closed first.
  266. // Dialog2 will be closed in this iteration.
  267. Assert.True (Application.Current == d2);
  268. Assert.False (Application.Current.Running);
  269. Assert.False (d1.Running);
  270. }
  271. else if (iterations == 4)
  272. {
  273. // Dialog1 will be closed in this iteration.
  274. Assert.True (Application.Current == d1);
  275. Assert.False (Application.Current.Running);
  276. }
  277. else
  278. {
  279. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  280. for (var i = 0; i < iterations; i++)
  281. {
  282. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  283. }
  284. }
  285. iterations--;
  286. };
  287. Application.Run (overlapped);
  288. Assert.Empty (Application.OverlappedChildren);
  289. Assert.NotNull (Application.OverlappedTop);
  290. Assert.NotNull (Application.Top);
  291. overlapped.Dispose ();
  292. }
  293. [Fact]
  294. [AutoInitShutdown]
  295. public void
  296. Modal_Toplevel_Can_Open_Another_Not_Modal_Toplevel_But_RequestStop_To_The_Caller_Also_Sets_Current_Running_To_False_Too ()
  297. {
  298. var overlapped = new Overlapped ();
  299. var c1 = new Toplevel ();
  300. var c2 = new Window ();
  301. var c3 = new Window ();
  302. var d1 = new Dialog ();
  303. var c4 = new Toplevel ();
  304. // OverlappedChild = c1, c2, c3, c4 = 4
  305. // d1 = 1
  306. var iterations = 5;
  307. overlapped.Ready += (s, e) =>
  308. {
  309. Assert.Empty (Application.OverlappedChildren);
  310. Application.Run (c1);
  311. };
  312. c1.Ready += (s, e) =>
  313. {
  314. Assert.Single (Application.OverlappedChildren);
  315. Application.Run (c2);
  316. };
  317. c2.Ready += (s, e) =>
  318. {
  319. Assert.Equal (2, Application.OverlappedChildren.Count);
  320. Application.Run (c3);
  321. };
  322. c3.Ready += (s, e) =>
  323. {
  324. Assert.Equal (3, Application.OverlappedChildren.Count);
  325. Application.Run (d1);
  326. };
  327. d1.Ready += (s, e) =>
  328. {
  329. Assert.Equal (3, Application.OverlappedChildren.Count);
  330. Application.Run (c4);
  331. };
  332. c4.Ready += (s, e) =>
  333. {
  334. Assert.Equal (4, Application.OverlappedChildren.Count);
  335. // Trying to close the Dialog1
  336. d1.RequestStop ();
  337. };
  338. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  339. d1.Closed += (s, e) => { overlapped.RequestStop (); };
  340. Application.Iteration += (s, a) =>
  341. {
  342. if (iterations == 5)
  343. {
  344. // The Dialog2 still is the current top and we can't request stop to OverlappedContainer
  345. // because Dialog2 and Dialog1 must be closed first.
  346. // Using request stop here will call the Dialog again without need
  347. Assert.True (Application.Current == d1);
  348. Assert.False (Application.Current.Running);
  349. Assert.True (c4.Running);
  350. }
  351. else
  352. {
  353. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  354. for (var i = 0; i < iterations; i++)
  355. {
  356. Assert.Equal (
  357. (iterations - i + (iterations == 4 && i == 0 ? 2 : 1)).ToString (),
  358. Application.OverlappedChildren [i].Id
  359. );
  360. }
  361. }
  362. iterations--;
  363. };
  364. Application.Run (overlapped);
  365. Assert.Empty (Application.OverlappedChildren);
  366. Assert.NotNull (Application.OverlappedTop);
  367. Assert.NotNull (Application.Top);
  368. overlapped.Dispose ();
  369. }
  370. [Fact]
  371. [AutoInitShutdown]
  372. public void MoveCurrent_Returns_False_If_The_Current_And_Top_Parameter_Are_Both_With_Running_Set_To_False ()
  373. {
  374. var overlapped = new Overlapped ();
  375. var c1 = new Toplevel ();
  376. var c2 = new Window ();
  377. var c3 = new Window ();
  378. // OverlappedChild = c1, c2, c3
  379. var iterations = 3;
  380. overlapped.Ready += (s, e) =>
  381. {
  382. Assert.Empty (Application.OverlappedChildren);
  383. Application.Run (c1);
  384. };
  385. c1.Ready += (s, e) =>
  386. {
  387. Assert.Single (Application.OverlappedChildren);
  388. Application.Run (c2);
  389. };
  390. c2.Ready += (s, e) =>
  391. {
  392. Assert.Equal (2, Application.OverlappedChildren.Count);
  393. Application.Run (c3);
  394. };
  395. c3.Ready += (s, e) =>
  396. {
  397. Assert.Equal (3, Application.OverlappedChildren.Count);
  398. c3.RequestStop ();
  399. c1.RequestStop ();
  400. };
  401. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  402. c1.Closed += (s, e) => { overlapped.RequestStop (); };
  403. Application.Iteration += (s, a) =>
  404. {
  405. if (iterations == 3)
  406. {
  407. // The Current still is c3 because Current.Running is false.
  408. Assert.True (Application.Current == c3);
  409. Assert.False (Application.Current.Running);
  410. // But the Children order were reorder by Running = false
  411. Assert.True (Application.OverlappedChildren [0] == c3);
  412. Assert.True (Application.OverlappedChildren [1] == c1);
  413. Assert.True (Application.OverlappedChildren [^1] == c2);
  414. }
  415. else if (iterations == 2)
  416. {
  417. // The Current is c1 and Current.Running is false.
  418. Assert.True (Application.Current == c1);
  419. Assert.False (Application.Current.Running);
  420. Assert.True (Application.OverlappedChildren [0] == c1);
  421. Assert.True (Application.OverlappedChildren [^1] == c2);
  422. }
  423. else if (iterations == 1)
  424. {
  425. // The Current is c2 and Current.Running is false.
  426. Assert.True (Application.Current == c2);
  427. Assert.False (Application.Current.Running);
  428. Assert.True (Application.OverlappedChildren [^1] == c2);
  429. }
  430. else
  431. {
  432. // The Current is overlapped.
  433. Assert.True (Application.Current == overlapped);
  434. Assert.Empty (Application.OverlappedChildren);
  435. }
  436. iterations--;
  437. };
  438. Application.Run (overlapped);
  439. Assert.Empty (Application.OverlappedChildren);
  440. Assert.NotNull (Application.OverlappedTop);
  441. Assert.NotNull (Application.Top);
  442. overlapped.Dispose ();
  443. }
  444. [Fact]
  445. public void MoveToOverlappedChild_Throw_NullReferenceException_Passing_Null_Parameter ()
  446. {
  447. Assert.Throws<NullReferenceException> (delegate { Application.MoveToOverlappedChild (null); });
  448. }
  449. [Fact]
  450. [AutoInitShutdown]
  451. public void OverlappedContainer_Open_And_Close_Modal_And_Open_Not_Modal_Toplevels_Randomly ()
  452. {
  453. var overlapped = new Overlapped ();
  454. var logger = new Toplevel ();
  455. var iterations = 1; // The logger
  456. var running = true;
  457. var stageCompleted = true;
  458. var allStageClosed = false;
  459. var overlappedRequestStop = false;
  460. overlapped.Ready += (s, e) =>
  461. {
  462. Assert.Empty (Application.OverlappedChildren);
  463. Application.Run (logger);
  464. };
  465. logger.Ready += (s, e) => Assert.Single (Application.OverlappedChildren);
  466. Application.Iteration += (s, a) =>
  467. {
  468. if (stageCompleted && running)
  469. {
  470. stageCompleted = false;
  471. var stage = new Window { Modal = true };
  472. stage.Ready += (s, e) =>
  473. {
  474. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  475. stage.RequestStop ();
  476. };
  477. stage.Closed += (_, _) =>
  478. {
  479. if (iterations == 11)
  480. {
  481. allStageClosed = true;
  482. }
  483. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  484. if (running)
  485. {
  486. stageCompleted = true;
  487. var rpt = new Window ();
  488. rpt.Ready += (s, e) =>
  489. {
  490. iterations++;
  491. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  492. };
  493. Application.Run (rpt);
  494. }
  495. };
  496. Application.Run (stage);
  497. }
  498. else if (iterations == 11 && running)
  499. {
  500. running = false;
  501. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  502. }
  503. else if (!overlappedRequestStop && running && !allStageClosed)
  504. {
  505. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  506. }
  507. else if (!overlappedRequestStop && !running && allStageClosed)
  508. {
  509. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  510. overlappedRequestStop = true;
  511. overlapped.RequestStop ();
  512. }
  513. else
  514. {
  515. Assert.Empty (Application.OverlappedChildren);
  516. }
  517. };
  518. Application.Run (overlapped);
  519. Assert.Empty (Application.OverlappedChildren);
  520. Assert.NotNull (Application.OverlappedTop);
  521. Assert.NotNull (Application.Top);
  522. overlapped.Dispose ();
  523. }
  524. [Fact]
  525. [AutoInitShutdown]
  526. public void OverlappedContainer_Throws_If_More_Than_One ()
  527. {
  528. var overlapped = new Overlapped ();
  529. var overlapped2 = new Overlapped ();
  530. overlapped.Ready += (s, e) =>
  531. {
  532. Assert.Throws<InvalidOperationException> (() => Application.Run (overlapped2));
  533. overlapped.RequestStop ();
  534. };
  535. Application.Run (overlapped);
  536. overlapped.Dispose ();
  537. }
  538. [Fact]
  539. [AutoInitShutdown]
  540. public void OverlappedContainer_With_Application_RequestStop_OverlappedTop_With_Params ()
  541. {
  542. var overlapped = new Overlapped ();
  543. var c1 = new Toplevel ();
  544. var c2 = new Window ();
  545. var c3 = new Window ();
  546. var d = new Dialog ();
  547. // OverlappedChild = c1, c2, c3
  548. // d1 = 1
  549. var iterations = 4;
  550. overlapped.Ready += (s, e) =>
  551. {
  552. Assert.Empty (Application.OverlappedChildren);
  553. Application.Run (c1);
  554. };
  555. c1.Ready += (s, e) =>
  556. {
  557. Assert.Single (Application.OverlappedChildren);
  558. Application.Run (c2);
  559. };
  560. c2.Ready += (s, e) =>
  561. {
  562. Assert.Equal (2, Application.OverlappedChildren.Count);
  563. Application.Run (c3);
  564. };
  565. c3.Ready += (s, e) =>
  566. {
  567. Assert.Equal (3, Application.OverlappedChildren.Count);
  568. Application.Run (d);
  569. };
  570. // Also easy because the Overlapped Container handles all at once
  571. d.Ready += (s, e) =>
  572. {
  573. Assert.Equal (3, Application.OverlappedChildren.Count);
  574. // This will not close the OverlappedContainer because d is a modal Toplevel
  575. Application.RequestStop (overlapped);
  576. };
  577. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  578. d.Closed += (s, e) => Application.RequestStop (overlapped);
  579. Application.Iteration += (s, a) =>
  580. {
  581. if (iterations == 4)
  582. {
  583. // The Dialog was not closed before and will be closed now.
  584. Assert.True (Application.Current == d);
  585. Assert.False (d.Running);
  586. }
  587. else
  588. {
  589. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  590. for (var i = 0; i < iterations; i++)
  591. {
  592. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  593. }
  594. }
  595. iterations--;
  596. };
  597. Application.Run (overlapped);
  598. Assert.Empty (Application.OverlappedChildren);
  599. Assert.NotNull (Application.OverlappedTop);
  600. Assert.NotNull (Application.Top);
  601. overlapped.Dispose ();
  602. }
  603. [Fact]
  604. [AutoInitShutdown]
  605. public void OverlappedContainer_With_Application_RequestStop_OverlappedTop_Without_Params ()
  606. {
  607. var overlapped = new Overlapped ();
  608. var c1 = new Toplevel ();
  609. var c2 = new Window ();
  610. var c3 = new Window ();
  611. var d = new Dialog ();
  612. // OverlappedChild = c1, c2, c3 = 3
  613. // d1 = 1
  614. var iterations = 4;
  615. overlapped.Ready += (s, e) =>
  616. {
  617. Assert.Empty (Application.OverlappedChildren);
  618. Application.Run (c1);
  619. };
  620. c1.Ready += (s, e) =>
  621. {
  622. Assert.Single (Application.OverlappedChildren);
  623. Application.Run (c2);
  624. };
  625. c2.Ready += (s, e) =>
  626. {
  627. Assert.Equal (2, Application.OverlappedChildren.Count);
  628. Application.Run (c3);
  629. };
  630. c3.Ready += (s, e) =>
  631. {
  632. Assert.Equal (3, Application.OverlappedChildren.Count);
  633. Application.Run (d);
  634. };
  635. //More harder because it's sequential.
  636. d.Ready += (s, e) =>
  637. {
  638. Assert.Equal (3, Application.OverlappedChildren.Count);
  639. // Close the Dialog
  640. Application.RequestStop ();
  641. };
  642. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  643. d.Closed += (s, e) => Application.RequestStop (overlapped);
  644. Application.Iteration += (s, a) =>
  645. {
  646. if (iterations == 4)
  647. {
  648. // The Dialog still is the current top and we can't request stop to OverlappedContainer
  649. // because we are not using parameter calls.
  650. Assert.True (Application.Current == d);
  651. Assert.False (d.Running);
  652. }
  653. else
  654. {
  655. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  656. for (var i = 0; i < iterations; i++)
  657. {
  658. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  659. }
  660. }
  661. iterations--;
  662. };
  663. Application.Run (overlapped);
  664. Assert.Empty (Application.OverlappedChildren);
  665. Assert.NotNull (Application.OverlappedTop);
  666. Assert.NotNull (Application.Top);
  667. overlapped.Dispose ();
  668. }
  669. [Fact]
  670. [AutoInitShutdown]
  671. public void OverlappedContainer_With_Toplevel_RequestStop_Balanced ()
  672. {
  673. var overlapped = new Overlapped ();
  674. var c1 = new Toplevel ();
  675. var c2 = new Window ();
  676. var c3 = new Window ();
  677. var d = new Dialog ();
  678. // OverlappedChild = c1, c2, c3
  679. // d1 = 1
  680. var iterations = 4;
  681. overlapped.Ready += (s, e) =>
  682. {
  683. Assert.Empty (Application.OverlappedChildren);
  684. Application.Run (c1);
  685. };
  686. c1.Ready += (s, e) =>
  687. {
  688. Assert.Single (Application.OverlappedChildren);
  689. Application.Run (c2);
  690. };
  691. c2.Ready += (s, e) =>
  692. {
  693. Assert.Equal (2, Application.OverlappedChildren.Count);
  694. Application.Run (c3);
  695. };
  696. c3.Ready += (s, e) =>
  697. {
  698. Assert.Equal (3, Application.OverlappedChildren.Count);
  699. Application.Run (d);
  700. };
  701. // More easy because the Overlapped Container handles all at once
  702. d.Ready += (s, e) =>
  703. {
  704. Assert.Equal (3, Application.OverlappedChildren.Count);
  705. // This will not close the OverlappedContainer because d is a modal Toplevel and will be closed.
  706. overlapped.RequestStop ();
  707. };
  708. // Now this will close the OverlappedContainer propagating through the OverlappedChildren.
  709. d.Closed += (s, e) => { overlapped.RequestStop (); };
  710. Application.Iteration += (s, a) =>
  711. {
  712. if (iterations == 4)
  713. {
  714. // The Dialog was not closed before and will be closed now.
  715. Assert.True (Application.Current == d);
  716. Assert.False (d.Running);
  717. }
  718. else
  719. {
  720. Assert.Equal (iterations, Application.OverlappedChildren.Count);
  721. for (var i = 0; i < iterations; i++)
  722. {
  723. Assert.Equal ((iterations - i + 1).ToString (), Application.OverlappedChildren [i].Id);
  724. }
  725. }
  726. iterations--;
  727. };
  728. Application.Run (overlapped);
  729. Assert.Empty (Application.OverlappedChildren);
  730. Assert.NotNull (Application.OverlappedTop);
  731. Assert.NotNull (Application.Top);
  732. overlapped.Dispose ();
  733. }
  734. [Fact]
  735. [AutoInitShutdown]
  736. public void Visible_False_Does_Not_Clear ()
  737. {
  738. var overlapped = new Overlapped ();
  739. var win1 = new Window { Width = 5, Height = 5, Visible = false };
  740. var win2 = new Window { X = 1, Y = 1, Width = 5, Height = 5 };
  741. ((FakeDriver)Application.Driver).SetBufferSize (10, 10);
  742. RunState rsOverlapped = Application.Begin (overlapped);
  743. // Need to fool MainLoop into thinking it's running
  744. Application.MainLoop.Running = true;
  745. // RunIteration must be call on each iteration because
  746. // it's using the Begin and not the Run method
  747. var firstIteration = false;
  748. Application.RunIteration (ref rsOverlapped, ref firstIteration);
  749. Assert.Equal (overlapped, rsOverlapped.Toplevel);
  750. Assert.Equal (Application.Top, rsOverlapped.Toplevel);
  751. Assert.Equal (Application.OverlappedTop, rsOverlapped.Toplevel);
  752. Assert.Equal (Application.Current, rsOverlapped.Toplevel);
  753. Assert.Equal (overlapped, Application.Current);
  754. RunState rsWin1 = Application.Begin (win1);
  755. Application.RunIteration (ref rsOverlapped, ref firstIteration);
  756. Assert.Equal (overlapped, rsOverlapped.Toplevel);
  757. Assert.Equal (Application.Top, rsOverlapped.Toplevel);
  758. Assert.Equal (Application.OverlappedTop, rsOverlapped.Toplevel);
  759. // The win1 Visible is false and cannot be set as the Current
  760. Assert.Equal (Application.Current, rsOverlapped.Toplevel);
  761. Assert.Equal (overlapped, Application.Current);
  762. Assert.Equal (win1, rsWin1.Toplevel);
  763. RunState rsWin2 = Application.Begin (win2);
  764. Application.RunIteration (ref rsOverlapped, ref firstIteration);
  765. // Here the Current and the rsOverlapped.Toplevel is now the win2
  766. // and not the original overlapped
  767. Assert.Equal (win2, rsOverlapped.Toplevel);
  768. Assert.Equal (Application.Top, overlapped);
  769. Assert.Equal (Application.OverlappedTop, overlapped);
  770. Assert.Equal (Application.Current, rsWin2.Toplevel);
  771. Assert.Equal (win2, Application.Current);
  772. Assert.Equal (win1, rsWin1.Toplevel);
  773. // Tests that rely on visuals are too fragile. If border style changes they break.
  774. // Instead we should just rely on the test above.
  775. Application.OnMouseEvent (new MouseEvent { Position = new (1, 1), Flags = MouseFlags.Button1Pressed });
  776. Assert.Equal (win2.Border, Application.MouseGrabView);
  777. Application.RunIteration (ref rsOverlapped, ref firstIteration);
  778. Assert.Equal (win2, rsOverlapped.Toplevel);
  779. Assert.Equal (Application.Top, overlapped);
  780. Assert.Equal (Application.OverlappedTop, overlapped);
  781. Assert.Equal (Application.Current, rsWin2.Toplevel);
  782. Assert.Equal (win2, Application.Current);
  783. Assert.Equal (win1, rsWin1.Toplevel);
  784. Application.OnMouseEvent (new MouseEvent
  785. {
  786. Position = new (2, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
  787. });
  788. Application.RunIteration (ref rsOverlapped, ref firstIteration);
  789. Assert.Equal (win2, rsOverlapped.Toplevel);
  790. Assert.Equal (Application.Top, overlapped);
  791. Assert.Equal (Application.OverlappedTop, overlapped);
  792. Assert.Equal (Application.Current, rsWin2.Toplevel);
  793. Assert.Equal (win2, Application.Current);
  794. Assert.Equal (win1, rsWin1.Toplevel);
  795. // Tests that rely on visuals are too fragile. If border style changes they break.
  796. // Instead we should just rely on the test above.
  797. // This will end the win2 and not the overlapped
  798. Application.End (rsOverlapped);
  799. // rsOverlapped has been disposed and Toplevel property is null
  800. // So we must use another valid RunState to iterate
  801. Application.RunIteration (ref rsWin1, ref firstIteration);
  802. #if DEBUG_IDISPOSABLE
  803. Assert.True (rsOverlapped.WasDisposed);
  804. #endif
  805. Assert.Null (rsOverlapped.Toplevel);
  806. Assert.Equal (Application.Top, overlapped);
  807. Assert.Equal (Application.OverlappedTop, overlapped);
  808. Assert.Equal (Application.Current, rsWin1.Toplevel);
  809. Assert.Equal (win1, Application.Current);
  810. Assert.Equal (win1, rsWin1.Toplevel);
  811. Application.End (rsWin1);
  812. // rsWin1 has been disposed and Toplevel property is null
  813. // So we must use another valid RunState to iterate
  814. Application.RunIteration (ref rsWin2, ref firstIteration);
  815. #if DEBUG_IDISPOSABLE
  816. Assert.True (rsOverlapped.WasDisposed);
  817. Assert.True (rsWin1.WasDisposed);
  818. #endif
  819. Assert.Null (rsOverlapped.Toplevel);
  820. Assert.Equal (Application.Top, overlapped);
  821. Assert.Equal (Application.OverlappedTop, overlapped);
  822. Assert.Equal (Application.Current, overlapped);
  823. Assert.Null (rsWin1.Toplevel);
  824. // See here that the only Toplevel that needs to End is the overlapped
  825. // which the rsWin2 now has the Toplevel set to the overlapped
  826. Assert.Equal (overlapped, rsWin2.Toplevel);
  827. Application.End (rsWin2);
  828. // There is no more RunState to iteration
  829. #if DEBUG_IDISPOSABLE
  830. Assert.True (rsOverlapped.WasDisposed);
  831. Assert.True (rsWin1.WasDisposed);
  832. Assert.True (rsWin2.WasDisposed);
  833. #endif
  834. Assert.Null (rsOverlapped.Toplevel);
  835. Assert.Equal (Application.Top, overlapped);
  836. Assert.Equal (Application.OverlappedTop, overlapped);
  837. Assert.Null (Application.Current);
  838. Assert.Null (rsWin1.Toplevel);
  839. Assert.Null (rsWin2.Toplevel);
  840. #if DEBUG_IDISPOSABLE
  841. Assert.False (win2.WasDisposed);
  842. Assert.False (win1.WasDisposed);
  843. Assert.False (overlapped.WasDisposed);
  844. #endif
  845. // Now dispose all them
  846. win2.Dispose ();
  847. win1.Dispose ();
  848. overlapped.Dispose ();
  849. Application.Shutdown ();
  850. }
  851. private class Overlapped : Toplevel
  852. {
  853. public Overlapped () { IsOverlappedContainer = true; }
  854. }
  855. [Fact]
  856. [AutoInitShutdown]
  857. public void KeyBindings_Command_With_OverlappedTop ()
  858. {
  859. Toplevel top = new ();
  860. Assert.Null (Application.OverlappedTop);
  861. top.IsOverlappedContainer = true;
  862. Application.Begin (top);
  863. Assert.Equal (Application.Top, Application.OverlappedTop);
  864. var isRunning = true;
  865. var win1 = new Window { Id = "win1", Width = Dim.Percent (50), Height = Dim.Fill () };
  866. var lblTf1W1 = new Label { Text = "Enter text in TextField on Win1:" };
  867. var tf1W1 = new TextField { X = Pos.Right (lblTf1W1) + 1, Width = Dim.Fill (), Text = "Text1 on Win1" };
  868. var lblTvW1 = new Label { Y = Pos.Bottom (lblTf1W1) + 1, Text = "Enter text in TextView on Win1:" };
  869. var tvW1 = new TextView
  870. {
  871. X = Pos.Left (tf1W1), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win1"
  872. };
  873. var lblTf2W1 = new Label { Y = Pos.Bottom (lblTvW1) + 1, Text = "Enter text in TextField on Win1:" };
  874. var tf2W1 = new TextField { X = Pos.Left (tf1W1), Width = Dim.Fill (), Text = "Text2 on Win1" };
  875. win1.Add (lblTf1W1, tf1W1, lblTvW1, tvW1, lblTf2W1, tf2W1);
  876. var win2 = new Window { Id = "win2", Width = Dim.Percent (50), Height = Dim.Fill () };
  877. var lblTf1W2 = new Label { Text = "Enter text in TextField on Win2:" };
  878. var tf1W2 = new TextField { X = Pos.Right (lblTf1W2) + 1, Width = Dim.Fill (), Text = "Text1 on Win2" };
  879. var lblTvW2 = new Label { Y = Pos.Bottom (lblTf1W2) + 1, Text = "Enter text in TextView on Win2:" };
  880. var tvW2 = new TextView
  881. {
  882. X = Pos.Left (tf1W2), Width = Dim.Fill (), Height = 2, Text = "First line Win1\nSecond line Win2"
  883. };
  884. var lblTf2W2 = new Label { Y = Pos.Bottom (lblTvW2) + 1, Text = "Enter text in TextField on Win2:" };
  885. var tf2W2 = new TextField { X = Pos.Left (tf1W2), Width = Dim.Fill (), Text = "Text2 on Win2" };
  886. win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
  887. win1.Closing += (s, e) => isRunning = false;
  888. Assert.Null (top.Focused);
  889. Assert.Equal (top, Application.Current);
  890. Assert.True (top.IsCurrentTop);
  891. Assert.Equal (top, Application.OverlappedTop);
  892. Application.Begin (win1);
  893. Assert.Equal (new (0, 0, 40, 25), win1.Frame);
  894. Assert.NotEqual (top, Application.Current);
  895. Assert.False (top.IsCurrentTop);
  896. Assert.Equal (win1, Application.Current);
  897. Assert.True (win1.IsCurrentTop);
  898. Assert.True (win1.IsOverlapped);
  899. Assert.Null (top.Focused);
  900. Assert.Null (top.MostFocused);
  901. Assert.Equal (tf1W1, win1.MostFocused);
  902. Assert.True (win1.IsOverlapped);
  903. Assert.Single (Application.OverlappedChildren);
  904. Application.Begin (win2);
  905. Assert.Equal (new (0, 0, 40, 25), win2.Frame);
  906. Assert.NotEqual (top, Application.Current);
  907. Assert.False (top.IsCurrentTop);
  908. Assert.Equal (win2, Application.Current);
  909. Assert.True (win2.IsCurrentTop);
  910. Assert.True (win2.IsOverlapped);
  911. Assert.Null (top.Focused);
  912. Assert.Null (top.MostFocused);
  913. Assert.Equal (tf1W2, win2.MostFocused);
  914. Assert.Equal (2, Application.OverlappedChildren.Count);
  915. Application.MoveToOverlappedChild (win1);
  916. Assert.Equal (win1, Application.Current);
  917. Assert.Equal (win1, Application.OverlappedChildren [0]);
  918. win1.Running = true;
  919. Assert.True (Application.OnKeyDown (Application.QuitKey));
  920. Assert.False (isRunning);
  921. Assert.False (win1.Running);
  922. Assert.Equal (win1, Application.OverlappedChildren [0]);
  923. Assert.True (
  924. Application.OnKeyDown (Key.Z.WithCtrl)
  925. );
  926. Assert.True (Application.OnKeyDown (Key.F5)); // refresh
  927. Assert.True (Application.OnKeyDown (Key.Tab));
  928. Assert.True (win1.IsCurrentTop);
  929. Assert.Equal (tvW1, win1.MostFocused);
  930. Assert.True (Application.OnKeyDown (Key.Tab));
  931. Assert.Equal ($"\tFirst line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  932. Assert.True (
  933. Application.OnKeyDown (Key.Tab.WithShift)
  934. );
  935. Assert.Equal ($"First line Win1{Environment.NewLine}Second line Win1", tvW1.Text);
  936. Assert.True (
  937. Application.OnKeyDown (Key.Tab.WithCtrl)
  938. );
  939. Assert.Equal (win1, Application.OverlappedChildren [0]);
  940. Assert.Equal (tf2W1, win1.MostFocused);
  941. Assert.True (Application.OnKeyDown (Key.Tab));
  942. Assert.Equal (win1, Application.OverlappedChildren [0]);
  943. Assert.Equal (tf1W1, win1.MostFocused);
  944. Assert.True (Application.OnKeyDown (Key.CursorRight));
  945. Assert.Equal (win1, Application.OverlappedChildren [0]);
  946. Assert.Equal (tf1W1, win1.MostFocused);
  947. Assert.True (Application.OnKeyDown (Key.CursorDown));
  948. Assert.Equal (win1, Application.OverlappedChildren [0]);
  949. Assert.Equal (tvW1, win1.MostFocused);
  950. #if UNIX_KEY_BINDINGS
  951. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.I.WithCtrl)));
  952. Assert.Equal (win1, Application.OverlappedChildren [0]);
  953. Assert.Equal (tf2W1, win1.MostFocused);
  954. #endif
  955. Assert.True (
  956. Application.OverlappedChildren [0]
  957. .NewKeyDownEvent (Key.Tab.WithShift)
  958. );
  959. Assert.Equal (win1, Application.OverlappedChildren [0]);
  960. Assert.Equal (tvW1, win1.MostFocused);
  961. Assert.True (Application.OnKeyDown (Key.CursorLeft));
  962. Assert.Equal (win1, Application.OverlappedChildren [0]);
  963. Assert.Equal (tf1W1, win1.MostFocused);
  964. Assert.True (Application.OnKeyDown (Key.CursorUp));
  965. Assert.Equal (win1, Application.OverlappedChildren [0]);
  966. Assert.Equal (tf2W1, win1.MostFocused);
  967. Assert.True (Application.OnKeyDown (Key.Tab));
  968. Assert.Equal (win1, Application.OverlappedChildren [0]);
  969. Assert.Equal (tf1W1, win1.MostFocused);
  970. Assert.True (
  971. Application.OverlappedChildren [0]
  972. .NewKeyDownEvent (Key.Tab.WithCtrl)
  973. );
  974. Assert.Equal (win2, Application.OverlappedChildren [0]);
  975. Assert.Equal (tf1W2, win2.MostFocused);
  976. tf2W2.SetFocus ();
  977. Assert.True (tf2W2.HasFocus);
  978. Assert.True (
  979. Application.OverlappedChildren [0]
  980. .NewKeyDownEvent (Key.Tab.WithCtrl.WithShift)
  981. );
  982. Assert.Equal (win1, Application.OverlappedChildren [0]);
  983. Assert.Equal (tf1W1, win1.MostFocused);
  984. Assert.True (Application.OnKeyDown (Application.AlternateForwardKey));
  985. Assert.Equal (win2, Application.OverlappedChildren [0]);
  986. Assert.Equal (tf2W2, win2.MostFocused);
  987. Assert.True (Application.OnKeyDown (Application.AlternateBackwardKey));
  988. Assert.Equal (win1, Application.OverlappedChildren [0]);
  989. Assert.Equal (tf1W1, win1.MostFocused);
  990. Assert.True (Application.OnKeyDown (Key.CursorDown));
  991. Assert.Equal (win1, Application.OverlappedChildren [0]);
  992. Assert.Equal (tvW1, win1.MostFocused);
  993. #if UNIX_KEY_BINDINGS
  994. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.B.WithCtrl)));
  995. #else
  996. Assert.True (Application.OnKeyDown (Key.CursorLeft));
  997. #endif
  998. Assert.Equal (win1, Application.OverlappedChildren [0]);
  999. Assert.Equal (tf1W1, win1.MostFocused);
  1000. Assert.True (Application.OnKeyDown (Key.CursorDown));
  1001. Assert.Equal (win1, Application.OverlappedChildren [0]);
  1002. Assert.Equal (tvW1, win1.MostFocused);
  1003. Assert.Equal (Point.Empty, tvW1.CursorPosition);
  1004. Assert.True (
  1005. Application.OverlappedChildren [0]
  1006. .NewKeyDownEvent (Key.End.WithCtrl)
  1007. );
  1008. Assert.Equal (win1, Application.OverlappedChildren [0]);
  1009. Assert.Equal (tvW1, win1.MostFocused);
  1010. Assert.Equal (new (16, 1), tvW1.CursorPosition);
  1011. #if UNIX_KEY_BINDINGS
  1012. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.F.WithCtrl)));
  1013. #else
  1014. Assert.True (Application.OnKeyDown (Key.CursorRight));
  1015. #endif
  1016. Assert.Equal (win1, Application.OverlappedChildren [0]);
  1017. Assert.Equal (tf2W1, win1.MostFocused);
  1018. #if UNIX_KEY_BINDINGS
  1019. Assert.True (Application.OverlappedChildren [0].ProcessKeyDown (new (Key.L.WithCtrl)));
  1020. #endif
  1021. win2.Dispose ();
  1022. win1.Dispose ();
  1023. top.Dispose ();
  1024. }
  1025. }