1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-28 21:52:54 +08:00
osu-lazer/osu.Game.Tests/Collections/IO/ImportCollectionsTest.cs

187 lines
6.6 KiB
C#
Raw Normal View History

2020-09-07 21:10:12 +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.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Extensions;
2020-09-07 21:10:12 +08:00
using osu.Framework.Platform;
using osu.Framework.Testing;
2020-09-07 21:10:12 +08:00
using osu.Game.Tests.Resources;
namespace osu.Game.Tests.Collections.IO
{
[TestFixture]
2020-09-18 17:05:33 +08:00
public class ImportCollectionsTest : ImportTest
2020-09-07 21:10:12 +08:00
{
[Test]
public async Task TestImportEmptyDatabase()
{
2020-09-08 18:41:05 +08:00
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
2020-09-07 21:10:12 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host);
2020-09-07 21:10:12 +08:00
await importCollectionsFromStream(osu, new MemoryStream());
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08:00
Assert.That(osu.CollectionManager.Collections.Count, Is.Zero);
2020-09-07 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
}
[Test]
public async Task TestImportWithNoBeatmaps()
2021-05-27 18:34:39 +08:00
{
2020-09-08 18:41:05 +08:00
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
2020-09-07 21:10:12 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host);
2020-09-07 21:10:12 +08:00
await importCollectionsFromStream(osu, TestResources.OpenResource("Collections/collections.db"));
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08:00
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
}
[Test]
public async Task TestImportWithBeatmaps()
{
2020-09-08 18:41:05 +08:00
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
2020-09-07 21:10:12 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 21:10:12 +08:00
await importCollectionsFromStream(osu, TestResources.OpenResource("Collections/collections.db"));
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08:00
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
}
[Test]
public async Task TestImportMalformedDatabase()
{
bool exceptionThrown = false;
UnhandledExceptionEventHandler setException = (_, __) => exceptionThrown = true;
2020-09-08 18:41:05 +08:00
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
2020-09-07 21:10:12 +08:00
{
try
{
AppDomain.CurrentDomain.UnhandledException += setException;
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 21:10:12 +08: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);
await importCollectionsFromStream(osu, ms);
2020-09-07 21:10:12 +08:00
}
Assert.That(exceptionThrown, Is.False);
2020-09-09 14:55:56 +08:00
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(0));
2020-09-07 21:10:12 +08:00
}
finally
{
host.Exit();
AppDomain.CurrentDomain.UnhandledException -= setException;
}
}
}
[Test]
public async Task TestSaveAndReload()
{
string firstRunName;
using (var host = new CleanRunHeadlessGameHost(bypassCleanup: true))
2020-09-07 21:10:12 +08:00
{
firstRunName = host.Name;
2020-09-07 21:10:12 +08:00
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 21:10:12 +08:00
await importCollectionsFromStream(osu, TestResources.OpenResource("Collections/collections.db"));
2020-09-07 21:10:12 +08:00
// Move first beatmap from second collection into the first.
2020-09-09 14:55:56 +08:00
osu.CollectionManager.Collections[0].Beatmaps.Add(osu.CollectionManager.Collections[1].Beatmaps[0]);
osu.CollectionManager.Collections[1].Beatmaps.RemoveAt(0);
2020-09-07 21:10:12 +08:00
// Rename the second collecction.
2020-09-09 14:55:56 +08:00
osu.CollectionManager.Collections[1].Name.Value = "Another";
2020-09-07 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
2021-12-22 14:44:47 +08:00
// Name matches the automatically chosen name from `CleanRunHeadlessGameHost` above, so we end up using the same storage location.
using (HeadlessGameHost host = new TestRunHeadlessGameHost(firstRunName, null))
2020-09-07 21:10:12 +08:00
{
try
{
2020-09-18 17:05:33 +08:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08:00
Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
2020-09-09 14:55:56 +08: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 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
}
private static async Task importCollectionsFromStream(TestOsuGameBase osu, Stream stream)
{
// intentionally spin this up on a separate task to avoid disposal deadlocks.
// see https://github.com/EventStore/EventStore/issues/1179
await Task.Factory.StartNew(() => osu.CollectionManager.Import(stream).WaitSafely(), TaskCreationOptions.LongRunning);
}
2020-09-07 21:10:12 +08:00
}
}