1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 03:42:58 +08:00
osu-lazer/osu.Game.Tests/Visual/Online/TestSceneCommentsContainer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

280 lines
10 KiB
C#
Raw Normal View History

2019-10-07 22:49:20 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2020-06-13 18:41:00 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2019-10-07 22:49:20 +08:00
using NUnit.Framework;
2020-01-27 20:07:24 +08:00
using osu.Game.Overlays;
using osu.Framework.Allocation;
2020-06-13 18:41:00 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2023-01-07 09:15:53 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
2020-06-13 18:41:00 +08:00
using osu.Framework.Testing;
2023-01-07 09:15:53 +08:00
using osu.Game.Graphics.UserInterfaceV2;
2020-06-13 18:41:00 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Comments;
2019-10-07 22:49:20 +08:00
namespace osu.Game.Tests.Visual.Online
{
[TestFixture]
public partial class TestSceneCommentsContainer : OsuTestScene
{
2020-01-27 20:07:24 +08:00
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
2020-06-13 18:41:00 +08:00
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
2023-01-15 04:42:10 +08:00
private CommentsContainer commentsContainer = null!;
2020-06-13 18:41:00 +08:00
2023-01-15 04:42:10 +08:00
private TextBox editorTextBox = null!;
2020-06-13 18:41:00 +08:00
[SetUp]
public void SetUp() => Schedule(() =>
{
2020-06-13 18:41:00 +08:00
Child = new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
2020-06-13 18:41:00 +08:00
Child = commentsContainer = new CommentsContainer()
};
editorTextBox = commentsContainer.ChildrenOfType<TextBox>().First();
});
2020-06-13 18:41:00 +08:00
[Test]
public void TestIdleState()
{
AddUntilStep("loading spinner shown",
() => commentsContainer.ChildrenOfType<CommentsShowMoreButton>().Single().IsLoading);
}
[TestCase(false)]
[TestCase(true)]
public void TestSingleCommentsPage(bool withPinned)
2020-06-13 18:41:00 +08:00
{
setUpCommentsResponse(getExampleComments(withPinned));
2020-06-13 18:41:00 +08:00
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
AddUntilStep("show more button hidden",
() => commentsContainer.ChildrenOfType<CommentsShowMoreButton>().Single().Alpha == 0);
2019-10-07 22:49:20 +08:00
}
[TestCase(false)]
[TestCase(true)]
public void TestMultipleCommentPages(bool withPinned)
{
var comments = getExampleComments(withPinned);
2020-06-13 18:41:00 +08:00
comments.HasMore = true;
comments.TopLevelCount = 10;
setUpCommentsResponse(comments);
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
AddUntilStep("show more button visible",
() => commentsContainer.ChildrenOfType<CommentsShowMoreButton>().Single().Alpha == 1);
}
2020-06-13 18:41:00 +08:00
[TestCase(false)]
[TestCase(true)]
public void TestMultipleLoads(bool withPinned)
2020-06-13 18:48:16 +08:00
{
var comments = getExampleComments(withPinned);
int topLevelCommentCount = comments.Comments.Count;
2020-06-13 18:48:16 +08:00
AddStep("hide container", () => commentsContainer.Hide());
setUpCommentsResponse(comments);
AddRepeatStep("show comments multiple times",
() => commentsContainer.ShowComments(CommentableType.Beatmapset, 456), 2);
AddStep("show container", () => commentsContainer.Show());
AddUntilStep("comment count is correct",
() => commentsContainer.ChildrenOfType<DrawableComment>().Count() == topLevelCommentCount);
}
2021-08-14 00:48:14 +08:00
[Test]
public void TestNoComment()
{
var comments = getExampleComments();
comments.Comments.Clear();
setUpCommentsResponse(comments);
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
2021-08-24 13:39:03 +08:00
AddAssert("no comment shown", () => !commentsContainer.ChildrenOfType<DrawableComment>().Any());
2021-08-14 00:48:14 +08:00
}
[TestCase(false)]
[TestCase(true)]
public void TestSingleComment(bool withPinned)
{
var comment = new Comment
{
Id = 1,
Message = "This is a single comment",
LegacyName = "SingleUser",
CreatedAt = DateTimeOffset.Now,
VotesCount = 0,
Pinned = withPinned,
};
var bundle = new CommentBundle
{
Comments = new List<Comment> { comment },
IncludedComments = new List<Comment>(),
PinnedComments = new List<Comment>(),
};
if (withPinned)
bundle.PinnedComments.Add(comment);
setUpCommentsResponse(bundle);
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
AddUntilStep("wait comment load", () => commentsContainer.ChildrenOfType<DrawableComment>().Any());
2021-08-24 13:39:03 +08:00
AddAssert("only one comment shown", () =>
2021-08-14 00:48:14 +08:00
commentsContainer.ChildrenOfType<DrawableComment>().Count(d => d.Comment.Pinned == withPinned) == 1);
}
[Test]
2023-01-07 09:15:53 +08:00
public void TestPost()
{
setUpCommentsResponse(new CommentBundle { Comments = new List<Comment>() });
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
AddAssert("no comments placeholder shown", () => commentsContainer.ChildrenOfType<CommentsContainer.NoCommentsPlaceholder>().Any());
setUpPostResponse();
AddStep("enter text", () => editorTextBox.Current.Value = "comm");
AddStep("submit", () => commentsContainer.ChildrenOfType<RoundedButton>().First().TriggerClick());
AddUntilStep("comment sent", () =>
{
string writtenText = editorTextBox.Current.Value;
2023-01-15 04:42:10 +08:00
var comment = commentsContainer.ChildrenOfType<DrawableComment>().LastOrDefault();
return comment != null && comment.ChildrenOfType<SpriteText>().Any(y => y.Text == writtenText);
});
AddAssert("no comments placeholder removed", () => !commentsContainer.ChildrenOfType<CommentsContainer.NoCommentsPlaceholder>().Any());
}
[Test]
public void TestPostWithExistingComments()
2023-01-07 09:15:53 +08:00
{
setUpCommentsResponse(getExampleComments());
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
2023-01-07 09:15:53 +08:00
setUpPostResponse();
AddStep("enter text", () => editorTextBox.Current.Value = "comm");
AddStep("submit", () => commentsContainer.ChildrenOfType<CommentEditor>().Single().ChildrenOfType<RoundedButton>().First().TriggerClick());
AddUntilStep("comment sent", () =>
2023-01-07 09:15:53 +08:00
{
string writtenText = editorTextBox.Current.Value;
2023-01-15 04:42:10 +08:00
var comment = commentsContainer.ChildrenOfType<DrawableComment>().LastOrDefault();
return comment != null && comment.ChildrenOfType<SpriteText>().Any(y => y.Text == writtenText);
2023-01-07 09:15:53 +08:00
});
}
2020-06-13 18:41:00 +08:00
private void setUpCommentsResponse(CommentBundle commentBundle)
=> AddStep("set up response", () =>
{
dummyAPI.HandleRequest = request =>
{
if (!(request is GetCommentsRequest getCommentsRequest))
return false;
2020-06-13 18:41:00 +08:00
getCommentsRequest.TriggerSuccess(commentBundle);
return true;
2020-06-13 18:41:00 +08:00
};
});
2023-01-07 09:15:53 +08:00
private void setUpPostResponse()
=> AddStep("set up response", () =>
{
dummyAPI.HandleRequest = request =>
{
if (!(request is CommentPostRequest req))
return false;
req.TriggerSuccess(new CommentBundle
{
Comments = new List<Comment>
{
new Comment
{
Id = 98,
Message = req.Message,
LegacyName = "FirstUser",
CreatedAt = DateTimeOffset.Now,
VotesCount = 98,
}
}
});
return true;
};
});
2023-01-07 09:26:52 +08:00
private static CommentBundle getExampleComments(bool withPinned = false)
2020-06-13 18:41:00 +08:00
{
var bundle = new CommentBundle
2020-06-13 18:41:00 +08:00
{
Comments = new List<Comment>
2020-06-13 18:41:00 +08:00
{
new Comment
{
Id = 1,
Message = "This is a comment",
LegacyName = "FirstUser",
CreatedAt = DateTimeOffset.Now,
VotesCount = 19,
RepliesCount = 1
},
new Comment
{
Id = 5,
ParentId = 1,
Message = "This is a child comment",
LegacyName = "SecondUser",
CreatedAt = DateTimeOffset.Now,
VotesCount = 4,
},
new Comment
{
Id = 10,
Message = "This is another comment",
LegacyName = "ThirdUser",
CreatedAt = DateTimeOffset.Now,
VotesCount = 0
},
2020-06-13 18:41:00 +08:00
},
IncludedComments = new List<Comment>(),
PinnedComments = new List<Comment>(),
};
if (withPinned)
{
var pinnedComment = new Comment
2020-06-13 18:41:00 +08:00
{
Id = 15,
Message = "This is pinned comment",
LegacyName = "PinnedUser",
2020-06-13 18:41:00 +08:00
CreatedAt = DateTimeOffset.Now,
VotesCount = 999,
Pinned = true,
RepliesCount = 1,
};
bundle.Comments.Add(pinnedComment);
bundle.PinnedComments.Add(pinnedComment);
bundle.Comments.Add(new Comment
2020-06-13 18:41:00 +08:00
{
Id = 20,
Message = "Reply to pinned comment",
LegacyName = "AbandonedUser",
2020-06-13 18:41:00 +08:00
CreatedAt = DateTimeOffset.Now,
VotesCount = 0,
ParentId = 15,
});
}
return bundle;
}
2019-10-07 22:49:20 +08:00
}
}