1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 00:45:43 +08:00
osu-lazer/osu.Game.Tests/Editing/Checks/CheckBackgroundQualityTest.cs

143 lines
4.5 KiB
C#
Raw Normal View History

2021-04-18 07:21:51 +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
using System;
2021-04-18 07:21:51 +08:00
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using Moq;
using NUnit.Framework;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Edit;
2021-04-18 07:21:51 +08:00
using osu.Game.Rulesets.Edit.Checks;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Tests.Editing.Checks
{
[TestFixture]
public class CheckBackgroundQualityTest
{
private CheckBackgroundQuality check;
private IBeatmap beatmap;
[SetUp]
public void Setup()
{
var file = CheckTestHelpers.CreateMockFile("jpg");
2021-04-18 07:21:51 +08:00
check = new CheckBackgroundQuality();
beatmap = new Beatmap<HitObject>
{
BeatmapInfo = new BeatmapInfo
{
Metadata = new BeatmapMetadata { BackgroundFile = file.Filename },
2021-04-18 07:21:51 +08:00
BeatmapSet = new BeatmapSetInfo
{
Files = { file }
2021-04-18 07:21:51 +08:00
}
}
};
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestMissing()
2021-04-18 07:21:51 +08:00
{
// While this is a problem, it is out of scope for this check and is caught by a different one.
beatmap.Metadata.BackgroundFile = string.Empty;
var context = getContext(null, new MemoryStream(Array.Empty<byte>()));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
Assert.That(check.Run(context), Is.Empty);
2021-04-18 07:21:51 +08:00
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestAcceptable()
2021-04-18 07:21:51 +08:00
{
var context = getContext(new Texture(1920, 1080));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
Assert.That(check.Run(context), Is.Empty);
2021-04-18 07:21:51 +08:00
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestTooHighResolution()
2021-04-18 07:21:51 +08:00
{
var context = getContext(new Texture(3840, 2160));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
var issues = check.Run(context).ToList();
2021-04-18 07:21:51 +08:00
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckBackgroundQuality.IssueTemplateTooHighResolution);
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestLowResolution()
2021-04-18 07:21:51 +08:00
{
var context = getContext(new Texture(640, 480));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
var issues = check.Run(context).ToList();
2021-04-18 07:21:51 +08:00
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckBackgroundQuality.IssueTemplateLowResolution);
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestTooLowResolution()
2021-04-18 07:21:51 +08:00
{
var context = getContext(new Texture(100, 100));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
var issues = check.Run(context).ToList();
2021-04-18 07:21:51 +08:00
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckBackgroundQuality.IssueTemplateTooLowResolution);
}
[Test]
2021-04-20 07:32:22 +08:00
public void TestTooUncompressed()
2021-04-18 07:21:51 +08:00
{
2021-11-10 13:18:40 +08:00
var context = getContext(new Texture(1920, 1080), new MemoryStream(new byte[1024 * 1024 * 3]));
2021-04-18 07:21:51 +08:00
2021-05-13 17:24:22 +08:00
var issues = check.Run(context).ToList();
2021-04-18 07:21:51 +08:00
Assert.That(issues, Has.Count.EqualTo(1));
Assert.That(issues.Single().Template is CheckBackgroundQuality.IssueTemplateTooUncompressed);
}
2021-11-10 12:04:30 +08:00
[Test]
public void TestStreamClosed()
{
var background = new Texture(1920, 1080);
var stream = new Mock<MemoryStream>(new byte[1024 * 1024]);
2021-11-10 13:18:40 +08:00
var context = getContext(background, stream.Object);
2021-11-10 12:04:30 +08:00
Assert.That(check.Run(context), Is.Empty);
stream.Verify(x => x.Close(), Times.Once());
}
2021-11-10 13:18:40 +08:00
private BeatmapVerifierContext getContext(Texture background, [CanBeNull] Stream stream = null)
{
2021-11-10 13:18:40 +08:00
return new BeatmapVerifierContext(beatmap, getMockWorkingBeatmap(background, stream).Object);
}
2021-04-18 07:21:51 +08:00
/// <summary>
2021-11-10 13:18:40 +08:00
/// Returns the mock of the working beatmap with the given background and its file stream.
2021-04-18 07:21:51 +08:00
/// </summary>
/// <param name="background">The texture of the background.</param>
2021-11-10 13:18:40 +08:00
/// <param name="stream">The stream representing the background file.</param>
private Mock<IWorkingBeatmap> getMockWorkingBeatmap(Texture background, [CanBeNull] Stream stream = null)
2021-04-18 07:21:51 +08:00
{
2021-11-10 13:18:40 +08:00
stream ??= new MemoryStream(new byte[1024 * 1024]);
2021-04-18 07:21:51 +08:00
var mock = new Mock<IWorkingBeatmap>();
2021-04-20 19:29:14 +08:00
mock.SetupGet(w => w.Beatmap).Returns(beatmap);
mock.SetupGet(w => w.Background).Returns(background);
mock.Setup(w => w.GetStream(It.IsAny<string>())).Returns(stream);
2021-04-18 07:21:51 +08:00
return mock;
}
}
}