-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for non-disposable DataContext issue (#12123)
- Loading branch information
flexxxxer
committed
Aug 5, 2023
1 parent
ca2ca4e
commit 5b18289
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using Avalonia.Controls; | ||
using Avalonia.Controls.ApplicationLifetimes; | ||
using Avalonia.Reactive; | ||
using Avalonia.Threading; | ||
using Avalonia.UnitTests; | ||
using JetBrains.dotMemoryUnit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Avalonia.LeakTests; | ||
|
||
internal class ViewModelForDisposingTest | ||
{ | ||
~ViewModelForDisposingTest() { ; } | ||
} | ||
|
||
[DotMemoryUnit(FailIfRunWithoutSupport = false)] | ||
public class DataContextTests | ||
{ | ||
public DataContextTests(ITestOutputHelper atr) | ||
{ | ||
DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine); | ||
} | ||
|
||
[Fact] | ||
public void Window_DataContext_Disposed_After_Window_Close_With_Lifetime() | ||
{ | ||
static IDisposable Run() | ||
{ | ||
var unitTestApp = UnitTestApplication.Start(TestServices.StyledWindow); | ||
var lifetime = new ClassicDesktopStyleApplicationLifetime(); | ||
lifetime.ShutdownMode = ShutdownMode.OnExplicitShutdown; | ||
var window = new Window { DataContext = new ViewModelForDisposingTest() }; | ||
window.Show(); | ||
window.Close(); | ||
|
||
return Disposable.Create(lifetime, lt => lt.Shutdown()) | ||
.DisposeWith(new CompositeDisposable(lifetime, unitTestApp)); | ||
} | ||
|
||
using var _ = Run(); | ||
// Process all Loaded events to free control reference(s) | ||
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); | ||
GC.Collect(); | ||
|
||
dotMemory.Check(m => Assert.Equal(0, | ||
m.GetObjects(o => o.Type.Is<ViewModelForDisposingTest>()).ObjectsCount)); | ||
} | ||
|
||
[Fact] | ||
public void Window_DataContext_Disposed_After_Window_Close_Without_Lifetime() | ||
{ | ||
static void Run() | ||
{ | ||
using var _ = UnitTestApplication.Start(TestServices.StyledWindow); | ||
var window = new Window { DataContext = new ViewModelForDisposingTest() }; | ||
window.Show(); | ||
window.Close(); | ||
} | ||
|
||
Run(); | ||
// Process all Loaded events to free control reference(s) | ||
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded); | ||
GC.Collect(); | ||
|
||
dotMemory.Check(m => Assert.Equal(0, | ||
m.GetObjects(o => o.Type.Is<ViewModelForDisposingTest>()).ObjectsCount)); | ||
} | ||
} |