DesktopStyleApplicationLifetimeTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Controls.Platform;
  6. using Avalonia.Platform;
  7. using Avalonia.Rendering;
  8. using Avalonia.Rendering.Composition;
  9. using Avalonia.Threading;
  10. using Avalonia.UnitTests;
  11. using Moq;
  12. using Xunit;
  13. namespace Avalonia.Controls.UnitTests
  14. {
  15. public class DesktopStyleApplicationLifetimeTests : ScopedTestBase
  16. {
  17. IDispatcherImpl CreateDispatcherWithInstantMainLoop()
  18. {
  19. var mock = new Mock<IControlledDispatcherImpl>();
  20. mock.Setup(x => x.RunLoop(It.IsAny<CancellationToken>()))
  21. .Callback(() => Dispatcher.UIThread.ExitAllFrames());
  22. mock.Setup(x => x.CurrentThreadIsLoopThread).Returns(true);
  23. return mock.Object;
  24. }
  25. [Fact]
  26. public void Should_Set_ExitCode_After_Shutdown()
  27. {
  28. using (UnitTestApplication.Start(new TestServices(dispatcherImpl: new ManagedDispatcherImpl(null))))
  29. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  30. {
  31. lifetime.SetupCore(Array.Empty<string>());
  32. Dispatcher.UIThread.Post(() => lifetime.Shutdown(1337));
  33. var exitCode = lifetime.Start(Array.Empty<string>());
  34. Assert.Equal(1337, exitCode);
  35. }
  36. }
  37. [Fact]
  38. public void Should_Close_All_Remaining_Open_Windows_After_Explicit_Exit_Call()
  39. {
  40. using (UnitTestApplication.Start(TestServices.StyledWindow))
  41. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  42. {
  43. lifetime.SetupCore(Array.Empty<string>());
  44. var windows = new List<Window> { new Window(), new Window(), new Window(), new Window() };
  45. foreach (var window in windows)
  46. {
  47. window.Show();
  48. }
  49. Assert.Equal(4, lifetime.Windows.Count);
  50. lifetime.Shutdown();
  51. Assert.Empty(lifetime.Windows);
  52. }
  53. }
  54. [Fact]
  55. public void Should_Only_Exit_On_Explicit_Exit()
  56. {
  57. using (UnitTestApplication.Start(TestServices.StyledWindow))
  58. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  59. {
  60. lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  61. lifetime.SetupCore(Array.Empty<string>());
  62. var hasExit = false;
  63. lifetime.Exit += (_, _) => hasExit = true;
  64. var windowA = new Window();
  65. windowA.Show();
  66. var windowB = new Window();
  67. windowB.Show();
  68. windowA.Close();
  69. Assert.False(hasExit);
  70. windowB.Close();
  71. Assert.False(hasExit);
  72. lifetime.Shutdown();
  73. Assert.True(hasExit);
  74. }
  75. }
  76. [Fact]
  77. public void Should_Exit_After_MainWindow_Closed()
  78. {
  79. using (UnitTestApplication.Start(TestServices.StyledWindow))
  80. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  81. {
  82. lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
  83. lifetime.SetupCore(Array.Empty<string>());
  84. var hasExit = false;
  85. lifetime.Exit += (_, _) => hasExit = true;
  86. var mainWindow = new Window();
  87. mainWindow.Show();
  88. lifetime.MainWindow = mainWindow;
  89. var window = new Window();
  90. window.Show();
  91. mainWindow.Close();
  92. Assert.True(hasExit);
  93. }
  94. }
  95. [Fact]
  96. public void OnMainWindowClose_Overrides_Secondary_Window_Cancellation()
  97. {
  98. using (UnitTestApplication.Start(TestServices.StyledWindow))
  99. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  100. {
  101. lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
  102. lifetime.SetupCore(Array.Empty<string>());
  103. var hasExit = false;
  104. var secondaryWindowClosingExecuted = false;
  105. var secondaryWindowClosedExecuted = false;
  106. lifetime.Exit += (_, _) => hasExit = true;
  107. var mainWindow = new Window();
  108. mainWindow.Show();
  109. lifetime.MainWindow = mainWindow;
  110. var window = new Window();
  111. window.Closing += (_, args) =>
  112. {
  113. secondaryWindowClosingExecuted = true;
  114. args.Cancel = true;
  115. };
  116. window.Closed += (_, _) =>
  117. {
  118. secondaryWindowClosedExecuted = true;
  119. };
  120. window.Show();
  121. mainWindow.Close();
  122. Assert.True(secondaryWindowClosingExecuted);
  123. Assert.True(secondaryWindowClosedExecuted);
  124. Assert.True(hasExit);
  125. }
  126. }
  127. [Fact]
  128. public void OnMainWindowClose_Overrides_Secondary_Window_Cancellation_From_TryShutdown()
  129. {
  130. using (UnitTestApplication.Start(TestServices.StyledWindow))
  131. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  132. {
  133. lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
  134. lifetime.SetupCore(Array.Empty<string>());
  135. var hasExit = false;
  136. var secondaryWindowClosingExecuted = false;
  137. var secondaryWindowClosedExecuted = false;
  138. lifetime.Exit += (_, _) => hasExit = true;
  139. var mainWindow = new Window();
  140. mainWindow.Show();
  141. lifetime.MainWindow = mainWindow;
  142. var window = new Window();
  143. window.Closing += (_, args) =>
  144. {
  145. secondaryWindowClosingExecuted = true;
  146. args.Cancel = true;
  147. };
  148. window.Closed += (_, _) =>
  149. {
  150. secondaryWindowClosedExecuted = true;
  151. };
  152. window.Show();
  153. lifetime.TryShutdown();
  154. Assert.True(secondaryWindowClosingExecuted);
  155. Assert.True(secondaryWindowClosedExecuted);
  156. Assert.True(hasExit);
  157. }
  158. }
  159. [Fact]
  160. public void Should_Exit_After_Last_Window_Closed()
  161. {
  162. using (UnitTestApplication.Start(TestServices.StyledWindow))
  163. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  164. {
  165. lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose;
  166. lifetime.SetupCore(Array.Empty<string>());
  167. var hasExit = false;
  168. lifetime.Exit += (_, _) => hasExit = true;
  169. var windowA = new Window();
  170. windowA.Show();
  171. var windowB = new Window();
  172. windowB.Show();
  173. windowA.Close();
  174. Assert.False(hasExit);
  175. windowB.Close();
  176. Assert.True(hasExit);
  177. }
  178. }
  179. [Fact]
  180. public void Show_Should_Add_Window_To_OpenWindows()
  181. {
  182. using (UnitTestApplication.Start(TestServices.StyledWindow))
  183. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  184. {
  185. lifetime.SetupCore(Array.Empty<string>());
  186. var window = new Window();
  187. window.Show();
  188. Assert.Equal(new[] { window }, lifetime.Windows);
  189. }
  190. }
  191. [Fact]
  192. public void Window_Should_Be_Added_To_OpenWindows_Only_Once()
  193. {
  194. using (UnitTestApplication.Start(TestServices.StyledWindow))
  195. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  196. {
  197. lifetime.SetupCore(Array.Empty<string>());
  198. var window = new Window();
  199. window.Show();
  200. window.Show();
  201. window.IsVisible = true;
  202. Assert.Equal(new[] { window }, lifetime.Windows);
  203. window.Close();
  204. }
  205. }
  206. [Fact]
  207. public void Close_Should_Remove_Window_From_OpenWindows()
  208. {
  209. using (UnitTestApplication.Start(TestServices.StyledWindow))
  210. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  211. {
  212. lifetime.SetupCore(Array.Empty<string>());
  213. var window = new Window();
  214. window.Show();
  215. Assert.Equal(1, lifetime.Windows.Count);
  216. window.Close();
  217. Assert.Empty(lifetime.Windows);
  218. }
  219. }
  220. [Fact]
  221. public void Impl_Closing_Should_Remove_Window_From_OpenWindows()
  222. {
  223. var windowImpl = new Mock<IWindowImpl>();
  224. windowImpl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
  225. windowImpl.SetupProperty(x => x.Closed);
  226. windowImpl.Setup(x => x.DesktopScaling).Returns(1);
  227. windowImpl.Setup(x => x.RenderScaling).Returns(1);
  228. var screen1 = new Mock<Screen>(1.75, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 966)), true);
  229. var screens = new Mock<IScreenImpl>();
  230. screens.Setup(x => x.ScreenFromWindow(It.IsAny<IWindowBaseImpl>())).Returns(screen1.Object);
  231. windowImpl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
  232. var services = TestServices.StyledWindow.With(
  233. windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
  234. using (UnitTestApplication.Start(services))
  235. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  236. {
  237. lifetime.SetupCore(Array.Empty<string>());
  238. var window = new Window();
  239. window.Show();
  240. Assert.Equal(1, lifetime.Windows.Count);
  241. windowImpl.Object.Closed();
  242. Assert.Empty(lifetime.Windows);
  243. }
  244. }
  245. [Fact]
  246. public void Should_Allow_Canceling_Shutdown_Via_ShutdownRequested_Event()
  247. {
  248. using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: new ManagedDispatcherImpl(null))))
  249. using (var lifetime = new ClassicDesktopStyleApplicationLifetime())
  250. {
  251. var lifetimeEvents = new Mock<IPlatformLifetimeEventsImpl>();
  252. AvaloniaLocator.CurrentMutable.Bind<IPlatformLifetimeEventsImpl>().ToConstant(lifetimeEvents.Object);
  253. // Force exit immediately
  254. Dispatcher.UIThread.Post(Dispatcher.UIThread.ExitAllFrames);
  255. lifetime.Start(Array.Empty<string>());
  256. var window = new Window();
  257. var raised = 0;
  258. window.Show();
  259. lifetime.ShutdownRequested += (_, e) =>
  260. {
  261. e.Cancel = true;
  262. ++raised;
  263. };
  264. lifetimeEvents.Raise(x => x.ShutdownRequested += null, new ShutdownRequestedEventArgs());
  265. Assert.Equal(1, raised);
  266. Assert.Equal(new[] { window }, lifetime.Windows);
  267. }
  268. }
  269. [Fact]
  270. public void MainWindow_Closed_Shutdown_Should_Be_Cancellable()
  271. {
  272. using (UnitTestApplication.Start(TestServices.StyledWindow))
  273. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  274. {
  275. lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
  276. lifetime.SetupCore(Array.Empty<string>());
  277. var hasExit = false;
  278. lifetime.Exit += (_, _) => hasExit = true;
  279. var mainWindow = new Window();
  280. mainWindow.Show();
  281. lifetime.MainWindow = mainWindow;
  282. var window = new Window();
  283. window.Show();
  284. var raised = 0;
  285. lifetime.ShutdownRequested += (_, e) =>
  286. {
  287. e.Cancel = true;
  288. ++raised;
  289. };
  290. mainWindow.Close();
  291. Assert.Equal(1, raised);
  292. Assert.False(hasExit);
  293. }
  294. }
  295. [Fact]
  296. public void LastWindow_Closed_Shutdown_Should_Be_Cancellable()
  297. {
  298. using (UnitTestApplication.Start(TestServices.StyledWindow))
  299. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  300. {
  301. lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose;
  302. lifetime.SetupCore(Array.Empty<string>());
  303. var hasExit = false;
  304. lifetime.Exit += (_, _) => hasExit = true;
  305. var windowA = new Window();
  306. windowA.Show();
  307. var windowB = new Window();
  308. windowB.Show();
  309. var raised = 0;
  310. lifetime.ShutdownRequested += (_, e) =>
  311. {
  312. e.Cancel = true;
  313. ++raised;
  314. };
  315. windowA.Close();
  316. Assert.False(hasExit);
  317. windowB.Close();
  318. Assert.Equal(1, raised);
  319. Assert.False(hasExit);
  320. }
  321. }
  322. [Fact]
  323. public void TryShutdown_Cancellable_By_Preventing_Window_Close()
  324. {
  325. using (UnitTestApplication.Start(TestServices.StyledWindow))
  326. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  327. {
  328. lifetime.SetupCore(Array.Empty<string>());
  329. lifetime.Exit += (_, _) => Assert.Fail("lifetime.Exit was called.");
  330. Dispatcher.UIThread.ShutdownStarted += UiThreadOnShutdownStarted;
  331. static void UiThreadOnShutdownStarted(object sender, EventArgs e)
  332. {
  333. Assert.Fail("Dispatcher.UIThread.ShutdownStarted was called.");
  334. }
  335. var windowA = new Window();
  336. windowA.Show();
  337. var windowB = new Window();
  338. windowB.Show();
  339. var raised = 0;
  340. windowA.Closing += (_, e) =>
  341. {
  342. e.Cancel = true;
  343. ++raised;
  344. };
  345. lifetime.TryShutdown();
  346. Assert.Equal(1, raised);
  347. Dispatcher.UIThread.ShutdownStarted -= UiThreadOnShutdownStarted;
  348. }
  349. }
  350. [Fact]
  351. public void Shutdown_NotCancellable_By_Preventing_Window_Close()
  352. {
  353. using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: CreateDispatcherWithInstantMainLoop())))
  354. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  355. {
  356. lifetime.SetupCore(Array.Empty<string>());
  357. var hasExit = false;
  358. var closingRaised = 0;
  359. var closedRaised = 0;
  360. lifetime.Exit += (_, _) => hasExit = true;
  361. var windowA = new Window();
  362. windowA.Show();
  363. var windowB = new Window();
  364. windowB.Show();
  365. windowA.Closing += (_, e) =>
  366. {
  367. e.Cancel = true;
  368. ++closingRaised;
  369. };
  370. windowA.Closed += (_, e) =>
  371. {
  372. ++closedRaised;
  373. };
  374. lifetime.Shutdown();
  375. Assert.Equal(1, closingRaised);
  376. Assert.Equal(1, closedRaised);
  377. Assert.True(hasExit);
  378. }
  379. }
  380. [Fact]
  381. public void Shutdown_Doesnt_Raise_Shutdown_Requested()
  382. {
  383. using (UnitTestApplication.Start(TestServices.StyledWindow))
  384. using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
  385. {
  386. lifetime.SetupCore(Array.Empty<string>());
  387. var hasExit = false;
  388. lifetime.Exit += (_, _) => hasExit = true;
  389. var raised = 0;
  390. lifetime.ShutdownRequested += (_, _) =>
  391. {
  392. ++raised;
  393. };
  394. lifetime.Shutdown();
  395. Assert.Equal(0, raised);
  396. Assert.True(hasExit);
  397. }
  398. }
  399. }
  400. }