2019-01-24 08:43:03 +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.
2018-04-13 09:19:50 +00:00
2018-05-11 12:40:48 +00:00
using System ;
2018-04-13 09:19:50 +00:00
using System.Collections.Generic ;
2019-03-24 16:02:36 +00:00
using System.Linq ;
2018-08-07 07:45:18 +00:00
using NUnit.Framework ;
2019-03-24 16:02:36 +00:00
using osu.Framework.Allocation ;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables ;
2019-03-24 16:02:36 +00:00
using osu.Framework.Graphics ;
2019-12-13 11:21:35 +00:00
using osu.Framework.Testing ;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics.Sprites ;
2019-03-24 16:02:36 +00:00
using osu.Game.Graphics.UserInterface ;
using osu.Game.Overlays.Mods ;
2018-07-31 09:00:42 +00:00
using osu.Game.Overlays.Mods.Sections ;
2019-03-24 16:02:36 +00:00
using osu.Game.Rulesets ;
2019-12-13 11:21:35 +00:00
using osu.Game.Rulesets.Mania ;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Mania.Mods ;
2019-03-24 16:02:36 +00:00
using osu.Game.Rulesets.Mods ;
2019-12-13 11:21:35 +00:00
using osu.Game.Rulesets.Osu ;
2019-03-24 16:02:36 +00:00
using osu.Game.Rulesets.Osu.Mods ;
2018-05-11 12:40:48 +00:00
using osu.Game.Rulesets.UI ;
2019-03-24 16:02:36 +00:00
using osu.Game.Screens.Play.HUD ;
using osuTK ;
2018-11-20 07:51:59 +00:00
using osuTK.Graphics ;
2018-04-13 09:19:50 +00:00
2019-03-24 16:02:36 +00:00
namespace osu.Game.Tests.Visual.UserInterface
2018-04-13 09:19:50 +00:00
{
[Description("mod select and icon display")]
2019-06-19 16:40:58 +00:00
public class TestSceneModSelectOverlay : OsuTestScene
2018-04-13 09:19:50 +00:00
{
2018-05-11 12:40:48 +00:00
public override IReadOnlyList < Type > RequiredTypes = > new [ ]
{
typeof ( ModDisplay ) ,
typeof ( ModSection ) ,
typeof ( ModIcon ) ,
typeof ( ModButton ) ,
typeof ( ModButtonEmpty ) ,
typeof ( DifficultyReductionSection ) ,
typeof ( DifficultyIncreaseSection ) ,
2018-07-31 09:00:42 +00:00
typeof ( AutomationSection ) ,
typeof ( ConversionSection ) ,
typeof ( FunSection ) ,
2018-05-11 12:40:48 +00:00
} ;
2018-04-13 09:19:50 +00:00
private RulesetStore rulesets ;
private ModDisplay modDisplay ;
private TestModSelectOverlay modSelect ;
[BackgroundDependencyLoader]
private void load ( RulesetStore rulesets )
{
this . rulesets = rulesets ;
2019-12-13 11:21:35 +00:00
}
2018-04-13 09:19:50 +00:00
2019-12-13 11:21:35 +00:00
[SetUp]
public void SetUp ( ) = > Schedule ( ( ) = >
{
Children = new Drawable [ ]
2018-04-13 09:19:50 +00:00
{
2019-12-13 11:21:35 +00:00
modSelect = new TestModSelectOverlay
{
Origin = Anchor . BottomCentre ,
Anchor = Anchor . BottomCentre ,
2020-01-30 10:55:25 +00:00
SelectedMods = { BindTarget = SelectedMods }
2019-12-13 11:21:35 +00:00
} ,
modDisplay = new ModDisplay
{
Anchor = Anchor . TopRight ,
Origin = Anchor . TopRight ,
AutoSizeAxes = Axes . Both ,
Position = new Vector2 ( 0 , 25 ) ,
Current = { BindTarget = modSelect . SelectedMods }
}
} ;
} ) ;
[SetUpSteps]
public void SetUpSteps ( )
{
AddStep ( "show" , ( ) = > modSelect . Show ( ) ) ;
2018-04-13 09:19:50 +00:00
}
2018-08-07 07:45:18 +00:00
[Test]
public void TestOsuMods ( )
2018-04-13 09:19:50 +00:00
{
2019-12-13 11:21:35 +00:00
changeRuleset ( 0 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
var osu = new OsuRuleset ( ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
var easierMods = osu . GetModsFor ( ModType . DifficultyReduction ) ;
var harderMods = osu . GetModsFor ( ModType . DifficultyIncrease ) ;
2018-04-13 09:19:50 +00:00
2019-12-13 11:21:35 +00:00
var noFailMod = osu . GetModsFor ( ModType . DifficultyReduction ) . FirstOrDefault ( m = > m is OsuModNoFail ) ;
2018-04-13 09:19:50 +00:00
var hiddenMod = harderMods . FirstOrDefault ( m = > m is OsuModHidden ) ;
var doubleTimeMod = harderMods . OfType < MultiMod > ( ) . FirstOrDefault ( m = > m . Mods . Any ( a = > a is OsuModDoubleTime ) ) ;
2019-05-12 13:04:37 +00:00
var spunOutMod = easierMods . FirstOrDefault ( m = > m is OsuModSpunOut ) ;
2018-04-13 09:19:50 +00:00
var easy = easierMods . FirstOrDefault ( m = > m is OsuModEasy ) ;
var hardRock = harderMods . FirstOrDefault ( m = > m is OsuModHardRock ) ;
testSingleMod ( noFailMod ) ;
testMultiMod ( doubleTimeMod ) ;
testIncompatibleMods ( easy , hardRock ) ;
testDeselectAll ( easierMods . Where ( m = > ! ( m is MultiMod ) ) ) ;
2019-12-13 11:21:35 +00:00
testMultiplierTextColour ( noFailMod , ( ) = > modSelect . LowMultiplierColour ) ;
testMultiplierTextColour ( hiddenMod , ( ) = > modSelect . HighMultiplierColour ) ;
2018-04-13 09:19:50 +00:00
2019-05-12 13:04:37 +00:00
testUnimplementedMod ( spunOutMod ) ;
2018-04-13 09:19:50 +00:00
}
2018-08-07 07:45:18 +00:00
[Test]
public void TestManiaMods ( )
2018-04-13 09:19:50 +00:00
{
2019-12-13 11:21:35 +00:00
changeRuleset ( 3 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
testRankedText ( new ManiaRuleset ( ) . GetModsFor ( ModType . Conversion ) . First ( m = > m is ManiaModRandom ) ) ;
2018-08-07 07:45:18 +00:00
}
[Test]
public void TestRulesetChanges ( )
{
2019-12-13 11:21:35 +00:00
changeRuleset ( 0 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
var noFailMod = new OsuRuleset ( ) . GetModsFor ( ModType . DifficultyReduction ) . FirstOrDefault ( m = > m is OsuModNoFail ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 12:45:38 +00:00
AddStep ( "set mods externally" , ( ) = > { SelectedMods . Value = new [ ] { noFailMod } ; } ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
changeRuleset ( 0 ) ;
2018-08-07 07:45:18 +00:00
AddAssert ( "ensure mods still selected" , ( ) = > modDisplay . Current . Value . Single ( m = > m is OsuModNoFail ) ! = null ) ;
2019-12-13 11:21:35 +00:00
changeRuleset ( 3 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
AddAssert ( "ensure mods not selected" , ( ) = > modDisplay . Current . Value . Count = = 0 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
changeRuleset ( 0 ) ;
2018-08-07 07:45:18 +00:00
2019-12-13 11:21:35 +00:00
AddAssert ( "ensure mods not selected" , ( ) = > modDisplay . Current . Value . Count = = 0 ) ;
2018-04-13 09:19:50 +00:00
}
private void testSingleMod ( Mod mod )
{
selectNext ( mod ) ;
checkSelected ( mod ) ;
selectPrevious ( mod ) ;
checkNotSelected ( mod ) ;
selectNext ( mod ) ;
selectNext ( mod ) ;
checkNotSelected ( mod ) ;
selectPrevious ( mod ) ;
selectPrevious ( mod ) ;
checkNotSelected ( mod ) ;
}
private void testMultiMod ( MultiMod multiMod )
{
foreach ( var mod in multiMod . Mods )
{
selectNext ( mod ) ;
checkSelected ( mod ) ;
}
for ( int index = multiMod . Mods . Length - 1 ; index > = 0 ; index - - )
selectPrevious ( multiMod . Mods [ index ] ) ;
foreach ( var mod in multiMod . Mods )
checkNotSelected ( mod ) ;
}
2018-07-05 02:32:09 +00:00
private void testUnimplementedMod ( Mod mod )
2018-04-13 09:19:50 +00:00
{
selectNext ( mod ) ;
checkNotSelected ( mod ) ;
}
private void testIncompatibleMods ( Mod modA , Mod modB )
{
selectNext ( modA ) ;
checkSelected ( modA ) ;
checkNotSelected ( modB ) ;
selectNext ( modB ) ;
checkSelected ( modB ) ;
checkNotSelected ( modA ) ;
selectPrevious ( modB ) ;
checkNotSelected ( modA ) ;
checkNotSelected ( modB ) ;
}
private void testDeselectAll ( IEnumerable < Mod > mods )
{
foreach ( var mod in mods )
selectNext ( mod ) ;
AddAssert ( "check for any selection" , ( ) = > modSelect . SelectedMods . Value . Any ( ) ) ;
2019-12-13 11:21:35 +00:00
AddStep ( "deselect all" , ( ) = > modSelect . DeselectAllButton . Action . Invoke ( ) ) ;
2018-04-13 09:19:50 +00:00
AddAssert ( "check for no selection" , ( ) = > ! modSelect . SelectedMods . Value . Any ( ) ) ;
}
2019-12-13 11:21:35 +00:00
private void testMultiplierTextColour ( Mod mod , Func < Color4 > getCorrectColour )
2018-04-13 09:19:50 +00:00
{
2019-12-13 11:21:35 +00:00
checkLabelColor ( ( ) = > Color4 . White ) ;
2018-04-13 09:19:50 +00:00
selectNext ( mod ) ;
2019-03-19 08:24:26 +00:00
AddWaitStep ( "wait for changing colour" , 1 ) ;
2019-12-13 11:21:35 +00:00
checkLabelColor ( getCorrectColour ) ;
2018-04-13 09:19:50 +00:00
selectPrevious ( mod ) ;
2019-03-19 08:24:26 +00:00
AddWaitStep ( "wait for changing colour" , 1 ) ;
2019-12-13 11:21:35 +00:00
checkLabelColor ( ( ) = > Color4 . White ) ;
2018-04-13 09:19:50 +00:00
}
2018-05-31 03:36:37 +00:00
private void testRankedText ( Mod mod )
2018-04-13 09:19:50 +00:00
{
2019-06-21 08:51:25 +00:00
AddUntilStep ( "check for ranked" , ( ) = > modSelect . UnrankedLabel . Alpha = = 0 ) ;
2018-04-13 09:19:50 +00:00
selectNext ( mod ) ;
2019-06-21 08:51:25 +00:00
AddUntilStep ( "check for unranked" , ( ) = > modSelect . UnrankedLabel . Alpha ! = 0 ) ;
2018-04-13 09:19:50 +00:00
selectPrevious ( mod ) ;
2019-06-21 08:51:25 +00:00
AddUntilStep ( "check for ranked" , ( ) = > modSelect . UnrankedLabel . Alpha = = 0 ) ;
2018-04-13 09:19:50 +00:00
}
private void selectNext ( Mod mod ) = > AddStep ( $"left click {mod.Name}" , ( ) = > modSelect . GetModButton ( mod ) ? . SelectNext ( 1 ) ) ;
private void selectPrevious ( Mod mod ) = > AddStep ( $"right click {mod.Name}" , ( ) = > modSelect . GetModButton ( mod ) ? . SelectNext ( - 1 ) ) ;
private void checkSelected ( Mod mod )
{
AddAssert ( $"check {mod.Name} is selected" , ( ) = >
{
var button = modSelect . GetModButton ( mod ) ;
return modSelect . SelectedMods . Value . Single ( m = > m . Name = = mod . Name ) ! = null & & button . SelectedMod . GetType ( ) = = mod . GetType ( ) & & button . Selected ;
} ) ;
}
2019-12-13 11:21:35 +00:00
private void changeRuleset ( int? id )
2019-06-20 15:12:39 +00:00
{
2019-12-13 11:35:34 +00:00
AddStep ( $"change ruleset to {(id?.ToString() ?? " none ")}" , ( ) = > { Ruleset . Value = rulesets . AvailableRulesets . FirstOrDefault ( r = > r . ID = = id ) ; } ) ;
2019-06-21 08:51:25 +00:00
waitForLoad ( ) ;
2019-06-20 15:12:39 +00:00
}
2019-06-21 08:51:25 +00:00
private void waitForLoad ( ) = >
AddUntilStep ( "wait for icons to load" , ( ) = > modSelect . AllLoaded ) ;
2018-04-13 09:19:50 +00:00
private void checkNotSelected ( Mod mod )
{
AddAssert ( $"check {mod.Name} is not selected" , ( ) = >
{
var button = modSelect . GetModButton ( mod ) ;
return modSelect . SelectedMods . Value . All ( m = > m . GetType ( ) ! = mod . GetType ( ) ) & & button . SelectedMod ? . GetType ( ) ! = mod . GetType ( ) ;
} ) ;
}
2019-12-13 11:21:35 +00:00
private void checkLabelColor ( Func < Color4 > getColour ) = > AddAssert ( "check label has expected colour" , ( ) = > modSelect . MultiplierLabel . Colour . AverageColour = = getColour ( ) ) ;
2018-04-13 09:19:50 +00:00
private class TestModSelectOverlay : ModSelectOverlay
{
2019-04-10 08:13:12 +00:00
public new Bindable < IReadOnlyList < Mod > > SelectedMods = > base . SelectedMods ;
2018-08-07 05:49:44 +00:00
2019-06-20 15:12:39 +00:00
public bool AllLoaded = > ModSectionsContainer . Children . All ( c = > c . ModIconsLoaded ) ;
2018-04-13 09:19:50 +00:00
public ModButton GetModButton ( Mod mod )
{
var section = ModSectionsContainer . Children . Single ( s = > s . ModType = = mod . Type ) ;
return section . ButtonsContainer . OfType < ModButton > ( ) . Single ( b = > b . Mods . Any ( m = > m . GetType ( ) = = mod . GetType ( ) ) ) ;
}
public new OsuSpriteText MultiplierLabel = > base . MultiplierLabel ;
2018-05-31 03:39:56 +00:00
public new OsuSpriteText UnrankedLabel = > base . UnrankedLabel ;
2018-04-13 09:19:50 +00:00
public new TriangleButton DeselectAllButton = > base . DeselectAllButton ;
public new Color4 LowMultiplierColour = > base . LowMultiplierColour ;
public new Color4 HighMultiplierColour = > base . HighMultiplierColour ;
}
}
}