1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 02:27:25 +08:00
osu-lazer/osu.Game.Tests/Visual/UserInterface/TestCaseLogoTrackingContainer.cs

304 lines
11 KiB
C#
Raw Normal View History

2019-03-24 14:18:38 +08: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.
using System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-03-24 14:18:38 +08:00
using osu.Framework.Graphics.Shapes;
2019-03-26 16:18:35 +08:00
using osu.Framework.Graphics.UserInterface;
2019-04-04 12:05:34 +08:00
using osu.Framework.MathUtils;
using osu.Framework.Testing;
2019-03-24 14:18:38 +08:00
using osu.Game.Configuration;
using osu.Game.Graphics.Containers;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Play;
2019-03-26 09:48:29 +08:00
using osuTK;
2019-03-24 14:18:38 +08:00
using osuTK.Graphics;
2019-04-04 12:05:34 +08:00
namespace osu.Game.Tests.Visual.UserInterface
2019-03-24 14:18:38 +08:00
{
public class TestCaseLogoTrackingContainer : OsuTestCase
2019-03-24 14:18:38 +08:00
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(PlayerLoader),
typeof(Player),
typeof(LogoTrackingContainer),
2019-03-26 16:18:35 +08:00
typeof(ButtonSystem),
typeof(ButtonSystemState),
typeof(Menu),
typeof(MainMenu)
2019-03-24 14:18:38 +08:00
};
private OsuLogo logo;
private readonly Bindable<float> uiScale = new Bindable<float>();
private TestLogoTrackingContainer trackingContainer;
private Container transferContainer;
private Box visualBox;
private Box transferContainerBox;
private Drawable logoFacade;
2019-04-04 10:22:05 +08:00
private bool randomPositions;
2019-03-24 14:18:38 +08:00
2019-04-04 12:13:03 +08:00
private const float visual_box_size = 72;
2019-03-24 14:18:38 +08:00
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.UIScale, uiScale);
AddSliderStep("Adjust scale", 0.8f, 1.5f, 1f, v => uiScale.Value = v);
2019-03-24 14:18:38 +08:00
}
[SetUpSteps]
public void SetUpSteps()
{
AddStep("Clear facades", () =>
{
Clear();
2019-04-04 10:22:05 +08:00
Add(logo = new OsuLogo { Scale = new Vector2(0.15f), RelativePositionAxes = Axes.Both });
trackingContainer = null;
transferContainer = null;
});
}
/// <summary>
/// Move the facade to 0,0, then move it to a random new location while the logo is still transforming to it.
/// Check if the logo is still tracking the facade.
/// </summary>
2019-03-26 09:48:29 +08:00
[Test]
public void MoveFacadeTest()
2019-03-26 09:48:29 +08:00
{
AddToggleStep("Toggle move continuously", b => randomPositions = b);
AddStep("Add tracking containers", addFacadeContainers);
2019-04-04 10:22:05 +08:00
AddStep("Move facade to random position", startTrackingRandom);
waitForMove();
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
2019-03-26 09:48:29 +08:00
}
/// <summary>
/// Check if the facade is removed from the container, the logo stops tracking.
/// </summary>
[Test]
public void RemoveFacadeTest()
2019-03-24 14:18:38 +08:00
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-04 10:22:05 +08:00
AddStep("Move facade to random position", startTrackingRandom);
AddStep("Remove facade from FacadeContainer", removeFacade);
waitForMove();
AddAssert("Logo is not tracking", () => !trackingContainer.IsLogoTracking);
}
/// <summary>
/// Check if the facade gets added to a new container, tracking starts on the new facade.
/// </summary>
[Test]
public void TransferFacadeTest()
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-04 10:22:05 +08:00
AddStep("Move facade to random position", startTrackingRandom);
AddStep("Remove facade from FacadeContainer", removeFacade);
AddStep("Transfer facade to a new container", addFacadeToNewContainer);
waitForMove();
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
2019-03-24 14:18:38 +08:00
}
/// <summary>
2019-04-04 12:13:03 +08:00
/// Add a facade to a flow container, move the logo to the center of the screen, then start tracking on the facade.
/// </summary>
[Test]
public void FlowContainerTest()
2019-03-26 09:48:29 +08:00
{
FillFlowContainer flowContainer;
2019-03-26 09:48:29 +08:00
2019-04-04 10:22:05 +08:00
AddStep("Create new flow container with facade", () =>
2019-03-26 09:48:29 +08:00
{
Add(trackingContainer = new TestLogoTrackingContainer
{
AutoSizeAxes = Axes.Both,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Child = flowContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Direction = FillDirection.Vertical,
}
});
flowContainer.Children = new Drawable[]
{
new Box
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Colour = Color4.Azure,
2019-04-04 12:13:03 +08:00
Size = new Vector2(visual_box_size)
},
new Container
{
Alpha = 0.35f,
RelativeSizeAxes = Axes.None,
2019-04-04 12:13:03 +08:00
Size = new Vector2(visual_box_size),
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Children = new Drawable[]
{
visualBox = new Box
{
Colour = Color4.White,
RelativeSizeAxes = Axes.Both,
},
trackingContainer.LogoFacade,
}
},
new Box
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Colour = Color4.Azure,
2019-04-04 12:13:03 +08:00
Size = new Vector2(visual_box_size)
},
};
});
AddStep("Perform logo movements", () =>
{
trackingContainer.Tracking = false;
logo.MoveTo(new Vector2(0.5f), 500, Easing.InOutExpo);
trackingContainer.SetLogo(logo, 1.0f, 1000, Easing.InOutExpo);
visualBox.Colour = Color4.White;
Scheduler.AddDelayed(() =>
{
trackingContainer.Tracking = true;
visualBox.Colour = Color4.Tomato;
}, 700);
});
2019-04-04 10:22:05 +08:00
waitForMove(8);
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
}
[Test]
public void SetFacadeSizeTest()
{
bool failed = false;
AddStep("Add tracking containers", addFacadeContainers);
AddStep("Try setting facade size", () =>
{
try
{
logoFacade.Size = new Vector2(0, 0);
}
catch (Exception e)
{
if (e is InvalidOperationException)
failed = true;
}
});
AddAssert("Exception thrown", () => failed);
}
[Test]
public void SetMultipleContainers()
{
bool failed = false;
LogoTrackingContainer newContainer = new LogoTrackingContainer();
AddStep("Add tracking containers", addFacadeContainers);
AddStep("Move facade to random position", startTrackingRandom);
AddStep("Add logo to new container", () => newContainer.SetLogo(logo));
AddStep("Try tracking new container", () =>
{
try
{
newContainer.Tracking = true;
}
catch (Exception e)
{
if (e is InvalidOperationException)
failed = true;
}
});
AddAssert("Exception thrown", () => failed);
}
private void addFacadeContainers()
{
AddRange(new Drawable[]
2019-03-26 09:48:29 +08:00
{
trackingContainer = new TestLogoTrackingContainer
{
Alpha = 0.35f,
RelativeSizeAxes = Axes.None,
2019-04-04 12:13:03 +08:00
Size = new Vector2(visual_box_size),
Child = visualBox = new Box
{
Colour = Color4.Tomato,
RelativeSizeAxes = Axes.Both,
}
},
transferContainer = new Container
{
Alpha = 0.35f,
RelativeSizeAxes = Axes.None,
2019-04-04 12:13:03 +08:00
Size = new Vector2(visual_box_size),
Child = transferContainerBox = new Box
{
Colour = Color4.White,
RelativeSizeAxes = Axes.Both,
}
},
});
2019-03-26 09:48:29 +08:00
trackingContainer.Add(logoFacade = trackingContainer.LogoFacade);
trackingContainer.SetLogo(logo, 1.0f, 1000);
}
2019-03-26 16:18:35 +08:00
private void waitForMove(int count = 5) => AddWaitStep("Wait for transforms to finish", count);
2019-04-04 10:22:05 +08:00
private void removeFacade()
{
trackingContainer.Remove(logoFacade);
visualBox.Colour = Color4.White;
moveLogoFacade();
}
2019-04-04 10:22:05 +08:00
private void startTrackingRandom()
{
trackingContainer.Tracking = true;
moveLogoFacade();
}
private void addFacadeToNewContainer()
{
transferContainer.Add(logoFacade);
transferContainerBox.Colour = Color4.Tomato;
moveLogoFacade();
}
private void moveLogoFacade()
{
2019-04-04 10:22:05 +08:00
if (logoFacade?.Transforms.Count == 0 && transferContainer?.Transforms.Count == 0)
{
2019-04-04 10:22:05 +08:00
Random random = new Random();
trackingContainer.Delay(500).MoveTo(new Vector2(random.Next(0, (int)logo.Parent.DrawWidth), random.Next(0, (int)logo.Parent.DrawHeight)), 300);
transferContainer.Delay(500).MoveTo(new Vector2(random.Next(0, (int)logo.Parent.DrawWidth), random.Next(0, (int)logo.Parent.DrawHeight)), 300);
2019-03-26 09:48:29 +08:00
}
if (randomPositions)
Schedule(moveLogoFacade);
2019-03-26 09:48:29 +08:00
}
2019-04-04 10:22:05 +08:00
private class TestLogoTrackingContainer : LogoTrackingContainer
2019-04-04 10:22:05 +08:00
{
/// <summary>
/// Check that the logo is tracking the position of the facade, with an acceptable precision lenience.
/// </summary>
public bool IsLogoTracking => Precision.AlmostEquals(Logo.Position, LogoTrackingPosition);
2019-04-04 10:22:05 +08:00
}
2019-03-24 14:18:38 +08:00
}
}