2019-01-24 17:43:03 +09: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 18:19:50 +09:00
using System ;
2022-02-27 19:23:02 +01:00
using System.Diagnostics ;
2020-03-18 22:23:06 +01:00
using System.Linq ;
2020-03-18 21:04:38 +02:00
using NUnit.Framework ;
2022-05-24 16:50:16 +09:00
using osu.Framework.Allocation ;
2018-04-13 18:19:50 +09:00
using osu.Framework.Graphics ;
2022-02-13 16:27:12 +01:00
using osu.Framework.Graphics.Cursor ;
2020-03-18 22:23:06 +01:00
using osu.Framework.Graphics.Shapes ;
using osu.Framework.Graphics.UserInterface ;
using osu.Framework.Testing ;
2022-05-24 16:50:16 +09:00
using osu.Game.Overlays ;
2018-11-06 18:28:22 +09:00
using osu.Game.Screens.Edit ;
using osu.Game.Screens.Edit.Compose.Components ;
2018-11-20 16:51:59 +09:00
using osuTK ;
2020-03-18 21:55:35 +01:00
using osuTK.Input ;
2018-04-13 18:19:50 +09:00
2020-04-23 11:07:55 +03:00
namespace osu.Game.Tests.Visual.Editing
2018-04-13 18:19:50 +09:00
{
2022-11-24 14:32:20 +09:00
public partial class TestSceneBeatDivisorControl : OsuManualInputManagerTestScene
2018-04-13 18:19:50 +09:00
{
2023-06-01 16:28:47 +09:00
private BeatDivisorControl beatDivisorControl = null ! ;
2018-04-13 18:19:50 +09:00
2022-02-13 15:50:40 +01:00
private SliderBar < int > tickSliderBar = > beatDivisorControl . ChildrenOfType < SliderBar < int > > ( ) . Single ( ) ;
2022-05-24 18:38:08 +09:00
private Triangle tickMarkerHead = > tickSliderBar . ChildrenOfType < Triangle > ( ) . Single ( ) ;
2020-03-18 22:23:06 +01:00
2022-05-24 16:50:16 +09:00
[Cached]
2022-05-24 17:36:43 +09:00
private readonly OverlayColourProvider overlayColour = new OverlayColourProvider ( OverlayColourScheme . Aquamarine ) ;
2022-05-24 16:50:16 +09:00
2023-07-14 14:02:48 +09:00
[Cached]
private readonly BindableBeatDivisor bindableBeatDivisor = new BindableBeatDivisor ( 16 ) ;
2020-03-18 21:54:17 +01:00
[SetUp]
public void SetUp ( ) = > Schedule ( ( ) = >
2018-04-13 18:19:50 +09:00
{
2023-07-14 14:02:48 +09:00
bindableBeatDivisor . ValidDivisors . SetDefault ( ) ;
bindableBeatDivisor . SetDefault ( ) ;
2022-02-13 16:27:12 +01:00
Child = new PopoverContainer
2018-04-13 18:19:50 +09:00
{
2022-02-13 16:27:12 +01:00
RelativeSizeAxes = Axes . Both ,
2023-07-14 14:02:48 +09:00
Child = beatDivisorControl = new BeatDivisorControl
2022-02-13 16:27:12 +01:00
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
2023-05-17 16:33:58 +09:00
Size = new Vector2 ( 90 , 90 ) ,
Scale = new Vector2 ( 3 ) ,
2022-02-13 16:27:12 +01:00
}
2018-04-13 18:19:50 +09:00
} ;
2020-03-18 21:54:17 +01:00
} ) ;
2020-03-18 21:04:38 +02:00
[Test]
public void TestBindableBeatDivisor ( )
{
2023-06-08 00:53:58 +09:00
AddRepeatStep ( "move previous" , ( ) = > bindableBeatDivisor . SelectPrevious ( ) , 2 ) ;
2020-03-18 22:23:06 +01:00
AddAssert ( "divisor is 4" , ( ) = > bindableBeatDivisor . Value = = 4 ) ;
2023-06-08 00:53:58 +09:00
AddRepeatStep ( "move next" , ( ) = > bindableBeatDivisor . SelectNext ( ) , 1 ) ;
2022-02-27 20:13:44 +01:00
AddAssert ( "divisor is 12" , ( ) = > bindableBeatDivisor . Value = = 8 ) ;
2020-03-18 21:04:38 +02:00
}
[Test]
public void TestMouseInput ( )
{
2020-03-18 22:23:06 +01:00
AddStep ( "hold marker" , ( ) = >
2020-03-18 21:04:38 +02:00
{
2020-03-18 22:23:06 +01:00
InputManager . MoveMouseTo ( tickMarkerHead . ScreenSpaceDrawQuad . Centre ) ;
2020-03-18 21:55:35 +01:00
InputManager . PressButton ( MouseButton . Left ) ;
2020-03-18 21:04:38 +02:00
} ) ;
2023-05-23 16:46:08 +02:00
AddStep ( "move to 1" , ( ) = > InputManager . MoveMouseTo ( getPositionForDivisor ( 1 ) ) ) ;
AddStep ( "move to 16 and release" , ( ) = >
2020-03-18 21:04:38 +02:00
{
2023-05-23 16:46:08 +02:00
InputManager . MoveMouseTo ( getPositionForDivisor ( 16 ) ) ;
2020-03-18 21:55:35 +01:00
InputManager . ReleaseButton ( MouseButton . Left ) ;
2020-03-18 21:04:38 +02:00
} ) ;
2023-05-23 16:46:08 +02:00
AddAssert ( "divisor is 16" , ( ) = > bindableBeatDivisor . Value = = 16 ) ;
2020-03-18 22:23:06 +01:00
AddStep ( "hold marker" , ( ) = > InputManager . PressButton ( MouseButton . Left ) ) ;
2023-05-23 16:46:08 +02:00
AddStep ( "move to ~6 and release" , ( ) = >
{
InputManager . MoveMouseTo ( getPositionForDivisor ( 6 ) ) ;
InputManager . ReleaseButton ( MouseButton . Left ) ;
} ) ;
AddAssert ( "divisor clamped to 8" , ( ) = > bindableBeatDivisor . Value = = 8 ) ;
AddStep ( "move to ~10 and click" , ( ) = >
2020-03-18 21:04:38 +02:00
{
2020-03-18 22:23:06 +01:00
InputManager . MoveMouseTo ( getPositionForDivisor ( 10 ) ) ;
2023-05-23 16:46:08 +02:00
InputManager . PressButton ( MouseButton . Left ) ;
2020-03-18 21:55:35 +01:00
InputManager . ReleaseButton ( MouseButton . Left ) ;
2020-03-18 21:04:38 +02:00
} ) ;
2020-03-18 22:23:06 +01:00
AddAssert ( "divisor clamped to 8" , ( ) = > bindableBeatDivisor . Value = = 8 ) ;
}
private Vector2 getPositionForDivisor ( int divisor )
{
2023-05-23 17:59:07 +02:00
float localX = ( 1 - 1 / ( float ) divisor ) * tickSliderBar . UsableWidth + tickSliderBar . RangePadding ;
2023-05-23 16:46:40 +02:00
return tickSliderBar . ToScreenSpace ( new Vector2 (
localX ,
2023-05-23 17:59:07 +02:00
tickSliderBar . DrawHeight / 2
2023-05-23 16:46:40 +02:00
) ) ;
2020-03-18 21:04:38 +02:00
}
2022-02-27 19:23:02 +01:00
[Test]
public void TestBeatChevronNavigation ( )
{
switchBeatSnap ( 1 ) ;
2023-06-08 00:53:58 +09:00
assertBeatSnap ( 16 ) ;
switchBeatSnap ( - 4 ) ;
2022-02-27 19:23:02 +01:00
assertBeatSnap ( 1 ) ;
switchBeatSnap ( 3 ) ;
assertBeatSnap ( 8 ) ;
2023-06-07 23:31:46 +02:00
switchBeatSnap ( 3 ) ;
assertBeatSnap ( 16 ) ;
switchBeatSnap ( - 2 ) ;
2022-02-27 19:23:02 +01:00
assertBeatSnap ( 4 ) ;
switchBeatSnap ( - 3 ) ;
2023-06-08 00:53:58 +09:00
assertBeatSnap ( 1 ) ;
2022-02-27 19:23:02 +01:00
}
2022-10-25 11:51:31 +09:00
[Test]
public void TestKeyboardNavigation ( )
{
pressKey ( 1 ) ;
assertBeatSnap ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
pressKey ( 2 ) ;
assertBeatSnap ( 2 ) ;
assertPreset ( BeatDivisorType . Common ) ;
pressKey ( 3 ) ;
assertBeatSnap ( 3 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
pressKey ( 4 ) ;
assertBeatSnap ( 4 ) ;
assertPreset ( BeatDivisorType . Common ) ;
pressKey ( 5 ) ;
assertBeatSnap ( 5 ) ;
assertPreset ( BeatDivisorType . Custom , 5 ) ;
pressKey ( 6 ) ;
assertBeatSnap ( 6 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
pressKey ( 7 ) ;
assertBeatSnap ( 7 ) ;
assertPreset ( BeatDivisorType . Custom , 7 ) ;
pressKey ( 8 ) ;
assertBeatSnap ( 8 ) ;
assertPreset ( BeatDivisorType . Common ) ;
void pressKey ( int key ) = > AddStep ( $"press shift+{key}" , ( ) = >
{
InputManager . PressKey ( Key . ShiftLeft ) ;
InputManager . Key ( Key . Number0 + key ) ;
InputManager . ReleaseKey ( Key . ShiftLeft ) ;
} ) ;
}
2022-02-27 19:23:02 +01:00
[Test]
public void TestBeatPresetNavigation ( )
{
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
2023-06-01 17:12:00 +09:00
assertBeatSnap ( 6 ) ;
2022-02-27 19:23:02 +01:00
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
2023-06-01 17:12:00 +09:00
assertBeatSnap ( 4 ) ;
2022-02-27 19:23:02 +01:00
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 ) ;
2023-06-01 17:12:00 +09:00
assertBeatSnap ( 15 ) ;
2022-02-27 19:23:02 +01:00
switchBeatSnap ( - 1 ) ;
assertBeatSnap ( 5 ) ;
switchBeatSnap ( - 1 ) ;
assertBeatSnap ( 3 ) ;
setDivisorViaInput ( 5 ) ;
assertPreset ( BeatDivisorType . Custom , 15 ) ;
2023-06-01 17:12:00 +09:00
assertBeatSnap ( 5 ) ;
2022-02-27 19:23:02 +01:00
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( - 1 ) ;
2023-06-01 17:12:00 +09:00
assertPreset ( BeatDivisorType . Custom , 15 ) ;
assertBeatSnap ( 15 ) ;
2024-01-24 08:01:41 -05:00
setDivisorViaInput ( 24 ) ;
assertPreset ( BeatDivisorType . Custom , 24 ) ;
switchPresets ( 1 ) ;
assertPreset ( BeatDivisorType . Common ) ;
switchPresets ( - 2 ) ;
assertPreset ( BeatDivisorType . Triplets ) ;
2022-02-27 19:23:02 +01:00
}
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}" ,
2023-06-08 00:53:58 +09:00
( ) = > bindableBeatDivisor . Value , ( ) = > Is . EqualTo ( expected ) ) ;
2022-02-27 19:23:02 +01:00
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 )
{
2023-06-01 17:12:00 +09:00
AddAssert ( $"preset is {type}" , ( ) = > bindableBeatDivisor . ValidDivisors . Value . Type , ( ) = > Is . EqualTo ( type ) ) ;
2022-02-27 19:23:02 +01:00
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 ) ;
} ) ;
2023-06-01 16:28:47 +09:00
BeatDivisorControl . CustomDivisorPopover ? popover = null ;
2022-02-27 19:23:02 +01:00
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 ) ;
} ) ;
AddUntilStep ( "wait for dismiss" , ( ) = > ! this . ChildrenOfType < BeatDivisorControl . CustomDivisorPopover > ( ) . Any ( ) ) ;
}
2018-04-13 18:19:50 +09:00
}
}