Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/Core/src/Animations/AnimationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,25 @@ void OnAnimationTick(Animation animation)

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
if (_disposedValue)
{
if (disposing && Ticker is IDisposable disposable)
disposable.Dispose();
return;
}

_disposedValue = true;

if (!disposing)
{
return;
}

_disposedValue = true;
Ticker.Fire -= OnFire;
End();
_animations.Clear();

if (Ticker is IDisposable disposable)
{
disposable.Dispose();
}
}

Expand Down
115 changes: 115 additions & 0 deletions src/Core/tests/UnitTests/Animations/AnimationManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#nullable enable
using System;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Animations;
using Xunit;

namespace Microsoft.Maui.UnitTests
{
[Category(TestCategory.Animations)]
public class AnimationManagerTests
{
[Fact]
public void DisposeReleasesOwnedTickerCallbackWithoutDisposingAnimation()
{
var ticker = new TestTicker();
var manager = new AnimationManager(ticker);
bool stepCalled = false;
bool finishedCalled = false;
var animation = new Animation(_ => stepCalled = true, duration: 10, finished: () => finishedCalled = true);

manager.Add(animation);
manager.Dispose();

Assert.Null(ticker.Fire);
Assert.False(ticker.IsRunning);
Assert.Equal(1, ticker.StopCount);
Assert.Equal(1, ticker.DisposeCount);
Assert.False(animation.IsDisposed);
Assert.NotNull(animation.Step);
Assert.NotNull(animation.Finished);
Assert.False(stepCalled);
Assert.False(finishedCalled);
}

[Fact]
public void DisposeReleasesAnimationPayloadWhenTickerOutlivesManager()
{
var ticker = new TestTicker();
WeakReference<object> payload = CreateDisposedManagerPayload(ticker);

CollectGarbage();

Assert.False(payload.TryGetTarget(out _));
GC.KeepAlive(ticker);
}

[Fact]
public void DisposeRemovesOwnedCallbackFromMulticastDelegate()
{
var ticker = new TestTicker();
var manager = new AnimationManager(ticker);
bool stepCalled = false;
int otherCallbackCount = 0;
manager.Add(new Animation(_ => stepCalled = true, duration: 10));
ticker.Fire += () => otherCallbackCount++;

manager.Dispose();
ticker.Fire?.Invoke();

Assert.False(stepCalled);
Assert.Equal(1, otherCallbackCount);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference<object> CreateDisposedManagerPayload(TestTicker ticker)
{
var payload = new object();
var manager = new AnimationManager(ticker);
manager.Add(new Animation(_ => GC.KeepAlive(payload), duration: 10));
manager.Dispose();
return new(payload);
}

static void CollectGarbage()
{
for (int i = 0; i < 3; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}

class TestTicker : ITicker, IDisposable
{
public bool IsRunning { get; private set; }

public bool SystemEnabled { get; set; } = true;

public int MaxFps { get; set; } = 60;

public Action? Fire { get; set; }

public int DisposeCount { get; private set; }

public int StopCount { get; private set; }

public void Start()
{
IsRunning = true;
}

public void Stop()
{
StopCount++;
IsRunning = false;
}

public void Dispose()
{
DisposeCount++;
}
}
}
}
Loading