From dcae86e39a0d9b4058d6f77a2cdc9c05bb3c922e Mon Sep 17 00:00:00 2001 From: David Zhao Date: Fri, 22 Mar 2019 14:10:38 +0900 Subject: [PATCH] Add a new test for OsuScreenStack, fix parallax --- .../Visual/TestCaseOsuScreenStack.cs | 45 +++++++++++++++++++ osu.Game/OsuGame.cs | 2 + osu.Game/Screens/OsuScreenStack.cs | 8 +++- osu.Game/Tests/Visual/AllPlayersTestCase.cs | 10 ++--- osu.Game/Tests/Visual/PlayerTestCase.cs | 7 +-- 5 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 osu.Game.Tests/Visual/TestCaseOsuScreenStack.cs diff --git a/osu.Game.Tests/Visual/TestCaseOsuScreenStack.cs b/osu.Game.Tests/Visual/TestCaseOsuScreenStack.cs new file mode 100644 index 0000000000..c3a9e94448 --- /dev/null +++ b/osu.Game.Tests/Visual/TestCaseOsuScreenStack.cs @@ -0,0 +1,45 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Framework.Graphics; +using osu.Framework.Screens; +using osu.Framework.Testing; +using osu.Game.Graphics.Containers; +using osu.Game.Screens; +using osu.Game.Screens.Play; + +namespace osu.Game.Tests.Visual +{ + [TestFixture] + public class TestCaseOsuScreenStack : OsuTestCase + { + private TestScreen baseScreen; + private TestOsuScreenStack stack; + + [SetUpSteps] + public void Setup() + { + AddStep("Create new screen stack", () => { Child = stack = new TestOsuScreenStack { RelativeSizeAxes = Axes.Both }; }); + AddStep("Push new base screen", () => stack.Push(baseScreen = new TestScreen())); + } + + [Test] + public void ParallaxAssignmentTest() + { + AddStep("Push new screen to base screen", () => baseScreen.Push(new TestScreen())); + AddAssert("Parallax is correct", () => stack.IsParallaxSet); + AddStep("Exit from new screen", () => { baseScreen.MakeCurrent(); }); + AddAssert("Parallax is correct", () => stack.IsParallaxSet); + } + + private class TestScreen : ScreenWithBeatmapBackground + { + } + + private class TestOsuScreenStack : OsuScreenStack + { + public bool IsParallaxSet => ParallaxAmount == ((TestScreen)CurrentScreen).BackgroundParallaxAmount * ParallaxContainer.DEFAULT_PARALLAX_AMOUNT; + } + } +} diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index ad2980d818..079a0d5bb6 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -406,6 +406,8 @@ namespace osu.Game loadComponentSingleFile(osuLogo, logo => { logoContainer.Add(logo); + + // Loader has to be created synchronously in order for DI to be successful for its background screen stack. screenStack.Push(new Loader { RelativeSizeAxes = Axes.Both diff --git a/osu.Game/Screens/OsuScreenStack.cs b/osu.Game/Screens/OsuScreenStack.cs index 2eea7bebbb..1c929470d1 100644 --- a/osu.Game/Screens/OsuScreenStack.cs +++ b/osu.Game/Screens/OsuScreenStack.cs @@ -15,6 +15,8 @@ namespace osu.Game.Screens private ParallaxContainer parallaxContainer; + protected float ParallaxAmount => parallaxContainer.ParallaxAmount; + public OsuScreenStack() { initializeStack(); @@ -35,11 +37,15 @@ namespace osu.Game.Screens }; ScreenPushed += setParallax; + ScreenExited += setParallax; } private void setParallax(IScreen prev, IScreen next) { - parallaxContainer.ParallaxAmount = ParallaxContainer.DEFAULT_PARALLAX_AMOUNT * ((IOsuScreen)next).BackgroundParallaxAmount; + if (next != null) + { + parallaxContainer.ParallaxAmount = ParallaxContainer.DEFAULT_PARALLAX_AMOUNT * ((IOsuScreen)next).BackgroundParallaxAmount; + } } } } diff --git a/osu.Game/Tests/Visual/AllPlayersTestCase.cs b/osu.Game/Tests/Visual/AllPlayersTestCase.cs index 507848730f..03bd7b218a 100644 --- a/osu.Game/Tests/Visual/AllPlayersTestCase.cs +++ b/osu.Game/Tests/Visual/AllPlayersTestCase.cs @@ -73,15 +73,11 @@ namespace osu.Game.Tests.Visual Player?.Exit(); Player = null; - var player = CreatePlayer(r); + Player = CreatePlayer(r); - LoadComponentAsync(player, p => - { - Player = p; - LoadScreen(p); - }); + LoadScreen(Player); - return player; + return Player; } protected virtual Player CreatePlayer(Ruleset ruleset) => new Player diff --git a/osu.Game/Tests/Visual/PlayerTestCase.cs b/osu.Game/Tests/Visual/PlayerTestCase.cs index ad01d82281..50cb839ed9 100644 --- a/osu.Game/Tests/Visual/PlayerTestCase.cs +++ b/osu.Game/Tests/Visual/PlayerTestCase.cs @@ -52,11 +52,8 @@ namespace osu.Game.Tests.Visual if (!AllowFail) Beatmap.Value.Mods.Value = new[] { ruleset.GetAllMods().First(m => m is ModNoFail) }; - LoadComponentAsync(Player = CreatePlayer(ruleset), p => - { - Player = p; - LoadScreen(p); - }); + Player = CreatePlayer(ruleset); + LoadScreen(Player); } protected virtual Player CreatePlayer(Ruleset ruleset) => new Player