| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using Avalonia.Controls.ApplicationLifetimes;
- using Avalonia.Controls.Platform;
- using Avalonia.Platform;
- using Avalonia.Rendering;
- using Avalonia.Rendering.Composition;
- using Avalonia.Threading;
- using Avalonia.UnitTests;
- using Moq;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
-
- public class DesktopStyleApplicationLifetimeTests : ScopedTestBase
- {
- IDispatcherImpl CreateDispatcherWithInstantMainLoop()
- {
- var mock = new Mock<IControlledDispatcherImpl>();
- mock.Setup(x => x.RunLoop(It.IsAny<CancellationToken>()))
- .Callback(() => Dispatcher.UIThread.ExitAllFrames());
- mock.Setup(x => x.CurrentThreadIsLoopThread).Returns(true);
- return mock.Object;
- }
-
- [Fact]
- public void Should_Set_ExitCode_After_Shutdown()
- {
- using (UnitTestApplication.Start(new TestServices(dispatcherImpl: new ManagedDispatcherImpl(null))))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- Dispatcher.UIThread.Post(() => lifetime.Shutdown(1337));
- var exitCode = lifetime.Start(Array.Empty<string>());
- Assert.Equal(1337, exitCode);
- }
- }
-
-
- [Fact]
- public void Should_Close_All_Remaining_Open_Windows_After_Explicit_Exit_Call()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var windows = new List<Window> { new Window(), new Window(), new Window(), new Window() };
- foreach (var window in windows)
- {
- window.Show();
- }
- Assert.Equal(4, lifetime.Windows.Count);
- lifetime.Shutdown();
- Assert.Empty(lifetime.Windows);
- }
- }
-
- [Fact]
- public void Should_Only_Exit_On_Explicit_Exit()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var windowA = new Window();
- windowA.Show();
- var windowB = new Window();
- windowB.Show();
- windowA.Close();
- Assert.False(hasExit);
- windowB.Close();
- Assert.False(hasExit);
- lifetime.Shutdown();
- Assert.True(hasExit);
- }
- }
-
- [Fact]
- public void Should_Exit_After_MainWindow_Closed()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var mainWindow = new Window();
- mainWindow.Show();
- lifetime.MainWindow = mainWindow;
- var window = new Window();
- window.Show();
- mainWindow.Close();
- Assert.True(hasExit);
- }
- }
- [Fact]
- public void OnMainWindowClose_Overrides_Secondary_Window_Cancellation()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- var secondaryWindowClosingExecuted = false;
- var secondaryWindowClosedExecuted = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var mainWindow = new Window();
- mainWindow.Show();
- lifetime.MainWindow = mainWindow;
- var window = new Window();
- window.Closing += (_, args) =>
- {
- secondaryWindowClosingExecuted = true;
- args.Cancel = true;
- };
- window.Closed += (_, _) =>
- {
- secondaryWindowClosedExecuted = true;
- };
- window.Show();
- mainWindow.Close();
- Assert.True(secondaryWindowClosingExecuted);
- Assert.True(secondaryWindowClosedExecuted);
- Assert.True(hasExit);
- }
- }
- [Fact]
- public void OnMainWindowClose_Overrides_Secondary_Window_Cancellation_From_TryShutdown()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- var secondaryWindowClosingExecuted = false;
- var secondaryWindowClosedExecuted = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var mainWindow = new Window();
- mainWindow.Show();
- lifetime.MainWindow = mainWindow;
- var window = new Window();
- window.Closing += (_, args) =>
- {
- secondaryWindowClosingExecuted = true;
- args.Cancel = true;
- };
- window.Closed += (_, _) =>
- {
- secondaryWindowClosedExecuted = true;
- };
- window.Show();
- lifetime.TryShutdown();
- Assert.True(secondaryWindowClosingExecuted);
- Assert.True(secondaryWindowClosedExecuted);
- Assert.True(hasExit);
- }
- }
- [Fact]
- public void Should_Exit_After_Last_Window_Closed()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var windowA = new Window();
- windowA.Show();
- var windowB = new Window();
- windowB.Show();
- windowA.Close();
- Assert.False(hasExit);
- windowB.Close();
- Assert.True(hasExit);
- }
- }
-
- [Fact]
- public void Show_Should_Add_Window_To_OpenWindows()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var window = new Window();
- window.Show();
- Assert.Equal(new[] { window }, lifetime.Windows);
- }
- }
- [Fact]
- public void Window_Should_Be_Added_To_OpenWindows_Only_Once()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var window = new Window();
- window.Show();
- window.Show();
- window.IsVisible = true;
- Assert.Equal(new[] { window }, lifetime.Windows);
- window.Close();
- }
- }
- [Fact]
- public void Close_Should_Remove_Window_From_OpenWindows()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var window = new Window();
- window.Show();
- Assert.Equal(1, lifetime.Windows.Count);
- window.Close();
- Assert.Empty(lifetime.Windows);
- }
- }
-
- [Fact]
- public void Impl_Closing_Should_Remove_Window_From_OpenWindows()
- {
- var windowImpl = new Mock<IWindowImpl>();
- windowImpl.Setup(x => x.Compositor).Returns(RendererMocks.CreateDummyCompositor());
- windowImpl.SetupProperty(x => x.Closed);
- windowImpl.Setup(x => x.DesktopScaling).Returns(1);
- windowImpl.Setup(x => x.RenderScaling).Returns(1);
- var screen1 = new Mock<Screen>(1.75, new PixelRect(new PixelSize(1920, 1080)), new PixelRect(new PixelSize(1920, 966)), true);
- var screens = new Mock<IScreenImpl>();
- screens.Setup(x => x.ScreenFromWindow(It.IsAny<IWindowBaseImpl>())).Returns(screen1.Object);
- windowImpl.Setup(x => x.TryGetFeature(It.Is<Type>(t => t == typeof(IScreenImpl)))).Returns(screens.Object);
-
- var services = TestServices.StyledWindow.With(
- windowingPlatform: new MockWindowingPlatform(() => windowImpl.Object));
- using (UnitTestApplication.Start(services))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var window = new Window();
- window.Show();
- Assert.Equal(1, lifetime.Windows.Count);
- windowImpl.Object.Closed();
- Assert.Empty(lifetime.Windows);
- }
- }
- [Fact]
- public void Should_Allow_Canceling_Shutdown_Via_ShutdownRequested_Event()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: new ManagedDispatcherImpl(null))))
- using (var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- var lifetimeEvents = new Mock<IPlatformLifetimeEventsImpl>();
- AvaloniaLocator.CurrentMutable.Bind<IPlatformLifetimeEventsImpl>().ToConstant(lifetimeEvents.Object);
-
- // Force exit immediately
- Dispatcher.UIThread.Post(Dispatcher.UIThread.ExitAllFrames);
- lifetime.Start(Array.Empty<string>());
- var window = new Window();
- var raised = 0;
- window.Show();
- lifetime.ShutdownRequested += (_, e) =>
- {
- e.Cancel = true;
- ++raised;
- };
- lifetimeEvents.Raise(x => x.ShutdownRequested += null, new ShutdownRequestedEventArgs());
- Assert.Equal(1, raised);
- Assert.Equal(new[] { window }, lifetime.Windows);
- }
- }
-
- [Fact]
- public void MainWindow_Closed_Shutdown_Should_Be_Cancellable()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnMainWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var mainWindow = new Window();
- mainWindow.Show();
- lifetime.MainWindow = mainWindow;
- var window = new Window();
- window.Show();
- var raised = 0;
-
- lifetime.ShutdownRequested += (_, e) =>
- {
- e.Cancel = true;
- ++raised;
- };
- mainWindow.Close();
-
- Assert.Equal(1, raised);
- Assert.False(hasExit);
- }
- }
-
- [Fact]
- public void LastWindow_Closed_Shutdown_Should_Be_Cancellable()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.ShutdownMode = ShutdownMode.OnLastWindowClose;
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
- var windowA = new Window();
- windowA.Show();
- var windowB = new Window();
- windowB.Show();
-
- var raised = 0;
-
- lifetime.ShutdownRequested += (_, e) =>
- {
- e.Cancel = true;
- ++raised;
- };
- windowA.Close();
- Assert.False(hasExit);
- windowB.Close();
- Assert.Equal(1, raised);
- Assert.False(hasExit);
- }
- }
-
- [Fact]
- public void TryShutdown_Cancellable_By_Preventing_Window_Close()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- lifetime.Exit += (_, _) => Assert.Fail("lifetime.Exit was called.");
- Dispatcher.UIThread.ShutdownStarted += UiThreadOnShutdownStarted;
- static void UiThreadOnShutdownStarted(object sender, EventArgs e)
- {
- Assert.Fail("Dispatcher.UIThread.ShutdownStarted was called.");
- }
- var windowA = new Window();
- windowA.Show();
- var windowB = new Window();
- windowB.Show();
-
- var raised = 0;
- windowA.Closing += (_, e) =>
- {
- e.Cancel = true;
- ++raised;
- };
- lifetime.TryShutdown();
- Assert.Equal(1, raised);
- Dispatcher.UIThread.ShutdownStarted -= UiThreadOnShutdownStarted;
- }
- }
-
- [Fact]
- public void Shutdown_NotCancellable_By_Preventing_Window_Close()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: CreateDispatcherWithInstantMainLoop())))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- var closingRaised = 0;
- var closedRaised = 0;
- lifetime.Exit += (_, _) => hasExit = true;
- var windowA = new Window();
- windowA.Show();
- var windowB = new Window();
- windowB.Show();
- windowA.Closing += (_, e) =>
- {
- e.Cancel = true;
- ++closingRaised;
- };
- windowA.Closed += (_, e) =>
- {
- ++closedRaised;
- };
- lifetime.Shutdown();
- Assert.Equal(1, closingRaised);
- Assert.Equal(1, closedRaised);
- Assert.True(hasExit);
- }
- }
-
- [Fact]
- public void Shutdown_Doesnt_Raise_Shutdown_Requested()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow))
- using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
- {
- lifetime.SetupCore(Array.Empty<string>());
- var hasExit = false;
- lifetime.Exit += (_, _) => hasExit = true;
-
- var raised = 0;
- lifetime.ShutdownRequested += (_, _) =>
- {
- ++raised;
- };
- lifetime.Shutdown();
- Assert.Equal(0, raised);
- Assert.True(hasExit);
- }
- }
- }
- }
|