1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 15:17:44 +08:00
This commit is contained in:
cdwcgt 2024-11-01 13:58:08 +08:00
parent 45cc830aee
commit 0b2f4facac
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
7 changed files with 99 additions and 10 deletions

View File

@ -155,7 +155,7 @@ namespace osu.Game.Tests.Visual.Gameplay
var api = (DummyAPIAccess)API; var api = (DummyAPIAccess)API;
api.Friends.Clear(); api.Friends.Clear();
api.Friends.Add(friend); api.Friends.Add(CreateAPIRelationFromAPIUser(friend));
}); });
int playerNumber = 1; int playerNumber = 1;

View File

@ -30,7 +30,7 @@ namespace osu.Game.Tests.Visual.Online
if (supportLevel > 3) if (supportLevel > 3)
supportLevel = 0; supportLevel = 0;
((DummyAPIAccess)API).Friends.Add(new APIUser ((DummyAPIAccess)API).Friends.Add(CreateAPIRelationFromAPIUser(new APIUser
{ {
Username = @"peppy", Username = @"peppy",
Id = 2, Id = 2,
@ -38,7 +38,7 @@ namespace osu.Game.Tests.Visual.Online
CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg", CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c3.jpg",
IsSupporter = supportLevel > 0, IsSupporter = supportLevel > 0,
SupportLevel = supportLevel SupportLevel = supportLevel
}); }));
} }
} }

View File

@ -3,17 +3,24 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Graphics.Containers; 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.Online.API.Requests.Responses;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Profile; using osu.Game.Overlays.Profile;
using osu.Game.Overlays.Profile.Header.Components;
using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu;
using osu.Game.Users; using osu.Game.Users;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Online namespace osu.Game.Tests.Visual.Online
{ {
@ -22,6 +29,10 @@ namespace osu.Game.Tests.Visual.Online
[Cached] [Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Green); private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Green);
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
private readonly ManualResetEventSlim requestLock = new ManualResetEventSlim();
[Resolved] [Resolved]
private OsuConfigManager configManager { get; set; } = null!; private OsuConfigManager configManager { get; set; } = null!;
@ -400,5 +411,74 @@ namespace osu.Game.Tests.Visual.Online
} }
}, new OsuRuleset().RulesetInfo)); }, 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<FollowersButton>().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<FollowersButton>().First().TriggerClick());
AddStep("Complete request", () => requestLock.Set());
AddUntilStep("Friend added", () => API.Friends.Any(f => f.TargetID == TestSceneUserProfileOverlay.TEST_USER.OnlineID));
}
} }
} }

View File

@ -9,11 +9,11 @@ namespace osu.Game.Online.API.Requests
{ {
public class FriendAddRequest : APIRequest<APIRelation> public class FriendAddRequest : APIRequest<APIRelation>
{ {
private readonly int targetId; public readonly int TargetId;
public FriendAddRequest(int targetId) public FriendAddRequest(int targetId)
{ {
this.targetId = targetId; TargetId = targetId;
} }
protected override WebRequest CreateWebRequest() protected override WebRequest CreateWebRequest()
@ -21,7 +21,7 @@ namespace osu.Game.Online.API.Requests
var req = base.CreateWebRequest(); var req = base.CreateWebRequest();
req.Method = HttpMethod.Post; req.Method = HttpMethod.Post;
req.AddParameter("target", targetId.ToString(), RequestParameterType.Query); req.AddParameter("target", TargetId.ToString(), RequestParameterType.Query);
return req; return req;
} }

View File

@ -8,11 +8,11 @@ namespace osu.Game.Online.API.Requests
{ {
public class FriendDeleteRequest : APIRequest public class FriendDeleteRequest : APIRequest
{ {
private readonly int targetId; public readonly int TargetId;
public FriendDeleteRequest(int targetId) public FriendDeleteRequest(int targetId)
{ {
this.targetId = targetId; TargetId = targetId;
} }
protected override WebRequest CreateWebRequest() protected override WebRequest CreateWebRequest()
@ -22,6 +22,6 @@ namespace osu.Game.Online.API.Requests
return req; return req;
} }
protected override string Target => $@"friends/{targetId}"; protected override string Target => $@"friends/{TargetId}";
} }
} }

View File

@ -37,7 +37,7 @@ namespace osu.Game.Overlays.Profile.Header.Components
private OverlayColourProvider colourProvider { get; set; } = null!; private OverlayColourProvider colourProvider { get; set; } = null!;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(IAPIProvider api, INotificationOverlay notifications) private void load(IAPIProvider api, INotificationOverlay? notifications)
{ {
localUser.BindTo(api.LocalUser); localUser.BindTo(api.LocalUser);

View File

@ -317,6 +317,15 @@ namespace osu.Game.Tests.Visual
return result; 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) => protected WorkingBeatmap CreateWorkingBeatmap(RulesetInfo ruleset) =>
CreateWorkingBeatmap(CreateBeatmap(ruleset)); CreateWorkingBeatmap(CreateBeatmap(ruleset));