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