2019-01-24 16:43:03 +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.
2018-04-13 17:19:50 +08:00
using System ;
2022-02-28 02:23:02 +08:00
using System.Diagnostics ;
2020-03-19 05:23:06 +08:00
using System.Linq ;
2020-03-19 03:04:38 +08:00
using NUnit.Framework ;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics ;
2022-02-13 23:27:12 +08:00
using osu.Framework.Graphics.Cursor ;
2020-03-19 05:23:06 +08:00
using osu.Framework.Graphics.Shapes ;
using osu.Framework.Graphics.UserInterface ;
using osu.Framework.Testing ;
2018-11-06 17:28:22 +08:00
using osu.Game.Screens.Edit ;
using osu.Game.Screens.Edit.Compose.Components ;
2018-11-20 15:51:59 +08:00
using osuTK ;
2020-03-19 04:55:35 +08:00
using osuTK.Input ;
2018-04-13 17:19:50 +08:00
2020-04-23 16:07:55 +08:00
namespace osu.Game.Tests.Visual.Editing
2018-04-13 17:19:50 +08:00
{
2020-03-23 09:01:33 +08:00
public class TestSceneBeatDivisorControl : OsuManualInputManagerTestScene
2018-04-13 17:19:50 +08:00
{
2020-03-19 03:04:38 +08:00
private BeatDivisorControl beatDivisorControl ;
private BindableBeatDivisor bindableBeatDivisor ;
2018-04-13 17:19:50 +08:00
2022-02-13 22:50:40 +08:00
private SliderBar < int > tickSliderBar = > beatDivisorControl . ChildrenOfType < SliderBar < int > > ( ) . Single ( ) ;
private EquilateralTriangle tickMarkerHead = > tickSliderBar . ChildrenOfType < EquilateralTriangle > ( ) . Single ( ) ;
2020-03-19 05:23:06 +08:00
2020-03-19 04:54:17 +08:00
[SetUp]
public void SetUp ( ) = > Schedule ( ( ) = >
2018-04-13 17:19:50 +08:00
{
2022-02-13 23:27:12 +08:00
Child = new PopoverContainer
2018-04-13 17:19:50 +08:00
{
2022-02-13 23:27:12 +08:00
RelativeSizeAxes = Axes . Both ,
Child = beatDivisorControl = new BeatDivisorControl ( bindableBeatDivisor = new BindableBeatDivisor ( 16 ) )
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Size = new Vector2 ( 90 , 90 )
}
2018-04-13 17:19:50 +08:00
} ;
2020-03-19 04:54:17 +08:00
} ) ;
2020-03-19 03:04:38 +08:00
[Test]
public void TestBindableBeatDivisor ( )
{
2020-03-19 05:23:06 +08:00
AddRepeatStep ( "move previous" , ( ) = > bindableBeatDivisor . Previous ( ) , 4 ) ;
AddAssert ( "divisor is 4" , ( ) = > bindableBeatDivisor . Value = = 4 ) ;
AddRepeatStep ( "move next" , ( ) = > bindableBeatDivisor . Next ( ) , 3 ) ;
AddAssert ( "divisor is 12" , ( ) = > bindableBeatDivisor . Value = = 12 ) ;
2020-03-19 03:04:38 +08:00
}
[Test]
public void TestMouseInput ( )
{
2020-03-19 05:23:06 +08:00
AddStep ( "hold marker" , ( ) = >
2020-03-19 03:04:38 +08:00
{
2020-03-19 05:23:06 +08:00
InputManager . MoveMouseTo ( tickMarkerHead . ScreenSpaceDrawQuad . Centre ) ;
2020-03-19 04:55:35 +08:00
InputManager . PressButton ( MouseButton . Left ) ;
2020-03-19 03:04:38 +08:00
} ) ;
2020-03-19 05:23:06 +08:00
AddStep ( "move to 8 and release" , ( ) = >
2020-03-19 03:04:38 +08:00
{
2020-03-19 05:23:06 +08:00
InputManager . MoveMouseTo ( tickSliderBar . ScreenSpaceDrawQuad . Centre ) ;
2020-03-19 04:55:35 +08:00
InputManager . ReleaseButton ( MouseButton . Left ) ;
2020-03-19 03:04:38 +08:00
} ) ;
2020-03-19 05:23:06 +08:00
AddAssert ( "divisor is 8" , ( ) = > bindableBeatDivisor . Value = = 8 ) ;
AddStep ( "hold marker" , ( ) = > InputManager . PressButton ( MouseButton . Left ) ) ;
AddStep ( "move to 16" , ( ) = > InputManager . MoveMouseTo ( getPositionForDivisor ( 16 ) ) ) ;
AddStep ( "move to ~10 and release" , ( ) = >
2020-03-19 03:04:38 +08:00
{
2020-03-19 05:23:06 +08:00
InputManager . MoveMouseTo ( getPositionForDivisor ( 10 ) ) ;
2020-03-19 04:55:35 +08:00
InputManager . ReleaseButton ( MouseButton . Left ) ;
2020-03-19 03:04:38 +08:00
} ) ;
2020-03-19 05:23:06 +08:00
AddAssert ( "divisor clamped to 8" , ( ) = > bindableBeatDivisor . Value = = 8 ) ;
}
private Vector2 getPositionForDivisor ( int divisor )
{
2021-10-27 12:04:41 +08:00
float relativePosition = ( float ) Math . Clamp ( divisor , 0 , 16 ) / 16 ;
2020-03-19 05:23:06 +08:00
var sliderDrawQuad = tickSliderBar . ScreenSpaceDrawQuad ;
return new Vector2 (
sliderDrawQuad . TopLeft . X + sliderDrawQuad . Width * relativePosition ,
sliderDrawQuad . Centre . Y
) ;
2020-03-19 03:04:38 +08:00
}
2022-02-28 02:23:02 +08:00
[Test]
public void TestBeatChevronNavigation ( )
{
switchBeatSnap ( 1 ) ;
assertBeatSnap ( 1 ) ;
switchBeatSnap ( 3 ) ;
assertBeatSnap ( 8 ) ;
switchBeatSnap ( - 1 ) ;
assertBeatSnap ( 4 ) ;
switchBeatSnap ( - 3 ) ;
assertBeatSnap ( 16 ) ;
}
[Test]
public void TestBeatPresetNavigation ( )
{
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( - 1 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
switchPresets ( - 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
setDivisorViaInput ( 3 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
setDivisorViaInput ( 8 ) ;
assertPreset ( BeatDivisorType . Common ) ;
setDivisorViaInput ( 15 ) ;
assertPreset ( BeatDivisorType . Custom , 15 ) ;
switchBeatSnap ( - 1 ) ;
assertBeatSnap ( 5 ) ;
switchBeatSnap ( - 1 ) ;
assertBeatSnap ( 3 ) ;
setDivisorViaInput ( 5 ) ;
assertPreset ( BeatDivisorType . Custom , 15 ) ;
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( - 1 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
}
private void switchBeatSnap ( int direction ) = > AddRepeatStep ( $"move snap {(direction > 0 ? " forward " : " backward ")}" , ( ) = >
{
int chevronIndex = direction > 0 ? 1 : 0 ;
var chevronButton = beatDivisorControl . ChildrenOfType < BeatDivisorControl . ChevronButton > ( ) . ElementAt ( chevronIndex ) ;
InputManager . MoveMouseTo ( chevronButton ) ;
InputManager . Click ( MouseButton . Left ) ;
} , Math . Abs ( direction ) ) ;
private void assertBeatSnap ( int expected ) = > AddAssert ( $"beat snap is {expected}" ,
( ) = > bindableBeatDivisor . Value = = expected ) ;
private void switchPresets ( int direction ) = > AddRepeatStep ( $"move presets {(direction > 0 ? " forward " : " backward ")}" , ( ) = >
{
int chevronIndex = direction > 0 ? 3 : 2 ;
var chevronButton = beatDivisorControl . ChildrenOfType < BeatDivisorControl . ChevronButton > ( ) . ElementAt ( chevronIndex ) ;
InputManager . MoveMouseTo ( chevronButton ) ;
InputManager . Click ( MouseButton . Left ) ;
} , Math . Abs ( direction ) ) ;
private void assertPreset ( BeatDivisorType type , int? maxDivisor = null )
{
AddAssert ( $"preset is {type}" , ( ) = > bindableBeatDivisor . ValidDivisors . Value . Type = = type ) ;
if ( type = = BeatDivisorType . Custom )
{
Debug . Assert ( maxDivisor ! = null ) ;
AddAssert ( $"max divisor is {maxDivisor}" , ( ) = > bindableBeatDivisor . ValidDivisors . Value . Presets . Max ( ) = = maxDivisor . Value ) ;
}
}
private void setDivisorViaInput ( int divisor )
{
AddStep ( "open divisor input popover" , ( ) = >
{
var button = beatDivisorControl . ChildrenOfType < BeatDivisorControl . DivisorDisplay > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
BeatDivisorControl . CustomDivisorPopover popover = null ;
AddUntilStep ( "wait for popover" , ( ) = > ( popover = this . ChildrenOfType < BeatDivisorControl . CustomDivisorPopover > ( ) . SingleOrDefault ( ) ) ! = null & & popover . IsLoaded ) ;
AddStep ( $"set divisor to {divisor}" , ( ) = >
{
var textBox = popover . ChildrenOfType < TextBox > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( textBox ) ;
InputManager . Click ( MouseButton . Left ) ;
textBox . Text = divisor . ToString ( ) ;
InputManager . Key ( Key . Enter ) ;
} ) ;
AddStep ( "dismiss popover" , ( ) = >
{
InputManager . Key ( Key . Escape ) ;
} ) ;
AddUntilStep ( "wait for dismiss" , ( ) = > ! this . ChildrenOfType < BeatDivisorControl . CustomDivisorPopover > ( ) . Any ( ) ) ;
}
2018-04-13 17:19:50 +08:00
}
}