1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 08:42:55 +08:00
osu-lazer/osu.Game.Tests/Collections/IO/ImportCollectionsTest.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 lines
8.0 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.
2022-06-17 15:37:17 +08:00
#nullable disable
2020-09-07 21:10:12 +08:00
using System;
using System.IO;
2022-07-27 14:59:36 +08:00
using System.Linq;
2020-09-07 21:10:12 +08:00
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;
using osu.Game.Collections;
using osu.Game.Database;
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
2022-07-27 14:59:36 +08:00
osu.Realm.Run(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
Assert.That(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
2022-07-27 14:59:36 +08:00
osu.Realm.Run(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
Assert.That(collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
// Even with no beatmaps imported, collections are tracking the hashes and will continue to.
// In the future this whole mechanism will be replaced with having the collections in realm,
// but until that happens it makes rough sense that we want to track not-yet-imported beatmaps
// and have them associate with collections if/when they become available.
2022-07-27 14:59:36 +08:00
Assert.That(collections[0].Name, Is.EqualTo("First"));
Assert.That(collections[0].BeatmapMD5Hashes.Count, Is.EqualTo(1));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
Assert.That(collections[1].Name, Is.EqualTo("Second"));
Assert.That(collections[1].BeatmapMD5Hashes.Count, Is.EqualTo(12));
});
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
2022-07-27 14:59:36 +08:00
osu.Realm.Run(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
Assert.That(collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
Assert.That(collections[0].Name, Is.EqualTo("First"));
Assert.That(collections[0].BeatmapMD5Hashes.Count, Is.EqualTo(1));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
Assert.That(collections[1].Name, Is.EqualTo("Second"));
Assert.That(collections[1].BeatmapMD5Hashes.Count, Is.EqualTo(12));
});
2020-09-07 21:10:12 +08:00
}
finally
{
host.Exit();
}
}
}
[Test]
public async Task TestImportMalformedDatabase()
{
bool exceptionThrown = false;
2022-06-24 20:25:23 +08:00
UnhandledExceptionEventHandler setException = (_, _) => exceptionThrown = true;
2020-09-07 21:10:12 +08:00
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);
2022-07-27 14:59:36 +08:00
osu.Realm.Run(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
Assert.That(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(bypassCleanupOnDispose: 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
2022-07-27 14:59:36 +08:00
// ReSharper disable once MethodHasAsyncOverload
osu.Realm.Write(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
// Move first beatmap from second collection into the first.
collections[0].BeatmapMD5Hashes.Add(collections[1].BeatmapMD5Hashes[0]);
collections[1].BeatmapMD5Hashes.RemoveAt(0);
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
// Rename the second collecction.
collections[1].Name = "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
2022-07-27 14:59:36 +08:00
osu.Realm.Run(realm =>
{
var collections = realm.All<BeatmapCollection>().ToList();
2022-07-27 14:59:36 +08:00
Assert.That(collections.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
Assert.That(collections[0].Name, Is.EqualTo("First"));
Assert.That(collections[0].BeatmapMD5Hashes.Count, Is.EqualTo(2));
2020-09-07 21:10:12 +08:00
2022-07-27 14:59:36 +08:00
Assert.That(collections[1].Name, Is.EqualTo("Another"));
Assert.That(collections[1].BeatmapMD5Hashes.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
2022-07-27 14:59:36 +08:00
await Task.Factory.StartNew(() => new LegacyCollectionImporter(osu.Realm).Import(stream).WaitSafely(), TaskCreationOptions.LongRunning);
}
2020-09-07 21:10:12 +08:00
}
}