osu/osu.Game.Tests/Collections/IO/ImportCollectionsTest.cs

180 lines
6.3 KiB
C#
Raw Normal View History

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.Tasks;
using NUnit.Framework;
using osu.Framework.Platform;
using osu.Game.Tests.Resources;
namespace osu.Game.Tests.Collections.IO
{
[TestFixture]
2020-09-18 09:05:33 +00:00
public class ImportCollectionsTest : ImportTest
2020-09-07 13:10:12 +00:00
{
[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-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(host);
2020-09-07 13:10:12 +00:00
await importCollectionsFromStream(osu, 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()
2021-05-27 10:34:39 +00:00
{
2020-09-08 10:41:05 +00:00
using (HeadlessGameHost host = new CleanRunHeadlessGameHost())
2020-09-07 13:10:12 +00:00
{
try
{
2020-09-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(host);
2020-09-07 13:10:12 +00:00
await importCollectionsFromStream(osu, 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-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 13:10:12 +00:00
await importCollectionsFromStream(osu, 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-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(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);
await importCollectionsFromStream(osu, ms);
2020-09-07 13:10:12 +00:00
}
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-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(host, true);
2020-09-07 13:10:12 +00:00
await importCollectionsFromStream(osu, 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-18 09:05:33 +00:00
var osu = LoadOsuIntoHost(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();
}
}
}
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.Run(() => osu.CollectionManager.Import(stream).Wait());
}
2020-09-07 13:10:12 +00:00
}
}