1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 13:32:54 +08:00

Added functioning tests.

This commit is contained in:
Terochi 2022-11-15 16:12:24 +01:00 committed by Terochi
parent a79af6671e
commit 3d4962e181
4 changed files with 69 additions and 26 deletions

View File

@ -3,16 +3,18 @@
#nullable disable
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays.Chat;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Online
{
[TestFixture]
public class TestSceneChatManipulation : OsuTestScene
public class TestSceneChatManipulation : OsuManualInputManagerTestScene
{
private ChatTextBox box;
private OsuSpriteText text;
@ -41,6 +43,7 @@ namespace osu.Game.Tests.Visual.Online
Font = OsuFont.Default.With(size: 20),
}
};
box.OnCommit += (_, __) =>
{
text.Text = $"{nameof(box.OnCommit)}: {box.Text}";
@ -48,27 +51,71 @@ namespace osu.Game.Tests.Visual.Online
box.TakeFocus();
text.FadeOutFromOne(1000, Easing.InQuint);
};
});
}
[Test]
public void TestReachingLimitOfMessages()
{
box.TakeFocus();
});
}
[Test]
public void TestStayOnLastIndex()
{
addMessages(2);
AddRepeatStep("Move to last", () => InputManager.Key(Key.Up), 2);
string lastText = string.Empty;
AddStep("Move up", () =>
{
lastText = box.Text;
InputManager.Key(Key.Up);
});
AddAssert("Text hasn't changed", () => lastText == box.Text);
}
[Test]
public void TestKeepOriginalMessage()
{
addMessages(1);
AddStep("Start writing", () => box.Text = "A random 文, ...");
AddStep("Move up", () => InputManager.Key(Key.Up));
AddStep("Rewrite old message", () => box.Text = "Old Message");
AddStep("Move back down", () => InputManager.Key(Key.Down));
AddAssert("Text back to previous", () => box.Text == "A random 文, ...");
}
[Test]
public void TestResetIndexOnEmpty()
{
addMessages(2);
AddRepeatStep("Move up", () => InputManager.Key(Key.Up), 2);
AddStep("Remove text", () => box.Text = string.Empty);
AddStep("Move up again", () => InputManager.Key(Key.Up));
AddAssert("Back to first message", () => box.Text == "Message 2");
}
[Test]
public void TestReachingLimitOfMessages()
{
addMessages(100);
AddAssert("List is full of <100-1>", () =>
Enumerable.Range(0, 100).Select(number => $"Message {100 - number}").SequenceEqual(box.MessageHistory));
addMessages(2);
AddAssert("List is full of <102-3>", () =>
Enumerable.Range(0, 100).Select(number => $"Message {102 - number}").SequenceEqual(box.MessageHistory));
}
private void addMessages(int count)
{
int iterations = 0;
AddRepeatStep("Add messages", () =>
{
box.Text = $"Message {++iterations}";
InputManager.Key(Key.Enter);
}, count);
}
}
}

View File

@ -7,56 +7,52 @@ using osuTK.Input;
namespace osu.Game.Graphics.UserInterface
{
public class MessageHistoryTextBox : FocusedTextBox
public class HistoryTextBox : FocusedTextBox
{
private readonly List<string> messageHistory = new List<string>();
private int messageIndex = -1;
public IReadOnlyList<string> MessageHistory => messageHistory;
private int historyIndex = -1;
private string originalMessage = string.Empty;
public MessageHistoryTextBox()
public HistoryTextBox()
{
Current.ValueChanged += text =>
{
if (string.IsNullOrEmpty(text.NewValue))
messageIndex = -1;
historyIndex = -1;
};
}
protected override bool OnKeyDown(KeyDownEvent e)
{
/* Behavior:
* add when on last element -> last element stays
* subtract when on first element -> sets to original text
* reset indexing when Text is set to Empty
*/
switch (e.Key)
{
case Key.Up:
if (messageIndex == -1)
if (historyIndex == -1)
originalMessage = Text;
if (messageIndex == messageHistory.Count - 1)
if (historyIndex == messageHistory.Count - 1)
return true;
Text = messageHistory[++messageIndex];
Text = messageHistory[++historyIndex];
return true;
case Key.Down:
if (messageIndex == -1)
if (historyIndex == -1)
return true;
if (messageIndex == 0)
if (historyIndex == 0)
{
messageIndex = -1;
historyIndex = -1;
Text = originalMessage;
return true;
}
Text = messageHistory[--messageIndex];
Text = messageHistory[--historyIndex];
return true;
}
@ -69,7 +65,7 @@ namespace osu.Game.Graphics.UserInterface
if (!string.IsNullOrEmpty(Text))
messageHistory.Insert(0, Text);
messageIndex = -1;
historyIndex = -1;
base.Commit();
}

View File

@ -120,7 +120,7 @@ namespace osu.Game.Online.Chat
AddInternal(drawableChannel);
}
public class ChatTextBox : MessageHistoryTextBox
public class ChatTextBox : HistoryTextBox
{
protected override bool OnKeyDown(KeyDownEvent e)
{

View File

@ -7,7 +7,7 @@ using osu.Game.Resources.Localisation.Web;
namespace osu.Game.Overlays.Chat
{
public class ChatTextBox : MessageHistoryTextBox
public class ChatTextBox : HistoryTextBox
{
public readonly BindableBool ShowSearch = new BindableBool();