2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2020-06-05 11:45:42 +08:00
// See the LICENCE file in the repository root for full licence text.
2021-10-17 18:36:21 +08:00
using System.Collections.Generic ;
using System.Linq ;
2020-06-05 11:45:42 +08:00
using NUnit.Framework ;
2021-10-17 18:36:21 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2020-06-05 11:45:42 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
2021-10-17 18:36:21 +08:00
using osu.Framework.Testing ;
2020-06-05 11:45:42 +08:00
using osu.Game.Graphics.UserInterface ;
2021-10-17 18:36:21 +08:00
using osu.Game.Overlays ;
2020-06-05 11:45:42 +08:00
using osuTK ;
2024-07-10 20:08:30 +08:00
using osuTK.Input ;
2020-06-05 11:45:42 +08:00
namespace osu.Game.Tests.Visual.UserInterface
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneOsuTextBox : ThemeComparisonTestScene
2020-06-05 11:45:42 +08:00
{
2021-10-17 18:36:21 +08:00
private IEnumerable < OsuNumberBox > numberBoxes = > this . ChildrenOfType < OsuNumberBox > ( ) ;
2020-06-05 11:45:42 +08:00
2021-10-17 18:36:21 +08:00
protected override Drawable CreateContent ( ) = > new FillFlowContainer
2020-06-05 11:45:42 +08:00
{
2021-10-17 18:36:21 +08:00
RelativeSizeAxes = Axes . X ,
AutoSizeAxes = Axes . Y ,
Direction = FillDirection . Vertical ,
Padding = new MarginPadding ( 50f ) ,
Spacing = new Vector2 ( 0f , 50f ) ,
Children = new [ ]
2020-06-05 11:45:42 +08:00
{
2021-10-17 18:36:21 +08:00
new OsuTextBox
2020-06-05 11:45:42 +08:00
{
2021-10-17 18:36:21 +08:00
RelativeSizeAxes = Axes . X ,
PlaceholderText = "Normal textbox" ,
} ,
new OsuPasswordTextBox
{
RelativeSizeAxes = Axes . X ,
PlaceholderText = "Password textbox" ,
} ,
new OsuNumberBox
{
RelativeSizeAxes = Axes . X ,
PlaceholderText = "Number textbox"
2020-06-05 11:45:42 +08:00
}
2021-10-17 18:36:21 +08:00
}
} ;
2020-06-05 11:45:42 +08:00
[Test]
public void TestNumberBox ( )
{
2021-10-17 18:36:21 +08:00
AddStep ( "create themed content" , ( ) = > CreateThemedContent ( OverlayColourScheme . Red ) ) ;
clearTextboxes ( numberBoxes ) ;
AddStep ( "enter numbers" , ( ) = > numberBoxes . ForEach ( numberBox = > numberBox . Text = "987654321" ) ) ;
expectedValue ( numberBoxes , "987654321" ) ;
2020-06-05 11:45:42 +08:00
2021-10-17 18:36:21 +08:00
clearTextboxes ( numberBoxes ) ;
AddStep ( "enter text + single number" , ( ) = > numberBoxes . ForEach ( numberBox = > numberBox . Text = "1 hello 2 world 3" ) ) ;
expectedValue ( numberBoxes , "123" ) ;
2020-06-05 11:45:42 +08:00
2021-10-17 18:36:21 +08:00
clearTextboxes ( numberBoxes ) ;
2020-06-05 11:45:42 +08:00
}
2024-07-10 20:08:30 +08:00
[Test]
public void TestSelectAllOnFocus ( )
{
AddStep ( "create themed content" , ( ) = > CreateThemedContent ( OverlayColourScheme . Red ) ) ;
AddStep ( "enter numbers" , ( ) = > numberBoxes . ForEach ( numberBox = > numberBox . Text = "987654321" ) ) ;
AddAssert ( "nothing selected" , ( ) = > string . IsNullOrEmpty ( numberBoxes . First ( ) . SelectedText ) ) ;
AddStep ( "click on a number box" , ( ) = >
{
InputManager . MoveMouseTo ( numberBoxes . First ( ) ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
AddAssert ( "text selected" , ( ) = > numberBoxes . First ( ) . SelectedText = = "987654321" ) ;
2024-07-25 04:19:04 +08:00
AddStep ( "click away" , ( ) = >
{
InputManager . MoveMouseTo ( Vector2 . Zero ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
Drawable textContainer = null ! ;
AddStep ( "move mouse to end of text" , ( ) = >
{
textContainer = numberBoxes . First ( ) . ChildrenOfType < Container > ( ) . ElementAt ( 1 ) ;
InputManager . MoveMouseTo ( textContainer . ScreenSpaceDrawQuad . TopRight ) ;
} ) ;
AddStep ( "hold mouse" , ( ) = > InputManager . PressButton ( MouseButton . Left ) ) ;
2024-07-25 04:26:23 +08:00
AddStep ( "drag to half" , ( ) = > InputManager . MoveMouseTo ( textContainer . ScreenSpaceDrawQuad . BottomRight - new Vector2 ( textContainer . ScreenSpaceDrawQuad . Width / 2 + 1f , 0 ) ) ) ;
2024-07-25 04:19:04 +08:00
AddStep ( "release mouse" , ( ) = > InputManager . ReleaseButton ( MouseButton . Left ) ) ;
AddAssert ( "half text selected" , ( ) = > numberBoxes . First ( ) . SelectedText = = "54321" ) ;
2024-07-10 20:08:30 +08:00
}
2021-10-17 18:36:21 +08:00
private void clearTextboxes ( IEnumerable < OsuTextBox > textBoxes ) = > AddStep ( "clear textbox" , ( ) = > textBoxes . ForEach ( textBox = > textBox . Text = null ) ) ;
2021-12-10 13:15:00 +08:00
private void expectedValue ( IEnumerable < OsuTextBox > textBoxes , string value ) = > AddAssert ( "expected textbox value" , ( ) = > textBoxes . All ( textBox = > textBox . Text = = value ) ) ;
2020-06-05 11:45:42 +08:00
}
}