diff --git a/osu.Game.Tests/Visual/Online/TestSceneChatManipulation.cs b/osu.Game.Tests/Visual/Online/TestSceneChatManipulation.cs index 6de2584c72..4fe0cb685e 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneChatManipulation.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneChatManipulation.cs @@ -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); } } } diff --git a/osu.Game/Graphics/UserInterface/MessageHistoryTextBox.cs b/osu.Game/Graphics/UserInterface/HistoryTextBox.cs similarity index 62% rename from osu.Game/Graphics/UserInterface/MessageHistoryTextBox.cs rename to osu.Game/Graphics/UserInterface/HistoryTextBox.cs index 45497e0451..1b553576d5 100644 --- a/osu.Game/Graphics/UserInterface/MessageHistoryTextBox.cs +++ b/osu.Game/Graphics/UserInterface/HistoryTextBox.cs @@ -7,56 +7,52 @@ using osuTK.Input; namespace osu.Game.Graphics.UserInterface { - public class MessageHistoryTextBox : FocusedTextBox + public class HistoryTextBox : FocusedTextBox { private readonly List messageHistory = new List(); - private int messageIndex = -1; + public IReadOnlyList 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(); } diff --git a/osu.Game/Online/Chat/StandAloneChatDisplay.cs b/osu.Game/Online/Chat/StandAloneChatDisplay.cs index fe279d50a9..03728b427f 100644 --- a/osu.Game/Online/Chat/StandAloneChatDisplay.cs +++ b/osu.Game/Online/Chat/StandAloneChatDisplay.cs @@ -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) { diff --git a/osu.Game/Overlays/Chat/ChatTextBox.cs b/osu.Game/Overlays/Chat/ChatTextBox.cs index 26ff4a5b4e..f0bdbce08d 100644 --- a/osu.Game/Overlays/Chat/ChatTextBox.cs +++ b/osu.Game/Overlays/Chat/ChatTextBox.cs @@ -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();