A game framework written with osu! in mind.
at master 132 lines 4.0 kB view raw
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; 5using NUnit.Framework; 6using osu.Framework.Graphics; 7using osu.Framework.Graphics.Containers; 8using osu.Framework.Graphics.Shapes; 9using osu.Framework.Graphics.Sprites; 10using osu.Framework.Text; 11using osuTK.Graphics; 12 13namespace osu.Framework.Tests.Visual.Sprites 14{ 15 public class TestSceneSpriteTextMaxWidth : FrameworkTestScene 16 { 17 private VisualDisplay display; 18 19 [Test] 20 public void TestAutoSizeLessThanMaxWidth() 21 { 22 createTest(s => 23 { 24 s.MaxWidth = 100; 25 s.Text = "test"; 26 }); 27 28 AddAssert("size < max", () => display.Text.DrawWidth < 100); 29 } 30 31 [TestCase(false)] 32 [TestCase(true)] 33 public void TestAutoSizeMoreThanMaxWidth(bool truncate) 34 { 35 createTest(s => 36 { 37 s.MaxWidth = 50; 38 s.Text = "some very long text that should exceed the max width"; 39 s.Truncate = truncate; 40 }); 41 42 AddAssert("size <= max", () => display.Text.DrawWidth <= 50); 43 } 44 45 [Test] 46 public void TestFixedSizeLessThanMaxWidth() 47 { 48 createTest(s => 49 { 50 s.MaxWidth = 50; 51 s.Width = 40; 52 s.Text = "test"; 53 }); 54 55 AddAssert("size <= 40", () => display.Text.DrawWidth <= 40); 56 } 57 58 [Test] 59 public void TestFixedSizeMoreThanMaxWidth() 60 { 61 createTest(s => 62 { 63 s.MaxWidth = 50; 64 s.Width = 100; 65 s.Text = "test"; 66 }); 67 68 AddAssert("size == 50", () => display.Text.DrawWidth <= 50); 69 } 70 71 [Test] 72 public void TestMaxWidthWithRelativeSize() 73 { 74 float textWidth = 0; 75 76 createTest(s => 77 { 78 s.RelativeSizeAxes = Axes.X; 79 s.MaxWidth = 0.5f; 80 s.Text = "some very long text that should exceed the max width"; 81 s.Truncate = true; 82 }, Axes.Y); 83 AddStep("store text width", () => textWidth = display.Text.TextBuilder.Bounds.X); 84 85 AddStep("set parent size", () => display.Width = 100); 86 AddAssert("size <= max", () => display.Text.DrawWidth <= 50); 87 AddAssert("width increased", () => display.Text.TextBuilder.Bounds.X > textWidth); 88 } 89 90 private void createTest(Action<SpriteText> initFunc, Axes autoSizeAxes = Axes.Both) 91 { 92 AddStep("create test", () => 93 { 94 Clear(); 95 Add(display = new VisualDisplay(initFunc, autoSizeAxes)); 96 }); 97 } 98 99 private class VisualDisplay : CompositeDrawable 100 { 101 public readonly TestSpriteText Text; 102 103 public VisualDisplay(Action<SpriteText> initFunc, Axes autoSizeAxes = Axes.Both) 104 { 105 Anchor = Anchor.Centre; 106 Origin = Anchor.Centre; 107 AutoSizeAxes = autoSizeAxes; 108 109 InternalChildren = new Drawable[] 110 { 111 new Box 112 { 113 RelativeSizeAxes = Axes.Both, 114 Alpha = 0.2f, 115 Colour = Color4.Pink 116 }, 117 Text = new TestSpriteText { AllowMultiline = false } 118 }; 119 120 initFunc?.Invoke(Text); 121 } 122 } 123 124 private class TestSpriteText : SpriteText 125 { 126 public TextBuilder TextBuilder { get; private set; } 127 128 protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) 129 => TextBuilder = base.CreateTextBuilder(store); 130 } 131 } 132}