1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 11:42:58 +08:00
osu-lazer/osu.Game.Tests/Visual/Gameplay/TestSceneReplayDownloadButton.cs

235 lines
8.2 KiB
C#
Raw Normal View History

// 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
using System.Linq;
using NUnit.Framework;
2019-06-29 13:25:30 +08:00
using osu.Framework.Graphics;
using osu.Game.Online;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Graphics.UserInterface;
2021-11-01 16:14:02 +08:00
using osu.Game.Rulesets.Osu;
using osu.Game.Scoring;
2020-03-17 16:43:16 +08:00
using osu.Game.Screens.Ranking;
using osu.Game.Tests.Resources;
using osuTK.Input;
using APIUser = osu.Game.Online.API.Requests.Responses.APIUser;
2019-06-29 13:25:30 +08:00
namespace osu.Game.Tests.Visual.Gameplay
{
[TestFixture]
public class TestSceneReplayDownloadButton : OsuManualInputManagerTestScene
2019-06-29 13:25:30 +08:00
{
private const long online_score_id = 2553163309;
private TestReplayDownloadButton downloadButton;
2019-06-29 13:25:30 +08:00
[Resolved]
private BeatmapManager beatmapManager { get; set; }
[Resolved]
private ScoreManager scoreManager { get; set; }
[BackgroundDependencyLoader]
private void load()
{
beatmapManager.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
}
[SetUpSteps]
public void SetUpSteps()
{
AddStep("delete previous imports", () =>
{
scoreManager.Delete(s => s.OnlineID == online_score_id);
});
}
[Test]
public void TestDisplayStates()
2019-06-29 13:25:30 +08:00
{
AddStep(@"create button with replay", () =>
{
Child = downloadButton = new TestReplayDownloadButton(getScoreInfo(true))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddStep(@"downloading state", () => downloadButton.SetDownloadState(DownloadState.Downloading));
AddStep(@"locally available state", () => downloadButton.SetDownloadState(DownloadState.LocallyAvailable));
AddStep(@"not downloaded state", () => downloadButton.SetDownloadState(DownloadState.NotDownloaded));
}
[Test]
public void TestButtonWithReplayStartsDownload()
{
bool downloadStarted = false;
bool downloadFinished = false;
AddStep(@"create button with replay", () =>
{
downloadStarted = false;
downloadFinished = false;
Child = downloadButton = new TestReplayDownloadButton(getScoreInfo(true))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
downloadButton.State.BindValueChanged(state =>
{
switch (state.NewValue)
{
case DownloadState.Downloading:
downloadStarted = true;
break;
}
switch (state.OldValue)
{
case DownloadState.Downloading:
downloadFinished = true;
break;
}
});
});
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddAssert("state is available", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddStep("click button", () =>
{
InputManager.MoveMouseTo(downloadButton);
InputManager.Click(MouseButton.Left);
});
AddAssert("state entered downloading", () => downloadStarted);
AddUntilStep("state left downloading", () => downloadFinished);
}
[Test]
public void TestButtonWithoutReplay()
{
AddStep("create button without replay", () =>
2019-06-29 13:25:30 +08:00
{
Child = downloadButton = new TestReplayDownloadButton(getScoreInfo(false))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
2019-06-29 13:25:30 +08:00
});
2020-05-28 20:46:02 +08:00
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddAssert("state is not downloaded", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddAssert("button is not enabled", () => !downloadButton.ChildrenOfType<DownloadButton>().First().Enabled.Value);
2020-05-28 20:46:02 +08:00
}
[Test]
public void TestLocallyAvailableWithoutReplay()
{
Live<ScoreInfo> imported = null;
AddStep("import score", () => imported = scoreManager.Import(getScoreInfo(false, false)));
AddStep("create button without replay", () =>
{
Child = downloadButton = new TestReplayDownloadButton(imported.Value)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddUntilStep("state is not downloaded", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddAssert("button is not enabled", () => !downloadButton.ChildrenOfType<DownloadButton>().First().Enabled.Value);
}
[Test]
public void TestScoreImportThenDelete()
{
Live<ScoreInfo> imported = null;
AddStep("create button without replay", () =>
{
Child = downloadButton = new TestReplayDownloadButton(getScoreInfo(false))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddUntilStep("state is not downloaded", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddStep("import score", () => imported = scoreManager.Import(getScoreInfo(true)));
AddUntilStep("state is available", () => downloadButton.State.Value == DownloadState.LocallyAvailable);
AddAssert("button is enabled", () => downloadButton.ChildrenOfType<DownloadButton>().First().Enabled.Value);
AddStep("delete score", () => scoreManager.Delete(imported.Value));
AddUntilStep("state is not downloaded", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddAssert("button is not enabled", () => !downloadButton.ChildrenOfType<DownloadButton>().First().Enabled.Value);
}
[Test]
public void CreateButtonWithNoScore()
2020-05-28 20:46:02 +08:00
{
AddStep("create button with null score", () =>
{
Child = downloadButton = new TestReplayDownloadButton(null)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
AddUntilStep("wait for load", () => downloadButton.IsLoaded);
AddAssert("state is not downloaded", () => downloadButton.State.Value == DownloadState.NotDownloaded);
AddAssert("button is not enabled", () => !downloadButton.ChildrenOfType<DownloadButton>().First().Enabled.Value);
2019-06-29 13:25:30 +08:00
}
private ScoreInfo getScoreInfo(bool replayAvailable, bool hasOnlineId = true) => new ScoreInfo
2019-06-29 13:25:30 +08:00
{
OnlineID = hasOnlineId ? online_score_id : 0,
Ruleset = new OsuRuleset().RulesetInfo,
BeatmapInfo = beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps.First(),
2022-07-25 20:19:32 +08:00
Hash = replayAvailable ? "online" : string.Empty,
User = new APIUser
2019-06-29 13:25:30 +08:00
{
Id = 39828,
Username = @"WubWoofWolf",
}
};
private class TestReplayDownloadButton : ReplayDownloadButton
{
public void SetDownloadState(DownloadState state) => State.Value = state;
public new Bindable<DownloadState> State => base.State;
2019-06-29 14:59:12 +08:00
public TestReplayDownloadButton(ScoreInfo score)
: base(score)
{
}
}
2019-06-29 13:25:30 +08:00
}
}