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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
30 additions and
15 deletions
-
src/Avalonia.ReactiveUI/AutoSuspendHelper.cs
-
tests/Avalonia.ReactiveUI.UnitTests/AutoSuspendHelperTest.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); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -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)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |