2021-11-11 21:00:09 +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-11-11 22:08:43 +08:00
using System ;
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Allocation ;
2022-10-25 12:01:24 +08:00
using osu.Framework.Bindables ;
2022-01-03 16:31:12 +08:00
using osu.Framework.Extensions ;
2021-11-11 22:08:43 +08:00
using osu.Framework.Screens ;
using osu.Framework.Testing ;
using osu.Game.Beatmaps ;
2022-10-25 12:01:24 +08:00
using osu.Game.Configuration ;
2021-11-11 21:00:09 +08:00
using osu.Game.Rulesets ;
2021-12-10 17:57:33 +08:00
using osu.Game.Rulesets.Mods ;
2021-11-11 21:00:09 +08:00
using osu.Game.Rulesets.Osu ;
2024-07-02 20:21:05 +08:00
using osu.Game.Rulesets.UI ;
2021-11-13 20:50:57 +08:00
using osu.Game.Screens.Backgrounds ;
2021-11-11 22:08:43 +08:00
using osu.Game.Screens.Edit ;
using osu.Game.Screens.Edit.Components.Timelines.Summary ;
2021-11-12 19:07:38 +08:00
using osu.Game.Screens.Edit.GameplayTest ;
2021-11-12 19:41:16 +08:00
using osu.Game.Screens.Play ;
2022-01-25 20:25:28 +08:00
using osu.Game.Storyboards ;
2021-11-11 22:08:43 +08:00
using osu.Game.Tests.Beatmaps.IO ;
using osuTK.Input ;
2021-11-11 21:00:09 +08:00
namespace osu.Game.Tests.Visual.Editing
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneEditorTestGameplay : EditorTestScene
2021-11-11 21:00:09 +08:00
{
2021-11-11 22:08:43 +08:00
protected override bool IsolateSavingFromDatabase = > false ;
2021-11-11 21:00:09 +08:00
protected override Ruleset CreateEditorRuleset ( ) = > new OsuRuleset ( ) ;
2021-11-11 22:08:43 +08:00
[Resolved]
private OsuGameBase game { get ; set ; }
[Resolved]
private BeatmapManager beatmaps { get ; set ; }
private BeatmapSetInfo importedBeatmapSet ;
2022-10-25 12:01:24 +08:00
private Bindable < float > editorDim ;
[BackgroundDependencyLoader]
private void load ( OsuConfigManager config )
{
editorDim = config . GetBindable < float > ( OsuSetting . EditorDim ) ;
}
2021-11-11 22:08:43 +08:00
public override void SetUpSteps ( )
{
2021-12-17 17:26:12 +08:00
AddStep ( "import test beatmap" , ( ) = > importedBeatmapSet = BeatmapImportHelper . LoadOszIntoOsu ( game ) . GetResultSafely ( ) ) ;
2021-11-11 22:08:43 +08:00
base . SetUpSteps ( ) ;
}
2022-01-25 20:25:28 +08:00
protected override WorkingBeatmap CreateWorkingBeatmap ( IBeatmap beatmap , Storyboard storyboard = null )
2022-01-27 14:19:48 +08:00
= > beatmaps . GetWorkingBeatmap ( importedBeatmapSet . Beatmaps . First ( b = > b . Ruleset . OnlineID = = 0 ) ) ;
2022-01-25 20:25:28 +08:00
2021-11-11 22:08:43 +08:00
protected override void LoadEditor ( )
{
2021-12-10 17:57:33 +08:00
SelectedMods . Value = new [ ] { new ModCinema ( ) } ;
2021-11-11 22:08:43 +08:00
base . LoadEditor ( ) ;
}
[Test]
public void TestBasicGameplayTest ( )
{
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
AddStep ( "exit player" , ( ) = > editorPlayer . Exit ( ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
2021-11-13 20:50:57 +08:00
AddUntilStep ( "background has correct params" , ( ) = >
{
2022-01-27 03:52:14 +08:00
// the test gameplay player's beatmap may be the "same" beatmap as the one being edited, *but* the `BeatmapInfo` references may differ
// due to the beatmap refetch logic ran on editor suspend.
// this test cares about checking the background belonging to the editor specifically, so check that using reference equality
// (as `.Equals()` cannot discern between the two, as they technically share the same database GUID).
var background = this . ChildrenOfType < BackgroundScreenBeatmap > ( ) . Single ( b = > ReferenceEquals ( b . Beatmap . BeatmapInfo , EditorBeatmap . BeatmapInfo ) ) ;
2022-11-02 16:49:52 +08:00
return background . DimWhenUserSettingsIgnored . Value = = editorDim . Value & & background . BlurAmount . Value = = 0 ;
2021-11-13 20:50:57 +08:00
} ) ;
2021-12-10 17:57:33 +08:00
AddAssert ( "no mods selected" , ( ) = > SelectedMods . Value . Count = = 0 ) ;
2021-11-11 22:08:43 +08:00
}
2021-11-13 21:01:00 +08:00
[Test]
public void TestGameplayTestWhenTrackRunning ( )
{
AddStep ( "start track" , ( ) = > EditorClock . Start ( ) ) ;
AddAssert ( "sample playback enabled" , ( ) = > ! Editor . SamplePlaybackDisabled . Value ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
AddAssert ( "editor track stopped" , ( ) = > ! EditorClock . IsRunning ) ;
AddAssert ( "sample playback disabled" , ( ) = > Editor . SamplePlaybackDisabled . Value ) ;
AddStep ( "exit player" , ( ) = > editorPlayer . Exit ( ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
AddUntilStep ( "background has correct params" , ( ) = >
{
2022-01-27 03:52:14 +08:00
// the test gameplay player's beatmap may be the "same" beatmap as the one being edited, *but* the `BeatmapInfo` references may differ
// due to the beatmap refetch logic ran on editor suspend.
// this test cares about checking the background belonging to the editor specifically, so check that using reference equality
// (as `.Equals()` cannot discern between the two, as they technically share the same database GUID).
var background = this . ChildrenOfType < BackgroundScreenBeatmap > ( ) . Single ( b = > ReferenceEquals ( b . Beatmap . BeatmapInfo , EditorBeatmap . BeatmapInfo ) ) ;
2022-11-02 16:49:52 +08:00
return background . DimWhenUserSettingsIgnored . Value = = editorDim . Value & & background . BlurAmount . Value = = 0 ;
2021-11-13 21:01:00 +08:00
} ) ;
AddStep ( "start track" , ( ) = > EditorClock . Start ( ) ) ;
AddAssert ( "sample playback re-enabled" , ( ) = > ! Editor . SamplePlaybackDisabled . Value ) ;
}
2024-06-28 15:30:28 +08:00
[TestCase(2000)] // chosen to be after last object in the map
[TestCase(22000)] // chosen to be in the middle of the last spinner
public void TestGameplayTestAtEndOfBeatmap ( int offsetFromEnd )
2024-01-10 15:53:54 +08:00
{
2024-06-28 15:30:28 +08:00
AddStep ( $"seek to end minus {offsetFromEnd}ms" , ( ) = > EditorClock . Seek ( importedBeatmapSet . MaxLength - offsetFromEnd ) ) ;
2024-01-10 15:53:54 +08:00
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
AddUntilStep ( "player pushed" , ( ) = > Stack . CurrentScreen is EditorPlayer ) ;
2024-06-28 15:30:28 +08:00
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
2024-01-10 15:53:54 +08:00
}
2021-11-11 22:08:43 +08:00
[Test]
public void TestCancelGameplayTestWithUnsavedChanges ( )
{
AddStep ( "delete all but first object" , ( ) = > EditorBeatmap . RemoveRange ( EditorBeatmap . HitObjects . Skip ( 1 ) . ToList ( ) ) ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
2023-10-25 13:33:06 +08:00
AddUntilStep ( "save prompt shown" , ( ) = > DialogOverlay . CurrentDialog is SaveRequiredPopupDialog ) ;
2021-11-11 22:08:43 +08:00
AddStep ( "dismiss prompt" , ( ) = >
{
2024-02-02 18:48:13 +08:00
var button = DialogOverlay . CurrentDialog ! . Buttons . Last ( ) ;
2021-11-11 22:08:43 +08:00
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
AddWaitStep ( "wait some" , 3 ) ;
AddAssert ( "stayed in editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
}
[Test]
public void TestSaveChangesBeforeGameplayTest ( )
{
AddStep ( "delete all but first object" , ( ) = > EditorBeatmap . RemoveRange ( EditorBeatmap . HitObjects . Skip ( 1 ) . ToList ( ) ) ) ;
// bit of a hack to ensure this test can be ran multiple times without running into UNIQUE constraint failures
AddStep ( "set unique difficulty name" , ( ) = > EditorBeatmap . BeatmapInfo . DifficultyName = Guid . NewGuid ( ) . ToString ( ) ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
2023-10-25 13:33:06 +08:00
AddUntilStep ( "save prompt shown" , ( ) = > DialogOverlay . CurrentDialog is SaveRequiredPopupDialog ) ;
2021-11-11 22:08:43 +08:00
2024-02-02 18:48:13 +08:00
AddStep ( "save changes" , ( ) = > DialogOverlay . CurrentDialog ! . PerformOkAction ( ) ) ;
2021-11-11 22:08:43 +08:00
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
AddAssert ( "beatmap has 1 object" , ( ) = > editorPlayer . Beatmap . Value . Beatmap . HitObjects . Count = = 1 ) ;
2021-11-11 22:45:10 +08:00
2021-11-11 22:25:58 +08:00
AddUntilStep ( "wait for return to editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
2021-11-11 22:45:10 +08:00
AddAssert ( "track stopped" , ( ) = > ! Beatmap . Value . Track . IsRunning ) ;
2021-11-11 22:08:43 +08:00
}
2021-11-12 19:41:16 +08:00
[Test]
2022-11-19 04:02:23 +08:00
public void TestClockTimeTransferIsOneDirectional ( )
2021-11-12 19:41:16 +08:00
{
AddStep ( "seek to 00:01:00" , ( ) = > EditorClock . Seek ( 60_000 ) ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
GameplayClockContainer gameplayClockContainer = null ;
AddStep ( "fetch gameplay clock" , ( ) = > gameplayClockContainer = editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . First ( ) ) ;
AddUntilStep ( "gameplay clock running" , ( ) = > gameplayClockContainer . IsRunning ) ;
2022-11-19 04:02:23 +08:00
// when the gameplay test is entered, the clock is expected to continue from where it was in the main editor...
2021-11-12 19:41:16 +08:00
AddAssert ( "gameplay time past 00:01:00" , ( ) = > gameplayClockContainer . CurrentTime > = 60_000 ) ;
AddWaitStep ( "wait some" , 5 ) ;
AddStep ( "exit player" , ( ) = > editorPlayer . Exit ( ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
2022-11-19 04:02:23 +08:00
// but when exiting from gameplay test back to editor, the expectation is that the editor time should revert to what it was at the point of initiating the gameplay test.
AddAssert ( "time reverted to 00:01:00" , ( ) = > EditorClock . CurrentTime , ( ) = > Is . EqualTo ( 60_000 ) ) ;
2021-11-12 19:41:16 +08:00
}
2024-07-02 20:21:05 +08:00
[Test]
public void TestAutoplayToggle ( )
{
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
AddUntilStep ( "no replay active" , ( ) = > editorPlayer . ChildrenOfType < DrawableRuleset > ( ) . Single ( ) . ReplayScore , ( ) = > Is . Null ) ;
AddStep ( "press Tab" , ( ) = > InputManager . Key ( Key . Tab ) ) ;
AddUntilStep ( "replay active" , ( ) = > editorPlayer . ChildrenOfType < DrawableRuleset > ( ) . Single ( ) . ReplayScore , ( ) = > Is . Not . Null ) ;
AddStep ( "press Tab" , ( ) = > InputManager . Key ( Key . Tab ) ) ;
AddUntilStep ( "no replay active" , ( ) = > editorPlayer . ChildrenOfType < DrawableRuleset > ( ) . Single ( ) . ReplayScore , ( ) = > Is . Null ) ;
AddStep ( "exit player" , ( ) = > editorPlayer . Exit ( ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
}
[Test]
public void TestQuickPause ( )
{
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
AddUntilStep ( "clock running" , ( ) = > editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . Single ( ) . IsPaused . Value , ( ) = > Is . False ) ;
AddStep ( "press Ctrl-P" , ( ) = >
{
InputManager . PressKey ( Key . ControlLeft ) ;
InputManager . Key ( Key . P ) ;
InputManager . ReleaseKey ( Key . ControlLeft ) ;
} ) ;
AddUntilStep ( "clock not running" , ( ) = > editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . Single ( ) . IsPaused . Value , ( ) = > Is . True ) ;
AddStep ( "press Ctrl-P" , ( ) = >
{
InputManager . PressKey ( Key . ControlLeft ) ;
InputManager . Key ( Key . P ) ;
InputManager . ReleaseKey ( Key . ControlLeft ) ;
} ) ;
AddUntilStep ( "clock running" , ( ) = > editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . Single ( ) . IsPaused . Value , ( ) = > Is . False ) ;
AddStep ( "exit player" , ( ) = > editorPlayer . Exit ( ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
}
[Test]
public void TestQuickExitAtInitialPosition ( )
{
AddStep ( "seek to 00:01:00" , ( ) = > EditorClock . Seek ( 60_000 ) ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
GameplayClockContainer gameplayClockContainer = null ;
AddStep ( "fetch gameplay clock" , ( ) = > gameplayClockContainer = editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . First ( ) ) ;
AddUntilStep ( "gameplay clock running" , ( ) = > gameplayClockContainer . IsRunning ) ;
// when the gameplay test is entered, the clock is expected to continue from where it was in the main editor...
AddAssert ( "gameplay time past 00:01:00" , ( ) = > gameplayClockContainer . CurrentTime > = 60_000 ) ;
AddWaitStep ( "wait some" , 5 ) ;
AddStep ( "exit player" , ( ) = > InputManager . PressKey ( Key . F1 ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
AddAssert ( "time reverted to 00:01:00" , ( ) = > EditorClock . CurrentTime , ( ) = > Is . EqualTo ( 60_000 ) ) ;
}
[Test]
public void TestQuickExitAtCurrentPosition ( )
{
AddStep ( "seek to 00:01:00" , ( ) = > EditorClock . Seek ( 60_000 ) ) ;
AddStep ( "click test gameplay button" , ( ) = >
{
var button = Editor . ChildrenOfType < TestGameplayButton > ( ) . Single ( ) ;
InputManager . MoveMouseTo ( button ) ;
InputManager . Click ( MouseButton . Left ) ;
} ) ;
EditorPlayer editorPlayer = null ;
AddUntilStep ( "player pushed" , ( ) = > ( editorPlayer = Stack . CurrentScreen as EditorPlayer ) ! = null ) ;
GameplayClockContainer gameplayClockContainer = null ;
AddStep ( "fetch gameplay clock" , ( ) = > gameplayClockContainer = editorPlayer . ChildrenOfType < GameplayClockContainer > ( ) . First ( ) ) ;
AddUntilStep ( "gameplay clock running" , ( ) = > gameplayClockContainer . IsRunning ) ;
// when the gameplay test is entered, the clock is expected to continue from where it was in the main editor...
AddAssert ( "gameplay time past 00:01:00" , ( ) = > gameplayClockContainer . CurrentTime > = 60_000 ) ;
AddWaitStep ( "wait some" , 5 ) ;
AddStep ( "exit player" , ( ) = > InputManager . PressKey ( Key . F2 ) ) ;
AddUntilStep ( "current screen is editor" , ( ) = > Stack . CurrentScreen is Editor ) ;
AddAssert ( "time moved forward" , ( ) = > EditorClock . CurrentTime , ( ) = > Is . GreaterThan ( 60_000 ) ) ;
}
2021-11-11 22:08:43 +08:00
public override void TearDownSteps ( )
{
base . TearDownSteps ( ) ;
2023-06-07 02:21:55 +08:00
AddStep ( "delete imported" , ( ) = > Realm . Write ( r = >
2021-11-11 22:08:43 +08:00
{
2023-06-07 02:21:55 +08:00
// delete from realm directly rather than via `BeatmapManager` to avoid cross-test pollution
// (`BeatmapManager.Delete()` uses soft deletion, which can lead to beatmap reuse between test cases).
r . RemoveAll < BeatmapMetadata > ( ) ;
r . RemoveAll < BeatmapInfo > ( ) ;
r . RemoveAll < BeatmapSetInfo > ( ) ;
} ) ) ;
2021-11-11 22:08:43 +08:00
}
2021-11-11 21:00:09 +08:00
}
}