Skip to content

Commit

Permalink
Merge pull request #5965 from bdach/adjustable-audio-component-unbind
Browse files Browse the repository at this point in the history
Fix `DrawableAudioWrapper` unapplying adjustments between removal from hierarchy and disposal
  • Loading branch information
peppy authored Aug 14, 2023
2 parents 6bc3e8f + a3f76c9 commit 3365c86
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
63 changes: 63 additions & 0 deletions osu.Framework.Tests/Audio/DrawableAudioWrapperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Threading;
using NUnit.Framework;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Audio;
using osu.Framework.Testing;
using osu.Framework.Tests.Visual;

namespace osu.Framework.Tests.Audio
{
[HeadlessTest]
public partial class DrawableAudioWrapperTest : FrameworkTestScene
{
[Test]
public void TestAdjustmentsPreservedOnOwnedComponentAfterRemoval()
{
TrackVirtual track = null!;
SlowDisposingDrawableAudioWrapper wrapper = null!;

AddStep("add slow disposing wrapper for owned track",
() => Child = wrapper = new SlowDisposingDrawableAudioWrapper(track = new TrackVirtual(1000)));
AddStep("mute wrapper", () => wrapper.AddAdjustment(AdjustableProperty.Volume, new BindableDouble()));
AddStep("expire wrapper", () => wrapper.Expire());
AddAssert("component still muted", () => track.AggregateVolume.Value, () => Is.EqualTo(0));
AddStep("allow disposal to complete", () => wrapper.AllowDisposal.Set());
}

[Test]
public void TestAdjustmentsRevertedOnOwnedComponentAfterRemoval()
{
TrackVirtual track = null!;
SlowDisposingDrawableAudioWrapper wrapper = null!;

AddStep("add slow disposing wrapper for non-owned track",
() => Child = wrapper = new SlowDisposingDrawableAudioWrapper(track = new TrackVirtual(1000), false));
AddStep("mute wrapper", () => wrapper.AddAdjustment(AdjustableProperty.Volume, new BindableDouble()));
AddStep("expire wrapper", () => wrapper.Expire());
AddAssert("component unmuted", () => track.AggregateVolume.Value, () => Is.EqualTo(1));
AddStep("allow disposal to complete", () => wrapper.AllowDisposal.Set());
}

private partial class SlowDisposingDrawableAudioWrapper : DrawableAudioWrapper
{
public ManualResetEvent AllowDisposal { get; private set; } = new ManualResetEvent(false);

public SlowDisposingDrawableAudioWrapper(IAdjustableAudioComponent component, bool disposeUnderlyingComponentOnDispose = true)
: base(component, disposeUnderlyingComponentOnDispose)
{
}

protected override void Dispose(bool isDisposing)
{
AllowDisposal.WaitOne(10_000);

base.Dispose(isDisposing);
}
}
}
}
3 changes: 2 additions & 1 deletion osu.Framework/Graphics/Audio/DrawableAudioWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ internal override void UnbindAllBindables()
{
base.UnbindAllBindables();

component?.UnbindAdjustments(adjustments);
if (!disposeUnderlyingComponentOnDispose)
component?.UnbindAdjustments(adjustments);
}

protected override void Dispose(bool isDisposing)
Expand Down

0 comments on commit 3365c86

Please sign in to comment.