Browse Source

Merge pull request #2798 from worldbeater/suspension-design-mode

AutoSuspendHelper: Don't throw when in design mode
pull/2809/head
Nikita Tsukanov 7 years ago
committed by GitHub
parent
commit
ad077b5f57
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/Avalonia.ReactiveUI/AutoSuspendHelper.cs
  2. 34
      tests/Avalonia.ReactiveUI.UnitTests/AutoSuspendHelperTest.cs

11
src/Avalonia.ReactiveUI/AutoSuspendHelper.cs

@ -35,7 +35,12 @@ namespace Avalonia.ReactiveUI
RxApp.SuspensionHost.IsResuming = Observable.Never<Unit>();
RxApp.SuspensionHost.IsLaunchingNew = _isLaunchingNew;
if (lifetime is IControlledApplicationLifetime controlled)
if (Avalonia.Controls.Design.IsDesignMode)
{
this.Log().Debug("Design mode detected. AutoSuspendHelper won't persist app state.");
RxApp.SuspensionHost.ShouldPersistState = Observable.Never<IDisposable>();
}
else if (lifetime is IControlledApplicationLifetime controlled)
{
this.Log().Debug("Using IControlledApplicationLifetime events to handle app exit.");
controlled.Exit += (sender, args) => OnControlledApplicationLifetimeExit();
@ -47,11 +52,11 @@ namespace Avalonia.ReactiveUI
var message = $"Don't know how to detect app exit event for {type}.";
throw new NotSupportedException(message);
}
else
else
{
var message = "ApplicationLifetime is null. "
+ "Ensure you are initializing AutoSuspendHelper "
+ "when Avalonia application initialization is completed.";
+ "after Avalonia application initialization is completed.";
throw new ArgumentNullException(message);
}

34
tests/Avalonia.ReactiveUI.UnitTests/AutoSuspendHelperTest.cs

@ -60,6 +60,28 @@ namespace Avalonia.ReactiveUI.UnitTests
}
}
[Fact]
public void AutoSuspendHelper_Should_Throw_When_Not_Supported_Lifetime_Is_Used()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
using (var lifetime = new ExoticApplicationLifetimeWithoutLifecycleEvents())
{
var application = AvaloniaLocator.Current.GetService<Application>();
application.ApplicationLifetime = lifetime;
Assert.Throws<NotSupportedException>(() => new AutoSuspendHelper(application.ApplicationLifetime));
}
}
[Fact]
public void AutoSuspendHelper_Should_Throw_When_Lifetime_Is_Null()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{
var application = AvaloniaLocator.Current.GetService<Application>();
Assert.Throws<ArgumentNullException>(() => new AutoSuspendHelper(application.ApplicationLifetime));
}
}
[Fact]
public void ShouldPersistState_Should_Fire_On_App_Exit_When_SuspensionDriver_Is_Initialized()
{
@ -82,17 +104,5 @@ namespace Avalonia.ReactiveUI.UnitTests
Assert.Equal("Foo", RxApp.SuspensionHost.GetAppState<AppState>().Example);
}
}
[Fact]
public void AutoSuspendHelper_Should_Throw_For_Not_Supported_Lifetimes()
{
using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
using (var lifetime = new ExoticApplicationLifetimeWithoutLifecycleEvents())
{
var application = AvaloniaLocator.Current.GetService<Application>();
application.ApplicationLifetime = lifetime;
Assert.Throws<NotSupportedException>(() => new AutoSuspendHelper(application.ApplicationLifetime));
}
}
}
}
Loading…
Cancel
Save