1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 02:22:59 +08:00
osu-lazer/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 23:46:49 +08:00
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
2020-02-13 09:06:34 +08:00
using NUnit.Framework;
2020-02-11 23:46:49 +08:00
using osu.Framework.Allocation;
2020-02-13 09:06:34 +08:00
using osu.Framework.Bindables;
2020-02-11 23:46:49 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
2022-11-29 10:13:54 +08:00
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
2020-02-11 23:46:49 +08:00
using osu.Game.Overlays;
using osu.Game.Overlays.Comments;
2020-02-12 01:47:51 +08:00
using osuTK;
2020-02-13 09:06:34 +08:00
using osuTK.Input;
2020-02-11 23:46:49 +08:00
namespace osu.Game.Tests.Visual.UserInterface
{
2022-11-24 13:32:20 +08:00
public partial class TestSceneCommentEditor : OsuManualInputManagerTestScene
2020-02-11 23:46:49 +08:00
{
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue);
2022-11-29 10:17:44 +08:00
private TestCommentEditor commentEditor = null!;
private TestCancellableCommentEditor cancellableCommentEditor = null!;
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
2020-02-12 07:05:45 +08:00
2020-02-14 03:44:02 +08:00
[SetUp]
2020-02-14 03:56:34 +08:00
public void SetUp() => Schedule(() =>
Add(new FillFlowContainer
2020-02-14 03:44:02 +08: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-14 03:56:34 +08:00
commentEditor = new TestCommentEditor(),
cancellableCommentEditor = new TestCancellableCommentEditor()
2020-02-14 03:44:02 +08:00
}
}));
2020-02-13 09:06:34 +08:00
[Test]
public void TestCommitViaKeyboard()
2020-02-11 23:46:49 +08:00
{
2020-02-14 04:01:25 +08:00
AddStep("click on text box", () =>
2020-02-12 07:05:45 +08:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 09:06:34 +08:00
InputManager.Click(MouseButton.Left);
});
2020-02-14 04:01:25 +08:00
AddStep("enter text", () => commentEditor.Current.Value = "text");
2020-11-05 22:41:56 +08:00
AddStep("press Enter", () => InputManager.Key(Key.Enter));
2020-02-14 04:01:25 +08:00
AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
2020-02-14 04:01:25 +08:00
AddAssert("text committed", () => commentEditor.CommittedText == "text");
AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
2020-02-13 09:06:34 +08:00
}
[Test]
public void TestCommitViaKeyboardWhenEmpty()
{
2020-02-14 04:01:25 +08:00
AddStep("click on text box", () =>
2020-02-13 09:06:34 +08:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 09:06:34 +08:00
InputManager.Click(MouseButton.Left);
2020-02-12 07:05:45 +08:00
});
2020-02-14 04:01:25 +08:00
2020-11-05 22:41:56 +08:00
AddStep("press Enter", () => InputManager.Key(Key.Enter));
2020-02-14 04:01:25 +08:00
AddAssert("button is not loading", () => !commentEditor.IsSpinnerShown);
2022-11-29 19:20:38 +08:00
AddAssert("no text committed", () => commentEditor.CommittedText.Length == 0);
2020-02-13 09:06:34 +08:00
}
[Test]
public void TestCommitViaButton()
{
2020-02-14 04:01:25 +08:00
AddStep("click on text box", () =>
2020-02-13 09:06:34 +08:00
{
InputManager.MoveMouseTo(commentEditor.ChildrenOfType<TextBox>().Single());
2020-02-13 09:06:34 +08:00
InputManager.Click(MouseButton.Left);
});
2020-02-14 04:01:25 +08:00
AddStep("enter text", () => commentEditor.Current.Value = "some other text");
AddStep("click submit", () =>
2020-02-13 09:06:34 +08:00
{
InputManager.MoveMouseTo(commentEditor.ButtonsContainer);
InputManager.Click(MouseButton.Left);
});
2020-02-14 04:01:25 +08:00
AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
2020-02-14 04:01:25 +08:00
AddAssert("text committed", () => commentEditor.CommittedText == "some other text");
AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
2020-02-13 09:06:34 +08:00
}
[Test]
public void TestLoggingInAndOut()
{
2023-06-23 05:10:16 +08: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-23 05:10:16 +08: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-23 05:10:16 +08:00
assertLoggedInState();
// moving from logged in -> logged out
AddStep("log out", () => dummyAPI.Logout());
2023-06-23 05:10:16 +08:00
assertLoggedOutState();
// moving from logged out -> logged in
AddStep("log back in", () => dummyAPI.Login("username", "password"));
2023-06-23 05:10:16 +08:00
assertLoggedInState();
}
2020-02-13 09:06:34 +08:00
[Test]
public void TestCancelAction()
{
2020-02-14 04:01:25 +08:00
AddStep("click cancel button", () =>
2020-02-13 09:06:34 +08:00
{
2023-06-21 14:15:02 +08:00
InputManager.MoveMouseTo(cancellableCommentEditor.ButtonsContainer[2]);
2020-02-13 09:06:34 +08:00
InputManager.Click(MouseButton.Left);
});
2020-02-14 04:01:25 +08:00
AddAssert("cancel action fired", () => cancellableCommentEditor.Cancelled);
2020-02-13 09:06:34 +08:00
}
2022-11-24 13:32:20 +08:00
private partial class TestCommentEditor : CommentEditor
2020-02-11 23:46:49 +08:00
{
2020-02-13 09:06:34 +08:00
public new Bindable<string> Current => base.Current;
public new FillFlowContainer ButtonsContainer => base.ButtonsContainer;
public new TextBox TextBox => base.TextBox;
2020-02-13 09:06:34 +08:00
2022-11-29 19:20:38 +08:00
public string CommittedText { get; private set; } = string.Empty;
2020-02-11 23:46:49 +08:00
public bool IsSpinnerShown => this.ChildrenOfType<LoadingSpinner>().Single().IsPresent;
2023-01-07 03:51:57 +08:00
protected override void OnCommit(string value)
2020-02-14 03:56:34 +08:00
{
ShowLoadingSpinner = true;
2020-02-14 03:56:34 +08:00
CommittedText = value;
Scheduler.AddDelayed(() => ShowLoadingSpinner = false, 1000);
2020-02-14 03:56:34 +08:00
}
2020-02-12 01:08:24 +08:00
2022-11-29 10:13:54 +08:00
protected override LocalisableString FooterText => @"Footer text. And it is pretty long. Cool.";
2023-06-23 05:00:52 +08: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 23:46:49 +08:00
}
2020-02-12 01:47:51 +08:00
2022-11-24 13:32:20 +08:00
private partial class TestCancellableCommentEditor : CancellableCommentEditor
2020-02-12 01:47:51 +08:00
{
2020-02-13 09:06:34 +08:00
public new FillFlowContainer ButtonsContainer => base.ButtonsContainer;
2023-01-07 03:51:57 +08:00
2022-11-29 10:13:54 +08:00
protected override LocalisableString FooterText => @"Wow, another one. Sicc";
2020-02-12 01:47:51 +08:00
2020-02-14 03:56:34 +08:00
public bool Cancelled { get; private set; }
2020-02-12 01:47:51 +08:00
2020-02-14 03:56:34 +08:00
public TestCancellableCommentEditor()
{
OnCancel = () => Cancelled = true;
}
2023-01-07 03:51:57 +08:00
protected override void OnCommit(string text)
{
}
2023-06-23 05:00:52 +08:00
protected override LocalisableString GetButtonText(bool isLoggedIn) => @"Save";
2023-06-20 10:08:45 +08:00
protected override LocalisableString GetPlaceholderText(bool isLoggedIn) => @"Multiline textboxes soon";
2020-02-12 01:47:51 +08:00
}
2020-02-11 23:46:49 +08:00
}
}