osu/osu.Game.Tests/Visual/Online/TestSceneCommentActions.cs

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

182 lines
6.5 KiB
C#
Raw Normal View History

2022-09-27 15:11:27 +00: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.
using System;
using System.Collections.Generic;
2022-09-27 16:03:02 +00:00
using System.Linq;
2022-09-27 15:11:27 +00:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
2022-09-27 16:03:02 +00:00
using osu.Game.Graphics.Sprites;
2022-09-27 15:11:27 +00:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Overlays.Comments;
2022-09-27 16:03:02 +00:00
using osuTK.Input;
2022-09-27 15:11:27 +00:00
namespace osu.Game.Tests.Visual.Online
{
2022-09-27 16:03:02 +00:00
public class TestSceneCommentActions : OsuManualInputManagerTestScene
2022-09-27 15:11:27 +00:00
{
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
[Cached]
private readonly DialogOverlay dialogOverlay = new DialogOverlay();
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
private CommentsContainer commentsContainer = null!;
[SetUpSteps]
public void SetUp()
{
Schedule(() =>
{
2022-09-27 19:40:53 +00:00
API.Login("test", "test");
2022-09-27 15:11:27 +00:00
if (dialogOverlay.Parent != null) Remove(dialogOverlay, false);
Children = new Container<Drawable>[]
{
new BasicScrollContainer
{
RelativeSizeAxes = Axes.Both,
Child = commentsContainer = new CommentsContainer()
},
dialogOverlay
};
});
}
[Test]
public void TestNonOwnCommentCantBeDeleted()
{
addTestComments();
2022-09-27 16:03:02 +00:00
2022-09-27 19:40:53 +00:00
AddUntilStep("First comment has button", () =>
2022-09-27 16:03:02 +00:00
{
var comments = this.ChildrenOfType<DrawableComment>();
2022-09-27 19:40:53 +00:00
var ourComment = comments.SingleOrDefault(x => x.Comment.Id == 1);
return ourComment != null && ourComment.ChildrenOfType<OsuSpriteText>().Any(x => x.Text == "Delete");
2022-09-27 16:03:02 +00:00
});
AddAssert("Second doesn't", () =>
{
var comments = this.ChildrenOfType<DrawableComment>();
var ourComment = comments.Single(x => x.Comment.Id == 2);
return ourComment.ChildrenOfType<OsuSpriteText>().All(x => x.Text != "Delete");
});
2022-09-27 15:11:27 +00:00
}
[Test]
public void TestDeletion()
{
2022-09-27 19:40:53 +00:00
DrawableComment? ourComment = null;
2022-09-27 16:03:02 +00:00
bool delete = false;
2022-09-27 15:11:27 +00:00
addTestComments();
2022-09-27 19:40:53 +00:00
AddUntilStep("Comment exists", () =>
2022-09-27 16:03:02 +00:00
{
var comments = this.ChildrenOfType<DrawableComment>();
2022-09-27 19:40:53 +00:00
ourComment = comments.SingleOrDefault(x => x.Comment.Id == 1);
return ourComment != null;
2022-09-27 16:03:02 +00:00
});
AddStep("It has delete button", () =>
{
var btn = ourComment.ChildrenOfType<OsuSpriteText>().Single(x => x.Text == "Delete");
InputManager.MoveMouseTo(btn);
});
AddStep("Click delete button", () =>
{
InputManager.Click(MouseButton.Left);
});
AddStep("Setup request handling", () =>
{
dummyAPI.HandleRequest = request =>
{
if (!(request is CommentDeleteRequest req))
return false;
2022-09-27 16:40:18 +00:00
if (req.CommentId != 1)
2022-09-27 16:03:02 +00:00
return false;
CommentBundle cb = new CommentBundle
{
Comments = new List<Comment>
{
new Comment
{
Id = 2,
Message = "This is a comment by another user",
UserId = API.LocalUser.Value.Id + 1,
CreatedAt = DateTimeOffset.Now,
User = new APIUser
{
Id = API.LocalUser.Value.Id + 1,
Username = "Another user"
}
},
},
IncludedComments = new List<Comment>(),
PinnedComments = new List<Comment>(),
};
delete = true;
req.TriggerSuccess(cb);
return true;
};
});
AddStep("Confirm dialog", () => InputManager.Key(Key.Number1));
AddUntilStep("Deletion requested", () => delete);
2022-09-27 15:11:27 +00:00
}
private void addTestComments()
{
CommentBundle cb = new CommentBundle
{
Comments = new List<Comment>
{
new Comment
{
Id = 1,
Message = "This is our comment",
UserId = API.LocalUser.Value.Id,
CreatedAt = DateTimeOffset.Now,
User = API.LocalUser.Value,
},
new Comment
{
Id = 2,
Message = "This is a comment by another user",
UserId = API.LocalUser.Value.Id + 1,
CreatedAt = DateTimeOffset.Now,
User = new APIUser
{
Id = API.LocalUser.Value.Id + 1,
Username = "Another user"
}
},
},
IncludedComments = new List<Comment>(),
PinnedComments = new List<Comment>(),
};
setUpCommentsResponse(cb);
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
}
private void setUpCommentsResponse(CommentBundle commentBundle)
=> AddStep("set up response", () =>
{
dummyAPI.HandleRequest = request =>
{
if (!(request is GetCommentsRequest getCommentsRequest))
return false;
getCommentsRequest.TriggerSuccess(commentBundle);
return true;
};
});
}
}