1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 21:02:55 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs

138 lines
6.2 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2018-04-13 17:19:50 +08:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
2020-06-03 17:30:27 +08:00
using osu.Framework.Extensions;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
2019-01-28 17:19:57 +08:00
using osu.Game.Tests.Resources;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Beatmaps.IO
{
[TestFixture]
2020-09-18 17:05:33 +08:00
public class ImportBeatmapTest : ImportTest
2018-04-13 17:19:50 +08:00
{
public static async Task<BeatmapSetInfo> LoadQuickOszIntoOsu(OsuGameBase osu)
{
string temp = TestResources.GetQuickTestBeatmapForImport();
var manager = osu.Dependencies.Get<BeatmapManager>();
2022-01-06 21:54:43 +08:00
var importedSet = manager.Import(new ImportTask(temp)).GetResultSafely();
ensureLoaded(osu).WaitSafely();
waitForOrAssert(() => !File.Exists(temp), "Temporary file still exists after standard import", 5000);
return manager.GetAllUsableBeatmapSets().Find(beatmapSet => beatmapSet.ID == importedSet.Value.ID);
}, TaskCreationOptions.LongRunning);
public static Task<BeatmapSetInfo> LoadOszIntoOsu(OsuGameBase osu, string path = null, bool virtualTrack = false) => Task.Factory.StartNew(() =>
2018-05-29 10:57:11 +08:00
{
string temp = path ?? TestResources.GetTestBeatmapForImport(virtualTrack);
2018-04-13 17:19:50 +08:00
var manager = osu.Dependencies.Get<BeatmapManager>();
2022-01-06 21:54:43 +08:00
var importedSet = manager.Import(new ImportTask(temp)).GetResultSafely();
2018-04-13 17:19:50 +08:00
ensureLoaded(osu).WaitSafely();
2018-04-13 17:19:50 +08:00
waitForOrAssert(() => !File.Exists(temp), "Temporary file still exists after standard import", 5000);
return manager.GetAllUsableBeatmapSets().Find(beatmapSet => beatmapSet.ID == importedSet.Value.ID);
}, TaskCreationOptions.LongRunning);
2018-04-13 17:19:50 +08:00
private void deleteBeatmapSet(BeatmapSetInfo imported, OsuGameBase osu)
{
var manager = osu.Dependencies.Get<BeatmapManager>();
manager.Delete(imported);
checkBeatmapSetCount(osu, 0);
checkBeatmapSetCount(osu, 1, true);
checkSingleReferencedFileCount(osu, 0);
2018-04-13 17:19:50 +08:00
Assert.IsTrue(manager.QueryBeatmapSets(_ => true).First().DeletePending);
}
2021-06-23 16:37:24 +08:00
private static void checkBeatmapSetCount(OsuGameBase osu, int expected, bool includeDeletePending = false)
{
var manager = osu.Dependencies.Get<BeatmapManager>();
Assert.AreEqual(expected, includeDeletePending
? manager.QueryBeatmapSets(_ => true).ToList().Count
: manager.GetAllUsableBeatmapSets().Count);
}
2021-06-23 16:37:24 +08:00
private static string hashFile(string filename)
{
using (var s = File.OpenRead(filename))
return s.ComputeMD5Hash();
}
2021-06-23 16:37:24 +08:00
private static void checkBeatmapCount(OsuGameBase osu, int expected)
{
Assert.AreEqual(expected, osu.Dependencies.Get<BeatmapManager>().QueryBeatmaps(_ => true).ToList().Count);
}
2021-06-23 16:37:24 +08:00
private static void checkSingleReferencedFileCount(OsuGameBase osu, int expected)
{
Assert.AreEqual(expected, osu.Dependencies.Get<DatabaseContextFactory>().Get().FileInfo.Count(f => f.ReferenceCount == 1));
}
private static Task ensureLoaded(OsuGameBase osu, int timeout = 60000) => Task.Factory.StartNew(() =>
2018-04-13 17:19:50 +08:00
{
IEnumerable<BeatmapSetInfo> resultSets = null;
var store = osu.Dependencies.Get<BeatmapManager>();
waitForOrAssert(() => (resultSets = store.QueryBeatmapSets(s => s.OnlineID == 241526)).Any(),
2018-04-13 17:19:50 +08:00
@"BeatmapSet did not import to the database in allocated time.", timeout);
2020-05-05 09:31:11 +08:00
// ensure we were stored to beatmap database backing...
2018-04-13 17:19:50 +08:00
Assert.IsTrue(resultSets.Count() == 1, $@"Incorrect result count found ({resultSets.Count()} but should be 1).");
IEnumerable<BeatmapInfo> queryBeatmaps() => store.QueryBeatmaps(s => s.BeatmapSet.OnlineID == 241526);
IEnumerable<BeatmapSetInfo> queryBeatmapSets() => store.QueryBeatmapSets(s => s.OnlineID == 241526);
2018-04-13 17:19:50 +08:00
2020-05-05 09:31:11 +08:00
// if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
2018-04-13 17:19:50 +08:00
waitForOrAssert(() => queryBeatmaps().Count() == 12,
@"Beatmaps did not import to the database in allocated time", timeout);
waitForOrAssert(() => queryBeatmapSets().Count() == 1,
@"BeatmapSet did not import to the database in allocated time", timeout);
int countBeatmapSetBeatmaps = 0;
int countBeatmaps = 0;
waitForOrAssert(() =>
(countBeatmapSetBeatmaps = queryBeatmapSets().First().Beatmaps.Count) ==
(countBeatmaps = queryBeatmaps().Count()),
$@"Incorrect database beatmap count post-import ({countBeatmaps} but should be {countBeatmapSetBeatmaps}).", timeout);
var set = queryBeatmapSets().First();
foreach (BeatmapInfo b in set.Beatmaps)
Assert.IsTrue(set.Beatmaps.Any(c => c.OnlineID == b.OnlineID));
2018-04-13 17:19:50 +08:00
Assert.IsTrue(set.Beatmaps.Count > 0);
var beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
2018-04-19 19:44:38 +08:00
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
2018-04-13 17:19:50 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
2018-04-19 19:44:38 +08:00
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
2018-04-13 17:19:50 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
2018-04-19 19:44:38 +08:00
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
2018-04-13 17:19:50 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
2018-04-19 19:44:38 +08:00
Assert.IsTrue(beatmap?.HitObjects.Any() == true);
}, TaskCreationOptions.LongRunning);
2018-04-13 17:19:50 +08:00
private static void waitForOrAssert(Func<bool> result, string failureMessage, int timeout = 60000)
2018-04-13 17:19:50 +08:00
{
Task task = Task.Factory.StartNew(() =>
2018-04-13 17:19:50 +08:00
{
while (!result()) Thread.Sleep(200);
}, TaskCreationOptions.LongRunning);
2018-04-13 17:19:50 +08:00
2018-06-04 21:25:18 +08:00
Assert.IsTrue(task.Wait(timeout), failureMessage);
2018-04-13 17:19:50 +08:00
}
}
}