osu/osu.Game.Tests/Visual/UserInterface/TestSceneCommentEditor.cs

192 lines
7.2 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2020-02-11 15:46:49 +00:00
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
2020-02-13 01:06:34 +00:00
using NUnit.Framework;
2020-02-11 15:46:49 +00:00
using osu.Framework.Allocation;
2020-02-13 01:06:34 +00:00
using osu.Framework.Bindables;
2020-02-11 15:46:49 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
2022-11-29 02:13:54 +00:00
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
2020-02-11 15:46:49 +00:00
using osu.Game.Overlays;
using osu.Game.Overlays.Comments;
2020-02-11 17:47:51 +00:00
using osuTK;
2020-02-13 01:06:34 +00:00
using osuTK.Input;
2020-02-11 15:46:49 +00:00
namespace osu.Game.Tests.Visual.UserInterface
{
2022-11-24 05:32:20 +00:00
public partial class TestSceneCommentEditor : OsuManualInputManagerTestScene
2020-02-11 15:46:49 +00:00
{
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue);
2022-11-29 02:17:44 +00:00
private TestCommentEditor commentEditor = null!;
private TestCancellableCommentEditor cancellableCommentEditor = null!;
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
2020-02-11 23:05:45 +00:00
2020-02-13 19:44:02 +00:00
[SetUp]
2020-02-13 19:56:34 +00:00
public void SetUp() => Schedule(() =>
Add(new FillFlowContainer
2020-02-13 19:44:02 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.Y,
Width = 800,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 20),
Children = new Drawable[]
{
2020-02-13 19:56:34 +00:00
commentEditor = new TestCommentEditor(),
cancellableCommentEditor = new TestCancellableCommentEditor()
2020-02-13 19:44:02 +00:00
}
}));
2020-02-13 01:06:34 +00:00
[Test]
public void TestCommitViaKeyboard()
2020-02-11 15:46:49 +00:00
{
2020-02-13 20:01:25 +00:00
AddStep("click on text box", () =>
2020-02-11 23:05:45 +00:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 01:06:34 +00:00
InputManager.Click(MouseButton.Left);
});
2020-02-13 20:01:25 +00:00
AddStep("enter text", () => commentEditor.Current.Value = "text");
2020-11-05 14:41:56 +00:00
AddStep("press Enter", () => InputManager.Key(Key.Enter));
2020-02-13 20:01:25 +00:00
AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
2020-02-13 20:01:25 +00:00
AddAssert("text committed", () => commentEditor.CommittedText == "text");
AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
2020-02-13 01:06:34 +00:00
}
[Test]
public void TestCommitViaKeyboardWhenEmpty()
{
2020-02-13 20:01:25 +00:00
AddStep("click on text box", () =>
2020-02-13 01:06:34 +00:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 01:06:34 +00:00
InputManager.Click(MouseButton.Left);
2020-02-11 23:05:45 +00:00
});
2020-02-13 20:01:25 +00:00
2020-11-05 14:41:56 +00:00
AddStep("press Enter", () => InputManager.Key(Key.Enter));
2020-02-13 20:01:25 +00:00
AddAssert("button is not loading", () => !commentEditor.IsSpinnerShown);
2022-11-29 11:20:38 +00:00
AddAssert("no text committed", () => commentEditor.CommittedText.Length == 0);
2020-02-13 01:06:34 +00:00
}
[Test]
public void TestCommitViaButton()
{
2020-02-13 20:01:25 +00:00
AddStep("click on text box", () =>
2020-02-13 01:06:34 +00:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 01:06:34 +00:00
InputManager.Click(MouseButton.Left);
});
2020-02-13 20:01:25 +00:00
AddStep("enter text", () => commentEditor.Current.Value = "some other text");
AddStep("click submit", () =>
2020-02-13 01:06:34 +00:00
{
InputManager.MoveMouseTo(commentEditor.ButtonsContainer);
InputManager.Click(MouseButton.Left);
});
2020-02-13 20:01:25 +00:00
AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
2020-02-13 20:01:25 +00:00
AddAssert("text committed", () => commentEditor.CommittedText == "some other text");
AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
2020-02-13 01:06:34 +00:00
}
[Test]
public void TestLoggingInAndOut()
{
2023-06-22 21:10:16 +00:00
void assertLoggedInState()
{
AddAssert("commit button visible", () => commentEditor.ButtonsContainer[0].Alpha == 1);
AddAssert("login button hidden", () => commentEditor.ButtonsContainer[1].Alpha == 0);
AddAssert("text box editable", () => !commentEditor.TextBox.ReadOnly);
}
2023-06-22 21:10:16 +00:00
void assertLoggedOutState()
{
AddAssert("commit button hidden", () => commentEditor.ButtonsContainer[0].Alpha == 0);
AddAssert("login button visible", () => commentEditor.ButtonsContainer[1].Alpha == 1);
AddAssert("text box readonly", () => commentEditor.TextBox.ReadOnly);
}
// there's also the case of starting logged out, but more annoying to test.
// starting logged in
2023-06-22 21:10:16 +00:00
assertLoggedInState();
// moving from logged in -> logged out
AddStep("log out", () => dummyAPI.Logout());
2023-06-22 21:10:16 +00:00
assertLoggedOutState();
// moving from logged out -> logged in
AddStep("log back in", () => dummyAPI.Login("username", "password"));
2023-06-22 21:10:16 +00:00
assertLoggedInState();
}
2020-02-13 01:06:34 +00:00
[Test]
public void TestCancelAction()
{
2020-02-13 20:01:25 +00:00
AddStep("click cancel button", () =>
2020-02-13 01:06:34 +00:00
{
2023-06-21 06:15:02 +00:00
InputManager.MoveMouseTo(cancellableCommentEditor.ButtonsContainer[2]);
2020-02-13 01:06:34 +00:00
InputManager.Click(MouseButton.Left);
});
2020-02-13 20:01:25 +00:00
AddAssert("cancel action fired", () => cancellableCommentEditor.Cancelled);
2020-02-13 01:06:34 +00:00
}
2022-11-24 05:32:20 +00:00
private partial class TestCommentEditor : CommentEditor
2020-02-11 15:46:49 +00:00
{
2020-02-13 01:06:34 +00:00
public new Bindable<string> Current => base.Current;
public new FillFlowContainer ButtonsContainer => base.ButtonsContainer;
public new TextBox TextBox => base.TextBox;
2020-02-13 01:06:34 +00:00
2022-11-29 11:20:38 +00:00
public string CommittedText { get; private set; } = string.Empty;
2020-02-11 15:46:49 +00:00
public bool IsSpinnerShown => this.ChildrenOfType<LoadingSpinner>().Single().IsPresent;
2023-01-06 19:51:57 +00:00
protected override void OnCommit(string value)
2020-02-13 19:56:34 +00:00
{
ShowLoadingSpinner = true;
2020-02-13 19:56:34 +00:00
CommittedText = value;
Scheduler.AddDelayed(() => ShowLoadingSpinner = false, 1000);
2020-02-13 19:56:34 +00:00
}
2020-02-11 17:08:24 +00:00
2022-11-29 02:13:54 +00:00
protected override LocalisableString FooterText => @"Footer text. And it is pretty long. Cool.";
2023-06-22 21:00:52 +00:00
protected override LocalisableString GetButtonText(bool isLoggedIn) =>
isLoggedIn ? @"Commit" : "You're logged out!";
protected override LocalisableString GetPlaceholderText(bool isLoggedIn) =>
isLoggedIn ? @"This text box is empty" : "Still empty, but now you can't type in it.";
2020-02-11 15:46:49 +00:00
}
2020-02-11 17:47:51 +00:00
2022-11-24 05:32:20 +00:00
private partial class TestCancellableCommentEditor : CancellableCommentEditor
2020-02-11 17:47:51 +00:00
{
2020-02-13 01:06:34 +00:00
public new FillFlowContainer ButtonsContainer => base.ButtonsContainer;
2023-01-06 19:51:57 +00:00
2022-11-29 02:13:54 +00:00
protected override LocalisableString FooterText => @"Wow, another one. Sicc";
2020-02-11 17:47:51 +00:00
2020-02-13 19:56:34 +00:00
public bool Cancelled { get; private set; }
2020-02-11 17:47:51 +00:00
2020-02-13 19:56:34 +00:00
public TestCancellableCommentEditor()
{
OnCancel = () => Cancelled = true;
}
2023-01-06 19:51:57 +00:00
protected override void OnCommit(string text)
{
}
2023-06-22 21:00:52 +00:00
protected override LocalisableString GetButtonText(bool isLoggedIn) => @"Save";
2023-06-20 02:08:45 +00:00
protected override LocalisableString GetPlaceholderText(bool isLoggedIn) => @"Multiline textboxes soon";
2020-02-11 17:47:51 +00:00
}
2020-02-11 15:46:49 +00:00
}
}