1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 04:42:55 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/IO/ImportBeatmapTest.cs

261 lines
10 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-09-25 11:52:01 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Platform;
using osu.Game.IPC;
using osu.Framework.Allocation;
using osu.Game.Beatmaps;
namespace osu.Game.Tests.Beatmaps.IO
{
[TestFixture]
public class ImportBeatmapTest
{
private const string osz_path = @"../../../osu-resources/osu.Game.Resources/Beatmaps/241526 Soleily - Renatus.osz";
[Test]
public void TestImportWhenClosed()
{
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportWhenClosed"))
2017-09-25 11:52:01 +08:00
{
try
{
2018-02-09 17:20:18 +08:00
loadOszIntoOsu(loadOsu(host));
}
finally
{
host.Exit();
}
2017-09-25 11:52:01 +08:00
}
}
2018-02-09 16:51:29 +08:00
[Test]
public void TestImportThenDelete()
{
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportThenDelete"))
{
try
{
var osu = loadOsu(host);
2018-02-09 16:51:29 +08:00
2018-02-09 17:20:18 +08:00
var imported = loadOszIntoOsu(osu);
2018-02-09 16:51:29 +08:00
2018-02-09 17:20:18 +08:00
deleteBeatmapSet(imported, osu);
}
finally
{
host.Exit();
}
2018-02-09 16:51:29 +08:00
}
}
2018-02-09 17:23:06 +08:00
[Test]
public void TestImportThenImport()
{
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportThenDeleteThenImport"))
{
try
{
var osu = loadOsu(host);
var imported = loadOszIntoOsu(osu);
var importedSecondTime = loadOszIntoOsu(osu);
// check the newly "imported" beatmap is actually just the restored previous import. since it matches hash.
Assert.IsTrue(imported.ID == importedSecondTime.ID);
Assert.IsTrue(imported.Beatmaps.First().ID == importedSecondTime.Beatmaps.First().ID);
var manager = osu.Dependencies.Get<BeatmapManager>();
Assert.IsTrue(manager.GetAllUsableBeatmapSets().Count == 1);
Assert.IsTrue(manager.QueryBeatmapSets(_ => true).ToList().Count == 1);
}
finally
{
host.Exit();
}
}
}
2018-02-09 16:58:53 +08:00
[Test]
public void TestImportThenDeleteThenImport()
{
//unfortunately for the time being we need to reference osu.Framework.Desktop for a game host here.
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportThenDeleteThenImport"))
{
try
{
var osu = loadOsu(host);
2018-02-09 16:58:53 +08:00
2018-02-09 17:20:18 +08:00
var imported = loadOszIntoOsu(osu);
2018-02-09 16:58:53 +08:00
2018-02-09 17:20:18 +08:00
deleteBeatmapSet(imported, osu);
2018-02-09 16:58:53 +08:00
2018-02-09 17:20:18 +08:00
var importedSecondTime = loadOszIntoOsu(osu);
2018-02-09 16:58:53 +08:00
// check the newly "imported" beatmap is actually just the restored previous import. since it matches hash.
2018-02-09 17:20:18 +08:00
Assert.IsTrue(imported.ID == importedSecondTime.ID);
Assert.IsTrue(imported.Beatmaps.First().ID == importedSecondTime.Beatmaps.First().ID);
}
finally
{
host.Exit();
}
2018-02-09 16:58:53 +08:00
}
}
2017-09-25 11:52:01 +08:00
[Test]
2017-10-24 05:13:39 +08:00
[NonParallelizable]
[Ignore("Binding IPC on Appveyor isn't working (port in use). Need to figure out why")]
2017-09-25 11:52:01 +08:00
public void TestImportOverIPC()
{
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("host", true))
using (HeadlessGameHost client = new CleanRunHeadlessGameHost("client", true))
2017-09-25 11:52:01 +08:00
{
try
{
Assert.IsTrue(host.IsPrimaryInstance);
Assert.IsFalse(client.IsPrimaryInstance);
2017-09-25 11:52:01 +08:00
var osu = loadOsu(host);
2017-09-25 11:52:01 +08:00
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp));
2017-09-25 11:52:01 +08:00
var importer = new BeatmapIPCChannel(client);
if (!importer.ImportAsync(temp).Wait(10000))
Assert.Fail(@"IPC took too long to send");
2017-09-25 11:52:01 +08:00
ensureLoaded(osu);
2017-09-25 11:52:01 +08:00
waitForOrAssert(() => !File.Exists(temp), "Temporary still exists after IPC import", 5000);
}
finally
{
host.Exit();
}
2017-09-25 11:52:01 +08:00
}
}
[Test]
public void TestImportWhenFileOpen()
{
using (HeadlessGameHost host = new CleanRunHeadlessGameHost("TestImportWhenFileOpen"))
2017-09-25 11:52:01 +08:00
{
try
{
var osu = loadOsu(host);
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp), "Temporary file copy never substantiated");
using (File.OpenRead(temp))
osu.Dependencies.Get<BeatmapManager>().Import(temp);
ensureLoaded(osu);
File.Delete(temp);
Assert.IsFalse(File.Exists(temp), "We likely held a read lock on the file when we shouldn't");
}
finally
{
host.Exit();
}
2017-09-25 11:52:01 +08:00
}
}
2018-02-09 17:20:18 +08:00
private BeatmapSetInfo loadOszIntoOsu(OsuGameBase osu)
{
var temp = prepareTempCopy(osz_path);
Assert.IsTrue(File.Exists(temp));
var imported = osu.Dependencies.Get<BeatmapManager>().Import(temp);
ensureLoaded(osu);
waitForOrAssert(() => !File.Exists(temp), "Temporary file still exists after standard import", 5000);
return imported.FirstOrDefault();
}
private void deleteBeatmapSet(BeatmapSetInfo imported, OsuGameBase osu)
{
var manager = osu.Dependencies.Get<BeatmapManager>();
manager.Delete(imported);
Assert.IsTrue(manager.GetAllUsableBeatmapSets().Count == 0);
Assert.IsTrue(manager.QueryBeatmapSets(_ => true).ToList().Count == 1);
Assert.IsTrue(manager.QueryBeatmapSets(_ => true).First().DeletePending);
}
2017-09-25 11:52:01 +08:00
private string prepareTempCopy(string path)
{
var temp = Path.GetTempFileName();
return new FileInfo(path).CopyTo(temp, true).FullName;
}
private OsuGameBase loadOsu(GameHost host)
{
var osu = new OsuGameBase();
Task.Run(() => host.Run(osu));
waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time");
2017-09-25 11:52:01 +08:00
return osu;
}
private void ensureLoaded(OsuGameBase osu, int timeout = 60000)
{
IEnumerable<BeatmapSetInfo> resultSets = null;
var store = osu.Dependencies.Get<BeatmapManager>();
2017-10-14 13:28:25 +08:00
waitForOrAssert(() => (resultSets = store.QueryBeatmapSets(s => s.OnlineBeatmapSetID == 241526)).Any(),
@"BeatmapSet did not import to the database in allocated time.", timeout);
2017-09-25 11:52:01 +08:00
//ensure we were stored to beatmap database backing...
Assert.IsTrue(resultSets.Count() == 1, $@"Incorrect result count found ({resultSets.Count()} but should be 1).");
2018-01-16 02:42:17 +08:00
IEnumerable<BeatmapInfo> queryBeatmaps() => store.QueryBeatmaps(s => s.BeatmapSet.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0);
IEnumerable<BeatmapSetInfo> queryBeatmapSets() => store.QueryBeatmapSets(s => s.OnlineBeatmapSetID == 241526);
2017-09-25 11:52:01 +08:00
//if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
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(() =>
2018-02-09 16:58:53 +08:00
(countBeatmapSetBeatmaps = queryBeatmapSets().First().Beatmaps.Count) ==
(countBeatmaps = queryBeatmaps().Count()),
$@"Incorrect database beatmap count post-import ({countBeatmaps} but should be {countBeatmapSetBeatmaps}).", timeout);
2017-09-25 11:52:01 +08:00
var set = queryBeatmapSets().First();
foreach (BeatmapInfo b in set.Beatmaps)
2017-10-16 12:09:01 +08:00
Assert.IsTrue(set.Beatmaps.Any(c => c.OnlineBeatmapID == b.OnlineBeatmapID));
Assert.IsTrue(set.Beatmaps.Count > 0);
2017-10-14 13:28:25 +08:00
var beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
2017-09-25 11:52:01 +08:00
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
2017-10-14 13:28:25 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
2017-09-25 11:52:01 +08:00
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
2017-10-14 13:28:25 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
2017-09-25 11:52:01 +08:00
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
2017-10-14 13:28:25 +08:00
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
2017-09-25 11:52:01 +08:00
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
}
private void waitForOrAssert(Func<bool> result, string failureMessage, int timeout = 60000)
{
2018-02-09 16:58:53 +08:00
Action waitAction = () =>
{
while (!result()) Thread.Sleep(200);
};
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout), failureMessage);
}
2017-09-25 11:52:01 +08:00
}
}