Remove "test container", make everything go through OnlinePlayTestScene

This commit is contained in:
smoogipoo
2021-06-25 13:02:19 +09:00
parent 8fba7d2423
commit aa5d22d04a
21 changed files with 370 additions and 230 deletions

View File

@ -0,0 +1,22 @@
// 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 osu.Game.Online.Multiplayer;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer
{
public interface IMultiplayerRoomTestDependencies : IRoomTestDependencies
{
/// <summary>
/// The cached <see cref="MultiplayerClient"/>.
/// </summary>
TestMultiplayerClient Client { get; }
/// <summary>
/// The cached <see cref="IRoomManager"/>.
/// </summary>
new TestMultiplayerRoomManager RoomManager { get; }
}
}

View File

@ -0,0 +1,23 @@
// 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 osu.Game.Online.Multiplayer;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer
{
public class MultiplayerRoomTestDependencies : RoomTestDependencies, IMultiplayerRoomTestDependencies
{
public TestMultiplayerClient Client { get; }
public new TestMultiplayerRoomManager RoomManager => (TestMultiplayerRoomManager)base.RoomManager;
public MultiplayerRoomTestDependencies()
{
Client = new TestMultiplayerClient(RoomManager);
CacheAs<MultiplayerClient>(Client);
}
protected override IRoomManager CreateRoomManager() => new TestMultiplayerRoomManager();
}
}

View File

@ -0,0 +1,17 @@
// 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 osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer
{
public class MultiplayerSubScreenTestScene : OnlinePlayTestScene, IMultiplayerRoomTestDependencies
{
public TestMultiplayerClient Client => RoomDependencies.Client;
public new TestMultiplayerRoomManager RoomManager => RoomDependencies.RoomManager;
protected new MultiplayerRoomTestDependencies RoomDependencies => (MultiplayerRoomTestDependencies)base.RoomDependencies;
protected override RoomTestDependencies CreateRoomDependencies() => new MultiplayerRoomTestDependencies();
}
}

View File

@ -8,11 +8,10 @@ using osu.Framework.Graphics.Containers;
using osu.Game.Online.Multiplayer;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
using osu.Game.Tests.Visual.OnlinePlay;
namespace osu.Game.Tests.Visual.Multiplayer
{
public class TestMultiplayerRoomContainer : TestRoomContainer
public class TestMultiplayerRoomContainer : Container
{
protected override Container<Drawable> Content => content;
private readonly Container content;

View File

@ -12,7 +12,7 @@ using osu.Game.Users;
namespace osu.Game.Tests.Visual.OnlinePlay
{
public class TestBasicRoomManager : IRoomManager
public class BasicTestRoomManager : IRoomManager
{
public event Action RoomsUpdated
{

View File

@ -0,0 +1,38 @@
// 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 osu.Framework.Bindables;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
namespace osu.Game.Tests.Visual.OnlinePlay
{
public interface IRoomTestDependencies
{
/// <summary>
/// The cached <see cref="Room"/>.
/// </summary>
Bindable<Room> SelectedRoom { get; }
/// <summary>
/// The cached <see cref="IRoomManager"/>
/// </summary>
IRoomManager RoomManager { get; }
/// <summary>
/// The cached <see cref="FilterCriteria"/>.
/// </summary>
Bindable<FilterCriteria> Filter { get; }
/// <summary>
/// The cached <see cref="OngoingOperationTracker"/>.
/// </summary>
OngoingOperationTracker OngoingOperationTracker { get; }
/// <summary>
/// The cached <see cref="OnlinePlayBeatmapAvailabilityTracker"/>.
/// </summary>
OnlinePlayBeatmapAvailabilityTracker AvailabilityTracker { get; }
}
}

View File

@ -1,83 +0,0 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.Rooms;
using osu.Game.Screens;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
namespace osu.Game.Tests.Visual.OnlinePlay
{
/// <summary>
/// A <see cref="ScreenTestScene"/> providing all the dependencies cached by <see cref="OnlinePlayScreen"/> for testing <see cref="OnlinePlaySubScreen"/>s.
/// </summary>
public abstract class OnlinePlaySubScreenTestScene : ScreenTestScene
{
/// <summary>
/// The cached <see cref="SelectedRoom"/>.
/// </summary>
protected Bindable<Room> SelectedRoom { get; private set; }
/// <summary>
/// The cached <see cref="IRoomManager"/>
/// </summary>
protected IRoomManager RoomManager { get; private set; }
protected Bindable<FilterCriteria> Filter { get; private set; }
protected OngoingOperationTracker OngoingOperationTracker { get; private set; }
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("create dependencies", () => LoadScreen(new DependenciesScreen(CreateScreenDependencies)));
}
/// <summary>
/// Creates dependencies for any <see cref="OsuScreen"/> pushed via <see cref="ScreenTestScene.LoadScreen"/>.
/// Invoked at the start of every test via <see cref="SetUpSteps"/>.
/// </summary>
/// <remarks>
/// This should be overridden to add any custom dependencies required by subclasses of <see cref="OnlinePlaySubScreen"/>.
/// </remarks>
/// <param name="parent">The parent dependency container.</param>
/// <returns>The resultant dependency container.</returns>
protected virtual IReadOnlyDependencyContainer CreateScreenDependencies(IReadOnlyDependencyContainer parent)
{
SelectedRoom = new Bindable<Room>();
RoomManager = CreateRoomManager();
Filter = new Bindable<FilterCriteria>(new FilterCriteria());
OngoingOperationTracker = new OngoingOperationTracker();
var dependencies = new DependencyContainer(new CachedModelDependencyContainer<Room>(parent) { Model = { BindTarget = SelectedRoom } });
dependencies.CacheAs(SelectedRoom);
dependencies.CacheAs(RoomManager);
dependencies.CacheAs(Filter);
dependencies.CacheAs(OngoingOperationTracker);
return dependencies;
}
protected virtual IRoomManager CreateRoomManager() => new TestBasicRoomManager();
/// <summary>
/// A dummy screen used for injecting new dependencies into the hierarchy before any screen is pushed via <see cref="ScreenTestScene.LoadScreen"/>.
/// </summary>
private class DependenciesScreen : OsuScreen
{
private readonly Func<IReadOnlyDependencyContainer, IReadOnlyDependencyContainer> createDependenciesFunc;
public DependenciesScreen(Func<IReadOnlyDependencyContainer, IReadOnlyDependencyContainer> createDependenciesFunc)
{
this.createDependenciesFunc = createDependenciesFunc;
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
=> createDependenciesFunc(base.CreateChildDependencies(parent));
}
}
}

View File

@ -0,0 +1,100 @@
// 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 NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
namespace osu.Game.Tests.Visual.OnlinePlay
{
/// <summary>
/// A <see cref="ScreenTestScene"/> providing all the dependencies cached by <see cref="OnlinePlayScreen"/> for testing <see cref="OnlinePlaySubScreen"/>s.
/// </summary>
public abstract class OnlinePlayTestScene : ScreenTestScene, IRoomTestDependencies
{
public Bindable<Room> SelectedRoom => RoomDependencies?.SelectedRoom;
public IRoomManager RoomManager => RoomDependencies?.RoomManager;
public Bindable<FilterCriteria> Filter => RoomDependencies?.Filter;
public OngoingOperationTracker OngoingOperationTracker => RoomDependencies?.OngoingOperationTracker;
public OnlinePlayBeatmapAvailabilityTracker AvailabilityTracker => RoomDependencies?.AvailabilityTracker;
protected RoomTestDependencies RoomDependencies => delegatedDependencies?.RoomDependencies;
private DelegatedRoomDependencyContainer delegatedDependencies;
protected override Container<Drawable> Content => content;
private readonly Container content;
private readonly Container drawableDependenciesContainer;
protected OnlinePlayTestScene()
{
base.Content.AddRange(new Drawable[]
{
drawableDependenciesContainer = new Container { RelativeSizeAxes = Axes.Both },
content = new Container { RelativeSizeAxes = Axes.Both },
});
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
delegatedDependencies = new DelegatedRoomDependencyContainer(base.CreateChildDependencies(parent));
return delegatedDependencies;
}
[SetUp]
public void Setup() => Schedule(() =>
{
// Reset the room dependencies to a fresh state.
drawableDependenciesContainer.Clear();
delegatedDependencies.RoomDependencies = CreateRoomDependencies();
drawableDependenciesContainer.AddRange(RoomDependencies.DrawableComponents);
});
/// <summary>
/// Creates the room dependencies. Called every <see cref="Setup"/>.
/// </summary>
/// <remarks>
/// Any custom dependencies required for online-play sub-classes should be added here.
/// </remarks>
protected virtual RoomTestDependencies CreateRoomDependencies() => new RoomTestDependencies();
/// <summary>
/// A <see cref="IReadOnlyDependencyContainer"/> providing a mutable lookup source for room dependencies.
/// </summary>
private class DelegatedRoomDependencyContainer : IReadOnlyDependencyContainer
{
/// <summary>
/// The room's dependencies.
/// </summary>
public RoomTestDependencies RoomDependencies { get; set; }
private readonly IReadOnlyDependencyContainer parent;
private readonly DependencyContainer injectableDependencies;
/// <summary>
/// Creates a new <see cref="DelegatedRoomDependencyContainer"/>.
/// </summary>
/// <param name="parent">The fallback <see cref="IReadOnlyDependencyContainer"/> to use when <see cref="RoomDependencies"/> cannot satisfy a dependency.</param>
public DelegatedRoomDependencyContainer(IReadOnlyDependencyContainer parent)
{
this.parent = parent;
injectableDependencies = new DependencyContainer(this);
}
public object Get(Type type)
=> RoomDependencies?.Get(type) ?? parent.Get(type);
public object Get(Type type, CacheInfo info)
=> RoomDependencies?.Get(type, info) ?? parent.Get(type, info);
public void Inject<T>(T instance)
where T : class
=> injectableDependencies.Inject(instance);
}
}
}

View File

@ -0,0 +1,78 @@
// 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;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Lounge.Components;
namespace osu.Game.Tests.Visual.OnlinePlay
{
/// <summary>
/// Contains dependencies for testing online-play rooms.
/// </summary>
public class RoomTestDependencies : IReadOnlyDependencyContainer, IRoomTestDependencies
{
public Bindable<Room> SelectedRoom { get; }
public IRoomManager RoomManager { get; }
public Bindable<FilterCriteria> Filter { get; }
public OngoingOperationTracker OngoingOperationTracker { get; }
public OnlinePlayBeatmapAvailabilityTracker AvailabilityTracker { get; }
/// <summary>
/// All cached dependencies which are also <see cref="Drawable"/> components.
/// </summary>
public IReadOnlyList<Drawable> DrawableComponents => drawableComponents;
private readonly List<Drawable> drawableComponents = new List<Drawable>();
private readonly DependencyContainer dependencies;
public RoomTestDependencies()
{
SelectedRoom = new Bindable<Room>();
RoomManager = CreateRoomManager();
Filter = new Bindable<FilterCriteria>(new FilterCriteria());
OngoingOperationTracker = new OngoingOperationTracker();
AvailabilityTracker = new OnlinePlayBeatmapAvailabilityTracker();
dependencies = new DependencyContainer(new CachedModelDependencyContainer<Room>(null) { Model = { BindTarget = SelectedRoom } });
CacheAs(SelectedRoom);
CacheAs(RoomManager);
CacheAs(Filter);
CacheAs(OngoingOperationTracker);
CacheAs(AvailabilityTracker);
}
public object Get(Type type)
=> dependencies.Get(type);
public object Get(Type type, CacheInfo info)
=> dependencies.Get(type, info);
public void Inject<T>(T instance)
where T : class
=> dependencies.Inject(instance);
protected void Cache(object instance)
{
dependencies.Cache(instance);
if (instance is Drawable drawable)
drawableComponents.Add(drawable);
}
protected void CacheAs<T>(T instance)
where T : class
{
dependencies.CacheAs(instance);
if (instance is Drawable drawable)
drawableComponents.Add(drawable);
}
protected virtual IRoomManager CreateRoomManager() => new BasicTestRoomManager();
}
}

View File

@ -1,37 +0,0 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.Rooms;
namespace osu.Game.Tests.Visual.OnlinePlay
{
/// <summary>
/// Contains a single <see cref="Room"/> that is resolvable by components in test scenes.
/// </summary>
public class TestRoomContainer : Container
{
/// <summary>
/// The cached <see cref="Room"/>.
/// </summary>
public readonly Room Room = new Room();
public TestRoomContainer()
{
RelativeSizeAxes = Axes.Both;
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(
new CachedModelDependencyContainer<Room>(base.CreateChildDependencies(parent)) { Model = { Value = Room } });
dependencies.Cache(new Bindable<Room>(Room));
return dependencies;
}
}
}