A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.Linq;
5using NUnit.Framework;
6using osu.Framework.Extensions.Color4Extensions;
7using osu.Framework.Graphics;
8using osu.Framework.Graphics.Containers;
9using osu.Framework.Graphics.Shapes;
10using osu.Framework.Graphics.Sprites;
11using osuTK.Graphics;
12
13namespace osu.Framework.Tests.Visual.Containers
14{
15 public class TestSceneTextFlowContainer : FrameworkTestScene
16 {
17 private const string default_text = "Default text\n\nnewline";
18
19 private TextFlowContainer textContainer;
20
21 [SetUp]
22 public void Setup() => Schedule(() =>
23 {
24 Child = new Container
25 {
26 Anchor = Anchor.Centre,
27 Origin = Anchor.Centre,
28 Width = 300,
29 AutoSizeAxes = Axes.Y,
30 Children = new Drawable[]
31 {
32 new Box
33 {
34 RelativeSizeAxes = Axes.Both,
35 Colour = Color4.White.Opacity(0.1f)
36 },
37 textContainer = new TextFlowContainer
38 {
39 RelativeSizeAxes = Axes.X,
40 AutoSizeAxes = Axes.Y,
41 Text = default_text
42 }
43 }
44 };
45 });
46
47 [TestCase(Anchor.TopLeft)]
48 [TestCase(Anchor.TopCentre)]
49 [TestCase(Anchor.TopRight)]
50 [TestCase(Anchor.BottomLeft)]
51 [TestCase(Anchor.BottomCentre)]
52 [TestCase(Anchor.BottomRight)]
53 public void TestChangeTextAnchor(Anchor anchor)
54 {
55 AddStep("change text anchor", () => textContainer.TextAnchor = anchor);
56 AddAssert("children have correct anchors", () => textContainer.Children.All(c => c.Anchor == anchor && c.Origin == anchor));
57 AddAssert("children are positioned correctly", () =>
58 {
59 var result = string.Concat(textContainer.Children
60 .OrderBy(c => c.ScreenSpaceDrawQuad.TopLeft.Y)
61 .ThenBy(c => c is TextFlowContainer.NewLineContainer ? 0 : c.ScreenSpaceDrawQuad.TopLeft.X)
62 .Select(c => (c as SpriteText)?.Text.ToString() ?? "\n"));
63 return result == default_text;
64 });
65 }
66
67 [Test]
68 public void TestAddTextWithTextAnchor()
69 {
70 AddStep("change text anchor", () => textContainer.TextAnchor = Anchor.TopCentre);
71 AddStep("add text", () => textContainer.AddText("added text"));
72 AddAssert("children have correct anchors", () => textContainer.Children.All(c => c.Anchor == Anchor.TopCentre && c.Origin == Anchor.TopCentre));
73 }
74 }
75}