1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 20:22:55 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/WorkingBeatmapManagerTest.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

139 lines
4.9 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.
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Extensions;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
2022-10-09 22:19:21 +08:00
using osu.Game.Collections;
using osu.Game.Database;
using osu.Game.Rulesets;
2022-10-09 22:19:21 +08:00
using osu.Game.Rulesets.Osu;
using osu.Game.Tests.Resources;
using osu.Game.Tests.Visual;
namespace osu.Game.Tests.Beatmaps
{
[HeadlessTest]
public partial class WorkingBeatmapManagerTest : OsuTestScene
{
private BeatmapManager beatmaps = null!;
private BeatmapSetInfo importedSet = null!;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio, RulesetStore rulesets)
{
Dependencies.Cache(beatmaps = new BeatmapManager(LocalStorage, Realm, null, audio, Resources, host, Beatmap.Default));
}
[SetUpSteps]
public void SetUpSteps()
{
AddStep("import beatmap", () =>
{
beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
importedSet = beatmaps.GetAllUsableBeatmapSets().First();
});
}
[Test]
public void TestGetWorkingBeatmap() => AddStep("run test", () =>
{
Assert.That(beatmaps.GetWorkingBeatmap(importedSet.Beatmaps.First()), Is.Not.Null);
});
[Test]
public void TestCachedRetrievalNoFiles() => AddStep("run test", () =>
{
var beatmap = importedSet.Beatmaps.First();
Assert.That(beatmap.BeatmapSet?.Files, Is.Empty);
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap);
Assert.That(first, Is.SameAs(second));
Assert.That(first.BeatmapInfo.BeatmapSet?.Files, Has.Count.GreaterThan(0));
});
[Test]
public void TestCachedRetrievalWithFiles() => AddStep("run test", () =>
{
2023-07-06 12:37:42 +08:00
var beatmap = Realm.Run(r => r.Find<BeatmapInfo>(importedSet.Beatmaps.First().ID)!.Detach());
Assert.That(beatmap.BeatmapSet?.Files, Has.Count.GreaterThan(0));
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap);
Assert.That(first, Is.SameAs(second));
Assert.That(first.BeatmapInfo.BeatmapSet?.Files, Has.Count.GreaterThan(0));
});
[Test]
public void TestForcedRefetchRetrievalNoFiles() => AddStep("run test", () =>
{
var beatmap = importedSet.Beatmaps.First();
Assert.That(beatmap.BeatmapSet?.Files, Is.Empty);
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap, true);
Assert.That(first, Is.Not.SameAs(second));
});
[Test]
public void TestForcedRefetchRetrievalWithFiles() => AddStep("run test", () =>
{
2023-07-06 12:37:42 +08:00
var beatmap = Realm.Run(r => r.Find<BeatmapInfo>(importedSet.Beatmaps.First().ID)!.Detach());
Assert.That(beatmap.BeatmapSet?.Files, Has.Count.GreaterThan(0));
var first = beatmaps.GetWorkingBeatmap(beatmap);
var second = beatmaps.GetWorkingBeatmap(beatmap, true);
Assert.That(first, Is.Not.SameAs(second));
});
2022-10-09 22:19:21 +08:00
[Test]
public void TestSavePreservesCollections() => AddStep("run test", () =>
{
2023-07-06 12:37:42 +08:00
var beatmap = Realm.Run(r => r.Find<BeatmapInfo>(importedSet.Beatmaps.First().ID)!.Detach());
2022-10-09 22:19:21 +08:00
2022-10-11 14:53:48 +08:00
var working = beatmaps.GetWorkingBeatmap(beatmap);
2022-10-09 22:19:21 +08:00
2022-10-11 14:53:48 +08:00
Assert.That(working.BeatmapInfo.BeatmapSet?.Files, Has.Count.GreaterThan(0));
2022-10-09 22:19:21 +08:00
2022-10-11 14:53:48 +08:00
string initialHash = working.BeatmapInfo.MD5Hash;
2022-10-09 22:19:21 +08:00
2022-10-11 14:53:48 +08:00
var preserveCollection = new BeatmapCollection("test contained");
2022-10-09 22:19:21 +08:00
preserveCollection.BeatmapMD5Hashes.Add(initialHash);
2022-10-11 14:53:48 +08:00
var noNewCollection = new BeatmapCollection("test not contained");
2022-10-09 22:19:21 +08:00
Realm.Write(r =>
{
r.Add(preserveCollection);
r.Add(noNewCollection);
});
Assert.That(preserveCollection.BeatmapMD5Hashes, Does.Contain(initialHash));
Assert.That(noNewCollection.BeatmapMD5Hashes, Does.Not.Contain(initialHash));
2022-10-10 14:31:45 +08:00
2022-10-11 14:53:48 +08:00
beatmaps.Save(working.BeatmapInfo, working.GetPlayableBeatmap(new OsuRuleset().RulesetInfo));
2022-10-09 22:19:21 +08:00
2022-10-11 14:53:48 +08:00
string finalHash = working.BeatmapInfo.MD5Hash;
2022-10-09 22:19:21 +08:00
Assert.That(finalHash, Is.Not.SameAs(initialHash));
Assert.That(preserveCollection.BeatmapMD5Hashes, Does.Not.Contain(initialHash));
Assert.That(preserveCollection.BeatmapMD5Hashes, Does.Contain(finalHash));
Assert.That(noNewCollection.BeatmapMD5Hashes, Does.Not.Contain(finalHash));
});
}
}