1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Merge pull request #13785 from ekrctb/dependency-providing-container

Add a convenient way to provide ad-hoc dependency to children in visual test
This commit is contained in:
Dean Herbert 2021-07-20 14:18:17 +09:00 committed by GitHub
commit 6cc81c24b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 19 deletions

View File

@ -31,22 +31,9 @@ namespace osu.Game.Rulesets.Catch.Tests
[Resolved]
private OsuConfigManager config { get; set; }
[Cached]
private readonly DroppedObjectContainer droppedObjectContainer;
private readonly Container trailContainer;
private TestCatcher catcher;
public TestSceneCatcher()
{
Add(trailContainer = new Container
{
Anchor = Anchor.Centre,
Depth = -1
});
Add(droppedObjectContainer = new DroppedObjectContainer());
}
private DroppedObjectContainer droppedObjectContainer;
[SetUp]
public void SetUp() => Schedule(() =>
@ -56,13 +43,25 @@ namespace osu.Game.Rulesets.Catch.Tests
CircleSize = 0,
};
if (catcher != null)
Remove(catcher);
Add(catcher = new TestCatcher(trailContainer, difficulty)
var trailContainer = new Container
{
Anchor = Anchor.Centre,
};
droppedObjectContainer = new DroppedObjectContainer();
Child = new DependencyProvidingContainer
{
CachedDependencies = new (Type, object)[]
{
(typeof(DroppedObjectContainer), droppedObjectContainer),
},
Children = new Drawable[]
{
droppedObjectContainer,
catcher = new TestCatcher(trailContainer, difficulty),
trailContainer
},
Anchor = Anchor.Centre
});
};
});
[Test]

View File

@ -0,0 +1,34 @@
// 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.Graphics.Containers;
namespace osu.Game.Tests.Visual
{
/// <summary>
/// A <see cref="Container"/> which providing ad-hoc dependencies to the child drawables.
/// </summary>
/// <remarks>
/// The <see cref="CachedDependencies"/> must be set while this <see cref="DependencyProvidingContainer"/> is not loaded.
/// </remarks>
public class DependencyProvidingContainer : Container
{
/// <summary>
/// The dependencies provided to the children.
/// </summary>
// TODO: should be an init-only property when C# 9
public (Type, object)[] CachedDependencies { get; set; } = Array.Empty<(Type, object)>();
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencyContainer = new DependencyContainer(base.CreateChildDependencies(parent));
foreach (var (type, value) in CachedDependencies)
dependencyContainer.CacheAs(type, value);
return dependencyContainer;
}
}
}