2019-09-04 12:36:50 +08: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.
2022-06-17 15:37:17 +08:00
#nullable disable
2021-05-31 16:25:21 +08:00
using System ;
2019-09-04 12:36:50 +08:00
using System.Collections.Generic ;
2020-04-06 18:35:27 +08:00
using System.IO ;
2019-10-10 02:08:54 +08:00
using System.Linq ;
2019-09-04 12:36:50 +08:00
using NUnit.Framework ;
using osu.Framework.Allocation ;
using osu.Framework.Audio.Sample ;
using osu.Framework.Bindables ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Framework.Graphics.Textures ;
2019-09-24 18:35:42 +08:00
using osu.Framework.Testing ;
2019-09-04 12:36:50 +08:00
using osu.Game.Audio ;
2020-04-06 18:35:27 +08:00
using osu.Game.IO ;
using osu.Game.Rulesets.Osu ;
2019-09-04 12:36:50 +08:00
using osu.Game.Skinning ;
2020-04-06 18:35:27 +08:00
using osu.Game.Tests.Beatmaps ;
2019-09-04 12:36:50 +08:00
using osu.Game.Tests.Visual ;
using osuTK.Graphics ;
namespace osu.Game.Tests.Skins
{
[TestFixture]
2019-09-24 18:35:42 +08:00
[HeadlessTest]
2019-09-04 12:36:50 +08:00
public partial class TestSceneSkinConfigurationLookup : OsuTestScene
{
2020-04-06 18:35:27 +08:00
private UserSkinSource userSource ;
private BeatmapSkinSource beatmapSource ;
2019-09-04 12:36:50 +08:00
private SkinRequester requester ;
[SetUp]
public void SetUp ( ) = > Schedule ( ( ) = >
{
2020-04-06 18:35:27 +08:00
Add ( new SkinProvidingContainer ( userSource = new UserSkinSource ( ) )
. WithChild ( new SkinProvidingContainer ( beatmapSource = new BeatmapSkinSource ( ) )
2019-09-04 12:36:50 +08:00
. WithChild ( requester = new SkinRequester ( ) ) ) ) ;
} ) ;
[Test]
public void TestBasicLookup ( )
{
AddStep ( "Add config values" , ( ) = >
{
2020-04-07 10:50:40 +08:00
userSource . Configuration . ConfigDictionary [ "Lookup" ] = "user skin" ;
beatmapSource . Configuration . ConfigDictionary [ "Lookup" ] = "beatmap skin" ;
2019-09-04 12:36:50 +08:00
} ) ;
2020-04-07 10:50:40 +08:00
AddAssert ( "Check lookup finds beatmap skin" , ( ) = > requester . GetConfig < string , string > ( "Lookup" ) ? . Value = = "beatmap skin" ) ;
2019-09-04 12:36:50 +08:00
}
[Test]
2019-09-05 15:55:24 +08:00
public void TestFloatLookup ( )
2019-09-04 12:36:50 +08:00
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config values" , ( ) = > userSource . Configuration . ConfigDictionary [ "FloatTest" ] = "1.1" ) ;
2019-09-04 12:36:50 +08:00
AddAssert ( "Check float parse lookup" , ( ) = > requester . GetConfig < string , float > ( "FloatTest" ) ? . Value = = 1.1f ) ;
2019-09-05 15:55:24 +08:00
}
2022-06-07 01:57:08 +08:00
[TestCase("0", false)]
[TestCase("1", true)]
[TestCase("2", true)] // https://github.com/ppy/osu/issues/18579
public void TestBoolLookup ( string originalValue , bool expectedParsedValue )
2019-09-05 15:55:24 +08:00
{
2022-06-07 01:57:08 +08:00
AddStep ( "Add config values" , ( ) = > userSource . Configuration . ConfigDictionary [ "BoolTest" ] = originalValue ) ;
AddAssert ( "Check bool parse lookup" , ( ) = > requester . GetConfig < string , bool > ( "BoolTest" ) ? . Value = = expectedParsedValue ) ;
2019-09-04 12:36:50 +08:00
}
[Test]
public void TestEnumLookup ( )
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config values" , ( ) = > userSource . Configuration . ConfigDictionary [ "Test" ] = "Test2" ) ;
2019-09-05 15:55:24 +08:00
AddAssert ( "Check enum parse lookup" , ( ) = > requester . GetConfig < LookupType , ValueType > ( LookupType . Test ) ? . Value = = ValueType . Test2 ) ;
2019-09-04 12:36:50 +08:00
}
[Test]
public void TestLookupFailure ( )
{
AddAssert ( "Check lookup failure" , ( ) = > requester . GetConfig < string , float > ( "Lookup" ) = = null ) ;
}
[Test]
public void TestLookupNull ( )
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config values" , ( ) = > userSource . Configuration . ConfigDictionary [ "Lookup" ] = null ) ;
2019-09-04 12:36:50 +08:00
AddAssert ( "Check lookup null" , ( ) = >
{
var bindable = requester . GetConfig < string , string > ( "Lookup" ) ;
return bindable ! = null & & bindable . Value = = null ;
} ) ;
}
[Test]
public void TestColourLookup ( )
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config colour" , ( ) = > userSource . Configuration . CustomColours [ "Lookup" ] = Color4 . Red ) ;
2019-09-04 12:36:50 +08:00
AddAssert ( "Check colour lookup" , ( ) = > requester . GetConfig < SkinCustomColourLookup , Color4 > ( new SkinCustomColourLookup ( "Lookup" ) ) ? . Value = = Color4 . Red ) ;
}
[Test]
public void TestGlobalLookup ( )
{
2020-02-07 13:58:07 +08:00
AddAssert ( "Check combo colours" , ( ) = > requester . GetConfig < GlobalSkinColours , IReadOnlyList < Color4 > > ( GlobalSkinColours . ComboColours ) ? . Value ? . Count > 0 ) ;
2019-09-04 12:36:50 +08:00
}
2019-09-05 15:39:58 +08:00
[Test]
public void TestWrongColourType ( )
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config colour" , ( ) = > userSource . Configuration . CustomColours [ "Lookup" ] = Color4 . Red ) ;
2019-09-05 15:39:58 +08:00
AddAssert ( "perform incorrect lookup" , ( ) = >
{
try
{
requester . GetConfig < SkinCustomColourLookup , int > ( new SkinCustomColourLookup ( "Lookup" ) ) ;
return false ;
}
catch
{
return true ;
}
} ) ;
}
2019-12-12 17:48:07 +08:00
[Test]
public void TestEmptyComboColours ( )
{
AddAssert ( "Check retrieved combo colours is skin default colours" , ( ) = >
2020-02-07 13:58:07 +08:00
requester . GetConfig < GlobalSkinColours , IReadOnlyList < Color4 > > ( GlobalSkinColours . ComboColours ) ? . Value ? . SequenceEqual ( SkinConfiguration . DefaultComboColours ) ? ? false ) ;
2019-12-12 17:48:07 +08:00
}
[Test]
public void TestEmptyComboColoursNoFallback ( )
2019-10-10 02:08:54 +08:00
{
2021-08-15 22:00:22 +08:00
AddStep ( "Add custom combo colours to user skin" , ( ) = > userSource . Configuration . CustomComboColours = new List < Color4 >
{
2019-10-10 02:08:54 +08:00
new Color4 ( 100 , 150 , 200 , 255 ) ,
new Color4 ( 55 , 110 , 166 , 255 ) ,
2019-12-12 19:08:35 +08:00
new Color4 ( 75 , 125 , 175 , 255 )
2021-08-15 22:00:22 +08:00
} ) ;
2019-12-12 19:08:35 +08:00
2020-04-07 10:50:40 +08:00
AddStep ( "Disallow default colours fallback in beatmap skin" , ( ) = > beatmapSource . Configuration . AllowDefaultComboColoursFallback = false ) ;
2019-10-10 02:08:54 +08:00
2020-04-07 10:50:40 +08:00
AddAssert ( "Check retrieved combo colours from user skin" , ( ) = >
2022-11-09 15:45:06 +08:00
userSource . Configuration . ComboColours ! = null & &
( requester . GetConfig < GlobalSkinColours , IReadOnlyList < Color4 > > ( GlobalSkinColours . ComboColours ) ? . Value ? . SequenceEqual ( userSource . Configuration . ComboColours ) ? ? false ) ) ;
2019-10-10 02:08:54 +08:00
}
2019-11-24 09:36:16 +08:00
[Test]
2020-04-06 18:35:27 +08:00
public void TestNullBeatmapVersionFallsBackToUserSkin ( )
2019-11-24 09:36:16 +08:00
{
2020-04-07 10:50:40 +08:00
AddStep ( "Set user skin version 2.3" , ( ) = > userSource . Configuration . LegacyVersion = 2.3 m ) ;
AddStep ( "Set beatmap skin version null" , ( ) = > beatmapSource . Configuration . LegacyVersion = null ) ;
2021-10-22 13:41:59 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < SkinConfiguration . LegacySetting , decimal > ( SkinConfiguration . LegacySetting . Version ) ? . Value = = 2.3 m ) ;
2019-11-24 09:36:16 +08:00
}
2020-04-06 18:35:27 +08:00
[Test]
2020-05-12 10:08:30 +08:00
public void TestSetBeatmapVersionFallsBackToUserSkin ( )
2020-04-06 18:35:27 +08:00
{
2020-05-12 10:08:30 +08:00
// completely ignoring beatmap versions for simplicity.
2020-04-07 10:50:40 +08:00
AddStep ( "Set user skin version 2.3" , ( ) = > userSource . Configuration . LegacyVersion = 2.3 m ) ;
AddStep ( "Set beatmap skin version null" , ( ) = > beatmapSource . Configuration . LegacyVersion = 1.7 m ) ;
2021-10-22 13:41:59 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < SkinConfiguration . LegacySetting , decimal > ( SkinConfiguration . LegacySetting . Version ) ? . Value = = 2.3 m ) ;
2020-04-06 18:35:27 +08:00
}
[Test]
public void TestNullBeatmapAndUserVersionFallsBackToLatest ( )
{
2020-04-07 10:50:40 +08:00
AddStep ( "Set user skin version 2.3" , ( ) = > userSource . Configuration . LegacyVersion = null ) ;
AddStep ( "Set beatmap skin version null" , ( ) = > beatmapSource . Configuration . LegacyVersion = null ) ;
2020-04-06 18:35:27 +08:00
AddAssert ( "Check legacy version lookup" ,
2021-10-22 13:41:59 +08:00
( ) = > requester . GetConfig < SkinConfiguration . LegacySetting , decimal > ( SkinConfiguration . LegacySetting . Version ) ? . Value = = SkinConfiguration . LATEST_VERSION ) ;
2020-04-06 18:35:27 +08:00
}
[Test]
public void TestIniWithNoVersionFallsBackTo1 ( )
{
AddStep ( "Parse skin with no version" , ( ) = > userSource . Configuration = new LegacySkinDecoder ( ) . Decode ( new LineBufferedReader ( new MemoryStream ( ) ) ) ) ;
2021-10-22 13:41:59 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < SkinConfiguration . LegacySetting , decimal > ( SkinConfiguration . LegacySetting . Version ) ? . Value = = 1.0 m ) ;
2020-04-06 18:35:27 +08:00
}
2019-09-04 12:36:50 +08:00
public enum LookupType
{
Test
}
public enum ValueType
{
Test1 ,
Test2 ,
Test3
}
2020-04-06 18:35:27 +08:00
public class UserSkinSource : LegacySkin
2019-09-04 12:36:50 +08:00
{
2020-04-06 18:35:27 +08:00
public UserSkinSource ( )
2019-09-04 12:36:50 +08:00
: base ( new SkinInfo ( ) , null , null , string . Empty )
{
}
}
2020-04-06 18:35:27 +08:00
public class BeatmapSkinSource : LegacyBeatmapSkin
{
public BeatmapSkinSource ( )
2022-03-22 18:23:22 +08:00
: base ( new TestBeatmap ( new OsuRuleset ( ) . RulesetInfo ) . BeatmapInfo , null )
2020-04-06 18:35:27 +08:00
{
}
}
2019-09-04 12:36:50 +08:00
public partial class SkinRequester : Drawable , ISkin
{
private ISkinSource skin ;
[BackgroundDependencyLoader]
private void load ( ISkinSource skin )
{
this . skin = skin ;
}
2022-11-09 15:04:56 +08:00
public Drawable GetDrawableComponent ( ISkinComponentLookup lookup ) = > skin . GetDrawableComponent ( lookup ) ;
2019-09-04 12:36:50 +08:00
2020-07-17 15:54:30 +08:00
public Texture GetTexture ( string componentName , WrapMode wrapModeS , WrapMode wrapModeT ) = > skin . GetTexture ( componentName , wrapModeS , wrapModeT ) ;
2019-09-04 12:36:50 +08:00
2021-02-18 17:32:28 +08:00
public ISample GetSample ( ISampleInfo sampleInfo ) = > skin . GetSample ( sampleInfo ) ;
2019-09-04 12:36:50 +08:00
public IBindable < TValue > GetConfig < TLookup , TValue > ( TLookup lookup ) = > skin . GetConfig < TLookup , TValue > ( lookup ) ;
2021-05-31 16:25:21 +08:00
public ISkin FindProvider ( Func < ISkin , bool > lookupFunction ) = > skin . FindProvider ( lookupFunction ) ;
2019-09-04 12:36:50 +08:00
}
}
}