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-11-24 04:25:26 +00:00
|
|
|
using System.Diagnostics;
|
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-05-31 09:37:32 +00:00
|
|
|
using osu.Framework.IO.Stores;
|
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]
|
2021-02-05 03:24:38 +00:00
|
|
|
public class TestSceneOnlinePlayBeatmapAvailabilityTracker : OsuTestScene
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
|
|
|
private RulesetStore rulesets;
|
|
|
|
private TestBeatmapManager beatmaps;
|
2021-11-25 08:23:46 +00:00
|
|
|
private TestBeatmapModelDownloader beatmapDownloader;
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
private string testBeatmapFile;
|
|
|
|
private BeatmapInfo testBeatmapInfo;
|
|
|
|
private BeatmapSetInfo testBeatmapSet;
|
|
|
|
|
|
|
|
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
|
2021-04-07 07:45:06 +00:00
|
|
|
private OnlinePlayBeatmapAvailabilityTracker availabilityTracker;
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(AudioManager audio, GameHost host)
|
|
|
|
{
|
2022-01-25 03:58:15 +00:00
|
|
|
Dependencies.Cache(rulesets = new RulesetStore(Realm));
|
|
|
|
Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
|
2022-02-01 20:52:16 +00:00
|
|
|
Dependencies.CacheAs<BeatmapModelDownloader>(beatmapDownloader = new TestBeatmapModelDownloader(beatmaps, API));
|
2021-01-16 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp() => Schedule(() =>
|
|
|
|
{
|
2021-01-17 18:02:33 +00:00
|
|
|
beatmaps.AllowImport = new TaskCompletionSource<bool>();
|
|
|
|
|
2021-02-22 05:53:32 +00:00
|
|
|
testBeatmapFile = TestResources.GetQuickTestBeatmapForImport();
|
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;
|
|
|
|
|
2022-01-25 03:58:15 +00:00
|
|
|
Realm.Write(r => r.RemoveAll<BeatmapSetInfo>());
|
|
|
|
Realm.Write(r => r.RemoveAll<BeatmapInfo>());
|
2021-01-16 20:31:15 +00:00
|
|
|
|
|
|
|
selectedItem.Value = new PlaylistItem
|
|
|
|
{
|
|
|
|
Beatmap = { Value = testBeatmapInfo },
|
|
|
|
Ruleset = { Value = testBeatmapInfo.Ruleset },
|
|
|
|
};
|
|
|
|
|
2021-04-07 07:45:06 +00:00
|
|
|
Child = availabilityTracker = new OnlinePlayBeatmapAvailabilityTracker
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
|
|
|
SelectedItem = { BindTarget = selectedItem, }
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBeatmapDownloadingFlow()
|
|
|
|
{
|
|
|
|
AddAssert("ensure beatmap unavailable", () => !beatmaps.IsAvailableLocally(testBeatmapSet));
|
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
2021-11-25 08:23:46 +00:00
|
|
|
AddStep("start downloading", () => beatmapDownloader.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-11-25 08:23:46 +00:00
|
|
|
AddStep("set progress 40%", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet)).SetProgress(0.4f));
|
2021-01-17 18:16:45 +00:00
|
|
|
addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f));
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-11-25 08:23:46 +00:00
|
|
|
AddStep("finish download", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile));
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing);
|
|
|
|
|
2021-01-17 18:02:33 +00:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true));
|
2022-01-25 06:23:51 +00:00
|
|
|
AddUntilStep("wait for import", () => beatmaps.CurrentImport != null);
|
2021-01-16 20:31:15 +00:00
|
|
|
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));
|
2022-01-03 08:02:15 +00:00
|
|
|
AddStep("import beatmap", () => beatmaps.Import(testBeatmapFile).WaitSafely());
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
|
2021-12-17 09:26:12 +00:00
|
|
|
AddStep("delete beatmap", () => beatmaps.Delete(beatmaps.QueryBeatmapSet(b => b.OnlineID == testBeatmapSet.OnlineID)!.Value));
|
2021-01-16 20:31:15 +00:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
2021-12-17 09:26:12 +00:00
|
|
|
AddStep("undelete beatmap", () => beatmaps.Undelete(beatmaps.QueryBeatmapSet(b => b.OnlineID == testBeatmapSet.OnlineID)!.Value));
|
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));
|
2022-01-03 08:02:15 +00:00
|
|
|
AddStep("import beatmap", () => beatmaps.Import(testBeatmapFile).WaitSafely());
|
2021-12-14 10:11:23 +00:00
|
|
|
addAvailabilityCheckStep("initially locally available", BeatmapAvailability.LocallyAvailable);
|
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
|
|
|
{
|
2022-01-03 08:02:15 +00:00
|
|
|
beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).WaitSafely();
|
2021-01-16 20:31:15 +00:00
|
|
|
});
|
2021-12-14 10:11:23 +00:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-04-07 07:45:06 +00:00
|
|
|
AddStep("recreate tracker", () => Child = availabilityTracker = new OnlinePlayBeatmapAvailabilityTracker
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
|
|
|
SelectedItem = { BindTarget = selectedItem }
|
|
|
|
});
|
|
|
|
addAvailabilityCheckStep("state not downloaded as well", BeatmapAvailability.NotDownloaded);
|
2021-12-14 10:11:23 +00:00
|
|
|
|
2022-01-03 08:02:15 +00:00
|
|
|
AddStep("reimport original beatmap", () => beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely());
|
2021-12-14 10:11:23 +00:00
|
|
|
addAvailabilityCheckStep("locally available after re-import", BeatmapAvailability.LocallyAvailable);
|
2021-01-16 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addAvailabilityCheckStep(string description, Func<BeatmapAvailability> expected)
|
|
|
|
{
|
2021-10-27 14:54:02 +00:00
|
|
|
AddUntilStep(description, () => availabilityTracker.Availability.Value.Equals(expected.Invoke()));
|
2021-01-16 20:31:15 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2021-11-24 04:25:26 +00:00
|
|
|
|
|
|
|
Debug.Assert(info.BeatmapSet != null);
|
|
|
|
|
|
|
|
info.BeatmapSet.Beatmaps.Add(info);
|
2021-01-19 15:43:16 +00:00
|
|
|
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
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public Live<BeatmapSetInfo> CurrentImport { get; private set; }
|
2021-02-02 13:54:40 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
public TestBeatmapManager(Storage storage, RealmAccess realm, RulesetStore rulesets, IAPIProvider api, [NotNull] AudioManager audioManager, IResourceStore<byte[]> resources, GameHost host = null, WorkingBeatmap defaultBeatmap = null)
|
|
|
|
: base(storage, realm, rulesets, api, audioManager, resources, host, defaultBeatmap)
|
2021-09-30 09:21:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
protected override BeatmapModelManager CreateBeatmapModelManager(Storage storage, RealmAccess realm, RulesetStore rulesets, BeatmapOnlineLookupQueue onlineLookupQueue)
|
2021-09-30 07:45:32 +00:00
|
|
|
{
|
2022-02-01 20:39:52 +00:00
|
|
|
return new TestBeatmapModelManager(this, storage, realm, onlineLookupQueue);
|
2021-09-30 07:45:32 +00:00
|
|
|
}
|
2021-01-16 20:31:15 +00:00
|
|
|
|
2021-09-30 07:45:32 +00:00
|
|
|
internal class TestBeatmapModelManager : BeatmapModelManager
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
2021-09-30 07:45:32 +00:00
|
|
|
private readonly TestBeatmapManager testBeatmapManager;
|
|
|
|
|
2022-02-01 20:39:52 +00:00
|
|
|
public TestBeatmapModelManager(TestBeatmapManager testBeatmapManager, Storage storage, RealmAccess databaseAccess, BeatmapOnlineLookupQueue beatmapOnlineLookupQueue)
|
2022-01-24 10:59:58 +00:00
|
|
|
: base(databaseAccess, storage, beatmapOnlineLookupQueue)
|
2021-09-30 07:45:32 +00:00
|
|
|
{
|
|
|
|
this.testBeatmapManager = testBeatmapManager;
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override Live<BeatmapSetInfo> Import(BeatmapSetInfo item, ArchiveReader archive = null, bool lowPriority = false, CancellationToken cancellationToken = default)
|
2021-09-30 07:45:32 +00:00
|
|
|
{
|
2022-01-25 06:23:51 +00:00
|
|
|
testBeatmapManager.AllowImport.Task.WaitSafely();
|
|
|
|
return (testBeatmapManager.CurrentImport = base.Import(item, archive, lowPriority, cancellationToken));
|
2021-09-30 07:45:32 +00:00
|
|
|
}
|
2021-01-16 20:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 08:23:46 +00:00
|
|
|
internal class TestBeatmapModelDownloader : BeatmapModelDownloader
|
|
|
|
{
|
2022-02-01 20:52:16 +00:00
|
|
|
public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider)
|
2021-11-25 08:42:41 +00:00
|
|
|
: base(importer, apiProvider)
|
2021-11-25 08:23:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize)
|
|
|
|
=> new TestDownloadRequest(set);
|
|
|
|
}
|
|
|
|
|
2021-11-05 08:37:05 +00:00
|
|
|
private class TestDownloadRequest : ArchiveDownloadRequest<IBeatmapSetInfo>
|
2021-01-16 20:31:15 +00:00
|
|
|
{
|
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
|
|
|
|
2021-11-05 08:37:05 +00:00
|
|
|
public TestDownloadRequest(IBeatmapSetInfo model)
|
2021-01-16 20:31:15 +00:00
|
|
|
: base(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string Target => null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|