Decouple changelog test scene from web

This commit is contained in:
Bartłomiej Dach
2020-12-25 19:28:33 +01:00
parent 0bd9f68cbd
commit dacf6d5a34

View File

@ -1,8 +1,13 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Humanizer;
using NUnit.Framework; using NUnit.Framework;
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.Changelog; using osu.Game.Overlays.Changelog;
@ -12,15 +17,63 @@ namespace osu.Game.Tests.Visual.Online
[TestFixture] [TestFixture]
public class TestSceneChangelogOverlay : OsuTestScene public class TestSceneChangelogOverlay : OsuTestScene
{ {
private DummyAPIAccess dummyAPI => (DummyAPIAccess)API;
private readonly Dictionary<string, APIUpdateStream> streams;
private readonly Dictionary<string, APIChangelogBuild> builds;
private APIChangelogBuild requestedBuild;
private TestChangelogOverlay changelog; private TestChangelogOverlay changelog;
protected override bool UseOnlineAPI => true; public TestSceneChangelogOverlay()
{
streams = APIUpdateStream.KNOWN_STREAMS.Keys.Select((stream, id) => new APIUpdateStream
{
Id = id + 1,
Name = stream,
DisplayName = stream.Humanize(), // not quite there, but good enough.
}).ToDictionary(stream => stream.Name);
string version = DateTimeOffset.Now.ToString("yyyy.Mdd.0");
builds = APIUpdateStream.KNOWN_STREAMS.Keys.Select(stream => new APIChangelogBuild
{
Version = version,
DisplayVersion = version,
UpdateStream = streams[stream],
ChangelogEntries = new List<APIChangelogEntry>()
}).ToDictionary(build => build.UpdateStream.Name);
foreach (var stream in streams.Values)
stream.LatestBuild = builds[stream.Name];
}
[SetUp] [SetUp]
public void SetUp() => Schedule(() => public void SetUp()
{ {
Child = changelog = new TestChangelogOverlay(); requestedBuild = null;
});
dummyAPI.HandleRequest = request =>
{
switch (request)
{
case GetChangelogRequest changelogRequest:
var changelogResponse = new APIChangelogIndex
{
Streams = streams.Values.ToList(),
Builds = builds.Values.ToList()
};
changelogRequest.TriggerSuccess(changelogResponse);
break;
case GetChangelogBuildRequest buildRequest:
if (requestedBuild != null)
buildRequest.TriggerSuccess(requestedBuild);
break;
}
};
Schedule(() => Child = changelog = new TestChangelogOverlay());
}
[Test] [Test]
public void ShowWithNoFetch() public void ShowWithNoFetch()
@ -41,26 +94,22 @@ namespace osu.Game.Tests.Visual.Online
} }
[Test] [Test]
[Ignore("needs to be updated to not be so server dependent")]
public void ShowWithBuild() public void ShowWithBuild()
{ {
AddStep(@"Show with Lazer 2018.712.0", () => showBuild(() => new APIChangelogBuild
{ {
changelog.ShowBuild(new APIChangelogBuild Version = "2018.712.0",
DisplayVersion = "2018.712.0",
UpdateStream = streams[OsuGameBase.CLIENT_STREAM_NAME],
ChangelogEntries = new List<APIChangelogEntry>
{ {
Version = "2018.712.0", new APIChangelogEntry
DisplayVersion = "2018.712.0",
UpdateStream = new APIUpdateStream { Id = 5, Name = OsuGameBase.CLIENT_STREAM_NAME },
ChangelogEntries = new List<APIChangelogEntry>
{ {
new APIChangelogEntry Category = "Test",
{ Title = "Title",
Category = "Test", MessageHtml = "Message",
Title = "Title",
MessageHtml = "Message",
}
} }
}); }
}); });
AddUntilStep(@"wait for streams", () => changelog.Streams?.Count > 0); AddUntilStep(@"wait for streams", () => changelog.Streams?.Count > 0);
@ -71,35 +120,38 @@ namespace osu.Game.Tests.Visual.Online
[Test] [Test]
public void TestHTMLUnescaping() public void TestHTMLUnescaping()
{ {
AddStep(@"Ensure HTML string unescaping", () => showBuild(() => new APIChangelogBuild
{ {
changelog.ShowBuild(new APIChangelogBuild Version = "2019.920.0",
DisplayVersion = "2019.920.0",
UpdateStream = new APIUpdateStream
{ {
Version = "2019.920.0", Name = "Test",
DisplayVersion = "2019.920.0", DisplayName = "Test"
UpdateStream = new APIUpdateStream },
ChangelogEntries = new List<APIChangelogEntry>
{
new APIChangelogEntry
{ {
Name = "Test", Category = "Testing HTML strings unescaping",
DisplayName = "Test" Title = "Ensuring HTML strings are being unescaped",
}, MessageHtml = "&quot;&quot;&quot;This text should appear triple-quoted&quot;&quot;&quot; &gt;_&lt;",
ChangelogEntries = new List<APIChangelogEntry> GithubUser = new APIChangelogUser
{
new APIChangelogEntry
{ {
Category = "Testing HTML strings unescaping", DisplayName = "Dummy",
Title = "Ensuring HTML strings are being unescaped", OsuUsername = "Dummy",
MessageHtml = "&quot;&quot;&quot;This text should appear triple-quoted&quot;&quot;&quot; &gt;_&lt;", }
GithubUser = new APIChangelogUser },
{ }
DisplayName = "Dummy",
OsuUsername = "Dummy",
}
},
}
});
}); });
} }
private void showBuild(Func<APIChangelogBuild> build)
{
AddStep("set up build", () => requestedBuild = build.Invoke());
AddStep("show build", () => changelog.ShowBuild(requestedBuild));
}
private class TestChangelogOverlay : ChangelogOverlay private class TestChangelogOverlay : ChangelogOverlay
{ {
public new List<APIUpdateStream> Streams => base.Streams; public new List<APIUpdateStream> Streams => base.Streams;