2021-01-17 04:31:15 +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
|
|
|
|
|
2021-01-17 04:31:15 +08:00
|
|
|
using System;
|
2022-02-15 22:30:39 +08:00
|
|
|
using System.Collections.Generic;
|
2021-11-24 12:25:26 +08:00
|
|
|
using System.Diagnostics;
|
2021-01-17 04:31:15 +08:00
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Bindables;
|
2021-01-19 23:43:16 +08:00
|
|
|
using osu.Framework.Extensions;
|
2022-02-15 22:30:39 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-05-31 17:37:32 +08:00
|
|
|
using osu.Framework.IO.Stores;
|
2021-01-17 04:31:15 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Testing;
|
|
|
|
using osu.Game.Beatmaps;
|
2021-01-19 23:43:16 +08:00
|
|
|
using osu.Game.Beatmaps.Formats;
|
2021-01-17 04:31:15 +08:00
|
|
|
using osu.Game.Database;
|
2021-01-19 23:43:16 +08:00
|
|
|
using osu.Game.IO;
|
2021-01-17 04:31:15 +08:00
|
|
|
using osu.Game.IO.Archives;
|
|
|
|
using osu.Game.Online.API;
|
2022-02-15 22:30:39 +08:00
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-01-17 04:31:15 +08:00
|
|
|
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 11:24:38 +08:00
|
|
|
public class TestSceneOnlinePlayBeatmapAvailabilityTracker : OsuTestScene
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
|
|
|
private RulesetStore rulesets;
|
|
|
|
private TestBeatmapManager beatmaps;
|
2021-11-25 16:23:46 +08:00
|
|
|
private TestBeatmapModelDownloader beatmapDownloader;
|
2021-01-17 04:31:15 +08:00
|
|
|
|
|
|
|
private string testBeatmapFile;
|
|
|
|
private BeatmapInfo testBeatmapInfo;
|
|
|
|
private BeatmapSetInfo testBeatmapSet;
|
|
|
|
|
|
|
|
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
|
2021-04-07 15:45:06 +08:00
|
|
|
private OnlinePlayBeatmapAvailabilityTracker availabilityTracker;
|
2021-01-17 04:31:15 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(AudioManager audio, GameHost host)
|
|
|
|
{
|
2022-02-16 16:13:51 +08:00
|
|
|
Dependencies.Cache(rulesets = new RealmRulesetStore(Realm));
|
2022-01-25 11:58:15 +08:00
|
|
|
Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, Realm, rulesets, API, audio, Resources, host, Beatmap.Default));
|
2022-02-02 04:52:16 +08:00
|
|
|
Dependencies.CacheAs<BeatmapModelDownloader>(beatmapDownloader = new TestBeatmapModelDownloader(beatmaps, API));
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp() => Schedule(() =>
|
|
|
|
{
|
2022-02-15 22:30:39 +08:00
|
|
|
((DummyAPIAccess)API).HandleRequest = req =>
|
|
|
|
{
|
|
|
|
switch (req)
|
|
|
|
{
|
|
|
|
case GetBeatmapsRequest beatmapsReq:
|
|
|
|
var beatmap = CreateAPIBeatmap();
|
|
|
|
beatmap.OnlineID = testBeatmapInfo.OnlineID;
|
|
|
|
beatmap.OnlineBeatmapSetID = testBeatmapSet.OnlineID;
|
|
|
|
beatmap.Checksum = testBeatmapInfo.MD5Hash;
|
|
|
|
beatmap.BeatmapSet!.OnlineID = testBeatmapSet.OnlineID;
|
|
|
|
|
|
|
|
beatmapsReq.TriggerSuccess(new GetBeatmapsResponse { Beatmaps = new List<APIBeatmap> { beatmap } });
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-04 13:58:00 +08:00
|
|
|
beatmaps.AllowImport.Reset();
|
2021-01-18 02:02:33 +08:00
|
|
|
|
2021-02-22 13:53:32 +08:00
|
|
|
testBeatmapFile = TestResources.GetQuickTestBeatmapForImport();
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2021-01-19 23:43:16 +08:00
|
|
|
testBeatmapInfo = getTestBeatmapInfo(testBeatmapFile);
|
2021-01-17 04:31:15 +08:00
|
|
|
testBeatmapSet = testBeatmapInfo.BeatmapSet;
|
|
|
|
|
2022-01-25 11:58:15 +08:00
|
|
|
Realm.Write(r => r.RemoveAll<BeatmapSetInfo>());
|
|
|
|
Realm.Write(r => r.RemoveAll<BeatmapInfo>());
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-02-15 22:33:26 +08:00
|
|
|
selectedItem.Value = new PlaylistItem(testBeatmapInfo)
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2022-02-15 15:01:14 +08:00
|
|
|
RulesetID = testBeatmapInfo.Ruleset.OnlineID,
|
2021-01-17 04:31:15 +08:00
|
|
|
};
|
|
|
|
|
2022-02-15 22:30:39 +08:00
|
|
|
recreateChildren();
|
|
|
|
});
|
|
|
|
|
|
|
|
private void recreateChildren()
|
|
|
|
{
|
|
|
|
var beatmapLookupCache = new BeatmapLookupCache();
|
|
|
|
|
|
|
|
Child = new DependencyProvidingContainer
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2022-02-15 22:30:39 +08:00
|
|
|
CachedDependencies = new[]
|
|
|
|
{
|
|
|
|
(typeof(BeatmapLookupCache), (object)beatmapLookupCache)
|
|
|
|
},
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
beatmapLookupCache,
|
|
|
|
availabilityTracker = new OnlinePlayBeatmapAvailabilityTracker
|
|
|
|
{
|
|
|
|
SelectedItem = { BindTarget = selectedItem, }
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 04:31:15 +08:00
|
|
|
};
|
2022-02-15 22:30:39 +08:00
|
|
|
}
|
2021-01-17 04:31:15 +08:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBeatmapDownloadingFlow()
|
|
|
|
{
|
2022-03-10 19:33:14 +08:00
|
|
|
AddUntilStep("ensure beatmap unavailable", () => !beatmaps.IsAvailableLocally(testBeatmapSet));
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
2021-11-25 16:23:46 +08:00
|
|
|
AddStep("start downloading", () => beatmapDownloader.Download(testBeatmapSet));
|
2021-01-18 02:16:45 +08:00
|
|
|
addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0f));
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-07-20 15:33:52 +08:00
|
|
|
AddStep("set progress 40%", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet))!.SetProgress(0.4f));
|
2021-01-18 02:16:45 +08:00
|
|
|
addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f));
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-07-20 15:33:52 +08:00
|
|
|
AddStep("finish download", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet))!.TriggerSuccess(testBeatmapFile));
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing);
|
|
|
|
|
2022-10-04 13:58:00 +08:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.Set());
|
2022-01-25 14:23:51 +08:00
|
|
|
AddUntilStep("wait for import", () => beatmaps.CurrentImport != null);
|
2022-03-10 19:33:14 +08:00
|
|
|
AddUntilStep("ensure beatmap available", () => beatmaps.IsAvailableLocally(testBeatmapSet));
|
2022-02-08 18:36:16 +08:00
|
|
|
addAvailabilityCheckStep("state is locally available", BeatmapAvailability.LocallyAvailable);
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestTrackerRespectsSoftDeleting()
|
|
|
|
{
|
2022-10-04 13:58:00 +08:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.Set());
|
2022-01-03 16:02:15 +08:00
|
|
|
AddStep("import beatmap", () => beatmaps.Import(testBeatmapFile).WaitSafely());
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
|
2021-12-17 17:26:12 +08:00
|
|
|
AddStep("delete beatmap", () => beatmaps.Delete(beatmaps.QueryBeatmapSet(b => b.OnlineID == testBeatmapSet.OnlineID)!.Value));
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
|
|
|
|
2021-12-17 17:26:12 +08:00
|
|
|
AddStep("undelete beatmap", () => beatmaps.Undelete(beatmaps.QueryBeatmapSet(b => b.OnlineID == testBeatmapSet.OnlineID)!.Value));
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestTrackerRespectsChecksum()
|
|
|
|
{
|
2022-10-04 13:58:00 +08:00
|
|
|
AddStep("allow importing", () => beatmaps.AllowImport.Set());
|
2022-01-03 16:02:15 +08:00
|
|
|
AddStep("import beatmap", () => beatmaps.Import(testBeatmapFile).WaitSafely());
|
2021-12-14 18:11:23 +08:00
|
|
|
addAvailabilityCheckStep("initially locally available", BeatmapAvailability.LocallyAvailable);
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2021-01-19 23:43:16 +08:00
|
|
|
AddStep("import altered beatmap", () =>
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2022-01-03 16:02:15 +08:00
|
|
|
beatmaps.Import(TestResources.GetTestBeatmapForImport(true)).WaitSafely();
|
2021-01-17 04:31:15 +08:00
|
|
|
});
|
2021-12-14 18:11:23 +08:00
|
|
|
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-02-15 22:30:39 +08:00
|
|
|
AddStep("recreate tracker", recreateChildren);
|
2021-01-17 04:31:15 +08:00
|
|
|
addAvailabilityCheckStep("state not downloaded as well", BeatmapAvailability.NotDownloaded);
|
2021-12-14 18:11:23 +08:00
|
|
|
|
2022-01-03 16:02:15 +08:00
|
|
|
AddStep("reimport original beatmap", () => beatmaps.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely());
|
2021-12-14 18:11:23 +08:00
|
|
|
addAvailabilityCheckStep("locally available after re-import", BeatmapAvailability.LocallyAvailable);
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void addAvailabilityCheckStep(string description, Func<BeatmapAvailability> expected)
|
|
|
|
{
|
2021-10-27 22:54:02 +08:00
|
|
|
AddUntilStep(description, () => availabilityTracker.Availability.Value.Equals(expected.Invoke()));
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
2021-01-19 23:43:16 +08:00
|
|
|
private static BeatmapInfo getTestBeatmapInfo(string archiveFile)
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2021-01-19 23:43:16 +08: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-17 04:31:15 +08:00
|
|
|
|
2021-01-19 23:43:16 +08:00
|
|
|
info = beatmap.BeatmapInfo;
|
2021-11-24 12:25:26 +08:00
|
|
|
|
|
|
|
Debug.Assert(info.BeatmapSet != null);
|
|
|
|
|
|
|
|
info.BeatmapSet.Beatmaps.Add(info);
|
2021-01-19 23:43:16 +08:00
|
|
|
info.MD5Hash = stream.ComputeMD5Hash();
|
|
|
|
info.Hash = stream.ComputeSHA2Hash();
|
|
|
|
}
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2021-01-19 23:43:16 +08:00
|
|
|
return info;
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private class TestBeatmapManager : BeatmapManager
|
|
|
|
{
|
2022-10-04 13:58:00 +08:00
|
|
|
public readonly ManualResetEventSlim AllowImport = new ManualResetEventSlim();
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-01-26 12:37:33 +08:00
|
|
|
public Live<BeatmapSetInfo> CurrentImport { get; private set; }
|
2021-02-02 21:54:40 +08:00
|
|
|
|
2022-02-15 22:30:39 +08:00
|
|
|
public TestBeatmapManager(Storage storage, RealmAccess realm, RulesetStore rulesets, IAPIProvider api, [NotNull] AudioManager audioManager, IResourceStore<byte[]> resources,
|
|
|
|
GameHost host = null, WorkingBeatmap defaultBeatmap = null)
|
2022-07-28 15:19:05 +08:00
|
|
|
: base(storage, realm, api, audioManager, resources, host, defaultBeatmap)
|
2021-09-30 17:21:16 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-07-04 17:13:53 +08:00
|
|
|
protected override BeatmapImporter CreateBeatmapImporter(Storage storage, RealmAccess realm)
|
2021-09-30 15:45:32 +08:00
|
|
|
{
|
2022-07-04 17:13:53 +08:00
|
|
|
return new TestBeatmapImporter(this, storage, realm);
|
2021-09-30 15:45:32 +08:00
|
|
|
}
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2022-06-16 17:07:04 +08:00
|
|
|
internal class TestBeatmapImporter : BeatmapImporter
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2021-09-30 15:45:32 +08:00
|
|
|
private readonly TestBeatmapManager testBeatmapManager;
|
|
|
|
|
2022-07-04 17:13:53 +08:00
|
|
|
public TestBeatmapImporter(TestBeatmapManager testBeatmapManager, Storage storage, RealmAccess databaseAccess)
|
|
|
|
: base(storage, databaseAccess)
|
2021-09-30 15:45:32 +08:00
|
|
|
{
|
|
|
|
this.testBeatmapManager = testBeatmapManager;
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:18:07 +08:00
|
|
|
public override Live<BeatmapSetInfo> ImportModel(BeatmapSetInfo item, ArchiveReader archive = null, bool batchImport = false, CancellationToken cancellationToken = default)
|
2021-09-30 15:45:32 +08:00
|
|
|
{
|
2022-10-04 13:58:00 +08:00
|
|
|
if (!testBeatmapManager.AllowImport.Wait(TimeSpan.FromSeconds(10), cancellationToken))
|
|
|
|
throw new TimeoutException("Timeout waiting for import to be allowed.");
|
|
|
|
|
2022-06-20 14:18:07 +08:00
|
|
|
return (testBeatmapManager.CurrentImport = base.ImportModel(item, archive, batchImport, cancellationToken));
|
2021-09-30 15:45:32 +08:00
|
|
|
}
|
2021-01-17 04:31:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 16:23:46 +08:00
|
|
|
internal class TestBeatmapModelDownloader : BeatmapModelDownloader
|
|
|
|
{
|
2022-02-02 04:52:16 +08:00
|
|
|
public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider)
|
2021-11-25 16:42:41 +08:00
|
|
|
: base(importer, apiProvider)
|
2021-11-25 16:23:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize)
|
|
|
|
=> new TestDownloadRequest(set);
|
|
|
|
}
|
|
|
|
|
2022-07-19 17:50:27 +08:00
|
|
|
internal class TestDownloadRequest : ArchiveDownloadRequest<IBeatmapSetInfo>
|
2021-01-17 04:31:15 +08:00
|
|
|
{
|
2021-01-18 02:16:45 +08:00
|
|
|
public new void SetProgress(float progress) => base.SetProgress(progress);
|
2021-01-18 03:08:28 +08:00
|
|
|
public new void TriggerSuccess(string filename) => base.TriggerSuccess(filename);
|
2021-01-17 04:31:15 +08:00
|
|
|
|
2021-11-05 16:37:05 +08:00
|
|
|
public TestDownloadRequest(IBeatmapSetInfo model)
|
2021-01-17 04:31:15 +08:00
|
|
|
: base(model)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override string Target => null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|