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.
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 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
}
[Test]
public void TestBoolLookup ( )
{
2020-04-06 18:35:27 +08:00
AddStep ( "Add config values" , ( ) = > userSource . Configuration . ConfigDictionary [ "BoolTest" ] = "1" ) ;
2019-09-04 12:36:50 +08:00
AddAssert ( "Check bool parse lookup" , ( ) = > requester . GetConfig < string , bool > ( "BoolTest" ) ? . Value = = true ) ;
}
[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
{
2020-04-07 10:50:40 +08:00
AddStep ( "Add custom combo colours to user skin" , ( ) = > userSource . Configuration . AddComboColours (
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 )
) ) ;
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" , ( ) = >
2020-04-06 18:35:27 +08:00
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 ) ;
2019-11-24 09:36:16 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < LegacySkinConfiguration . LegacySetting , decimal > ( LegacySkinConfiguration . LegacySetting . Version ) ? . Value = = 2.3 m ) ;
}
2020-04-06 18:35:27 +08:00
[Test]
public void TestSetBeatmapVersionNoFallback ( )
{
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 ) ;
2020-04-06 18:35:27 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < LegacySkinConfiguration . LegacySetting , decimal > ( LegacySkinConfiguration . LegacySetting . Version ) ? . Value = = 1.7 m ) ;
}
[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" ,
( ) = > 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 ( ) ) ) ) ;
2020-04-07 10:50:40 +08:00
AddStep ( "Set beatmap skin version null" , ( ) = > beatmapSource . Configuration . LegacyVersion = null ) ;
2020-04-06 18:35:27 +08:00
AddAssert ( "Check legacy version lookup" , ( ) = > requester . GetConfig < LegacySkinConfiguration . LegacySetting , decimal > ( LegacySkinConfiguration . LegacySetting . Version ) ? . Value = = 1.0 m ) ;
}
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 ( )
: base ( new TestBeatmap ( new OsuRuleset ( ) . RulesetInfo ) . BeatmapInfo , null , null )
{
}
}
2019-09-04 12:36:50 +08: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 ) ;
public Texture GetTexture ( string componentName ) = > skin . GetTexture ( componentName ) ;
public SampleChannel GetSample ( ISampleInfo sampleInfo ) = > skin . GetSample ( sampleInfo ) ;
public IBindable < TValue > GetConfig < TLookup , TValue > ( TLookup lookup ) = > skin . GetConfig < TLookup , TValue > ( lookup ) ;
}
}
}