Fix tests

This commit is contained in:
smoogipoo 2020-09-09 15:55:56 +09:00
parent 2d7e85f622
commit b1b99e4d6f

View File

@ -8,7 +8,6 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Collections; using osu.Game.Collections;
using osu.Game.Tests.Resources; using osu.Game.Tests.Resources;
@ -27,10 +26,9 @@ namespace osu.Game.Tests.Collections.IO
{ {
var osu = loadOsu(host); var osu = loadOsu(host);
var collectionManager = osu.Dependencies.Get<CollectionManager>(); await osu.CollectionManager.Import(new MemoryStream());
await collectionManager.Import(new MemoryStream());
Assert.That(collectionManager.Collections.Count, Is.Zero); Assert.That(osu.CollectionManager.Collections.Count, Is.Zero);
} }
finally finally
{ {
@ -48,16 +46,15 @@ namespace osu.Game.Tests.Collections.IO
{ {
var osu = loadOsu(host); var osu = loadOsu(host);
var collectionManager = osu.Dependencies.Get<CollectionManager>(); await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
await collectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
Assert.That(collectionManager.Collections.Count, Is.EqualTo(2)); Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
Assert.That(collectionManager.Collections[0].Name.Value, Is.EqualTo("First")); Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
Assert.That(collectionManager.Collections[0].Beatmaps.Count, Is.Zero); Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.Zero);
Assert.That(collectionManager.Collections[1].Name.Value, Is.EqualTo("Second")); Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Second"));
Assert.That(collectionManager.Collections[1].Beatmaps.Count, Is.Zero); Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.Zero);
} }
finally finally
{ {
@ -75,16 +72,15 @@ namespace osu.Game.Tests.Collections.IO
{ {
var osu = loadOsu(host, true); var osu = loadOsu(host, true);
var collectionManager = osu.Dependencies.Get<CollectionManager>(); await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
await collectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
Assert.That(collectionManager.Collections.Count, Is.EqualTo(2)); Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
Assert.That(collectionManager.Collections[0].Name.Value, Is.EqualTo("First")); Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
Assert.That(collectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(1)); Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(1));
Assert.That(collectionManager.Collections[1].Name.Value, Is.EqualTo("Second")); Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Second"));
Assert.That(collectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(12)); Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(12));
} }
finally finally
{ {
@ -107,8 +103,6 @@ namespace osu.Game.Tests.Collections.IO
var osu = loadOsu(host, true); var osu = loadOsu(host, true);
var collectionManager = osu.Dependencies.Get<CollectionManager>();
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {
using (var bw = new BinaryWriter(ms, Encoding.UTF8, true)) using (var bw = new BinaryWriter(ms, Encoding.UTF8, true))
@ -119,12 +113,12 @@ namespace osu.Game.Tests.Collections.IO
ms.Seek(0, SeekOrigin.Begin); ms.Seek(0, SeekOrigin.Begin);
await collectionManager.Import(ms); await osu.CollectionManager.Import(ms);
} }
Assert.That(host.UpdateThread.Running, Is.True); Assert.That(host.UpdateThread.Running, Is.True);
Assert.That(exceptionThrown, Is.False); Assert.That(exceptionThrown, Is.False);
Assert.That(collectionManager.Collections.Count, Is.EqualTo(0)); Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(0));
} }
finally finally
{ {
@ -143,15 +137,14 @@ namespace osu.Game.Tests.Collections.IO
{ {
var osu = loadOsu(host, true); var osu = loadOsu(host, true);
var collectionManager = osu.Dependencies.Get<CollectionManager>(); await osu.CollectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
await collectionManager.Import(TestResources.OpenResource("Collections/collections.db"));
// Move first beatmap from second collection into the first. // Move first beatmap from second collection into the first.
collectionManager.Collections[0].Beatmaps.Add(collectionManager.Collections[1].Beatmaps[0]); osu.CollectionManager.Collections[0].Beatmaps.Add(osu.CollectionManager.Collections[1].Beatmaps[0]);
collectionManager.Collections[1].Beatmaps.RemoveAt(0); osu.CollectionManager.Collections[1].Beatmaps.RemoveAt(0);
// Rename the second collecction. // Rename the second collecction.
collectionManager.Collections[1].Name.Value = "Another"; osu.CollectionManager.Collections[1].Name.Value = "Another";
} }
finally finally
{ {
@ -165,15 +158,13 @@ namespace osu.Game.Tests.Collections.IO
{ {
var osu = loadOsu(host, true); var osu = loadOsu(host, true);
var collectionManager = osu.Dependencies.Get<CollectionManager>(); Assert.That(osu.CollectionManager.Collections.Count, Is.EqualTo(2));
Assert.That(collectionManager.Collections.Count, Is.EqualTo(2)); Assert.That(osu.CollectionManager.Collections[0].Name.Value, Is.EqualTo("First"));
Assert.That(osu.CollectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(2));
Assert.That(collectionManager.Collections[0].Name.Value, Is.EqualTo("First")); Assert.That(osu.CollectionManager.Collections[1].Name.Value, Is.EqualTo("Another"));
Assert.That(collectionManager.Collections[0].Beatmaps.Count, Is.EqualTo(2)); Assert.That(osu.CollectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(11));
Assert.That(collectionManager.Collections[1].Name.Value, Is.EqualTo("Another"));
Assert.That(collectionManager.Collections[1].Beatmaps.Count, Is.EqualTo(11));
} }
finally finally
{ {
@ -182,7 +173,7 @@ namespace osu.Game.Tests.Collections.IO
} }
} }
private OsuGameBase loadOsu(GameHost host, bool withBeatmap = false) private TestOsuGameBase loadOsu(GameHost host, bool withBeatmap = false)
{ {
var osu = new TestOsuGameBase(withBeatmap); var osu = new TestOsuGameBase(withBeatmap);
@ -192,9 +183,6 @@ namespace osu.Game.Tests.Collections.IO
waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time"); waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time");
var collectionManager = osu.Dependencies.Get<CollectionManager>();
waitForOrAssert(() => collectionManager.DatabaseLoaded, "Collection database did not load in a reasonable amount of time");
return osu; return osu;
} }
@ -210,6 +198,8 @@ namespace osu.Game.Tests.Collections.IO
private class TestOsuGameBase : OsuGameBase private class TestOsuGameBase : OsuGameBase
{ {
public CollectionManager CollectionManager { get; private set; }
private readonly bool withBeatmap; private readonly bool withBeatmap;
public TestOsuGameBase(bool withBeatmap) public TestOsuGameBase(bool withBeatmap)
@ -217,13 +207,14 @@ namespace osu.Game.Tests.Collections.IO
this.withBeatmap = withBeatmap; this.withBeatmap = withBeatmap;
} }
protected override void AddInternal(Drawable drawable) [BackgroundDependencyLoader]
private void load()
{ {
// The beatmap must be imported just before the collection manager is loaded. // Beatmap must be imported before the collection manager is loaded.
if (drawable is CollectionManager && withBeatmap) if (withBeatmap)
BeatmapManager.Import(TestResources.GetTestBeatmapForImport()).Wait(); BeatmapManager.Import(TestResources.GetTestBeatmapForImport()).Wait();
base.AddInternal(drawable); AddInternal(CollectionManager = new CollectionManager(Storage));
} }
} }
} }