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

332 lines
12 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;
using System.Threading;
using System.Threading.Tasks;
2022-09-27 15:11:27 +00:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2022-10-14 12:52:09 +00:00
using osu.Framework.Graphics.Cursor;
2022-09-27 15:11:27 +00:00
using osu.Framework.Testing;
2022-10-08 14:20:25 +00:00
using osu.Game.Graphics.Containers;
2022-09-27 16:03:02 +00:00
using osu.Game.Graphics.Sprites;
2022-10-08 14:41:51 +00:00
using osu.Game.Graphics.UserInterface;
2022-10-14 13:51:48 +00:00
using osu.Game.Graphics.UserInterfaceV2;
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-11-24 05:32:20 +00:00
public partial class TestSceneCommentActions : OsuManualInputManagerTestScene
2022-09-27 15:11:27 +00:00
{
2022-10-08 14:20:25 +00:00
private Container<Drawable> content = null!;
protected override Container<Drawable> Content => content;
2022-09-27 15:11:27 +00:00
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
2022-09-28 15:26:36 +00:00
[Cached(typeof(IDialogOverlay))]
2022-09-27 15:11:27 +00:00
private readonly DialogOverlay dialogOverlay = new DialogOverlay();
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
private CommentsContainer commentsContainer = null!;
2022-10-14 13:51:48 +00:00
private readonly ManualResetEventSlim requestLock = new ManualResetEventSlim();
2022-10-08 14:20:25 +00:00
[BackgroundDependencyLoader]
private void load()
{
base.Content.AddRange(new Drawable[]
{
2022-10-14 13:51:48 +00:00
new PopoverContainer
2022-10-08 14:20:25 +00:00
{
2022-10-14 12:52:09 +00:00
RelativeSizeAxes = Axes.Both,
Child = content = new OsuScrollContainer
{
RelativeSizeAxes = Axes.Both
}
2022-10-08 14:20:25 +00:00
},
dialogOverlay
});
}
2022-09-27 15:11:27 +00:00
[SetUpSteps]
public void SetUp()
{
Schedule(() =>
{
2022-09-27 19:40:53 +00:00
API.Login("test", "test");
2022-10-08 14:20:25 +00:00
Child = commentsContainer = new CommentsContainer();
2022-09-27 15:11:27 +00:00
});
}
[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);
2022-12-19 05:12:38 +00:00
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);
2022-12-19 05:12:38 +00:00
return ourComment.ChildrenOfType<OsuSpriteText>().All(x => x.Text != "delete");
2022-09-27 16:03:02 +00:00
});
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
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", () =>
{
2022-12-19 05:12:38 +00:00
var btn = ourComment.ChildrenOfType<OsuSpriteText>().Single(x => x.Text == "delete");
2022-09-27 16:03:02 +00:00
InputManager.MoveMouseTo(btn);
});
AddStep("Click delete button", () =>
{
InputManager.Click(MouseButton.Left);
});
AddStep("Setup request handling", () =>
{
2022-10-14 13:51:48 +00:00
requestLock.Reset();
2022-09-27 16:03:02 +00:00
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>(),
};
Task.Run(() =>
{
2022-10-14 13:51:48 +00:00
requestLock.Wait(10000);
req.TriggerSuccess(cb);
});
2022-09-27 16:03:02 +00:00
return true;
};
});
AddStep("Confirm dialog", () => InputManager.Key(Key.Number1));
2022-10-08 14:41:51 +00:00
AddAssert("Loading spinner shown", () => commentsContainer.ChildrenOfType<LoadingSpinner>().Any(d => d.IsPresent));
2022-10-14 13:51:48 +00:00
AddStep("Complete request", () => requestLock.Set());
AddUntilStep("Comment is deleted locally", () => this.ChildrenOfType<DrawableComment>().Single(x => x.Comment.Id == 1).WasDeleted);
}
[Test]
public void TestDeletionFail()
{
DrawableComment? ourComment = null;
bool delete = false;
addTestComments();
AddUntilStep("Comment exists", () =>
{
var comments = this.ChildrenOfType<DrawableComment>();
ourComment = comments.SingleOrDefault(x => x.Comment.Id == 1);
return ourComment != null;
});
AddStep("It has delete button", () =>
{
2022-12-19 05:12:38 +00:00
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 not CommentDeleteRequest req)
return false;
req.TriggerFailure(new InvalidOperationException());
delete = true;
return false;
};
});
AddStep("Confirm dialog", () => InputManager.Key(Key.Number1));
AddUntilStep("Deletion requested", () => delete);
AddUntilStep("Comment is available", () =>
{
return !this.ChildrenOfType<DrawableComment>().Single(x => x.Comment.Id == 1).WasDeleted;
});
AddAssert("Loading spinner hidden", () =>
{
return ourComment.ChildrenOfType<LoadingSpinner>().All(d => !d.IsPresent);
});
AddAssert("Actions available", () =>
{
return ourComment.ChildrenOfType<LinkFlowContainer>().Single(x => x.Name == @"Actions buttons").IsPresent;
2022-09-28 14:52:12 +00:00
});
2022-09-27 15:11:27 +00:00
}
2022-10-14 13:51:48 +00:00
[Test]
public void TestReport()
{
const string report_text = "I don't like this comment";
DrawableComment? targetComment = null;
CommentReportRequest? request = null;
addTestComments();
AddUntilStep("Comment exists", () =>
{
var comments = this.ChildrenOfType<DrawableComment>();
targetComment = comments.SingleOrDefault(x => x.Comment.Id == 2);
return targetComment != null;
});
AddStep("Setup request handling", () =>
{
requestLock.Reset();
dummyAPI.HandleRequest = r =>
{
if (!(r is CommentReportRequest req))
return false;
Task.Run(() =>
{
request = req;
requestLock.Wait(10000);
req.TriggerSuccess();
});
return true;
};
});
AddStep("Click the button", () =>
{
2022-12-19 05:12:38 +00:00
var btn = targetComment.ChildrenOfType<OsuSpriteText>().Single(x => x.Text == "report");
2022-10-14 13:51:48 +00:00
InputManager.MoveMouseTo(btn);
InputManager.Click(MouseButton.Left);
});
AddStep("Try to report", () =>
{
var btn = this.ChildrenOfType<ReportCommentPopover>().Single().ChildrenOfType<RoundedButton>().Single();
InputManager.MoveMouseTo(btn);
InputManager.Click(MouseButton.Left);
});
AddWaitStep("Wait", 3);
AddAssert("Nothing happened", () => this.ChildrenOfType<ReportCommentPopover>().Any());
2022-10-17 17:23:25 +00:00
AddStep("Set report data", () =>
2022-10-14 13:51:48 +00:00
{
2022-10-17 10:46:13 +00:00
var field = this.ChildrenOfType<OsuTextBox>().Single();
2022-10-14 13:51:48 +00:00
field.Current.Value = report_text;
2022-10-17 17:23:25 +00:00
var reason = this.ChildrenOfType<OsuEnumDropdown<CommentReportReason>>().Single();
reason.Current.Value = CommentReportReason.Other;
2022-10-14 13:51:48 +00:00
});
AddStep("Try to report", () =>
{
var btn = this.ChildrenOfType<ReportCommentPopover>().Single().ChildrenOfType<RoundedButton>().Single();
InputManager.MoveMouseTo(btn);
InputManager.Click(MouseButton.Left);
});
AddWaitStep("Wait", 3);
AddAssert("Overlay closed", () => !this.ChildrenOfType<ReportCommentPopover>().Any());
AddAssert("Loading spinner shown", () => targetComment.ChildrenOfType<LoadingSpinner>().Any(d => d.IsPresent));
AddStep("Complete request", () => requestLock.Set());
AddUntilStep("Request sent", () => request != null);
2022-10-16 17:14:05 +00:00
AddAssert("Request is correct", () => request != null && request.CommentID == 2 && request.Comment == report_text && request.Reason == CommentReportReason.Other);
2022-10-14 13:51:48 +00:00
}
2022-09-27 15:11:27 +00:00
private void addTestComments()
{
2022-09-28 15:26:36 +00:00
AddStep("set up response", () =>
2022-09-27 15:11:27 +00:00
{
2022-09-28 15:26:36 +00:00
CommentBundle cb = new CommentBundle
2022-09-27 15:11:27 +00:00
{
2022-09-28 15:26:36 +00:00
Comments = new List<Comment>
2022-09-27 15:11:27 +00:00
{
2022-09-28 15:26:36 +00:00
new Comment
{
Id = 1,
Message = "This is our comment",
UserId = API.LocalUser.Value.Id,
CreatedAt = DateTimeOffset.Now,
User = API.LocalUser.Value,
},
new Comment
2022-09-27 15:11:27 +00:00
{
2022-09-28 15:26:36 +00:00
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"
}
},
2022-09-27 15:11:27 +00:00
},
2022-09-28 15:26:36 +00:00
IncludedComments = new List<Comment>(),
PinnedComments = new List<Comment>(),
};
setUpCommentsResponse(cb);
});
2022-09-27 15:11:27 +00:00
AddStep("show comments", () => commentsContainer.ShowComments(CommentableType.Beatmapset, 123));
}
private void setUpCommentsResponse(CommentBundle commentBundle)
2022-09-28 15:26:36 +00:00
{
dummyAPI.HandleRequest = request =>
2022-09-27 15:11:27 +00:00
{
2022-09-28 15:26:36 +00:00
if (!(request is GetCommentsRequest getCommentsRequest))
return false;
2022-09-27 15:11:27 +00:00
2022-09-28 15:26:36 +00:00
getCommentsRequest.TriggerSuccess(commentBundle);
return true;
};
}
2022-09-27 15:11:27 +00:00
}
}