2021-01-16 20:31:15 +00: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.
|
|
|
|
|
|
|
|
using System;
|
2021-01-19 15:43:16 +00:00
|
|
|
using System.Collections.Generic;
|
2021-01-16 20:31:15 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Bindables;
|
2021-01-19 15:43:16 +00:00
|
|
|
using osu.Framework.Extensions;
|
2021-01-16 20:31:15 +00:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Beatmaps;
|
2021-01-19 15:43:16 +00:00
|
|
|
using osu.Game.Beatmaps.Formats;
|
2021-01-16 20:31:15 +00:00
|
|
|
using osu.Game.Database;
|
2021-01-19 15:43:16 +00:00
|
|
|
using osu.Game.IO;
|
2021-01-16 20:31:15 +00:00
|
|
|
using osu.Game.IO.Archives;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.Rooms;
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Online
|
|
|
|
{
|
|
|
|
[HeadlessTest]
|
|
|
|
public class TestSceneMultiplayerBeatmapTracker : OsuTestScene
|
|
|
|
{
|
|
|
|
private RulesetStore rulesets;
|
|
|
|
private TestBeatmapManager beatmaps;
|
|
|
|
|
|
|
|
private string testBeatmapFile;
|
|
|
|
private BeatmapInfo testBeatmapInfo;
|
|
|
|
private BeatmapSetInfo testBeatmapSet;
|
|
|
|
|
|
|
|
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
|
|
|
|
private MultiplayerBeatmapTracker tracker;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(AudioManager audio, GameHost host)
|
|
|
|
{
|
|
|
|
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
|
|
|
|
Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, ContextFactory, rulesets, API, audio, host, Beatmap.Default));
|
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp() => Schedule(() =>
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
beatmaps.AllowImport = new TaskCompletionSource<bool>();
|
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
testBeatmapFile = TestResources.GetTestBeatmapForImport();
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
testBeatmapInfo = getTestBeatmapInfo(testBeatmapFile);
|
2021-01-16 20:31:15 +00:00
|
|
|
testBeatmapSet = testBeatmapInfo.BeatmapSet;
|
|
|
|
|
|
|
|
var existing = beatmaps.QueryBeatmapSet(s => s.OnlineBeatmapSetID == testBeatmapSet.OnlineBeatmapSetID);
|
|
|
|
if (existing != null)
|
|
|
|
beatmaps.Delete(existing);
|
|
|
|
|
|
|
|
selectedItem.Value = new PlaylistItem
|
|
|
|
{
|
|
|
|
Beatmap = { Value = testBeatmapInfo },
|
|
|
|
Ruleset = { Value = testBeatmapInfo.Ruleset },
|
|
|
|
};
|
|
|
|
|
|
|
|
Child = tracker = new MultiplayerBeatmapTracker
|
|
|
|
{
|
|
|
|
SelectedItem = { BindTarget = selectedItem, }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBeatmapDownloadingFlow()
|
|
|
|
{
|
|
|
|
AddAssert("ensure beatmap unavailable", () => !beatmaps.IsAvailableLocally(testBeatmapSet));
|
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
|
|
|
AddStep("start downloading", () => beatmaps.Download(testBeatmapSet));
|
2021-01-17 18:16:45 +00:00
|
|
|
addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0f));
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-01-17 18:16:45 +00:00
|
|
|
AddStep("set progress 40%", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).SetProgress(0.4f));
|
|
|
|
addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f));
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
AddStep("finish download", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile));
|
|
|
|
addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing);
|
|
|
|
|
2021-01-17 18:02:33 +00:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true));
|
2021-01-16 20:31:15 +00:00
|
|
|
AddUntilStep("wait for import", () => beatmaps.IsAvailableLocally(testBeatmapSet));
|
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestTrackerRespectsSoftDeleting()
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true));
|
2021-01-19 15:36:05 +00:00
|
|
|
AddStep("import beatmap", () => beatmaps.Import(testBeatmapFile).Wait());
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
|
2021-01-19 15:36:05 +00:00
|
|
|
AddStep("delete beatmap", () => beatmaps.Delete(beatmaps.QueryBeatmapSet(b => b.OnlineBeatmapSetID == testBeatmapSet.OnlineBeatmapSetID)));
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
2021-01-19 15:36:05 +00:00
|
|
|
AddStep("undelete beatmap", () => beatmaps.Undelete(beatmaps.QueryBeatmapSet(b => b.OnlineBeatmapSetID == testBeatmapSet.OnlineBeatmapSetID)));
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestTrackerRespectsChecksum()
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true));
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
AddStep("import altered beatmap", () =>
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
2021-01-19 15:43:16 +00:00
|
|
|
beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).Wait();
|
2021-01-16 20:31:15 +00:00
|
|
|
});
|
|
|
|
addAvailabilityCheckStep("state still not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
|
|
|
AddStep("recreate tracker", () => Child = tracker = new MultiplayerBeatmapTracker
|
|
|
|
{
|
|
|
|
SelectedItem = { BindTarget = selectedItem }
|
|
|
|
});
|
|
|
|
addAvailabilityCheckStep("state not downloaded as well", BeatmapAvailability.NotDownloaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addAvailabilityCheckStep(string description, Func<BeatmapAvailability> expected)
|
|
|
|
{
|
2021-01-18 16:20:19 +00:00
|
|
|
// In DownloadTrackingComposite, state changes are scheduled one frame later, wait one step.
|
|
|
|
AddWaitStep("wait for potential change", 1);
|
2021-01-16 20:31:15 +00:00
|
|
|
AddAssert(description, () => tracker.Availability.Value.Equals(expected.Invoke()));
|
|
|
|
}
|
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
private static BeatmapInfo getTestBeatmapInfo(string archiveFile)
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
2021-01-19 15:43:16 +00:00
|
|
|
BeatmapInfo info;
|
|
|
|
|
|
|
|
using (var archive = new ZipArchiveReader(File.OpenRead(archiveFile)))
|
|
|
|
using (var stream = archive.GetStream("Soleily - Renatus (Gamu) [Insane].osu"))
|
|
|
|
using (var reader = new LineBufferedReader(stream))
|
|
|
|
{
|
|
|
|
var decoder = Decoder.GetDecoder<Beatmap>(reader);
|
|
|
|
var beatmap = decoder.Decode(reader);
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
info = beatmap.BeatmapInfo;
|
|
|
|
info.BeatmapSet.Beatmaps = new List<BeatmapInfo> { info };
|
|
|
|
info.BeatmapSet.Metadata = info.Metadata;
|
|
|
|
info.MD5Hash = stream.ComputeMD5Hash();
|
|
|
|
info.Hash = stream.ComputeSHA2Hash();
|
|
|
|
}
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-01-19 15:43:16 +00:00
|
|
|
return info;
|
2021-01-16 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class TestBeatmapManager : BeatmapManager
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
public TaskCompletionSource<bool> AllowImport = new TaskCompletionSource<bool>();
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
protected override ArchiveDownloadRequest<BeatmapSetInfo> CreateDownloadRequest(BeatmapSetInfo set, bool minimiseDownloadSize)
|
|
|
|
=> new TestDownloadRequest(set);
|
|
|
|
|
|
|
|
public TestBeatmapManager(Storage storage, IDatabaseContextFactory contextFactory, RulesetStore rulesets, IAPIProvider api, [NotNull] AudioManager audioManager, GameHost host = null, WorkingBeatmap defaultBeatmap = null, bool performOnlineLookups = false)
|
|
|
|
: base(storage, contextFactory, rulesets, api, audioManager, host, defaultBeatmap, performOnlineLookups)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task<BeatmapSetInfo> Import(BeatmapSetInfo item, ArchiveReader archive = null, CancellationToken cancellationToken = default)
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
await AllowImport.Task;
|
2021-01-16 20:31:15 +00:00
|
|
|
return await base.Import(item, archive, cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class TestDownloadRequest : ArchiveDownloadRequest<BeatmapSetInfo>
|
|
|
|
{
|
2021-01-17 18:16:45 +00:00
|
|
|
public new void SetProgress(float progress) => base.SetProgress(progress);
|
2021-01-17 19:08:28 +00:00
|
|
|
public new void TriggerSuccess(string filename) => base.TriggerSuccess(filename);
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
public TestDownloadRequest(BeatmapSetInfo model)
|
|
|
|
: base(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string Target => null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|