osu/osu.Game.Tests/Visual/UserInterface/TestSceneLogoTrackingContai...

300 lines
10 KiB
C#
Raw Normal View History

2019-03-24 06:18:38 +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.
using System;
using System.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2019-03-24 06:18:38 +00:00
using osu.Framework.Graphics.Shapes;
2019-03-26 08:18:35 +00:00
using osu.Framework.Graphics.UserInterface;
2020-01-09 04:43:44 +00:00
using osu.Framework.Utils;
using osu.Framework.Testing;
2019-03-24 06:18:38 +00:00
using osu.Game.Graphics.Containers;
using osu.Game.Screens.Menu;
using osu.Game.Screens.Play;
2019-03-26 01:48:29 +00:00
using osuTK;
2019-03-24 06:18:38 +00:00
using osuTK.Graphics;
2019-04-04 04:05:34 +00:00
namespace osu.Game.Tests.Visual.UserInterface
2019-03-24 06:18:38 +00:00
{
public class TestSceneLogoTrackingContainer : OsuTestScene
2019-03-24 06:18:38 +00:00
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(PlayerLoader),
typeof(Player),
typeof(LogoTrackingContainer),
2019-03-26 08:18:35 +00:00
typeof(ButtonSystem),
typeof(ButtonSystemState),
typeof(Menu),
typeof(MainMenu)
2019-03-24 06:18:38 +00:00
};
private OsuLogo logo;
private TestLogoTrackingContainer trackingContainer;
private Container transferContainer;
private Box visualBox;
private Box transferContainerBox;
private Drawable logoFacade;
2019-04-04 02:22:05 +00:00
private bool randomPositions;
2019-03-24 06:18:38 +00:00
2019-04-04 04:13:03 +00:00
private const float visual_box_size = 72;
[SetUpSteps]
public void SetUpSteps()
{
AddStep("Clear facades", () =>
{
Clear();
2019-04-04 02:22:05 +00: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 01:48:29 +00:00
[Test]
2019-04-17 08:15:23 +00:00
public void TestMoveFacade()
2019-03-26 01:48:29 +00:00
{
AddToggleStep("Toggle move continuously", b => randomPositions = b);
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 06:24:09 +00:00
AddStep("Move facade to random position", moveLogoFacade);
waitForMove();
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
2019-03-26 01:48:29 +00:00
}
/// <summary>
/// Check if the facade is removed from the container, the logo stops tracking.
/// </summary>
[Test]
2019-04-17 08:15:23 +00:00
public void TestRemoveFacade()
2019-03-24 06:18:38 +00:00
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 06:24:09 +00:00
AddStep("Move facade to random position", moveLogoFacade);
2019-04-04 02:22:05 +00: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 08:15:23 +00:00
public void TestTransferFacade()
{
AddStep("Add tracking containers", addFacadeContainers);
2019-04-08 06:24:09 +00:00
AddStep("Move facade to random position", moveLogoFacade);
2019-04-04 02:22:05 +00:00
AddStep("Remove facade from FacadeContainer", removeFacade);
2019-04-05 06:48:48 +00:00
AddStep("Transfer facade to a new container", () =>
{
transferContainer.Add(logoFacade);
transferContainerBox.Colour = Color4.Tomato;
moveLogoFacade();
});
2019-04-17 08:15:23 +00:00
waitForMove();
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
2019-03-24 06:18:38 +00:00
}
/// <summary>
2019-04-04 04:13:03 +00: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 08:15:23 +00:00
public void TestFlowContainer()
2019-03-26 01:48:29 +00:00
{
FillFlowContainer flowContainer;
2019-03-26 01:48:29 +00:00
2019-04-04 02:22:05 +00:00
AddStep("Create new flow container with facade", () =>
2019-03-26 01:48:29 +00: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 04:13:03 +00:00
Size = new Vector2(visual_box_size)
},
new Container
{
Alpha = 0.35f,
RelativeSizeAxes = Axes.None,
2019-04-04 04:13:03 +00: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 04:13:03 +00:00
Size = new Vector2(visual_box_size)
},
};
});
AddStep("Perform logo movements", () =>
{
2019-04-08 06:24:09 +00:00
trackingContainer.StopTracking();
logo.MoveTo(new Vector2(0.5f), 500, Easing.InOutExpo);
2019-04-08 06:24:09 +00:00
visualBox.Colour = Color4.White;
Scheduler.AddDelayed(() =>
{
2019-04-08 06:24:09 +00:00
trackingContainer.StartTracking(logo, 1000, Easing.InOutExpo);
visualBox.Colour = Color4.Tomato;
}, 700);
});
2019-04-04 02:22:05 +00:00
waitForMove(8);
AddAssert("Logo is tracking", () => trackingContainer.IsLogoTracking);
}
[Test]
2019-04-17 08:15:23 +00:00
public void TestSetFacadeSize()
{
bool failed = false;
2019-04-17 08:15:23 +00:00
2019-04-05 06:48:48 +00:00
AddStep("Set up scenario", () =>
{
failed = false;
addFacadeContainers();
});
2019-04-17 08:15:23 +00: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 08:15:23 +00:00
AddAssert("Exception thrown", () => failed);
}
[Test]
2019-04-17 08:15:23 +00:00
public void TestSetMultipleContainers()
{
bool failed = false;
LogoTrackingContainer newContainer = new LogoTrackingContainer();
2019-04-17 08:15:23 +00:00
2019-04-05 06:48:48 +00:00
AddStep("Set up scenario", () =>
{
failed = false;
newContainer = new LogoTrackingContainer();
addFacadeContainers();
2019-04-08 06:24:09 +00:00
moveLogoFacade();
2019-04-05 06:48:48 +00:00
});
2019-04-17 08:15:23 +00:00
AddStep("Try tracking new container", () =>
{
try
{
2019-04-08 06:24:09 +00:00
newContainer.StartTracking(logo);
}
catch (Exception e)
{
if (e is InvalidOperationException)
failed = true;
}
});
2019-04-17 08:15:23 +00:00
AddAssert("Exception thrown", () => failed);
}
private void addFacadeContainers()
{
AddRange(new Drawable[]
2019-03-26 01:48:29 +00:00
{
trackingContainer = new TestLogoTrackingContainer
{
Alpha = 0.35f,
RelativeSizeAxes = Axes.None,
2019-04-04 04:13:03 +00: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 04:13:03 +00:00
Size = new Vector2(visual_box_size),
Child = transferContainerBox = new Box
{
Colour = Color4.White,
RelativeSizeAxes = Axes.Both,
}
},
});
2019-03-26 01:48:29 +00:00
trackingContainer.Add(logoFacade = trackingContainer.LogoFacade);
2019-04-08 06:24:09 +00:00
trackingContainer.StartTracking(logo, 1000);
}
2019-03-26 08:18:35 +00:00
private void waitForMove(int count = 5) => AddWaitStep("Wait for transforms to finish", count);
2019-04-04 02:22:05 +00:00
private void removeFacade()
{
trackingContainer.Remove(logoFacade);
visualBox.Colour = Color4.White;
moveLogoFacade();
}
private void moveLogoFacade()
{
2019-04-04 02:22:05 +00:00
if (logoFacade?.Transforms.Count == 0 && transferContainer?.Transforms.Count == 0)
{
2019-04-04 02:22:05 +00: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 01:48:29 +00:00
}
if (randomPositions)
Schedule(moveLogoFacade);
2019-03-26 01:48:29 +00:00
}
2019-04-04 02:22:05 +00:00
private class TestLogoTrackingContainer : LogoTrackingContainer
2019-04-04 02:22:05 +00:00
{
/// <summary>
/// Check that the logo is tracking the position of the facade, with an acceptable precision lenience.
/// </summary>
2019-04-08 06:24:09 +00:00
public bool IsLogoTracking => Precision.AlmostEquals(Logo.Position, ComputeLogoTrackingPosition());
2019-04-04 02:22:05 +00:00
}
2019-03-24 06:18:38 +00:00
}
}