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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-09-07 13:10:12 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using NUnit.Framework;
|
2022-01-03 08:02:15 +00:00
|
|
|
using osu.Framework.Extensions;
|
2020-09-07 13:10:12 +00:00
|
|
|
using osu.Framework.Platform;
|
2021-08-18 06:32:59 +00:00
|
|
|
using osu.Framework.Testing;
|
2020-09-07 13:10:12 +00:00
|
|
|
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
|
|
|
|
2021-05-27 10:16:22 +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
|
|
|
|
2021-05-27 10:16:22 +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
|
|
|
|
2022-06-08 10:26:34 +00: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.
|
|
|
|
|
2020-09-09 06:55:56 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].BeatmapHashes.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"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].BeatmapHashes.Count, Is.EqualTo(12));
|
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
|
|
|
|
2021-05-27 10:16:22 +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"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].BeatmapHashes.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"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].BeatmapHashes.Count, Is.EqualTo(12));
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public async Task TestImportMalformedDatabase()
|
|
|
|
{
|
|
|
|
bool exceptionThrown = false;
|
2022-06-24 12:25:23 +00:00
|
|
|
UnhandledExceptionEventHandler setException = (_, _) => exceptionThrown = true;
|
2020-09-07 13:10:12 +00:00
|
|
|
|
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);
|
|
|
|
|
2021-05-27 10:16:22 +00:00
|
|
|
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()
|
|
|
|
{
|
2021-12-24 11:24:37 +00:00
|
|
|
string firstRunName;
|
|
|
|
|
|
|
|
using (var host = new CleanRunHeadlessGameHost(bypassCleanup: true))
|
2020-09-07 13:10:12 +00:00
|
|
|
{
|
2021-12-24 11:24:37 +00:00
|
|
|
firstRunName = host.Name;
|
|
|
|
|
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
|
|
|
|
2021-05-27 10:16:22 +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.
|
2022-06-10 05:03:51 +00:00
|
|
|
osu.CollectionManager.Collections[0].BeatmapHashes.Add(osu.CollectionManager.Collections[1].BeatmapHashes[0]);
|
|
|
|
osu.CollectionManager.Collections[1].BeatmapHashes.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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-22 06:44:47 +00:00
|
|
|
// Name matches the automatically chosen name from `CleanRunHeadlessGameHost` above, so we end up using the same storage location.
|
2022-01-24 10:10:14 +00:00
|
|
|
using (HeadlessGameHost host = new TestRunHeadlessGameHost(firstRunName, null))
|
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
|
|
|
|
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"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[0].BeatmapHashes.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"));
|
2022-06-10 05:03:51 +00:00
|
|
|
Assert.That(osu.CollectionManager.Collections[1].BeatmapHashes.Count, Is.EqualTo(11));
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
host.Exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 10:16:22 +00:00
|
|
|
|
|
|
|
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-01-03 08:02:15 +00:00
|
|
|
await Task.Factory.StartNew(() => osu.CollectionManager.Import(stream).WaitSafely(), TaskCreationOptions.LongRunning);
|
2021-05-27 10:16:22 +00:00
|
|
|
}
|
2020-09-07 13:10:12 +00:00
|
|
|
}
|
|
|
|
}
|