Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed May 18, 2021
1 parent 61a41d9 commit a31a694
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
168 changes: 168 additions & 0 deletions osu.Game.Tests/Editing/TestSceneHitObjectContainerEventQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// 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.Graphics;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Edit.Compose;
using osu.Game.Tests.Visual;

namespace osu.Game.Tests.Editing
{
public class TestSceneHitObjectContainerEventQueue : OsuTestScene
{
private readonly TestHitObject testObj = new TestHitObject();

private TestPlayfield playfield1;
private TestPlayfield playfield2;
private TestDrawable intermediateDrawable;
private HitObjectContainerEventQueue eventQueue;

private HitObject beganUsage;
private HitObject finishedUsage;
private HitObject transferredUsage;

[SetUp]
public void Setup() => Schedule(() =>
{
reset();

if (eventQueue != null)
{
eventQueue.HitObjectUsageBegan -= onHitObjectUsageBegan;
eventQueue.HitObjectUsageFinished -= onHitObjectUsageFinished;
eventQueue.HitObjectUsageTransferred -= onHitObjectUsageTransferred;
}

var topPlayfield = new TestPlayfield();
topPlayfield.AddNested(playfield1 = new TestPlayfield());
topPlayfield.AddNested(playfield2 = new TestPlayfield());

eventQueue = new HitObjectContainerEventQueue(topPlayfield);
eventQueue.HitObjectUsageBegan += onHitObjectUsageBegan;
eventQueue.HitObjectUsageFinished += onHitObjectUsageFinished;
eventQueue.HitObjectUsageTransferred += onHitObjectUsageTransferred;

Children = new Drawable[]
{
topPlayfield,
intermediateDrawable = new TestDrawable(),
eventQueue
};
});

private void onHitObjectUsageBegan(HitObject obj) => beganUsage = obj;

private void onHitObjectUsageFinished(HitObject obj) => finishedUsage = obj;

private void onHitObjectUsageTransferred(HitObject obj, DrawableHitObject drawableObj) => transferredUsage = obj;

[Test]
public void TestUsageBeganAfterAdd()
{
AddStep("add hitobject", () => playfield1.Add(testObj));
addCheckStep(began: true);
}

[Test]
public void TestUsageFinishedAfterRemove()
{
AddStep("add hitobject", () => playfield1.Add(testObj));
addResetStep();
AddStep("remove hitobject", () => playfield1.Remove(testObj));
addCheckStep(finished: true);
}

[Test]
public void TestUsageTransferredWhenMovedBetweenPlayfields()
{
AddStep("add hitobject", () => playfield1.Add(testObj));
addResetStep();
AddStep("transfer hitobject to other playfield", () =>
{
playfield1.Remove(testObj);
playfield2.Add(testObj);
});

addCheckStep(transferred: true);
}

[Test]
public void TestRemoveImmediatelyAfterUsageBegan()
{
AddStep("add hitobject and schedule removal", () =>
{
playfield1.Add(testObj);
intermediateDrawable.Schedule(() => playfield1.Remove(testObj));
});

addCheckStep();
}

[Test]
public void TestRemoveImmediatelyAfterTransferred()
{
AddStep("add hitobject", () => playfield1.Add(testObj));
addResetStep();
AddStep("transfer hitobject to other playfield and schedule removal", () =>
{
playfield1.Remove(testObj);
playfield2.Add(testObj);
intermediateDrawable.Schedule(() => playfield2.Remove(testObj));
});

addCheckStep(finished: true);
}

private void addResetStep() => AddStep("reset", reset);

private void reset()
{
beganUsage = null;
finishedUsage = null;
transferredUsage = null;
}

private void addCheckStep(bool began = false, bool finished = false, bool transferred = false)
=> AddAssert($"began = {began}, finished = {finished}, transferred = {transferred}",
() => (beganUsage == testObj) == began && (finishedUsage == testObj) == finished && (transferredUsage == testObj) == transferred);

private class TestPlayfield : Playfield
{
public TestPlayfield()
{
RegisterPool<TestHitObject, TestDrawableHitObject>(1);
}

public new void AddNested(Playfield playfield)
{
AddInternal(playfield);
base.AddNested(playfield);
}

protected override HitObjectLifetimeEntry CreateLifetimeEntry(HitObject hitObject)
{
var entry = base.CreateLifetimeEntry(hitObject);
entry.KeepAlive = true;
return entry;
}
}

private class TestHitObject : HitObject
{
public override string ToString() => "TestHitObject";
}

private class TestDrawableHitObject : DrawableHitObject
{
}

private class TestDrawable : Drawable
{
public new void Schedule(Action action) => base.Schedule(action);
}
}
}
7 changes: 5 additions & 2 deletions osu.Game/Rulesets/UI/Playfield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,11 @@ DrawableHitObject IPooledHitObjectProvider.GetPooledDrawableRepresentation(HitOb

// If this is the first time this DHO is being used, then apply the DHO mods.
// This is done before Apply() so that the state is updated once when the hitobject is applied.
foreach (var m in mods.OfType<IApplicableToDrawableHitObjects>())
m.ApplyToDrawableHitObjects(dho.Yield());
if (mods != null)
{
foreach (var m in mods.OfType<IApplicableToDrawableHitObjects>())
m.ApplyToDrawableHitObjects(dho.Yield());
}
}

if (!lifetimeEntryMap.TryGetValue(hitObject, out var entry))
Expand Down

0 comments on commit a31a694

Please sign in to comment.