2020-09-07 13:10:12 +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;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Collections;
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Collections.IO
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class ImportCollectionsTest
|
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public async Task TestImportEmptyDatabase()
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
await osu.CollectionManager.Import(new MemoryStream());
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections.Count, Is.Zero);
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestImportWithNoBeatmaps()
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.Zero);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Second"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.Zero);
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestImportWithBeatmaps()
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host, true);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(1));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Second"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(12));
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestImportMalformedDatabase()
|
|
|
|
{
|
|
|
|
bool exceptionThrown = false;
|
|
|
|
UnhandledExceptionEventHandler setException = (_, __) => exceptionThrown = true;
|
|
|
|
|
2020-09-08 10:41:05 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
AppDomain.CurrentDomain.UnhandledException += setException;
|
|
|
|
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host, true);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
{
|
|
|
|
using (var bw = new BinaryWriter(ms, Encoding.UTF8, true))
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 10000; i++)
|
|
|
|
bw.Write((byte)i);
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
await osu.CollectionManager.Import(ms);
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Assert.That(host.UpdateThread.Running, Is.True);
|
|
|
|
Assert.That(exceptionThrown, Is.False);
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(0));
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
AppDomain.CurrentDomain.UnhandledException -= setException;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestSaveAndReload()
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host, true);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
|
|
|
// Move first beatmap from second collection into the first.
|
2020-09-09 06:55:56 +00:00
|
|
|
osu.CollectionManager.Collections[0].Beatmaps.Add(osu.CollectionManager.Collections[1].Beatmaps[0]);
|
|
|
|
osu.CollectionManager.Collections[1].Beatmaps.RemoveAt(0);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
|
|
|
// Rename the second collecction.
|
2020-09-09 06:55:56 +00:00
|
|
|
osu.CollectionManager.Collections[1].Name.Value = "Another";
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using (HeadlessGameHost host = new HeadlessGameHost("TestSaveAndReload"))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = loadOsu(host, true);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(2));
|
2020-09-07 13:10:12 +00:00
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Another"));
|
|
|
|
Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(11));
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
private TestOsuGameBase loadOsu(GameHost host, bool withBeatmap = false)
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
2020-09-08 10:41:05 +00:00
|
|
|
var osu = new TestOsuGameBase(withBeatmap);
|
2020-09-07 13:10:12 +00:00
|
|
|
|
|
|
|
#pragma warning disable 4014
|
|
|
|
Task.Run(() => host.Run(osu));
|
|
|
|
#pragma warning restore 4014
|
|
|
|
|
|
|
|
waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time");
|
|
|
|
|
|
|
|
return osu;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void waitForOrAssert(Func<bool> result, string failureMessage, int timeout = 60000)
|
|
|
|
{
|
|
|
|
Task task = Task.Run(() =>
|
|
|
|
{
|
|
|
|
while (!result()) Thread.Sleep(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
Assert.IsTrue(task.Wait(timeout), failureMessage);
|
|
|
|
}
|
2020-09-08 10:41:05 +00:00
|
|
|
|
|
|
|
private class TestOsuGameBase : OsuGameBase
|
|
|
|
{
|
2020-09-09 06:55:56 +00:00
|
|
|
public CollectionManager CollectionManager { get; private set; }
|
|
|
|
|
2020-09-08 10:41:05 +00:00
|
|
|
private readonly bool withBeatmap;
|
|
|
|
|
|
|
|
public TestOsuGameBase(bool withBeatmap)
|
|
|
|
{
|
|
|
|
this.withBeatmap = withBeatmap;
|
|
|
|
}
|
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
2020-09-08 10:41:05 +00:00
|
|
|
{
|
2020-09-09 06:55:56 +00:00
|
|
|
// Beatmap must be imported before the collection manager is loaded.
|
|
|
|
if (withBeatmap)
|
2020-09-08 10:41:05 +00:00
|
|
|
BeatmapManager.Import(TestResources.GetTestBeatmapForImport()).Wait();
|
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
AddInternal(CollectionManager = new CollectionManager(Storage));
|
2020-09-08 10:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
}
|