2018-01-09 13:34:52 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-12-07 20:17:51 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-12-28 08:12:13 +08:00
using OpenTK.Graphics ;
2017-12-07 20:17:51 +08:00
using osu.Framework.Allocation ;
2017-12-07 17:29:33 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
2017-12-31 07:51:47 +08:00
using osu.Game.Graphics ;
2017-12-07 17:29:33 +08:00
using osu.Game.Online.Chat ;
using osu.Game.Overlays.Chat ;
using osu.Game.Users ;
using System ;
2018-01-29 16:45:23 +08:00
using System.Collections.Generic ;
2017-12-07 17:29:33 +08:00
using System.Linq ;
2018-03-02 14:34:31 +08:00
using NUnit.Framework ;
2018-01-29 16:45:23 +08:00
using osu.Game.Graphics.Containers ;
2018-01-09 19:22:23 +08:00
using osu.Game.Graphics.Sprites ;
2018-04-09 00:21:48 +08:00
using osu.Game.Online.API ;
2018-01-30 16:43:19 +08:00
using osu.Game.Overlays ;
2017-12-07 17:29:33 +08:00
namespace osu.Game.Tests.Visual
{
2018-03-02 14:34:31 +08:00
[TestFixture]
2017-12-26 02:43:35 +08:00
public class TestCaseChatLink : OsuTestCase
2017-12-07 17:29:33 +08:00
{
2017-12-07 18:11:43 +08:00
private readonly TestChatLineContainer textContainer ;
2018-01-09 23:11:45 +08:00
private Color4 linkColour ;
2017-12-07 17:29:33 +08:00
2018-01-29 16:45:23 +08:00
public override IReadOnlyList < Type > RequiredTypes = > new [ ]
{
typeof ( ChatLine ) ,
typeof ( Message ) ,
typeof ( LinkFlowContainer ) ,
typeof ( DummyEchoMessage ) ,
typeof ( LocalEchoMessage ) ,
typeof ( MessageFormatter )
} ;
2018-01-30 16:43:19 +08:00
private DependencyContainer dependencies ;
protected override IReadOnlyDependencyContainer CreateLocalDependencies ( IReadOnlyDependencyContainer parent ) = > dependencies = new DependencyContainer ( parent ) ;
2017-12-07 17:29:33 +08:00
public TestCaseChatLink ( )
{
2017-12-07 18:11:43 +08:00
Add ( textContainer = new TestChatLineContainer
2017-12-07 17:29:33 +08:00
{
Padding = new MarginPadding { Left = 20 , Right = 20 } ,
RelativeSizeAxes = Axes . X ,
AutoSizeAxes = Axes . Y ,
Direction = FillDirection . Vertical ,
} ) ;
2018-01-30 16:43:19 +08:00
}
[BackgroundDependencyLoader]
2018-04-12 02:53:35 +08:00
private void load ( OsuColour colours )
2018-01-30 16:43:19 +08:00
{
linkColour = colours . Blue ;
2018-04-09 00:21:48 +08:00
2018-04-12 02:53:35 +08:00
var chatManager = new ChannelManager ( ) ;
2018-04-12 02:01:57 +08:00
chatManager . AvailableChannels . Add ( new Channel { Name = "#english" } ) ;
chatManager . AvailableChannels . Add ( new Channel { Name = "#japanese" } ) ;
2018-04-09 00:21:48 +08:00
dependencies . Cache ( chatManager ) ;
2017-12-07 17:29:33 +08:00
2018-04-10 03:06:28 +08:00
dependencies . Cache ( new ChatOverlay ( ) ) ;
2017-12-28 08:12:13 +08:00
testLinksGeneral ( ) ;
testEcho ( ) ;
2017-12-28 00:14:08 +08:00
}
2017-12-28 08:12:13 +08:00
private void clear ( ) = > AddStep ( "clear messages" , textContainer . Clear ) ;
2018-01-09 23:11:45 +08:00
private void addMessageWithChecks ( string text , int linkAmount = 0 , bool isAction = false , bool isImportant = false , params LinkAction [ ] expectedActions )
2017-12-28 08:12:13 +08:00
{
2018-01-29 17:47:14 +08:00
int index = textContainer . Count + 1 ;
var newLine = new ChatLine ( new DummyMessage ( text , isAction , isImportant , index ) ) ;
2017-12-28 08:12:13 +08:00
textContainer . Add ( newLine ) ;
2018-01-29 17:47:14 +08:00
AddAssert ( $"msg #{index} has {linkAmount} link(s)" , ( ) = > newLine . Message . Links . Count = = linkAmount ) ;
AddAssert ( $"msg #{index} has the right action" , hasExpectedActions ) ;
AddAssert ( $"msg #{index} is " + ( isAction ? "italic" : "not italic" ) , ( ) = > newLine . ContentFlow . Any ( ) & & isAction = = isItalic ( ) ) ;
AddAssert ( $"msg #{index} shows {linkAmount} link(s)" , isShowingLinks ) ;
2017-12-29 04:45:58 +08:00
2018-01-09 23:11:45 +08:00
bool hasExpectedActions ( )
{
var expectedActionsList = expectedActions . ToList ( ) ;
if ( expectedActionsList . Count ! = newLine . Message . Links . Count )
return false ;
for ( int i = 0 ; i < newLine . Message . Links . Count ; i + + )
{
var action = newLine . Message . Links [ i ] . Action ;
if ( action ! = expectedActions [ i ] ) return false ;
}
return true ;
}
bool isItalic ( ) = > newLine . ContentFlow . Where ( d = > d is OsuSpriteText ) . Cast < OsuSpriteText > ( ) . All ( sprite = > sprite . Font = = "Exo2.0-MediumItalic" ) ;
2017-12-29 03:11:21 +08:00
2017-12-31 07:51:47 +08:00
bool isShowingLinks ( )
{
bool hasBackground = ! string . IsNullOrEmpty ( newLine . Message . Sender . Colour ) ;
2018-01-09 23:11:45 +08:00
Color4 textColour = isAction & & hasBackground ? OsuColour . FromHex ( newLine . Message . Sender . Colour ) : Color4 . White ;
2017-12-31 07:51:47 +08:00
2018-01-09 23:11:45 +08:00
var linkCompilers = newLine . ContentFlow . Where ( d = > d is DrawableLinkCompiler ) . ToList ( ) ;
var linkSprites = linkCompilers . SelectMany ( comp = > ( ( DrawableLinkCompiler ) comp ) . Parts ) ;
return linkSprites . All ( d = > d . Colour = = linkColour )
2018-01-29 16:45:39 +08:00
& & newLine . ContentFlow . Except ( linkSprites . Concat ( linkCompilers ) ) . All ( d = > d . Colour = = textColour ) ;
2017-12-31 07:51:47 +08:00
}
2017-12-28 08:12:13 +08:00
}
private void testLinksGeneral ( )
2017-12-28 00:14:08 +08:00
{
2017-12-28 08:12:13 +08:00
addMessageWithChecks ( "test!" ) ;
addMessageWithChecks ( "osu.ppy.sh!" ) ;
2018-01-09 23:11:45 +08:00
addMessageWithChecks ( "https://osu.ppy.sh!" , 1 , expectedActions : LinkAction . External ) ;
addMessageWithChecks ( "00:12:345 (1,2) - Test?" , 1 , expectedActions : LinkAction . OpenEditorTimestamp ) ;
addMessageWithChecks ( "Wiki link for tasty [[Performance Points]]" , 1 , expectedActions : LinkAction . External ) ;
addMessageWithChecks ( "(osu forums)[https://osu.ppy.sh/forum] (old link format)" , 1 , expectedActions : LinkAction . External ) ;
addMessageWithChecks ( "[https://osu.ppy.sh/home New site] (new link format)" , 1 , expectedActions : LinkAction . External ) ;
2018-01-30 15:37:59 +08:00
addMessageWithChecks ( "[osu forums](https://osu.ppy.sh/forum) (new link format 2)" , 1 , expectedActions : LinkAction . External ) ;
2018-01-09 23:11:45 +08:00
addMessageWithChecks ( "[https://osu.ppy.sh/home This is only a link to the new osu webpage but this is supposed to test word wrap.]" , 1 , expectedActions : LinkAction . External ) ;
addMessageWithChecks ( "is now listening to [https://osu.ppy.sh/s/93523 IMAGE -MATERIAL- <Version 0>]" , 1 , true , expectedActions : LinkAction . OpenBeatmapSet ) ;
addMessageWithChecks ( "is now playing [https://osu.ppy.sh/b/252238 IMAGE -MATERIAL- <Version 0>]" , 1 , true , expectedActions : LinkAction . OpenBeatmap ) ;
2018-01-29 16:45:39 +08:00
addMessageWithChecks ( "Let's (try)[https://osu.ppy.sh/home] [https://osu.ppy.sh/b/252238 multiple links] https://osu.ppy.sh/home" , 3 ,
expectedActions : new [ ] { LinkAction . External , LinkAction . OpenBeatmap , LinkAction . External } ) ;
2017-12-28 08:12:13 +08:00
// note that there's 0 links here (they get removed if a channel is not found)
addMessageWithChecks ( "#lobby or #osu would be blue (and work) in the ChatDisplay test (when a proper ChatOverlay is present)." ) ;
2017-12-31 07:51:47 +08:00
addMessageWithChecks ( "I am important!" , 0 , false , true ) ;
addMessageWithChecks ( "feels important" , 0 , true , true ) ;
2018-01-09 23:11:45 +08:00
addMessageWithChecks ( "likes to post this [https://osu.ppy.sh/home link]." , 1 , true , true , expectedActions : LinkAction . External ) ;
2018-01-30 16:43:19 +08:00
addMessageWithChecks ( "Join my multiplayer game osump://12346." , 1 , expectedActions : LinkAction . JoinMultiplayerMatch ) ;
2018-01-29 17:47:14 +08:00
addMessageWithChecks ( "Join my [multiplayer game](osump://12346)." , 1 , expectedActions : LinkAction . JoinMultiplayerMatch ) ;
2018-01-30 16:43:19 +08:00
addMessageWithChecks ( "Join my [#english](osu://chan/#english)." , 1 , expectedActions : LinkAction . OpenChannel ) ;
2018-01-30 15:37:59 +08:00
addMessageWithChecks ( "Join my osu://chan/#english." , 1 , expectedActions : LinkAction . OpenChannel ) ;
2018-01-30 16:43:19 +08:00
addMessageWithChecks ( "Join my #english or #japanese channels." , 2 , expectedActions : new [ ] { LinkAction . OpenChannel , LinkAction . OpenChannel } ) ;
addMessageWithChecks ( "Join my #english or #nonexistent #hashtag channels." , 1 , expectedActions : LinkAction . OpenChannel ) ;
2017-12-28 08:12:13 +08:00
}
private void testEcho ( )
{
int echoCounter = 0 ;
addEchoWithWait ( "sent!" , "received!" ) ;
addEchoWithWait ( "https://osu.ppy.sh/home" , null , 500 ) ;
addEchoWithWait ( "[https://osu.ppy.sh/forum let's try multiple words too!]" ) ;
addEchoWithWait ( "(long loading times! clickable while loading?)[https://osu.ppy.sh/home]" , null , 5000 ) ;
void addEchoWithWait ( string text , string completeText = null , double delay = 250 )
{
var newLine = new ChatLine ( new DummyEchoMessage ( text ) ) ;
AddStep ( $"send msg #{++echoCounter} after {delay}ms" , ( ) = >
{
textContainer . Add ( newLine ) ;
Scheduler . AddDelayed ( ( ) = > newLine . Message = new DummyMessage ( completeText ? ? text ) , delay ) ;
} ) ;
AddUntilStep ( ( ) = > textContainer . All ( line = > line . Message is DummyMessage ) , $"wait for msg #{echoCounter}" ) ;
}
2017-12-07 17:29:33 +08:00
}
2017-12-28 08:12:13 +08:00
private class DummyEchoMessage : LocalEchoMessage
{
public DummyEchoMessage ( string text )
{
Content = text ;
Timestamp = DateTimeOffset . Now ;
Sender = DummyMessage . TEST_SENDER ;
}
}
2017-12-07 17:29:33 +08:00
private class DummyMessage : Message
{
2017-12-07 23:41:46 +08:00
private static long messageCounter ;
2018-01-30 16:43:19 +08:00
2017-12-31 07:51:47 +08:00
internal static readonly User TEST_SENDER_BACKGROUND = new User
{
Username = @"i-am-important" ,
Id = 42 ,
Colour = "#250cc9" ,
} ;
2017-12-28 08:12:13 +08:00
internal static readonly User TEST_SENDER = new User
2017-12-07 17:29:33 +08:00
{
Username = @"Somebody" ,
Id = 1 ,
} ;
2017-12-07 20:17:51 +08:00
2017-12-07 17:29:33 +08:00
public new DateTimeOffset Timestamp = DateTimeOffset . Now ;
2018-01-29 17:47:14 +08:00
public DummyMessage ( string text , bool isAction = false , bool isImportant = false , int number = 0 )
2017-12-07 17:29:33 +08:00
: base ( messageCounter + + )
{
Content = text ;
IsAction = isAction ;
2018-01-29 17:47:14 +08:00
Sender = new User
{
Username = $"User {number}" ,
Id = number ,
Colour = isImportant ? "#250cc9" : null ,
} ;
2017-12-07 17:29:33 +08:00
}
}
2017-12-07 18:11:43 +08:00
private class TestChatLineContainer : FillFlowContainer < ChatLine >
2017-12-07 17:29:33 +08:00
{
protected override int Compare ( Drawable x , Drawable y )
{
var xC = ( ChatLine ) x ;
var yC = ( ChatLine ) y ;
return xC . Message . CompareTo ( yC . Message ) ;
}
}
}
}