2021-01-23 14:44:30 +00:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-01-23 14:44:30 +00:00
|
|
|
using System;
|
|
|
|
using NUnit.Framework;
|
2021-01-30 19:55:56 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-01-23 14:44:30 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-01-30 19:55:56 +00:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Screens;
|
2021-01-23 14:44:30 +00:00
|
|
|
using osu.Framework.Testing;
|
2021-01-30 19:55:56 +00:00
|
|
|
using osu.Game.Screens;
|
2021-01-23 14:44:30 +00:00
|
|
|
using osu.Game.Screens.OnlinePlay;
|
|
|
|
using osu.Game.Tests.Visual;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.NonVisual
|
|
|
|
{
|
|
|
|
[HeadlessTest]
|
|
|
|
public partial class OngoingOperationTrackerTest : OsuTestScene
|
|
|
|
{
|
|
|
|
private OngoingOperationTracker tracker;
|
|
|
|
private IBindable<bool> operationInProgress;
|
|
|
|
|
|
|
|
[SetUpSteps]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
AddStep("create tracker", () => Child = tracker = new OngoingOperationTracker());
|
|
|
|
AddStep("bind to operation status", () => operationInProgress = tracker.InProgress.GetBoundCopy());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestOperationTracking()
|
|
|
|
{
|
|
|
|
IDisposable firstOperation = null;
|
|
|
|
IDisposable secondOperation = null;
|
|
|
|
|
|
|
|
AddStep("begin first operation", () => firstOperation = tracker.BeginOperation());
|
2021-01-25 07:53:38 +00:00
|
|
|
AddAssert("first operation in progress", () => operationInProgress.Value);
|
2021-01-23 14:44:30 +00:00
|
|
|
|
|
|
|
AddStep("cannot start another operation",
|
|
|
|
() => Assert.Throws<InvalidOperationException>(() => tracker.BeginOperation()));
|
|
|
|
|
|
|
|
AddStep("end first operation", () => firstOperation.Dispose());
|
2021-01-25 07:53:38 +00:00
|
|
|
AddAssert("first operation is ended", () => !operationInProgress.Value);
|
2021-01-23 14:44:30 +00:00
|
|
|
|
|
|
|
AddStep("start second operation", () => secondOperation = tracker.BeginOperation());
|
2021-01-25 07:53:38 +00:00
|
|
|
AddAssert("second operation in progress", () => operationInProgress.Value);
|
2021-01-23 14:44:30 +00:00
|
|
|
|
|
|
|
AddStep("dispose first operation again", () => firstOperation.Dispose());
|
2021-01-25 07:53:38 +00:00
|
|
|
AddAssert("second operation still in progress", () => operationInProgress.Value);
|
2021-01-23 14:44:30 +00:00
|
|
|
|
|
|
|
AddStep("dispose second operation", () => secondOperation.Dispose());
|
2021-01-25 07:53:38 +00:00
|
|
|
AddAssert("second operation is ended", () => !operationInProgress.Value);
|
2021-01-23 14:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestOperationDisposalAfterTracker()
|
|
|
|
{
|
|
|
|
IDisposable operation = null;
|
|
|
|
|
|
|
|
AddStep("begin operation", () => operation = tracker.BeginOperation());
|
|
|
|
AddStep("dispose tracker", () => tracker.Expire());
|
|
|
|
AddStep("end operation", () => operation.Dispose());
|
|
|
|
AddAssert("operation is ended", () => !operationInProgress.Value);
|
|
|
|
}
|
2021-01-30 19:55:56 +00:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestOperationDisposalAfterScreenExit()
|
|
|
|
{
|
|
|
|
TestScreenWithTracker screen = null;
|
|
|
|
OsuScreenStack stack;
|
|
|
|
IDisposable operation = null;
|
|
|
|
|
|
|
|
AddStep("create screen with tracker", () =>
|
|
|
|
{
|
|
|
|
Child = stack = new OsuScreenStack
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
};
|
|
|
|
|
|
|
|
stack.Push(screen = new TestScreenWithTracker());
|
|
|
|
});
|
|
|
|
AddUntilStep("wait for loaded", () => screen.IsLoaded);
|
|
|
|
|
|
|
|
AddStep("begin operation", () => operation = screen.OngoingOperationTracker.BeginOperation());
|
|
|
|
AddAssert("operation in progress", () => screen.OngoingOperationTracker.InProgress.Value);
|
|
|
|
|
|
|
|
AddStep("dispose after screen exit", () =>
|
|
|
|
{
|
|
|
|
screen.Exit();
|
|
|
|
operation.Dispose();
|
|
|
|
});
|
|
|
|
AddAssert("operation ended", () => !screen.OngoingOperationTracker.InProgress.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
private partial class TestScreenWithTracker : OsuScreen
|
|
|
|
{
|
|
|
|
public OngoingOperationTracker OngoingOperationTracker { get; private set; }
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
InternalChild = OngoingOperationTracker = new OngoingOperationTracker();
|
|
|
|
}
|
|
|
|
}
|
2021-01-23 14:44:30 +00:00
|
|
|
}
|
|
|
|
}
|