2021-12-08 19:38:18 +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-12-08 19:38:18 +08:00
using System.Collections.Generic ;
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Framework.Testing ;
using osu.Game.Beatmaps ;
using osu.Game.Graphics.Containers ;
2021-12-14 18:47:11 +08:00
using osu.Game.Models ;
2022-02-15 15:01:14 +08:00
using osu.Game.Online.API ;
2021-12-08 19:38:18 +08:00
using osu.Game.Online.Rooms ;
using osu.Game.Rulesets.Osu ;
using osu.Game.Rulesets.Osu.Mods ;
using osu.Game.Screens.OnlinePlay ;
using osu.Game.Screens.OnlinePlay.Playlists ;
using osu.Game.Tests.Beatmaps ;
2022-02-15 20:08:27 +08:00
using osu.Game.Tests.Visual.OnlinePlay ;
2021-12-08 19:38:18 +08:00
using osuTK ;
using osuTK.Input ;
namespace osu.Game.Tests.Visual.Multiplayer
{
2022-11-24 13:32:20 +08:00
public partial class TestScenePlaylistsRoomSettingsPlaylist : OnlinePlayTestScene
2021-12-08 19:38:18 +08:00
{
private TestPlaylist playlist ;
[Test]
public void TestItemRemovedOnDeletion ( )
{
PlaylistItem selectedItem = null ;
2021-12-09 00:04:28 +08:00
createPlaylist ( ) ;
2021-12-08 19:38:18 +08:00
moveToItem ( 0 ) ;
AddStep ( "click" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddStep ( "retrieve selection" , ( ) = > selectedItem = playlist . SelectedItem . Value ) ;
moveToDeleteButton ( 0 ) ;
AddStep ( "click delete button" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddAssert ( "item removed" , ( ) = > ! playlist . Items . Contains ( selectedItem ) ) ;
}
[Test]
public void TestNextItemSelectedAfterDeletion ( )
{
2021-12-09 00:04:28 +08:00
createPlaylist ( ) ;
2021-12-08 19:38:18 +08:00
moveToItem ( 0 ) ;
AddStep ( "click" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
moveToDeleteButton ( 0 ) ;
AddStep ( "click delete button" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddAssert ( "item 0 is selected" , ( ) = > playlist . SelectedItem . Value = = playlist . Items [ 0 ] ) ;
}
[Test]
public void TestLastItemSelectedAfterLastItemDeleted ( )
{
2021-12-09 00:04:28 +08:00
createPlaylist ( ) ;
2021-12-08 19:38:18 +08:00
AddWaitStep ( "wait for flow" , 5 ) ; // Items may take 1 update frame to flow. A wait count of 5 is guaranteed to result in the flow being updated as desired.
AddStep ( "scroll to bottom" , ( ) = > playlist . ChildrenOfType < ScrollContainer < Drawable > > ( ) . First ( ) . ScrollToEnd ( false ) ) ;
moveToItem ( 19 ) ;
AddStep ( "click" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
moveToDeleteButton ( 19 ) ;
AddStep ( "click delete button" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddAssert ( "item 18 is selected" , ( ) = > playlist . SelectedItem . Value = = playlist . Items [ 18 ] ) ;
}
[Test]
public void TestSelectionResetWhenAllItemsDeleted ( )
{
2021-12-09 00:04:28 +08:00
createPlaylist ( ) ;
2021-12-08 19:38:18 +08:00
AddStep ( "remove all but one item" , ( ) = >
{
playlist . Items . RemoveRange ( 1 , playlist . Items . Count - 1 ) ;
} ) ;
moveToItem ( 0 ) ;
AddStep ( "click" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
moveToDeleteButton ( 0 ) ;
AddStep ( "click delete button" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddAssert ( "no item selected" , ( ) = > playlist . SelectedItem . Value = = null ) ;
}
// Todo: currently not possible due to bindable list shortcomings (https://github.com/ppy/osu-framework/issues/3081)
// [Test]
public void TestNextItemSelectedAfterExternalDeletion ( )
{
2021-12-09 00:04:28 +08:00
createPlaylist ( ) ;
2021-12-08 19:38:18 +08:00
moveToItem ( 0 ) ;
AddStep ( "click" , ( ) = > InputManager . Click ( MouseButton . Left ) ) ;
AddStep ( "remove item 0" , ( ) = > playlist . Items . RemoveAt ( 0 ) ) ;
AddAssert ( "item 0 is selected" , ( ) = > playlist . SelectedItem . Value = = playlist . Items [ 0 ] ) ;
}
private void moveToItem ( int index , Vector2 ? offset = null )
2022-02-15 22:51:44 +08:00
= > AddStep ( $"move mouse to item {index}" , ( ) = > InputManager . MoveMouseTo ( playlist . ChildrenOfType < DrawableRoomPlaylistItem > ( ) . ElementAt ( index ) , offset ) ) ;
2021-12-08 19:38:18 +08:00
private void moveToDeleteButton ( int index , Vector2 ? offset = null ) = > AddStep ( $"move mouse to delete button {index}" , ( ) = >
{
var item = playlist . ChildrenOfType < OsuRearrangeableListItem < PlaylistItem > > ( ) . ElementAt ( index ) ;
InputManager . MoveMouseTo ( item . ChildrenOfType < DrawableRoomPlaylistItem . PlaylistRemoveButton > ( ) . ElementAt ( 0 ) , offset ) ;
} ) ;
2021-12-09 00:04:28 +08:00
private void createPlaylist ( )
2021-12-08 19:38:18 +08:00
{
AddStep ( "create playlist" , ( ) = >
{
2021-12-09 00:04:28 +08:00
Child = playlist = new TestPlaylist
2021-12-08 19:38:18 +08:00
{
Anchor = Anchor . Centre ,
Origin = Anchor . Centre ,
Size = new Vector2 ( 500 , 300 )
} ;
for ( int i = 0 ; i < 20 ; i + + )
{
2022-02-15 22:33:26 +08:00
playlist . Items . Add ( new PlaylistItem ( i % 2 = = 1
? new TestBeatmap ( new OsuRuleset ( ) . RulesetInfo ) . BeatmapInfo
: new BeatmapInfo
{
Metadata = new BeatmapMetadata
{
Artist = "Artist" ,
Author = new RealmUser { Username = "Creator name here" } ,
Title = "Long title used to check background colour" ,
} ,
BeatmapSet = new BeatmapSetInfo ( )
} )
2021-12-08 19:38:18 +08:00
{
ID = i ,
OwnerID = 2 ,
2022-02-15 15:01:14 +08:00
RulesetID = new OsuRuleset ( ) . RulesetInfo . OnlineID ,
RequiredMods = new [ ]
2021-12-08 19:38:18 +08:00
{
2022-02-15 15:01:14 +08:00
new APIMod ( new OsuModHardRock ( ) ) ,
new APIMod ( new OsuModDoubleTime ( ) ) ,
new APIMod ( new OsuModAutoplay ( ) )
2021-12-08 19:38:18 +08:00
}
} ) ;
}
} ) ;
AddUntilStep ( "wait for items to load" , ( ) = > playlist . ItemMap . Values . All ( i = > i . IsLoaded ) ) ;
}
2022-11-24 13:32:20 +08:00
private partial class TestPlaylist : PlaylistsRoomSettingsPlaylist
2021-12-08 19:38:18 +08:00
{
public new IReadOnlyDictionary < PlaylistItem , RearrangeableListItem < PlaylistItem > > ItemMap = > base . ItemMap ;
2021-12-09 00:04:28 +08:00
public TestPlaylist ( )
2021-12-08 19:38:18 +08:00
{
2021-12-09 00:16:37 +08:00
AllowSelection = true ;
2021-12-08 19:38:18 +08:00
}
}
}
}