osukey/osu.Game/Tests/Visual/OnlinePlay/OnlinePlaySubScreenTestScene.cs
2021-06-24 17:01:28 +09:00

65 lines
2.5 KiB
C#

// 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;
namespace osu.Game.Tests.Visual.OnlinePlay
{
public abstract class OnlinePlaySubScreenTestScene : ScreenTestScene
{
/// <summary>
/// The cached <see cref="Room"/>.
/// </summary>
protected Room Room { 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)
{
Room = new Room();
var dependencies = new DependencyContainer(
new CachedModelDependencyContainer<Room>(parent) { Model = { Value = Room } });
dependencies.Cache(new Bindable<Room>(Room));
return dependencies;
}
/// <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));
}
}
}