1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 23:32:54 +08:00
osu-lazer/osu.Game/Tests/Visual/TestCasePlayer.cs

132 lines
3.8 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-06-13 14:12:10 +08:00
using System;
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Shapes;
2018-06-13 14:12:10 +08:00
using osu.Framework.Lists;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Play;
using osu.Game.Tests.Beatmaps;
using OpenTK.Graphics;
namespace osu.Game.Tests.Visual
{
public abstract class TestCasePlayer : ScreenTestCase
{
private readonly Ruleset ruleset;
protected Player Player;
protected TestCasePlayer(Ruleset ruleset)
{
this.ruleset = ruleset;
}
protected TestCasePlayer()
{
}
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
{
Add(new Box
{
RelativeSizeAxes = Framework.Graphics.Axes.Both,
Colour = Color4.Black,
Depth = int.MaxValue
});
if (ruleset != null)
{
Player p = null;
AddStep(ruleset.RulesetInfo.Name, () => p = loadPlayerFor(ruleset));
AddUntilStep(() => ContinueCondition(p));
2018-04-13 17:19:50 +08:00
}
else
{
foreach (var r in rulesets.AvailableRulesets)
{
Player p = null;
AddStep(r.Name, () => p = loadPlayerFor(r));
AddUntilStep(() => ContinueCondition(p));
2018-06-13 14:26:05 +08:00
2018-06-13 14:12:10 +08:00
AddAssert("no leaked beatmaps", () =>
{
p = null;
GC.Collect();
GC.WaitForPendingFinalizers();
int count = 0;
workingWeakReferences.ForEachAlive(_ => count++);
2018-06-13 14:26:05 +08:00
return count == 1;
});
2018-06-13 14:12:10 +08:00
2018-06-13 14:26:05 +08:00
AddAssert("no leaked players", () =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
int count = 0;
2018-06-13 14:12:10 +08:00
2018-06-13 14:26:05 +08:00
playerWeakReferences.ForEachAlive(_ => count++);
2018-06-13 14:12:10 +08:00
return count == 1;
});
2018-04-13 17:19:50 +08:00
}
}
}
protected virtual bool ContinueCondition(Player player) => player.IsLoaded;
2018-04-19 19:44:38 +08:00
protected virtual IBeatmap CreateBeatmap(Ruleset ruleset) => new TestBeatmap(ruleset.RulesetInfo);
2018-04-13 17:19:50 +08:00
2018-06-13 14:12:10 +08:00
private readonly WeakList<WorkingBeatmap> workingWeakReferences = new WeakList<WorkingBeatmap>();
2018-06-13 14:26:05 +08:00
private readonly WeakList<Player> playerWeakReferences = new WeakList<Player>();
2018-06-13 14:12:10 +08:00
2018-04-13 17:19:50 +08:00
private Player loadPlayerFor(RulesetInfo ri) => loadPlayerFor(ri.CreateInstance());
private Player loadPlayerFor(Ruleset r)
{
var beatmap = CreateBeatmap(r);
2018-06-13 14:12:10 +08:00
var working = new TestWorkingBeatmap(beatmap);
workingWeakReferences.Add(working);
2018-04-13 17:19:50 +08:00
2018-06-13 14:12:10 +08:00
Beatmap.Value = working;
Beatmap.Value.Mods.Value = new[] { r.GetAllMods().First(m => m is ModNoFail) };
2018-04-13 17:19:50 +08:00
2018-06-13 14:12:10 +08:00
Player?.Exit();
2018-04-13 17:19:50 +08:00
var player = CreatePlayer(r);
2018-04-13 17:19:50 +08:00
2018-06-13 14:26:05 +08:00
playerWeakReferences.Add(player);
2018-06-13 14:12:10 +08:00
LoadComponentAsync(player, p =>
{
Player = p;
LoadScreen(p);
});
2018-04-13 17:19:50 +08:00
return player;
}
protected override void Update()
{
base.Update();
// note that this will override any mod rate application
Beatmap.Value.Track.Rate = Clock.Rate;
2018-04-13 17:19:50 +08:00
}
protected virtual Player CreatePlayer(Ruleset ruleset) => new Player
2018-04-13 17:19:50 +08:00
{
AllowPause = false,
AllowLeadIn = false,
AllowResults = false,
};
}
}