1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 12:33:01 +08:00

Merge pull request #9273 from bdach/fix-double-comments

Fix comments from multiple maps potentially showing on beatmap set overlay
This commit is contained in:
Dan Balasescu 2020-06-15 15:47:28 +09:00 committed by GitHub
commit 73ec39307a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 108 additions and 29 deletions

View File

@ -1,52 +1,128 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Game.Online.API.Requests;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Game.Overlays.Comments;
using osu.Game.Overlays;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Users;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Comments;
namespace osu.Game.Tests.Visual.Online
{
[TestFixture]
public class TestSceneCommentsContainer : OsuTestScene
{
protected override bool UseOnlineAPI => true;
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
public TestSceneCommentsContainer()
{
BasicScrollContainer scroll;
TestCommentsContainer comments;
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
Add(scroll = new BasicScrollContainer
private CommentsContainer commentsContainer;
[SetUp]
public void SetUp() => Schedule(() =>
Child = new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
Child = comments = new TestCommentsContainer()
Child = commentsContainer = new CommentsContainer()
});
AddStep("Big Black comments", () => comments.ShowComments(CommentableType.Beatmapset, 41823));
AddStep("Airman comments", () => comments.ShowComments(CommentableType.Beatmapset, 24313));
AddStep("Lazer build comments", () => comments.ShowComments(CommentableType.Build, 4772));
AddStep("News comments", () => comments.ShowComments(CommentableType.NewsPost, 715));
AddStep("Trigger user change", comments.User.TriggerChange);
AddStep("Idle state", () =>
[Test]
public void TestIdleState()
{
scroll.Clear();
scroll.Add(comments = new TestCommentsContainer());
});
AddUntilStep("loading spinner shown",
() => commentsContainer.ChildrenOfType<CommentsShowMoreButton>().Single().IsLoading);
}
private class TestCommentsContainer : CommentsContainer
[Test]
public void TestSingleCommentsPage()
{
public new Bindable<User> User => base.User;
}
setUpCommentsResponse(exampleComments);
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
AddUntilStep("show more button hidden",
() => commentsContainer.ChildrenOfType<CommentsShowMoreButton>().Single().Alpha == 0);
}
[Test]
public void TestMultipleCommentPages()
{
var comments = exampleComments;
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);
}
[Test]
public void TestMultipleLoads()
{
var comments = exampleComments;
int topLevelCommentCount = exampleComments.Comments.Count(comment => comment.IsTopLevel);
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);
}
private void setUpCommentsResponse(CommentBundle commentBundle)
=> AddStep("set up response", () =>
{
dummyAPI.HandleRequest = request =>
{
if (!(request is GetCommentsRequest getCommentsRequest))
return;
getCommentsRequest.TriggerSuccess(commentBundle);
};
});
private CommentBundle exampleComments => new CommentBundle
{
Comments = new List<Comment>
{
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
},
},
IncludedComments = new List<Comment>(),
};
}
}

View File

@ -12,6 +12,7 @@ using osu.Game.Online.API.Requests.Responses;
using System.Threading;
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Threading;
using osu.Game.Users;
namespace osu.Game.Overlays.Comments
@ -30,6 +31,7 @@ namespace osu.Game.Overlays.Comments
private IAPIProvider api { get; set; }
private GetCommentsRequest request;
private ScheduledDelegate scheduledCommentsLoad;
private CancellationTokenSource loadCancellation;
private int currentPage;
@ -152,8 +154,9 @@ namespace osu.Game.Overlays.Comments
request?.Cancel();
loadCancellation?.Cancel();
scheduledCommentsLoad?.Cancel();
request = new GetCommentsRequest(id.Value, type.Value, Sort.Value, currentPage++, 0);
request.Success += res => Schedule(() => onSuccess(res));
request.Success += res => scheduledCommentsLoad = Schedule(() => onSuccess(res));
api.PerformAsync(request);
}