osu/osu.Game.Tests/Skins/TestSceneSkinConfigurationL...

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

236 lines
9.1 KiB
C#
Raw Normal View History

// 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 07:37:17 +00:00
#nullable disable
2021-05-31 08:25:21 +00:00
using System;
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;
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;
using osu.Framework.Graphics.Textures;
using osu.Framework.Testing;
using osu.Game.Audio;
2020-04-06 10:35:27 +00:00
using osu.Game.IO;
using osu.Game.Rulesets.Osu;
using osu.Game.Skinning;
2020-04-06 10:35:27 +00:00
using osu.Game.Tests.Beatmaps;
using osu.Game.Tests.Visual;
using osuTK.Graphics;
namespace osu.Game.Tests.Skins
{
[TestFixture]
[HeadlessTest]
public class TestSceneSkinConfigurationLookup : OsuTestScene
{
2020-04-06 10:35:27 +00:00
private UserSkinSource userSource;
private BeatmapSkinSource beatmapSource;
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())
.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";
});
2020-04-07 02:50:40 +00:00
AddAssert("Check lookup finds beatmap skin", () => requester.GetConfig<string, string>("Lookup")?.Value == "beatmap skin");
}
[Test]
2019-09-05 07:55:24 +00:00
public void TestFloatLookup()
{
2020-04-06 10:35:27 +00:00
AddStep("Add config values", () => userSource.Configuration.ConfigDictionary["FloatTest"] = "1.1");
AddAssert("Check float parse lookup", () => requester.GetConfig<string, float>("FloatTest")?.Value == 1.1f);
2019-09-05 07:55:24 +00: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 07:55:24 +00:00
{
AddStep("Add config values", () => userSource.Configuration.ConfigDictionary["BoolTest"] = originalValue);
AddAssert("Check bool parse lookup", () => requester.GetConfig<string, bool>("BoolTest")?.Value == expectedParsedValue);
}
[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);
}
[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);
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);
AddAssert("Check colour lookup", () => requester.GetConfig<SkinCustomColourLookup, Color4>(new SkinCustomColourLookup("Lookup"))?.Value == Color4.Red);
}
[Test]
public void TestGlobalLookup()
{
AddAssert("Check combo colours", () => requester.GetConfig<GlobalSkinColours, IReadOnlyList<Color4>>(GlobalSkinColours.ComboColours)?.Value?.Count > 0);
}
[Test]
public void TestWrongColourType()
{
2020-04-06 10:35:27 +00:00
AddStep("Add config colour", () => userSource.Configuration.CustomColours["Lookup"] = Color4.Red);
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", () =>
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
{
AddStep("Add custom combo colours to user skin", () => userSource.Configuration.CustomComboColours = new List<Color4>
{
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)
});
2019-12-12 11:08:35 +00:00
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
}
[Test]
2020-04-06 10:35:27 +00:00
public void TestNullBeatmapVersionFallsBackToUserSkin()
{
2020-04-07 02:50:40 +00:00
AddStep("Set user skin version 2.3", () => userSource.Configuration.LegacyVersion = 2.3m);
AddStep("Set beatmap skin version null", () => beatmapSource.Configuration.LegacyVersion = null);
AddAssert("Check legacy version lookup", () => requester.GetConfig<SkinConfiguration.LegacySetting, decimal>(SkinConfiguration.LegacySetting.Version)?.Value == 2.3m);
}
2020-04-06 10:35:27 +00:00
[Test]
public void TestSetBeatmapVersionFallsBackToUserSkin()
2020-04-06 10:35:27 +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.3m);
AddStep("Set beatmap skin version null", () => beatmapSource.Configuration.LegacyVersion = 1.7m);
AddAssert("Check legacy version lookup", () => requester.GetConfig<SkinConfiguration.LegacySetting, decimal>(SkinConfiguration.LegacySetting.Version)?.Value == 2.3m);
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<SkinConfiguration.LegacySetting, decimal>(SkinConfiguration.LegacySetting.Version)?.Value == SkinConfiguration.LATEST_VERSION);
2020-04-06 10:35:27 +00:00
}
[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<SkinConfiguration.LegacySetting, decimal>(SkinConfiguration.LegacySetting.Version)?.Value == 1.0m);
2020-04-06 10:35:27 +00:00
}
public enum LookupType
{
Test
}
public enum ValueType
{
Test1,
Test2,
Test3
}
2020-04-06 10:35:27 +00:00
public class UserSkinSource : LegacySkin
{
2020-04-06 10:35:27 +00:00
public UserSkinSource()
: 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)
2020-04-06 10:35:27 +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);
public ISample GetSample(ISampleInfo sampleInfo) => skin.GetSample(sampleInfo);
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);
}
}
}