mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 04:13:00 +08:00
Add simple tests for current implementation
This commit is contained in:
parent
d467dd5747
commit
6233dc22e0
79
osu.Game.Tests/Visual/Gameplay/TestSceneInGameLeaderboard.cs
Normal file
79
osu.Game.Tests/Visual/Gameplay/TestSceneInGameLeaderboard.cs
Normal file
@ -0,0 +1,79 @@
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Screens.Play;
|
||||
using osu.Game.Users;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Tests.Visual.Gameplay
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestSceneInGameLeaderboard : OsuTestScene
|
||||
{
|
||||
public override IReadOnlyList<Type> RequiredTypes => new[]
|
||||
{
|
||||
typeof(InGameLeaderboard),
|
||||
typeof(InGameScoreContainer),
|
||||
};
|
||||
|
||||
private readonly TestInGameLeaderboard leaderboard;
|
||||
private readonly BindableDouble playerScore;
|
||||
|
||||
public TestSceneInGameLeaderboard()
|
||||
{
|
||||
Add(leaderboard = new TestInGameLeaderboard
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
Scale = new Vector2(2),
|
||||
RelativeSizeAxes = Axes.X,
|
||||
PlayerCurrentScore = { BindTarget = playerScore = new BindableDouble(1222333) }
|
||||
});
|
||||
|
||||
AddStep("add player user", () => leaderboard.PlayerUser = new User { Username = "You" });
|
||||
AddSliderStep("set player score", 50, 5000000, 1222333, v => playerScore.Value = v);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestPlayerScore()
|
||||
{
|
||||
var player2Score = new BindableDouble(1234567);
|
||||
var player3Score = new BindableDouble(1111111);
|
||||
|
||||
AddStep("add player 2", () => leaderboard.AddDummyPlayer(player2Score, "Player 2"));
|
||||
AddStep("add player 3", () => leaderboard.AddDummyPlayer(player3Score, "Player 3"));
|
||||
|
||||
AddAssert("is player 2 position #1", () => leaderboard.CheckPositionByUsername("Player 2", 1));
|
||||
AddAssert("is player position #2", () => leaderboard.CheckPositionByUsername("You", 2));
|
||||
AddAssert("is player 3 position #3", () => leaderboard.CheckPositionByUsername("Player 3", 3));
|
||||
|
||||
AddStep("set score above player 3", () => player2Score.Value = playerScore.Value - 500);
|
||||
AddAssert("is player position #1", () => leaderboard.CheckPositionByUsername("You", 1));
|
||||
AddAssert("is player 2 position #2", () => leaderboard.CheckPositionByUsername("Player 2", 2));
|
||||
AddAssert("is player 3 position #3", () => leaderboard.CheckPositionByUsername("Player 3", 3));
|
||||
|
||||
AddStep("set score below players", () => player2Score.Value = playerScore.Value - 123456);
|
||||
AddAssert("is player position #1", () => leaderboard.CheckPositionByUsername("You", 1));
|
||||
AddAssert("is player 3 position #2", () => leaderboard.CheckPositionByUsername("Player 3", 2));
|
||||
AddAssert("is player 2 position #3", () => leaderboard.CheckPositionByUsername("Player 2", 3));
|
||||
}
|
||||
|
||||
private class TestInGameLeaderboard : InGameLeaderboard
|
||||
{
|
||||
public bool CheckPositionByUsername(string username, int? estimatedPosition)
|
||||
{
|
||||
var scoreItem = ScoresContainer.Where(i => i.User.Username == username).FirstOrDefault();
|
||||
|
||||
return scoreItem != null && scoreItem.ScorePosition == estimatedPosition;
|
||||
}
|
||||
|
||||
public void AddDummyPlayer(BindableDouble currentScore, string username) => ScoresContainer.AddRealTimePlayer(currentScore, new User { Username = username });
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user