From 0b2f4facace29a1fa6eedaf2726abb7af7d92fcf Mon Sep 17 00:00:00 2001 From: cdwcgt Date: Fri, 1 Nov 2024 13:58:08 +0800 Subject: [PATCH] add test --- .../Gameplay/TestSceneGameplayLeaderboard.cs | 2 +- .../Online/TestSceneDashboardOverlay.cs | 4 +- .../Online/TestSceneUserProfileHeader.cs | 80 +++++++++++++++++++ .../Online/API/Requests/FriendAddRequest.cs | 6 +- .../API/Requests/FriendDeleteRequest.cs | 6 +- .../Header/Components/FollowersButton.cs | 2 +- osu.Game/Tests/Visual/OsuTestScene.cs | 9 +++ 7 files changed, 99 insertions(+), 10 deletions(-) diff --git a/osu.Game.Tests/Visual/Gameplay/TestSceneGameplayLeaderboard.cs b/osu.Game.Tests/Visual/Gameplay/TestSceneGameplayLeaderboard.cs index 193e8b2571..135c1fd50c 100644 --- a/osu.Game.Tests/Visual/Gameplay/TestSceneGameplayLeaderboard.cs +++ b/osu.Game.Tests/Visual/Gameplay/TestSceneGameplayLeaderboard.cs @@ -155,7 +155,7 @@ namespace osu.Game.Tests.Visual.Gameplay var api = (DummyAPIAccess)API; api.Friends.Clear(); - api.Friends.Add(friend); + api.Friends.Add(CreateAPIRelationFromAPIUser(friend)); }); int playerNumber = 1; diff --git a/osu.Game.Tests/Visual/Online/TestSceneDashboardOverlay.cs b/osu.Game.Tests/Visual/Online/TestSceneDashboardOverlay.cs index b6a300322f..f2ea084f40 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneDashboardOverlay.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneDashboardOverlay.cs @@ -30,7 +30,7 @@ namespace osu.Game.Tests.Visual.Online if (supportLevel > 3) supportLevel = 0; - ((DummyAPIAccess)API).Friends.Add(new APIUser + ((DummyAPIAccess)API).Friends.Add(CreateAPIRelationFromAPIUser(new APIUser { Username = @"peppy", Id = 2, @@ -38,7 +38,7 @@ namespace osu.Game.Tests.Visual.Online CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg", IsSupporter = supportLevel > 0, SupportLevel = supportLevel - }); + })); } } diff --git a/osu.Game.Tests/Visual/Online/TestSceneUserProfileHeader.cs b/osu.Game.Tests/Visual/Online/TestSceneUserProfileHeader.cs index c9e5a3315c..a8ef11e20c 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneUserProfileHeader.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneUserProfileHeader.cs @@ -3,17 +3,24 @@ using System; using System.Linq; +using System.Threading; +using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Testing; using osu.Game.Configuration; using osu.Game.Graphics.Containers; +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.Profile; +using osu.Game.Overlays.Profile.Header.Components; using osu.Game.Rulesets.Osu; using osu.Game.Users; +using osuTK.Input; namespace osu.Game.Tests.Visual.Online { @@ -22,6 +29,10 @@ namespace osu.Game.Tests.Visual.Online [Cached] private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Green); + private DummyAPIAccess dummyAPI => (DummyAPIAccess)API; + + private readonly ManualResetEventSlim requestLock = new ManualResetEventSlim(); + [Resolved] private OsuConfigManager configManager { get; set; } = null!; @@ -400,5 +411,74 @@ namespace osu.Game.Tests.Visual.Online } }, new OsuRuleset().RulesetInfo)); } + + [Test] + public void TestAddFriend() + { + AddStep("clear friend list", () => dummyAPI.Friends.Clear()); + AddStep("Show non-friend user", () => header.User.Value = new UserProfileData(TestSceneUserProfileOverlay.TEST_USER, new OsuRuleset().RulesetInfo)); + AddStep("Setup request", () => + { + requestLock.Reset(); + + dummyAPI.HandleRequest += request => + { + if (request is not FriendAddRequest req) + return false; + + if (req.TargetId != 1) + return false; + + var apiRelation = CreateAPIRelationFromAPIUser(TestSceneUserProfileOverlay.TEST_USER); + + Task.Run(() => + { + requestLock.Wait(3000); + req.TriggerSuccess(apiRelation); + }); + + dummyAPI.Friends.Add(apiRelation); + return true; + }; + }); + AddStep("Click followers button", () => this.ChildrenOfType().First().TriggerClick()); + AddStep("Complete request", () => requestLock.Set()); + AddUntilStep("Friend added", () => API.Friends.Any(f => f.TargetID == TestSceneUserProfileOverlay.TEST_USER.OnlineID)); + } + + [Test] + public void TestAddFriendNonMutual() + { + AddStep("clear friend list", () => dummyAPI.Friends.Clear()); + AddStep("Show non-friend user", () => header.User.Value = new UserProfileData(TestSceneUserProfileOverlay.TEST_USER, new OsuRuleset().RulesetInfo)); + AddStep("Setup request", () => + { + requestLock.Reset(); + + dummyAPI.HandleRequest += request => + { + if (request is not FriendAddRequest req) + return false; + + if (req.TargetId != 1) + return false; + + var apiRelation = CreateAPIRelationFromAPIUser(TestSceneUserProfileOverlay.TEST_USER); + apiRelation.Mutual = false; + + Task.Run(() => + { + requestLock.Wait(3000); + req.TriggerSuccess(apiRelation); + }); + + dummyAPI.Friends.Add(apiRelation); + return true; + }; + }); + AddStep("Click followers button", () => this.ChildrenOfType().First().TriggerClick()); + AddStep("Complete request", () => requestLock.Set()); + AddUntilStep("Friend added", () => API.Friends.Any(f => f.TargetID == TestSceneUserProfileOverlay.TEST_USER.OnlineID)); + } } } diff --git a/osu.Game/Online/API/Requests/FriendAddRequest.cs b/osu.Game/Online/API/Requests/FriendAddRequest.cs index 3efba4a740..80aa7cb995 100644 --- a/osu.Game/Online/API/Requests/FriendAddRequest.cs +++ b/osu.Game/Online/API/Requests/FriendAddRequest.cs @@ -9,11 +9,11 @@ namespace osu.Game.Online.API.Requests { public class FriendAddRequest : APIRequest { - private readonly int targetId; + public readonly int TargetId; public FriendAddRequest(int targetId) { - this.targetId = targetId; + TargetId = targetId; } protected override WebRequest CreateWebRequest() @@ -21,7 +21,7 @@ namespace osu.Game.Online.API.Requests var req = base.CreateWebRequest(); req.Method = HttpMethod.Post; - req.AddParameter("target", targetId.ToString(), RequestParameterType.Query); + req.AddParameter("target", TargetId.ToString(), RequestParameterType.Query); return req; } diff --git a/osu.Game/Online/API/Requests/FriendDeleteRequest.cs b/osu.Game/Online/API/Requests/FriendDeleteRequest.cs index d365031c8e..9b6c4081da 100644 --- a/osu.Game/Online/API/Requests/FriendDeleteRequest.cs +++ b/osu.Game/Online/API/Requests/FriendDeleteRequest.cs @@ -8,11 +8,11 @@ namespace osu.Game.Online.API.Requests { public class FriendDeleteRequest : APIRequest { - private readonly int targetId; + public readonly int TargetId; public FriendDeleteRequest(int targetId) { - this.targetId = targetId; + TargetId = targetId; } protected override WebRequest CreateWebRequest() @@ -22,6 +22,6 @@ namespace osu.Game.Online.API.Requests return req; } - protected override string Target => $@"friends/{targetId}"; + protected override string Target => $@"friends/{TargetId}"; } } diff --git a/osu.Game/Overlays/Profile/Header/Components/FollowersButton.cs b/osu.Game/Overlays/Profile/Header/Components/FollowersButton.cs index 305724ae07..d76b979033 100644 --- a/osu.Game/Overlays/Profile/Header/Components/FollowersButton.cs +++ b/osu.Game/Overlays/Profile/Header/Components/FollowersButton.cs @@ -37,7 +37,7 @@ namespace osu.Game.Overlays.Profile.Header.Components private OverlayColourProvider colourProvider { get; set; } = null!; [BackgroundDependencyLoader] - private void load(IAPIProvider api, INotificationOverlay notifications) + private void load(IAPIProvider api, INotificationOverlay? notifications) { localUser.BindTo(api.LocalUser); diff --git a/osu.Game/Tests/Visual/OsuTestScene.cs b/osu.Game/Tests/Visual/OsuTestScene.cs index 09cfe5ecad..853c5d5f5c 100644 --- a/osu.Game/Tests/Visual/OsuTestScene.cs +++ b/osu.Game/Tests/Visual/OsuTestScene.cs @@ -317,6 +317,15 @@ namespace osu.Game.Tests.Visual return result; } + public static APIRelation CreateAPIRelationFromAPIUser(APIUser user) => + new APIRelation + { + Mutual = true, + RelationType = RelationType.Friend, + TargetID = user.OnlineID, + TargetUser = user + }; + protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) => CreateWorkingBeatmap(CreateBeatmap(ruleset));