Add return bool to HandleRequest to better trigger failures

This commit is contained in:
Dean Herbert
2021-03-23 18:08:32 +09:00
parent ce452565f4
commit aeff9bd853
11 changed files with 75 additions and 26 deletions

View File

@ -23,8 +23,10 @@ namespace osu.Game.Tests.Online
{ {
case CommentVoteRequest cRequest: case CommentVoteRequest cRequest:
cRequest.TriggerSuccess(new CommentBundle()); cRequest.TriggerSuccess(new CommentBundle());
break; return true;
} }
return false;
}); });
CommentVoteRequest request = null; CommentVoteRequest request = null;
@ -108,8 +110,10 @@ namespace osu.Game.Tests.Online
{ {
case LeaveChannelRequest cRequest: case LeaveChannelRequest cRequest:
cRequest.TriggerSuccess(); cRequest.TriggerSuccess();
break; return true;
} }
return false;
}); });
} }
} }

View File

@ -135,13 +135,15 @@ namespace osu.Game.Tests.Visual.Background
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (dummyAPI.State.Value != APIState.Online || !(request is GetSeasonalBackgroundsRequest backgroundsRequest)) if (dummyAPI.State.Value != APIState.Online || !(request is GetSeasonalBackgroundsRequest backgroundsRequest))
return; return false;
backgroundsRequest.TriggerSuccess(new APISeasonalBackgrounds backgroundsRequest.TriggerSuccess(new APISeasonalBackgrounds
{ {
Backgrounds = seasonal_background_urls.Select(url => new APISeasonalBackground { Url = url }).ToList(), Backgrounds = seasonal_background_urls.Select(url => new APISeasonalBackground { Url = url }).ToList(),
EndDate = endDate EndDate = endDate
}); });
return true;
}; };
}); });

View File

@ -30,13 +30,14 @@ namespace osu.Game.Tests.Visual.Online
((DummyAPIAccess)API).HandleRequest = req => ((DummyAPIAccess)API).HandleRequest = req =>
{ {
if (req is SearchBeatmapSetsRequest searchBeatmapSetsRequest) if (!(req is SearchBeatmapSetsRequest searchBeatmapSetsRequest)) return false;
{
searchBeatmapSetsRequest.TriggerSuccess(new SearchBeatmapSetsResponse searchBeatmapSetsRequest.TriggerSuccess(new SearchBeatmapSetsResponse
{ {
BeatmapSets = setsForResponse, BeatmapSets = setsForResponse,
}); });
}
return true;
}; };
} }

View File

@ -63,13 +63,15 @@ namespace osu.Game.Tests.Visual.Online
Builds = builds.Values.ToList() Builds = builds.Values.ToList()
}; };
changelogRequest.TriggerSuccess(changelogResponse); changelogRequest.TriggerSuccess(changelogResponse);
break; return true;
case GetChangelogBuildRequest buildRequest: case GetChangelogBuildRequest buildRequest:
if (requestedBuild != null) if (requestedBuild != null)
buildRequest.TriggerSuccess(requestedBuild); buildRequest.TriggerSuccess(requestedBuild);
break; return true;
} }
return false;
}; };
Child = changelog = new TestChangelogOverlay(); Child = changelog = new TestChangelogOverlay();

View File

@ -11,6 +11,8 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.Chat; using osu.Game.Online.Chat;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Chat.Selection; using osu.Game.Overlays.Chat.Selection;
@ -64,6 +66,24 @@ namespace osu.Game.Tests.Visual.Online
}); });
} }
[SetUpSteps]
public void SetUpSteps()
{
AddStep("register request handling", () =>
{
((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case JoinChannelRequest _:
return true;
}
return false;
};
});
}
[Test] [Test]
public void TestHideOverlay() public void TestHideOverlay()
{ {

View File

@ -85,9 +85,10 @@ namespace osu.Game.Tests.Visual.Online
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (!(request is GetCommentsRequest getCommentsRequest)) if (!(request is GetCommentsRequest getCommentsRequest))
return; return false;
getCommentsRequest.TriggerSuccess(commentBundle); getCommentsRequest.TriggerSuccess(commentBundle);
return true;
}; };
}); });

View File

@ -33,9 +33,10 @@ namespace osu.Game.Tests.Visual.Online
dummyAPI.HandleRequest = request => dummyAPI.HandleRequest = request =>
{ {
if (!(request is GetNewsRequest getNewsRequest)) if (!(request is GetNewsRequest getNewsRequest))
return; return false;
getNewsRequest.TriggerSuccess(r); getNewsRequest.TriggerSuccess(r);
return true;
}; };
}); });

View File

@ -170,6 +170,17 @@ namespace osu.Game.Tests.Visual.Playlists
private void bindHandler(bool delayed = false, ScoreInfo userScore = null, bool failRequests = false) => ((DummyAPIAccess)API).HandleRequest = request => private void bindHandler(bool delayed = false, ScoreInfo userScore = null, bool failRequests = false) => ((DummyAPIAccess)API).HandleRequest = request =>
{ {
// pre-check for requests we should be handling (as they are scheduled below).
switch (request)
{
case ShowPlaylistUserScoreRequest _:
case IndexPlaylistScoresRequest _:
break;
default:
return false;
}
requestComplete = false; requestComplete = false;
double delay = delayed ? 3000 : 0; double delay = delayed ? 3000 : 0;
@ -196,6 +207,8 @@ namespace osu.Game.Tests.Visual.Playlists
break; break;
} }
}, delay); }, delay);
return true;
}; };
private void triggerSuccess<T>(APIRequest<T> req, T result) private void triggerSuccess<T>(APIRequest<T> req, T result)

View File

@ -32,8 +32,10 @@ namespace osu.Game.Tests.Visual.SongSelect
{ {
case GetUserRequest userRequest: case GetUserRequest userRequest:
userRequest.TriggerSuccess(getUser(userRequest.Ruleset.ID)); userRequest.TriggerSuccess(getUser(userRequest.Ruleset.ID));
break; return true;
} }
return false;
}; };
}); });

View File

@ -34,8 +34,9 @@ namespace osu.Game.Online.API
/// <summary> /// <summary>
/// Provide handling logic for an arbitrary API request. /// Provide handling logic for an arbitrary API request.
/// Should return true is a request was handled. If null or false return, the request will be failed with a <see cref="NotSupportedException"/>.
/// </summary> /// </summary>
public Action<APIRequest> HandleRequest; public Func<APIRequest, bool> HandleRequest;
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online); private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
@ -55,13 +56,13 @@ namespace osu.Game.Online.API
public virtual void Queue(APIRequest request) public virtual void Queue(APIRequest request)
{ {
if (HandleRequest != null) if (HandleRequest?.Invoke(request) != true)
HandleRequest.Invoke(request); {
else
// this will fail due to not receiving an APIAccess, and trigger a failure on the request. // this will fail due to not receiving an APIAccess, and trigger a failure on the request.
// this is intended - any request in testing that needs non-failures should use HandleRequest. // this is intended - any request in testing that needs non-failures should use HandleRequest.
request.Perform(this); request.Perform(this);
} }
}
public void Perform(APIRequest request) => HandleRequest?.Invoke(request); public void Perform(APIRequest request) => HandleRequest?.Invoke(request);

View File

@ -52,15 +52,15 @@ namespace osu.Game.Tests.Visual.Multiplayer
Rooms.Add(createdRoom); Rooms.Add(createdRoom);
createRoomRequest.TriggerSuccess(createdRoom); createRoomRequest.TriggerSuccess(createdRoom);
break; return true;
case JoinRoomRequest joinRoomRequest: case JoinRoomRequest joinRoomRequest:
joinRoomRequest.TriggerSuccess(); joinRoomRequest.TriggerSuccess();
break; return true;
case PartRoomRequest partRoomRequest: case PartRoomRequest partRoomRequest:
partRoomRequest.TriggerSuccess(); partRoomRequest.TriggerSuccess();
break; return true;
case GetRoomsRequest getRoomsRequest: case GetRoomsRequest getRoomsRequest:
var roomsWithoutParticipants = new List<Room>(); var roomsWithoutParticipants = new List<Room>();
@ -76,11 +76,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
} }
getRoomsRequest.TriggerSuccess(roomsWithoutParticipants); getRoomsRequest.TriggerSuccess(roomsWithoutParticipants);
break; return true;
case GetRoomRequest getRoomRequest: case GetRoomRequest getRoomRequest:
getRoomRequest.TriggerSuccess(Rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId)); getRoomRequest.TriggerSuccess(Rooms.Single(r => r.RoomID.Value == getRoomRequest.RoomId));
break; return true;
case GetBeatmapSetRequest getBeatmapSetRequest: case GetBeatmapSetRequest getBeatmapSetRequest:
var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type); var onlineReq = new GetBeatmapSetRequest(getBeatmapSetRequest.ID, getBeatmapSetRequest.Type);
@ -89,11 +89,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
// Get the online API from the game's dependencies. // Get the online API from the game's dependencies.
game.Dependencies.Get<IAPIProvider>().Queue(onlineReq); game.Dependencies.Get<IAPIProvider>().Queue(onlineReq);
break; return true;
case CreateRoomScoreRequest createRoomScoreRequest: case CreateRoomScoreRequest createRoomScoreRequest:
createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 }); createRoomScoreRequest.TriggerSuccess(new APIScoreToken { ID = 1 });
break; return true;
case SubmitRoomScoreRequest submitRoomScoreRequest: case SubmitRoomScoreRequest submitRoomScoreRequest:
submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore submitRoomScoreRequest.TriggerSuccess(new MultiplayerScore
@ -108,8 +108,10 @@ namespace osu.Game.Tests.Visual.Multiplayer
User = api.LocalUser.Value, User = api.LocalUser.Value,
Statistics = new Dictionary<HitResult, int>() Statistics = new Dictionary<HitResult, int>()
}); });
break; return true;
} }
return false;
}; };
} }