1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 19:27:52 +08:00
osu-lazer/osu.Game.Tests/Visual/Menus/TestSceneLoader.cs
Bartłomiej Dach 15fb1a099e Modify assert to avoid false failures
In headless tests it was possible for TestInstantLoad() to erroneously
fail. There were two scenarios in which LoadingSpinner could be null:

1. If the test runner was quick enough, the assert could end up running
   even before Loader.OnEntering() had even had a chance to, meaning
   that the spinner was never even actually assigned to or instantiated
   at that point in time.

2. Even if Loader.OnEntering() had managed to run, there was also
   a possibility that the spinner itself wasn't loaded at the point
   of checking the assertion. As the spinner is accessed through
   ChildrenOfType(), which only checks InternalChildren and ignores
   all currently-loading drawables, it would therefore return null.

As null != 0, both of these cases would actually fail the test (this is
best seen running headless, preferably with a [Repeat] attribute
attached).

To resolve, allow the spinner to be null at the point of asserting and
duplicate the assertion step at the end. This weakens the test, as case
(1) should probably be waited for and case (2) could be solved with
exposition as protected in the base, but when attempting to wait for
the loader itself to be loaded there were also cases where the
appropriate until step would take so much time that the spinner would
actually become visible in line with the delayed display logic, so this
is a best-effort attempt to address both points without radical
changes.
2020-03-28 00:43:57 +01:00

114 lines
3.6 KiB
C#

// 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.Linq;
using System.Threading;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Screens;
using osu.Game.Screens.Menu;
using osuTK.Graphics;
namespace osu.Game.Tests.Visual.Menus
{
[TestFixture]
public class TestSceneLoader : ScreenTestScene
{
private TestLoader loader;
[Cached]
private OsuLogo logo;
public TestSceneLoader()
{
Child = logo = new OsuLogo
{
Alpha = 0,
Depth = float.MinValue
};
}
[Test]
public void TestInstantLoad()
{
AddStep("load immediately", () =>
{
loader = new TestLoader();
loader.AllowLoad.Set();
LoadScreen(loader);
});
spinnerNotPresentOrHidden();
AddUntilStep("loaded", () => loader.ScreenLoaded);
AddUntilStep("not current", () => !loader.IsCurrentScreen());
spinnerNotPresentOrHidden();
}
private void spinnerNotPresentOrHidden() =>
AddAssert("spinner did not display", () => loader.LoadingSpinner == null || loader.LoadingSpinner.Alpha == 0);
[Test]
public void TestDelayedLoad()
{
AddStep("begin loading", () => LoadScreen(loader = new TestLoader()));
AddUntilStep("wait for spinner visible", () => loader.LoadingSpinner?.Alpha > 0);
AddStep("finish loading", () => loader.AllowLoad.Set());
AddUntilStep("spinner gone", () => loader.LoadingSpinner?.Alpha == 0);
AddUntilStep("loaded", () => loader.ScreenLoaded);
AddUntilStep("not current", () => !loader.IsCurrentScreen());
}
private class TestLoader : Loader
{
public readonly ManualResetEventSlim AllowLoad = new ManualResetEventSlim();
public LoadingSpinner LoadingSpinner => this.ChildrenOfType<LoadingSpinner>().FirstOrDefault();
private TestScreen screen;
public bool ScreenLoaded => screen.IsCurrentScreen();
protected override OsuScreen CreateLoadableScreen() => screen = new TestScreen();
protected override ShaderPrecompiler CreateShaderPrecompiler() => new TestShaderPrecompiler(AllowLoad);
private class TestShaderPrecompiler : ShaderPrecompiler
{
private readonly ManualResetEventSlim allowLoad;
public TestShaderPrecompiler(ManualResetEventSlim allowLoad)
{
this.allowLoad = allowLoad;
}
protected override bool AllLoaded => allowLoad.IsSet;
}
private class TestScreen : OsuScreen
{
public TestScreen()
{
InternalChild = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.DarkSlateGray,
Alpha = 0,
};
}
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
base.LogoArriving(logo, resuming);
InternalChild.FadeInFromZero(200);
}
}
}
}
}