1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 03:52:57 +08:00
osu-lazer/osu.Game.Tests/Visual/UserInterface/TestSceneLogoTrackingContainer.cs

287 lines
9.9 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;
2020-08-04 20:16:59 +08:00
using System.Linq;
2019-03-24 14:18:38 +08:00
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-03-24 14:18:38 +08:00
using osu.Framework.Graphics.Shapes;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Framework.Testing;
2019-03-24 14:18:38 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Screens.Menu;
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 TestSceneLogoTrackingContainer : OsuTestScene
2019-03-24 14:18:38 +08:00
{
private OsuLogo logo;
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;
[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]
2019-04-17 16:15:23 +08:00
public void TestMoveFacade()
2019-03-26 09:48:29 +08:00
{
AddToggleStep("Toggle move continuously", b => randomPositions = b);
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 14:24:09 +08:00
AddStep("Move facade to random position", moveLogoFacade);
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]
2019-04-17 16:15:23 +08:00
public void TestRemoveFacade()
2019-03-24 14:18:38 +08:00
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 14:24:09 +08:00
AddStep("Move facade to random position", moveLogoFacade);
2019-04-04 10:22:05 +08:00
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]
2019-04-17 16:15:23 +08:00
public void TestTransferFacade()
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 14:24:09 +08:00
AddStep("Move facade to random position", moveLogoFacade);
2019-04-04 10:22:05 +08:00
AddStep("Remove facade from FacadeContainer", removeFacade);
2019-04-05 14:48:48 +08:00
AddStep("Transfer facade to a new container", () =>
{
transferContainer.Add(logoFacade);
transferContainerBox.Colour = Color4.Tomato;
moveLogoFacade();
});
2019-04-17 16:15:23 +08:00
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]
2019-04-17 16:15:23 +08:00
public void TestFlowContainer()
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", () =>
{
2019-04-08 14:24:09 +08:00
trackingContainer.StopTracking();
logo.MoveTo(new Vector2(0.5f), 500, Easing.InOutExpo);
2019-04-08 14:24:09 +08:00
visualBox.Colour = Color4.White;
Scheduler.AddDelayed(() =>
{
2019-04-08 14:24:09 +08:00
trackingContainer.StartTracking(logo, 1000, Easing.InOutExpo);
visualBox.Colour = Color4.Tomato;
}, 700);
});
2019-04-04 10:22:05 +08:00
waitForMove(8);
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
}
[Test]
2019-04-17 16:15:23 +08:00
public void TestSetFacadeSize()
{
bool failed = false;
2019-04-17 16:15:23 +08:00
2019-04-05 14:48:48 +08:00
AddStep("Set up scenario", () =>
{
failed = false;
addFacadeContainers();
});
2019-04-17 16:15:23 +08:00
AddStep("Try setting facade size", () =>
{
try
{
logoFacade.Size = new Vector2(0, 0);
}
catch (Exception e)
{
if (e is InvalidOperationException)
failed = true;
}
});
2019-04-17 16:15:23 +08:00
AddAssert("Exception thrown", () => failed);
}
[Test]
2019-04-17 16:15:23 +08:00
public void TestSetMultipleContainers()
{
bool failed = false;
LogoTrackingContainer newContainer = new LogoTrackingContainer();
2019-04-17 16:15:23 +08:00
2019-04-05 14:48:48 +08:00
AddStep("Set up scenario", () =>
{
failed = false;
newContainer = new LogoTrackingContainer();
addFacadeContainers();
2019-04-08 14:24:09 +08:00
moveLogoFacade();
2019-04-05 14:48:48 +08:00
});
2019-04-17 16:15:23 +08:00
AddStep("Try tracking new container", () =>
{
try
{
2019-04-08 14:24:09 +08:00
newContainer.StartTracking(logo);
}
catch (Exception e)
{
if (e is InvalidOperationException)
failed = true;
}
});
2019-04-17 16:15:23 +08:00
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);
2019-04-08 14:24:09 +08:00
trackingContainer.StartTracking(logo, 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();
}
private void moveLogoFacade()
{
if (!logoFacade.Transforms.Any() && !transferContainer.Transforms.Any())
{
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>
2019-04-08 14:24:09 +08:00
public bool IsLogoTracking => Precision.AlmostEquals(Logo.Position, ComputeLogoTrackingPosition());
2019-04-04 10:22:05 +08:00
}
2019-03-24 14:18:38 +08:00
}
}