1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 13:02:56 +08:00
osu-lazer/osu.Game.Tests/Visual/UserInterface/TestSceneUpdateableBeatmapSetCover.cs

184 lines
6.6 KiB
C#
Raw Normal View History

2021-01-24 04:29:32 +08:00
// 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.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-01-24 04:29:32 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests.Responses;
2021-01-24 04:29:32 +08:00
using osuTK;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneUpdateableBeatmapSetCover : OsuTestScene
{
[Test]
public void TestLocal([Values] BeatmapSetCoverType coverType)
{
AddStep("setup cover", () => Child = new UpdateableOnlineBeatmapSetCover(coverType)
2021-01-24 04:29:32 +08:00
{
OnlineInfo = CreateAPIBeatmapSet(),
2021-01-24 04:29:32 +08:00
RelativeSizeAxes = Axes.Both,
Masking = true,
});
AddUntilStep("wait for load", () => this.ChildrenOfType<OnlineBeatmapSetCover>().SingleOrDefault()?.IsLoaded ?? false);
2021-01-24 04:29:32 +08:00
}
[Test]
public void TestUnloadAndReload()
{
OsuScrollContainer scroll = null;
List<UpdateableOnlineBeatmapSetCover> covers = new List<UpdateableOnlineBeatmapSetCover>();
2021-01-24 04:29:32 +08:00
AddStep("setup covers", () =>
{
var beatmapSet = CreateAPIBeatmapSet();
2021-01-24 04:29:32 +08:00
FillFlowContainer fillFlow;
Child = scroll = new OsuScrollContainer
{
Size = new Vector2(500f),
Child = fillFlow = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(10),
Padding = new MarginPadding { Bottom = 550 }
}
};
2021-05-03 21:57:57 +08:00
var coverTypes = Enum.GetValues(typeof(BeatmapSetCoverType))
.Cast<BeatmapSetCoverType>()
.ToList();
2021-01-24 04:29:32 +08:00
for (int i = 0; i < 25; i++)
{
2021-05-03 21:57:57 +08:00
var coverType = coverTypes[i % coverTypes.Count];
2021-01-24 04:29:32 +08:00
var cover = new UpdateableOnlineBeatmapSetCover(coverType)
2021-01-24 04:29:32 +08:00
{
OnlineInfo = beatmapSet,
2021-01-24 04:29:32 +08:00
Height = 100,
Masking = true,
};
if (coverType == BeatmapSetCoverType.Cover)
cover.Width = 500;
else if (coverType == BeatmapSetCoverType.Card)
cover.Width = 400;
else if (coverType == BeatmapSetCoverType.List)
cover.Size = new Vector2(100, 50);
fillFlow.Add(cover);
covers.Add(cover);
}
});
var loadedCovers = covers.Where(c => c.ChildrenOfType<OnlineBeatmapSetCover>().SingleOrDefault()?.IsLoaded ?? false);
2021-01-24 04:29:32 +08:00
AddUntilStep("some loaded", () => loadedCovers.Any());
AddStep("scroll to end", () => scroll.ScrollToEnd());
AddUntilStep("all unloaded", () => !loadedCovers.Any());
}
[Test]
public void TestSetNullBeatmapWhileLoading()
{
TestUpdateableOnlineBeatmapSetCover updateableCover = null;
2021-01-24 04:29:32 +08:00
AddStep("setup cover", () => Child = updateableCover = new TestUpdateableOnlineBeatmapSetCover
2021-01-24 04:29:32 +08:00
{
OnlineInfo = CreateAPIBeatmapSet(),
2021-01-24 04:29:32 +08:00
RelativeSizeAxes = Axes.Both,
Masking = true,
});
AddStep("change model", () => updateableCover.OnlineInfo = null);
2021-01-24 04:29:32 +08:00
AddWaitStep("wait some", 5);
AddAssert("no cover added", () => !updateableCover.ChildrenOfType<DelayedLoadUnloadWrapper>().Any());
}
[Test]
public void TestCoverChangeOnNewBeatmap()
{
TestUpdateableOnlineBeatmapSetCover updateableCover = null;
OnlineBeatmapSetCover initialCover = null;
AddStep("setup cover", () => Child = updateableCover = new TestUpdateableOnlineBeatmapSetCover(0)
{
OnlineInfo = createBeatmapWithCover("https://assets.ppy.sh/beatmaps/1189904/covers/cover.jpg"),
RelativeSizeAxes = Axes.Both,
Masking = true,
Alpha = 0.4f
});
AddUntilStep("cover loaded", () => updateableCover.ChildrenOfType<OnlineBeatmapSetCover>().Any());
AddStep("store initial cover", () => initialCover = updateableCover.ChildrenOfType<OnlineBeatmapSetCover>().Single());
AddUntilStep("wait for fade complete", () => initialCover.Alpha == 1);
AddStep("switch beatmap",
() => updateableCover.OnlineInfo = createBeatmapWithCover("https://assets.ppy.sh/beatmaps/1079428/covers/cover.jpg"));
AddUntilStep("new cover loaded", () => updateableCover.ChildrenOfType<OnlineBeatmapSetCover>().Except(new[] { initialCover }).Any());
}
private static APIBeatmapSet createBeatmapWithCover(string coverUrl) => new APIBeatmapSet
{
Covers = new BeatmapSetOnlineCovers { Cover = coverUrl }
};
private class TestUpdateableOnlineBeatmapSetCover : UpdateableOnlineBeatmapSetCover
2021-01-24 04:29:32 +08:00
{
private readonly int loadDelay;
public TestUpdateableOnlineBeatmapSetCover(int loadDelay = 10000)
{
this.loadDelay = loadDelay;
}
protected override Drawable CreateDrawable(IBeatmapSetOnlineInfo model)
{
if (model == null)
return null;
return new TestOnlineBeatmapSetCover(model, loadDelay)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fill,
};
}
2021-01-24 04:29:32 +08:00
}
private class TestOnlineBeatmapSetCover : OnlineBeatmapSetCover
2021-01-24 04:29:32 +08:00
{
private readonly int loadDelay;
public TestOnlineBeatmapSetCover(IBeatmapSetOnlineInfo set, int loadDelay)
: base(set)
2021-01-24 04:29:32 +08:00
{
this.loadDelay = loadDelay;
2021-01-24 04:29:32 +08:00
}
[BackgroundDependencyLoader]
private void load()
{
Thread.Sleep(loadDelay);
2021-01-24 04:29:32 +08:00
}
}
}
}