| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using Avalonia.Animation;
- using Avalonia.Controls;
- using Avalonia.Data;
- using Avalonia.Styling;
- using Avalonia.Threading;
- using Avalonia.UnitTests;
- using JetBrains.dotMemoryUnit;
- using Xunit;
- using Xunit.Abstractions;
- namespace Avalonia.LeakTests
- {
- [DotMemoryUnit(FailIfRunWithoutSupport = false)]
- public class TransitionTests
- {
- public TransitionTests(ITestOutputHelper atr)
- {
- DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine);
- }
- [Fact]
- public void Transition_On_StyledProperty_Is_Freed()
- {
- var clock = new MockGlobalClock();
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(globalClock: clock)))
- {
- Func<Border> run = () =>
- {
- var border = new Border
- {
- Transitions = new Transitions()
- {
- new DoubleTransition
- {
- Duration = TimeSpan.FromSeconds(1),
- Property = Border.OpacityProperty,
- }
- }
- };
- var window = new Window();
- window.Content = border;
- window.Show();
- border.Opacity = 0;
- clock.Pulse(TimeSpan.FromSeconds(0));
- clock.Pulse(TimeSpan.FromSeconds(0.5));
- Assert.Equal(0.5, border.Opacity);
- clock.Pulse(TimeSpan.FromSeconds(1));
- Assert.Equal(0, border.Opacity);
- window.Close();
- return border;
- };
- var result = run();
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<TransitionInstance>()).ObjectsCount));
- }
- }
-
- [Fact]
- public void Shared_Transition_Collection_Is_Not_Leaking()
- {
- var clock = new MockGlobalClock();
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(globalClock: clock)))
- {
- // Our themes do share transition collections, so we need to test this scenario well.
- var sharedTransitions = new Transitions
- {
- new TransformOperationsTransition
- {
- Property = Visual.RenderTransformProperty, Duration = TimeSpan.FromSeconds(0.750)
- }
- };
- var controlTheme = new ControlTheme(typeof(Button))
- {
- BasedOn = Application.Current?.Resources[typeof(Button)] as ControlTheme,
- Setters = { new Setter(Animatable.TransitionsProperty, sharedTransitions) }
- };
-
- Func<Window> run = () =>
- {
- var button = new Button() { Theme = controlTheme };
- var window = new Window();
- window.Content = button;
- window.Show();
- window.Content = null;
- window.Close();
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Button>()).ObjectsCount));
- }
- }
- [Fact]
- public void Lazily_Created_Control_Should_Not_Leak_Transitions()
- {
- var clock = new MockGlobalClock();
- using (UnitTestApplication.Start(TestServices.StyledWindow.With(globalClock: clock)))
- {
- var sharedTransitions = new Transitions
- {
- new TransformOperationsTransition
- {
- Property = Visual.RenderTransformProperty, Duration = TimeSpan.FromSeconds(0.750)
- }
- };
- var controlTheme = new ControlTheme(typeof(Button))
- {
- BasedOn = Application.Current?.Resources[typeof(Button)] as ControlTheme,
- Setters = { new Setter(Animatable.TransitionsProperty, sharedTransitions) }
- };
-
- Func<Window> run = () =>
- {
- var window = new Window();
- window.Show();
- window.Content = new UserControl
- {
- Content = new Button() { Theme = controlTheme },
- // When invisible, Button won't be attached to the visual tree
- IsVisible = false
- };
- window.Content = null;
- window.Close();
- return window;
- };
- var result = run();
- // Process all Loaded events to free control reference(s)
- Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Button>()).ObjectsCount));
- }
- }
- }
- }
|