|
@@ -1,6 +1,8 @@
|
|
|
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.Threading;
|
|
@@ -13,17 +15,22 @@ namespace Avalonia.Controls.UnitTests
|
|
|
|
|
|
public class DesktopStyleApplicationLifetimeTests
|
|
|
{
|
|
|
- IDispatcherImpl CreateDispatcherWithInstantMainLoop() => Mock.Of<IControlledDispatcherImpl>(x => x.CurrentThreadIsLoopThread == true);
|
|
|
-
|
|
|
-
|
|
|
+ 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: CreateDispatcherWithInstantMainLoop())))
|
|
|
+ using (UnitTestApplication.Start(new TestServices(dispatcherImpl: new ManagedDispatcherImpl(null))))
|
|
|
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
|
|
|
{
|
|
|
- lifetime.Shutdown(1337);
|
|
|
-
|
|
|
+ Dispatcher.UIThread.Post(() => lifetime.Shutdown(1337));
|
|
|
var exitCode = lifetime.Start(Array.Empty<string>());
|
|
|
|
|
|
Assert.Equal(1337, exitCode);
|
|
@@ -219,11 +226,14 @@ namespace Avalonia.Controls.UnitTests
|
|
|
[Fact]
|
|
|
public void Should_Allow_Canceling_Shutdown_Via_ShutdownRequested_Event()
|
|
|
{
|
|
|
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: CreateDispatcherWithInstantMainLoop())))
|
|
|
+ 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();
|
|
@@ -356,7 +366,7 @@ namespace Avalonia.Controls.UnitTests
|
|
|
[Fact]
|
|
|
public void Shutdown_NotCancellable_By_Preventing_Window_Close()
|
|
|
{
|
|
|
- using (UnitTestApplication.Start(TestServices.StyledWindow))
|
|
|
+ using (UnitTestApplication.Start(TestServices.StyledWindow.With(dispatcherImpl: CreateDispatcherWithInstantMainLoop())))
|
|
|
using(var lifetime = new ClassicDesktopStyleApplicationLifetime())
|
|
|
{
|
|
|
var hasExit = false;
|