1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:26:07 +08:00

Fix disposed beatmap backgrounds being loaded (#4723)

Fix disposed beatmap backgrounds being loaded
This commit is contained in:
Dean Herbert 2019-05-08 13:33:55 +09:00 committed by GitHub
commit 7b91439506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 20 deletions

View File

@ -1,22 +1,27 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Rulesets;
using osu.Game.Tests.Beatmaps.IO;
using osuTK;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestCaseUpdateableBeatmapBackgroundSprite : OsuTestCase
{
private TestUpdateableBeatmapBackgroundSprite backgroundSprite;
private BeatmapSetInfo testBeatmap;
private IAPIProvider api;
private RulesetStore rulesets;
[Resolved]
private BeatmapManager beatmaps { get; set; }
@ -24,40 +29,121 @@ namespace osu.Game.Tests.Visual.UserInterface
[BackgroundDependencyLoader]
private void load(OsuGameBase osu, IAPIProvider api, RulesetStore rulesets)
{
Bindable<BeatmapInfo> beatmapBindable = new Bindable<BeatmapInfo>();
this.api = api;
this.rulesets = rulesets;
var imported = ImportBeatmapTest.LoadOszIntoOsu(osu);
testBeatmap = ImportBeatmapTest.LoadOszIntoOsu(osu);
}
Child = backgroundSprite = new TestUpdateableBeatmapBackgroundSprite { RelativeSizeAxes = Axes.Both };
[Test]
public void TestNullBeatmap()
{
TestUpdateableBeatmapBackgroundSprite background = null;
backgroundSprite.Beatmap.BindTo(beatmapBindable);
AddStep("load null beatmap", () => Child = background = new TestUpdateableBeatmapBackgroundSprite { RelativeSizeAxes = Axes.Both });
AddUntilStep("wait for load", () => background.ContentLoaded);
}
var req = new GetBeatmapSetRequest(1);
api.Queue(req);
[Test]
public void TestLocalBeatmap()
{
TestUpdateableBeatmapBackgroundSprite background = null;
AddStep("load null beatmap", () => beatmapBindable.Value = null);
AddUntilStep("wait for cleanup...", () => backgroundSprite.ChildCount == 1);
AddStep("load imported beatmap", () => beatmapBindable.Value = imported.Beatmaps.First());
AddUntilStep("wait for cleanup...", () => backgroundSprite.ChildCount == 1);
AddStep("load local beatmap", () =>
{
Child = background = new TestUpdateableBeatmapBackgroundSprite
{
RelativeSizeAxes = Axes.Both,
Beatmap = { Value = testBeatmap.Beatmaps.First() }
};
});
AddUntilStep("wait for load", () => background.ContentLoaded);
}
[Test]
public void TestOnlineBeatmap()
{
if (api.IsLoggedIn)
{
var req = new GetBeatmapSetRequest(1);
api.Queue(req);
AddUntilStep("wait for api response", () => req.Result != null);
AddStep("load online beatmap", () => beatmapBindable.Value = new BeatmapInfo
TestUpdateableBeatmapBackgroundSprite background = null;
AddStep("load online beatmap", () =>
{
BeatmapSet = req.Result?.ToBeatmapSet(rulesets)
Child = background = new TestUpdateableBeatmapBackgroundSprite
{
RelativeSizeAxes = Axes.Both,
Beatmap = { Value = new BeatmapInfo { BeatmapSet = req.Result?.ToBeatmapSet(rulesets) } }
};
});
AddUntilStep("wait for cleanup...", () => backgroundSprite.ChildCount == 1);
AddUntilStep("wait for load", () => background.ContentLoaded);
}
else
{
AddStep("online (login first)", () => { });
}
}
[Test]
public void TestUnloadAndReload()
{
var backgrounds = new List<TestUpdateableBeatmapBackgroundSprite>();
ScrollContainer scrollContainer = null;
AddStep("create backgrounds hierarchy", () =>
{
FillFlowContainer backgroundFlow;
Child = scrollContainer = new ScrollContainer
{
Size = new Vector2(500),
Child = backgroundFlow = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(10),
Padding = new MarginPadding { Bottom = 250 }
}
};
for (int i = 0; i < 25; i++)
{
var background = new TestUpdateableBeatmapBackgroundSprite { RelativeSizeAxes = Axes.Both };
if (i % 2 == 0)
background.Beatmap.Value = testBeatmap.Beatmaps.First();
backgroundFlow.Add(new Container
{
RelativeSizeAxes = Axes.X,
Height = 100,
Masking = true,
Child = background
});
backgrounds.Add(background);
}
});
var loadedBackgrounds = backgrounds.Where(b => b.ContentLoaded);
int initialLoadCount = 0;
AddUntilStep("some loaded", () => (initialLoadCount = loadedBackgrounds.Count()) > 0);
AddStep("scroll to bottom", () => scrollContainer.ScrollToEnd());
AddUntilStep("some unloaded", () => loadedBackgrounds.Count() < initialLoadCount);
}
private class TestUpdateableBeatmapBackgroundSprite : UpdateableBeatmapBackgroundSprite
{
public int ChildCount => InternalChildren.Count;
protected override double UnloadDelay => 2000;
public bool ContentLoaded => ((DelayedLoadUnloadWrapper)InternalChildren.LastOrDefault())?.Content?.IsLoaded ?? false;
}
}
}

View File

@ -26,6 +26,11 @@ namespace osu.Game.Beatmaps.Drawables
this.beatmapSetCoverType = beatmapSetCoverType;
}
/// <summary>
/// Delay before the background is unloaded while off-screen.
/// </summary>
protected virtual double UnloadDelay => 10000;
private BeatmapInfo lastModel;
protected override DelayedLoadWrapper CreateDelayedLoadWrapper(Drawable content, double timeBeforeLoad)
@ -34,13 +39,13 @@ namespace osu.Game.Beatmaps.Drawables
{
// If DelayedLoadUnloadWrapper is attempting to RELOAD the same content (Beatmap), that means that it was
// previously UNLOADED and thus its children have been disposed of, so we need to recreate them here.
if (lastModel == Beatmap.Value && Beatmap.Value != null)
if (lastModel == Beatmap.Value)
return CreateDrawable(Beatmap.Value);
// If the model has changed since the previous unload (or if there was no load), then we can safely use the given content
lastModel = Beatmap.Value;
return content;
}, timeBeforeLoad, 10000);
}, timeBeforeLoad, UnloadDelay);
}
protected override Drawable CreateDrawable(BeatmapInfo model)