2020-09-18 09:05:33 +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.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using osu.Framework.Allocation;
|
2022-01-03 08:02:15 +00:00
|
|
|
|
using osu.Framework.Extensions;
|
2020-09-18 09:05:33 +00:00
|
|
|
|
using osu.Framework.Platform;
|
2022-07-27 06:59:36 +00:00
|
|
|
|
using osu.Game.Database;
|
2023-10-30 06:44:16 +00:00
|
|
|
|
using osu.Game.Online.API;
|
2020-09-18 09:05:33 +00:00
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Tests
|
|
|
|
|
{
|
|
|
|
|
public abstract partial class ImportTest
|
|
|
|
|
{
|
|
|
|
|
protected virtual TestOsuGameBase LoadOsuIntoHost(GameHost host, bool withBeatmap = false)
|
|
|
|
|
{
|
|
|
|
|
var osu = new TestOsuGameBase(withBeatmap);
|
2021-10-12 08:13:36 +00:00
|
|
|
|
Task.Factory.StartNew(() => host.Run(osu), TaskCreationOptions.LongRunning)
|
2021-06-29 08:21:09 +00:00
|
|
|
|
.ContinueWith(t => Assert.Fail($"Host threw exception {t.Exception}"), TaskContinuationOptions.OnlyOnFaulted);
|
2020-09-18 09:05:33 +00:00
|
|
|
|
|
|
|
|
|
waitForOrAssert(() => osu.IsLoaded, @"osu! failed to start in a reasonable amount of time");
|
|
|
|
|
|
|
|
|
|
bool ready = false;
|
|
|
|
|
// wait for two update frames to be executed. this ensures that all components have had a change to run LoadComplete and hopefully avoid
|
|
|
|
|
// database access (GlobalActionContainer is one to do this).
|
|
|
|
|
host.UpdateThread.Scheduler.Add(() => host.UpdateThread.Scheduler.Add(() => ready = true));
|
|
|
|
|
|
|
|
|
|
waitForOrAssert(() => ready, @"osu! failed to start in a reasonable amount of time");
|
|
|
|
|
|
|
|
|
|
return osu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void waitForOrAssert(Func<bool> result, string failureMessage, int timeout = 60000)
|
|
|
|
|
{
|
|
|
|
|
Task task = Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
while (!result()) Thread.Sleep(200);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(task.Wait(timeout), failureMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public partial class TestOsuGameBase : OsuGameBase
|
|
|
|
|
{
|
2022-07-27 06:59:36 +00:00
|
|
|
|
public RealmAccess Realm => Dependencies.Get<RealmAccess>();
|
2023-10-30 06:44:16 +00:00
|
|
|
|
public new IAPIProvider API => base.API;
|
2020-09-18 09:05:33 +00:00
|
|
|
|
|
|
|
|
|
private readonly bool withBeatmap;
|
|
|
|
|
|
|
|
|
|
public TestOsuGameBase(bool withBeatmap)
|
|
|
|
|
{
|
|
|
|
|
this.withBeatmap = withBeatmap;
|
2023-10-30 06:44:16 +00:00
|
|
|
|
|
|
|
|
|
base.API = new DummyAPIAccess();
|
2020-09-18 09:05:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
// Beatmap must be imported before the collection manager is loaded.
|
|
|
|
|
if (withBeatmap)
|
2022-01-03 08:02:15 +00:00
|
|
|
|
BeatmapManager.Import(TestResources.GetTestBeatmapForImport()).WaitSafely();
|
2020-09-18 09:05:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|