2021-03-03 02:18:01 +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-03-03 02:18:01 +08:00
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Bindables ;
using osu.Framework.Testing ;
2021-10-19 03:01:43 +08:00
using osu.Framework.Utils ;
2022-05-31 13:01:42 +08:00
using osu.Game.Graphics.Containers ;
2021-10-19 03:01:43 +08:00
using osu.Game.Graphics.Sprites ;
using osu.Game.Graphics.UserInterface ;
2021-03-03 02:18:01 +08:00
using osu.Game.Overlays.Settings ;
2021-05-14 23:01:17 +08:00
using osu.Game.Overlays ;
2021-03-03 02:18:01 +08:00
namespace osu.Game.Tests.Visual.Settings
{
[TestFixture]
public partial class TestSceneSettingsItem : OsuTestScene
{
[Test]
public void TestRestoreDefaultValueButtonVisibility ( )
{
2021-07-11 00:56:44 +08:00
SettingsTextBox textBox = null ;
2023-07-13 12:46:50 +08:00
RevertToDefaultButton < string > revertToDefaultButton = null ;
2021-03-03 02:18:01 +08:00
2021-07-11 00:56:44 +08:00
AddStep ( "create settings item" , ( ) = >
2021-03-03 02:18:01 +08:00
{
2021-07-11 00:56:44 +08:00
Child = textBox = new SettingsTextBox
2021-03-03 02:18:01 +08:00
{
2022-09-26 04:02:40 +08:00
Current = new Bindable < string > ( "test" )
2021-07-11 00:56:44 +08:00
} ;
2021-03-03 02:18:01 +08:00
} ) ;
2021-10-16 04:44:28 +08:00
AddUntilStep ( "wait for loaded" , ( ) = > textBox . IsLoaded ) ;
2023-07-13 12:46:50 +08:00
AddStep ( "retrieve restore default button" , ( ) = > revertToDefaultButton = textBox . ChildrenOfType < RevertToDefaultButton < string > > ( ) . Single ( ) ) ;
2021-10-16 04:44:28 +08:00
2023-07-13 12:46:50 +08:00
AddAssert ( "restore button hidden" , ( ) = > revertToDefaultButton . Alpha = = 0 ) ;
2021-03-03 02:18:01 +08:00
AddStep ( "change value from default" , ( ) = > textBox . Current . Value = "non-default" ) ;
2023-07-13 12:46:50 +08:00
AddUntilStep ( "restore button shown" , ( ) = > revertToDefaultButton . Alpha > 0 ) ;
2021-03-03 02:18:01 +08:00
AddStep ( "restore default" , ( ) = > textBox . Current . SetDefault ( ) ) ;
2023-07-13 12:46:50 +08:00
AddUntilStep ( "restore button hidden" , ( ) = > revertToDefaultButton . Alpha = = 0 ) ;
2021-03-03 02:18:01 +08:00
}
2021-10-16 04:44:28 +08:00
[Test]
public void TestSetAndClearLabelText ( )
{
SettingsTextBox textBox = null ;
2023-07-13 12:46:50 +08:00
RevertToDefaultButton < string > revertToDefaultButton = null ;
2021-10-19 03:01:43 +08:00
OsuTextBox control = null ;
2021-10-16 04:44:28 +08:00
AddStep ( "create settings item" , ( ) = >
{
Child = textBox = new SettingsTextBox
{
2022-09-26 04:02:40 +08:00
Current = new Bindable < string > ( "test" )
2021-10-16 04:44:28 +08:00
} ;
} ) ;
AddUntilStep ( "wait for loaded" , ( ) = > textBox . IsLoaded ) ;
AddStep ( "retrieve components" , ( ) = >
{
2023-07-13 12:46:50 +08:00
revertToDefaultButton = textBox . ChildrenOfType < RevertToDefaultButton < string > > ( ) . Single ( ) ;
2021-10-19 03:01:43 +08:00
control = textBox . ChildrenOfType < OsuTextBox > ( ) . Single ( ) ;
2021-10-16 04:44:28 +08:00
} ) ;
2023-07-13 12:46:50 +08:00
AddStep ( "set non-default value" , ( ) = > revertToDefaultButton . Current . Value = "non-default" ) ;
2023-10-17 16:40:44 +08:00
AddAssert ( "default value button centre aligned to control size" , ( ) = > Precision . AlmostEquals ( revertToDefaultButton . Parent ! . DrawHeight , control . DrawHeight , 1 ) ) ;
2021-10-16 04:44:28 +08:00
AddStep ( "set label" , ( ) = > textBox . LabelText = "label text" ) ;
2021-10-19 03:01:43 +08:00
AddAssert ( "default value button centre aligned to label size" , ( ) = >
{
var label = textBox . ChildrenOfType < OsuSpriteText > ( ) . Single ( spriteText = > spriteText . Text = = "label text" ) ;
2023-10-17 16:40:44 +08:00
return Precision . AlmostEquals ( revertToDefaultButton . Parent ! . DrawHeight , label . DrawHeight , 1 ) ;
2021-10-19 03:01:43 +08:00
} ) ;
2021-10-16 04:44:28 +08:00
AddStep ( "clear label" , ( ) = > textBox . LabelText = default ) ;
2023-10-17 16:40:44 +08:00
AddAssert ( "default value button centre aligned to control size" , ( ) = > Precision . AlmostEquals ( revertToDefaultButton . Parent ! . DrawHeight , control . DrawHeight , 1 ) ) ;
2021-10-19 03:01:43 +08:00
2022-06-01 15:51:34 +08:00
AddStep ( "set warning text" , ( ) = > textBox . SetNoticeText ( "This is some very important warning text! Hopefully it doesn't break the alignment of the default value indicator..." , true ) ) ;
2023-10-17 16:40:44 +08:00
AddAssert ( "default value button centre aligned to control size" , ( ) = > Precision . AlmostEquals ( revertToDefaultButton . Parent ! . DrawHeight , control . DrawHeight , 1 ) ) ;
2021-10-16 04:44:28 +08:00
}
2021-07-11 00:56:44 +08:00
/// <summary>
2021-07-11 23:26:00 +08:00
/// Ensures that the reset to default button uses the correct implementation of IsDefault to determine whether it should be shown or not.
/// Values have been chosen so that after being set, Value != Default (but they are close enough that the difference is negligible compared to Precision).
2021-07-11 00:56:44 +08:00
/// </summary>
[TestCase(4.2f)]
[TestCase(9.9f)]
public void TestRestoreDefaultValueButtonPrecision ( float initialValue )
2021-03-03 02:18:01 +08:00
{
2021-07-11 23:26:00 +08:00
BindableFloat current = null ;
2021-07-11 00:56:44 +08:00
SettingsSlider < float > sliderBar = null ;
2023-07-13 12:46:50 +08:00
RevertToDefaultButton < float > revertToDefaultButton = null ;
2021-07-11 00:56:44 +08:00
AddStep ( "create settings item" , ( ) = >
{
Child = sliderBar = new SettingsSlider < float >
{
2021-07-11 23:26:00 +08:00
Current = current = new BindableFloat ( initialValue )
2021-07-11 00:56:44 +08:00
{
MinValue = 0f ,
MaxValue = 10f ,
Precision = 0.1f ,
}
} ;
} ) ;
2021-10-16 04:44:28 +08:00
AddUntilStep ( "wait for loaded" , ( ) = > sliderBar . IsLoaded ) ;
2023-07-13 12:46:50 +08:00
AddStep ( "retrieve restore default button" , ( ) = > revertToDefaultButton = sliderBar . ChildrenOfType < RevertToDefaultButton < float > > ( ) . Single ( ) ) ;
2021-07-11 00:56:44 +08:00
2023-07-13 12:46:50 +08:00
AddAssert ( "restore button hidden" , ( ) = > revertToDefaultButton . Alpha = = 0 ) ;
2021-07-11 00:56:44 +08:00
2021-07-11 23:26:00 +08:00
AddStep ( "change value to next closest" , ( ) = > sliderBar . Current . Value + = current . Precision * 0.6f ) ;
2023-07-13 12:46:50 +08:00
AddUntilStep ( "restore button shown" , ( ) = > revertToDefaultButton . Alpha > 0 ) ;
2021-07-11 00:56:44 +08:00
AddStep ( "restore default" , ( ) = > sliderBar . Current . SetDefault ( ) ) ;
2023-07-13 12:46:50 +08:00
AddUntilStep ( "restore button hidden" , ( ) = > revertToDefaultButton . Alpha = = 0 ) ;
2021-03-03 02:18:01 +08:00
}
2021-08-20 04:31:11 +08:00
[Test]
public void TestWarningTextVisibility ( )
{
SettingsNumberBox numberBox = null ;
AddStep ( "create settings item" , ( ) = > Child = numberBox = new SettingsNumberBox ( ) ) ;
2022-05-31 13:01:42 +08:00
AddAssert ( "warning text not created" , ( ) = > ! numberBox . ChildrenOfType < LinkFlowContainer > ( ) . Any ( ) ) ;
2021-08-20 04:31:11 +08:00
2022-06-01 15:51:34 +08:00
AddStep ( "set warning text" , ( ) = > numberBox . SetNoticeText ( "this is a warning!" , true ) ) ;
2022-05-31 13:01:42 +08:00
AddAssert ( "warning text created" , ( ) = > numberBox . ChildrenOfType < LinkFlowContainer > ( ) . Single ( ) . Alpha = = 1 ) ;
2021-08-20 04:31:11 +08:00
2022-06-01 15:51:34 +08:00
AddStep ( "unset warning text" , ( ) = > numberBox . ClearNoticeText ( ) ) ;
2022-05-31 13:01:42 +08:00
AddAssert ( "warning text hidden" , ( ) = > ! numberBox . ChildrenOfType < LinkFlowContainer > ( ) . Any ( ) ) ;
2021-08-20 04:31:11 +08:00
2022-06-01 15:51:34 +08:00
AddStep ( "set warning text again" , ( ) = > numberBox . SetNoticeText ( "another warning!" , true ) ) ;
2022-05-31 13:01:42 +08:00
AddAssert ( "warning text shown again" , ( ) = > numberBox . ChildrenOfType < LinkFlowContainer > ( ) . Single ( ) . Alpha = = 1 ) ;
2022-06-01 15:51:34 +08:00
AddStep ( "set non warning text" , ( ) = > numberBox . SetNoticeText ( "you did good!" ) ) ;
2021-08-20 04:31:11 +08:00
}
2021-03-03 02:18:01 +08:00
}
2021-07-11 00:56:44 +08:00
}